📄 calendar.rc4.js
字号:
this.check(cal); // checks other cals this.calendars.each(function(kal) { // update cal graphic if visible if (kal.visible) { this.display(kal); } }, this); }, // check: checks other calendars to make sure no overlapping values // @param cal (obj) check: function(cal) { this.calendars.each(function(kal, i) { if (kal.val) { // if calendar has value set var change = false; if (i < cal.id) { // preceding calendar var bound = new Date(Date.parse(cal.val)); bound.setDate(bound.getDate() - (this.options.pad * (cal.id - i))); if (bound < kal.val) { change = true; } } if (i > cal.id) { // following calendar var bound = new Date(Date.parse(cal.val)); bound.setDate(bound.getDate() + (this.options.pad * (i - cal.id))); if (bound > kal.val) { change = true; } } if (change) { if (kal.start > bound) { bound = kal.start; } if (kal.end < bound) { bound = kal.end; } kal.month = bound.getMonth(); kal.year = bound.getFullYear(); $extend(kal, this.values(kal)); // TODO - IN THE CASE OF SELECT MOVE TO NEAREST VALID VALUE // IN THE CASE OF INPUT DISABLE // if new date is not valid better unset cal value // otherwise it would mean incrementally checking to find the nearest valid date which could be months / years away kal.val = kal.days.contains(bound.getDate()) ? bound : null; this.write(kal); if (kal.visible) { this.display(kal); } // update cal graphic if visible } } else { kal.month = cal.month; kal.year = cal.year; } }, this); }, // clicked: run when a valid day is clicked in the calendar // @param cal (obj) clicked: function(td, day, cal) { cal.val = (this.value(cal) == day) ? null : new Date(cal.year, cal.month, day); // set new value - if same then disable this.write(cal); // ok - in the special case that it's all selects and there's always a date no matter what (at least as far as the form is concerned) // we can't let the calendar undo a date selection - it's just not possible!! if (!cal.val) { cal.val = this.read(cal); } if (cal.val) { this.check(cal); // checks other cals this.toggle(cal); // hide cal } else { // remove active class and replace with valid td.addClass(this.classes.valid); td.removeClass(this.classes.active); } }, // display: create calendar element // @param cal (obj) display: function(cal) { // 1. header and navigation this.calendar.empty(); // init div this.calendar.className = this.classes.calendar + ' ' + this.options.months[cal.month].toLowerCase(); var div = new Element('div').injectInside(this.calendar); // a wrapper div to help correct browser css problems with the caption element var table = new Element('table').injectInside(div).adopt(this.caption(cal)); // 2. day names var thead = new Element('thead').injectInside(table); var tr = new Element('tr').injectInside(thead); for (var i = 0; i <= 6; i++) { var th = this.options.days[(i + this.options.offset) % 7]; tr.adopt(new Element('th', { 'title': th }).appendText(th.substr(0, 1))); } // 3. day numbers var tbody = new Element('tbody').injectInside(table); var tr = new Element('tr').injectInside(tbody); var d = new Date(cal.year, cal.month, 1); var offset = ((d.getDay() - this.options.offset) + 7) % 7; // day of the week (offset) var last = new Date(cal.year, cal.month + 1, 0).getDate(); // last day of this month var prev = new Date(cal.year, cal.month, 0).getDate(); // last day of previous month var active = this.value(cal); // active date (if set and within curr month) var valid = cal.days; // valid days for curr month var inactive = []; // active dates set by other calendars var hilited = []; this.calendars.each(function(kal, i) { if (kal != cal && kal.val) { if (cal.year == kal.val.getFullYear() && cal.month == kal.val.getMonth()) { inactive.push(kal.val.getDate()); } if (cal.val) { for (var day = 1; day <= last; day++) { d.setDate(day); if ((i < cal.id && d > kal.val && d < cal.val) || (i > cal.id && d > cal.val && d < kal.val)) { if (!hilited.contains(day)) { hilited.push(day); } } } } } }, this); var d = new Date(); var today = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime(); // today obv for (var i = 1; i < 43; i++) { // 1 to 42 (6 x 7 or 6 weeks) if ((i - 1) % 7 == 0) { tr = new Element('tr').injectInside(tbody); } // each week is it's own table row var td = new Element('td').injectInside(tr); var day = i - offset; var date = new Date(cal.year, cal.month, day); var cls = ''; if (day === active) { cls = this.classes.active; } // active else if (inactive.contains(day)) { cls = this.classes.inactive; } // inactive else if (valid.contains(day)) { cls = this.classes.valid; } // valid else if (day >= 1 && day <= last) { cls = this.classes.invalid; } // invalid if (date.getTime() == today) { cls = cls + ' ' + this.classes.today; } // adds class for today if (hilited.contains(day)) { cls = cls + ' ' + this.classes.hilite; } // adds class if hilited td.addClass(cls); if (valid.contains(day)) { // if it's a valid - clickable - day we add interaction td.setProperty('title', this.format(date, 'D M jS Y')); td.addEvents({ 'click': function(td, day, cal) { this.clicked(td, day, cal); }.pass([td, day, cal], this), 'mouseover': function(td, cls) { td.addClass(cls); }.pass([td, this.classes.hover]), 'mouseout': function(td, cls) { td.removeClass(cls); }.pass([td, this.classes.hover]) }); } // pad calendar with last days of prev month and first days of next month if (day < 1) { day = prev + day; } else if (day > last) { day = day - last; } td.appendText(day); } }, // element: helper function // @param el (string) element id // @param f (string) format string // @param cal (obj) element: function(el, f, cal) { if ($type(f) == 'object') { // in the case of multiple inputs per calendar for (var i in f) { if (!this.element(i, f[i], cal)) { return false; } } return true; } el = $(el); if (!el) { return false; } el.format = f; if (el.getTag() == 'select') { // select elements allow the user to manually set the date via select option el.addEvent('change', function(cal) { this.changed(cal); }.pass(cal, this)); } else { // input (type text) elements restrict the user to only setting the date via the calendar el.readOnly = true; el.addEvent('focus', function(cal) { this.toggle(cal); }.pass(cal, this)); } cal.els.push(el); return true; }, // format: formats a date object according to passed in instructions // @param date (obj) // @param f (string) any combination of punctuation / separators and d, j, D, l, S, m, n, F, M, y, Y // @returns string format: function(date, format) { var str = ''; if (date) { var j = date.getDate(); // 1 - 31 var w = date.getDay(); // 0 - 6 var l = this.options.days[w]; // Sunday - Saturday var n = date.getMonth() + 1; // 1 - 12 var f = this.options.months[n - 1]; // January - December var y = date.getFullYear() + ''; // 19xx - 20xx for (var i = 0, len = format.length; i < len; i++) { var cha = format.charAt(i); // format char switch(cha) { // year cases case 'y': // xx - xx y = y.substr(2); case 'Y': // 19xx - 20xx str += y; break; // month cases case 'm': // 01 - 12 if (n < 10) { n = '0' + n; } case 'n': // 1 - 12 str += n; break; case 'M': // Jan - Dec f = f.substr(0, 3); case 'F': // January - December str += f; break; // day cases case 'd': // 01 - 31 if (j < 10) { j = '0' + j; } case 'j': // 1 - 31 str += j; break; case 'D': // Sun - Sat l = l.substr(0, 3); case 'l': // Sunday - Saturday str += l; break; case 'N': // 1 - 7 w += 1; case 'w': // 0 - 6 str += w; break; case 'S': // st, nd, rd or th (works well with j) if (j % 10 == 1 && j != '11') { str += 'st'; } else if (j % 10 == 2 && j != '12') { str += 'nd'; } else if (j % 10 == 3 && j != '13') { str += 'rd'; } else { str += 'th'; } break; default: str += cha; } } } return str; // return format with values replaced }, // navigate: calendar navigation // @param cal (obj) // @param type (str) m or y for month or year // @param n (int) + or - for next or prev navigate: function(cal, type, n) { switch (type) { case 'm': // month if ($type(cal.months) == 'array') { var i = cal.months.indexOf(cal.month) + n; // index of current month if (i < 0 || i == cal.months.length) { // out of range if (this.options.navigation == 1) { // if type 1 nav we'll need to increment the year this.navigate(cal, 'y', n); } i = (i < 0) ? cal.months.length - 1 : 0; } cal.month = cal.months[i]; } else { var i = cal.month + n; if (i < 0 || i == 12) { if (this.options.navigation == 1) { this.navigate(cal, 'y', n); } i = (i < 0) ? 11 : 0; } cal.month = i; } break; case 'y': // year if ($type(cal.years) == 'array') { var i = cal.years.indexOf(cal.year) + n; cal.year = cal.years[i]; } else { cal.year += n; } break; } $extend(cal, this.values(cal)); if ($type(cal.months) == 'array') { // if the calendar has a months select var i = cal.months.indexOf(cal.month); // and make sure the curr months exists for the new year if (i < 0) { cal.month = cal.months[0]; } // otherwise we'll reset the month } this.display(cal); }, // read: compiles cal value based on array of inputs passed in // @param cal (obj) // @returns date (obj) or (null) read: function(cal) { var arr = [null, null, null]; cal.els.each(function(el) { // returns an array which may contain empty values var values = this.unformat(el.value, el.format); values.each(function(val, i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -