📄 calendar.rc4.js
字号:
if ($type(val) == 'number') { arr[i] = val; } }); }, this); // we can update the cals month and year values if ($type(arr[0]) == 'number') { cal.year = arr[0]; } if ($type(arr[1]) == 'number') { cal.month = arr[1]; } var val = null; if (arr.every(function(i) { return $type(i) == 'number'; })) { // if valid date var last = new Date(arr[0], arr[1] + 1, 0).getDate(); // last day of month if (arr[2] > last) { arr[2] = last; } // make sure we stay within the month (ex in case default day of select is 31 and month is feb) val = new Date(arr[0], arr[1], arr[2]); } return (cal.val == val) ? null : val; // if new date matches old return null (same date clicked twice = disable) }, // rebuild: rebuilds days + months selects // @param cal (obj) rebuild: function(cal) { cal.els.each(function(el) { /* if (el.getTag() == 'select' && el.format.test('^(F|m|M|n)$')) { // special case for months-only select if (!cal.options) { cal.options = el.clone(); } // clone a copy of months select var val = (cal.val) ? cal.val.getMonth() : el.value.toInt(); el.empty(); // initialize select cal.months.each(function(month) { // create an option element var option = new Element('option', { 'selected': (val == month), 'value': this.format(new Date(1, month, 1), el.format); }).appendText(day).injectInside(el); }, this); } */ if (el.getTag() == 'select' && el.format.test('^(d|j)$')) { // special case for days-only select var d = this.value(cal); if (!d) { d = el.value.toInt(); } // if the calendar doesn't have a set value, try to use value from select el.empty(); // initialize select cal.days.each(function(day) { // create an option element var option = new Element('option', { 'selected': (d == day), 'value': ((el.format == 'd' && day < 10) ? '0' + day : day) }).appendText(day).injectInside(el); }, this); } }, this); }, // sort: helper function for numerical sorting sort: function(a, b) { return a - b; }, // toggle: show / hide calendar // @param cal (obj) toggle: function(cal) { document.removeEvent('mousedown', this.fn); // always remove the current mousedown script first if (cal.visible) { // simply hide curr cal cal.visible = false; cal.button.removeClass(this.classes.active); // active this.fx.start(1, 0); } else { // otherwise show (may have to hide others) // hide cal on out-of-bounds click this.fn = function(e, cal) { var e = new Event(e); var el = e.target; var stop = false; while (el != document.body && el.nodeType == 1) { if (el == this.calendar) { stop = true; } this.calendars.each(function(kal) { if (kal.button == el || kal.els.contains(el)) { stop = true; } }); if (stop) { e.stop(); return false; } else { el = el.parentNode; } } this.toggle(cal); }.create({ 'arguments': cal, 'bind': this, 'event': true }); document.addEvent('mousedown', this.fn); this.calendars.each(function(kal) { if (kal == cal) { kal.visible = true; kal.button.addClass(this.classes.active); // css c-icon-active } else { kal.visible = false; kal.button.removeClass(this.classes.active); // css c-icon-active } }, this); var size = window.getSize().scrollSize; var coord = cal.button.getCoordinates(); var x = coord.right + this.options.tweak.x; var y = coord.top + this.options.tweak.y; // make sure the calendar doesn't open off screen if (!this.calendar.coord) { this.calendar.coord = this.calendar.getCoordinates(); } if (x + this.calendar.coord.width > size.x) { x -= (x + this.calendar.coord.width - size.x); } if (y + this.calendar.coord.height > size.y) { y -= (y + this.calendar.coord.height - size.y); } this.calendar.setStyles({ left: x + 'px', top: y + 'px' }); if (window.ie6) { this.iframe.setStyles({ height: this.calendar.coord.height + 'px', left: x + 'px', top: y + 'px', width: this.calendar.coord.width + 'px' }); } this.display(cal); this.fx.start(0, 1); } }, // unformat: takes a value from an input and parses the d, m and y elements // @param val (string) // @param f (string) any combination of punctuation / separators and d, j, D, l, S, m, n, F, M, y, Y // @returns array unformat: function(val, f) { f = f.escapeRegExp(); var re = { d: '([0-9]{2})', j: '([0-9]{1,2})', D: '(' + this.options.days.map(function(day) { return day.substr(0, 3); }).join('|') + ')', l: '(' + this.options.days.join('|') + ')', S: '(st|nd|rd|th)', F: '(' + this.options.months.join('|') + ')', m: '([0-9]{2})', M: '(' + this.options.months.map(function(month) { return month.substr(0, 3); }).join('|') + ')', n: '([0-9]{1,2})', Y: '([0-9]{4})', y: '([0-9]{2})' } var arr = []; // array of indexes var g = ''; // convert our format string to regexp for (var i = 0; i < f.length; i++) { var c = f.charAt(i); if (re[c]) { arr.push(c); g += re[c]; } else { g += c; } } // match against date var matches = val.match('^' + g + '$'); var dates = new Array(3); if (matches) { matches = matches.slice(1); // remove first match which is the date arr.each(function(c, i) { i = matches[i]; switch(c) { // year cases case 'y': i = '19' + i; // 2 digit year assumes 19th century (same as JS) case 'Y': dates[0] = i.toInt(); break; // month cases case 'F': i = i.substr(0, 3); case 'M': i = this.options.months.map(function(month) { return month.substr(0, 3); }).indexOf(i) + 1; case 'm': case 'n': dates[1] = i.toInt() - 1; break; // day cases case 'd': case 'j': dates[2] = i.toInt(); break; } }, this); } return dates; }, // value: returns day value of calendar if set // @param cal (obj) // @returns day (int) or null value: function(cal) { var day = null; if (cal.val) { if (cal.year == cal.val.getFullYear() && cal.month == cal.val.getMonth()) { day = cal.val.getDate(); } } return day; }, // values: returns the years, months (for curr year) and days (for curr month and year) for the calendar // @param cal (obj) // @returns obj values: function(cal) { var years, months, days; cal.els.each(function(el) { if (el.getTag() == 'select') { if (el.format.test('(y|Y)')) { // search for a year select years = []; el.getChildren().each(function(option) { // get options var values = this.unformat(option.value, el.format); if (!years.contains(values[0])) { years.push(values[0]); } // add to years array }, this); years.sort(this.sort); } if (el.format.test('(F|m|M|n)')) { // search for a month select months = []; // 0 - 11 should be el.getChildren().each(function(option) { // get options var values = this.unformat(option.value, el.format); if ($type(values[0]) != 'number' || values[0] == cal.year) { // if it's a year / month combo for curr year, or simply a month select if (!months.contains(values[1])) { months.push(values[1]); } // add to months array } }, this); months.sort(this.sort); } if (el.format.test('(d|j)') && !el.format.test('^(d|j)$')) { // search for a day select, but NOT a days only select days = []; // 1 - 31 el.getChildren().each(function(option) { // get options var values = this.unformat(option.value, el.format); // in the special case of days we dont want the value if its a days only select // otherwise that will screw up the options rebuilding // we will take the values if they are exact dates though if (values[0] == cal.year && values[1] == cal.month) { if (!days.contains(values[2])) { days.push(values[2]); } // add to days array } }, this); } } }, this); // we start with what would be the first and last days were there no restrictions var first = 1; var last = new Date(cal.year, cal.month + 1, 0).getDate(); // last day of the month // if we're in an out of bounds year if (cal.year == cal.start.getFullYear()) { // in the special case of improved navigation but no months array, we'll need to construct one if (months == null && this.options.navigation == 2) { months = []; for (var i = 0; i < 12; i ++) { if (i >= cal.start.getMonth()) { months.push(i); } } } // if we're in an out of bounds month if (cal.month == cal.start.getMonth()) { first = cal.start.getDate(); // first day equals day of bound } } if (cal.year == cal.end.getFullYear()) { // in the special case of improved navigation but no months array, we'll need to construct one if (months == null && this.options.navigation == 2) { months = []; for (var i = 0; i < 12; i ++) { if (i <= cal.end.getMonth()) { months.push(i); } } } if (cal.month == cal.end.getMonth()) { last = cal.end.getDate(); // last day equals day of bound } } // let's get our invalid days var blocked = this.blocked(cal); // finally we can prepare all the valid days in a neat little array if ($type(days) == 'array') { // somewhere there was a days select days = days.filter(function(day) { if (day >= first && day <= last && !blocked.contains(day)) { return day; } }); } else { // no days select we'll need to construct a valid days array days = []; for (var i = first; i <= last; i++) { if (!blocked.contains(i)) { days.push(i); } } } days.sort(this.sort); // sorting our days will give us first and last of month return { 'days': days, 'months': months, 'years': years }; }, // write: sets calendars value to form elements // @param cal (obj) write: function(cal) { this.rebuild(cal); // in the case of options, we'll need to make sure we have the correct number of days available cal.els.each(function(el) { // then we can set the value to the field el.value = this.format(cal.val, el.format); }, this); }});Calendar.implement(new Events, new Options);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -