📄 jquery-calendar.js
字号:
/* jQuery Calendar v2.7 Written by Marc Grabanski (m@marcgrabanski.com) and enhanced by Keith Wood (kbwood@iprimus.com.au). Copyright (c) 2007 Marc Grabanski (http://marcgrabanski.com/code/jquery-calendar) Dual licensed under the GPL (http://www.gnu.org/licenses/gpl-3.0.txt) and CC (http://creativecommons.org/licenses/by/3.0/) licenses. "Share or Remix it but please Attribute the authors." Date: 09-03-2007 *//* PopUp Calendar manager. Use the singleton instance of this class, popUpCal, to interact with the calendar. Settings for (groups of) calendars are maintained in an instance object (PopUpCalInstance), allowing multiple different settings on the same page. */function PopUpCal() { this._nextId = 0; // Next ID for a calendar instance this._inst = []; // List of instances indexed by ID this._curInst = null; // The current instance in use this._disabledInputs = []; // List of calendar inputs that have been disabled this._popUpShowing = false; // True if the popup calendar is showing , false if not this._inDialog = false; // True if showing within a "dialog", false if not this.regional = []; // Available regional settings, indexed by language code this.regional[''] = { // Default regional settings clearText: 'Clear', // Display text for clear link closeText: 'Close', // Display text for close link prevText: '<Prev', // Display text for previous month link nextText: 'Next>', // Display text for next month link currentText: 'Today', // Display text for current month link dayNames: ['Su','Mo','Tu','We','Th','Fr','Sa'], // Names of days starting at Sunday monthNames: ['January','February','March','April','May','June', 'July','August','September','October','November','December'], // Names of months dateFormat: 'DMY/' // First three are day, month, year in the required order, // fourth (optional) is the separator, e.g. US would be 'MDY/', ISO would be 'YMD-' }; this._defaults = { // Global defaults for all the calendar instances autoPopUp: 'focus', // 'focus' for popup on focus, // 'button' for trigger button, or 'both' for either appendText: '', // Display text following the input box, e.g. showing the format buttonText: '...', // Text for trigger button buttonImage: '', // URL for trigger button image buttonImageOnly: false, // True if the image appears alone, false if it appears on a button closeAtTop: true, // True to have the clear/close at the top, // false to have them at the bottom hideIfNoPrevNext: false, // True to hide next/previous month links // if not applicable, false to just disable them changeMonth: true, // True if month can be selected directly, false if only prev/next changeYear: true, // True if year can be selected directly, false if only prev/next yearRange: '-10:+10', // Range of years to display in drop-down, // either relative to current year (-nn:+nn) or absolute (nnnn:nnnn) firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ... changeFirstDay: true, // True to click on day name to change, false to remain as set showOtherMonths: false, // True to show dates in other months, false to leave blank minDate: null, // The earliest selectable date, or null for no limit maxDate: null, // The latest selectable date, or null for no limit speed: 'medium', // Speed of display/closure customDate: null, // Function that takes a date and returns an array with // [0] = true if selectable, false if not, // [1] = custom CSS class name(s) or '', e.g. popUpCal.noWeekends fieldSettings: null, // Function that takes an input field and // returns a set of custom settings for the calendar onSelect: null // Define a callback function when a date is selected }; $.extend(this._defaults, this.regional['']); this._calendarDiv = $('<div id="calendar_div"></div>'); $(document.body).append(this._calendarDiv); $(document.body).mousedown(this._checkExternalClick);}$.extend(PopUpCal.prototype, { /* Register a new calendar instance - with custom settings. */ _register: function(inst) { var id = this._nextId++; this._inst[id] = inst; return id; }, /* Retrieve a particular calendar instance based on its ID. */ _getInst: function(id) { return this._inst[id] || id; }, /* Override the default settings for all instances of the calendar. @param settings object - the new settings to use as defaults (anonymous object) @return void */ setDefaults: function(settings) { $.extend(this._defaults, settings || {}); }, /* Handle keystrokes. */ _doKeyDown: function(e) { var inst = popUpCal._getInst(this._calId); if (popUpCal._popUpShowing) { switch (e.keyCode) { case 9: popUpCal.hideCalendar(inst, ''); break; // hide on tab out case 13: popUpCal._selectDate(inst); break; // select the value on enter case 27: popUpCal.hideCalendar(inst, inst._get('speed')); break; // hide on escape case 33: popUpCal._adjustDate(inst, -1, (e.ctrlKey ? 'Y' : 'M')); break; // previous month/year on page up/+ ctrl case 34: popUpCal._adjustDate(inst, +1, (e.ctrlKey ? 'Y' : 'M')); break; // next month/year on page down/+ ctrl case 35: if (e.ctrlKey) popUpCal._clearDate(inst); break; // clear on ctrl+end case 36: if (e.ctrlKey) popUpCal._gotoToday(inst); break; // current on ctrl+home case 37: if (e.ctrlKey) popUpCal._adjustDate(inst, -1, 'D'); break; // -1 day on ctrl+left case 38: if (e.ctrlKey) popUpCal._adjustDate(inst, -7, 'D'); break; // -1 week on ctrl+up case 39: if (e.ctrlKey) popUpCal._adjustDate(inst, +1, 'D'); break; // +1 day on ctrl+right case 40: if (e.ctrlKey) popUpCal._adjustDate(inst, +7, 'D'); break; // +1 week on ctrl+down } } else if (e.keyCode == 36 && e.ctrlKey) { // display the calendar on ctrl+home popUpCal.showFor(this); } }, /* Filter entered characters. */ _doKeyPress: function(e) { var inst = popUpCal._getInst(this._calId); var chr = String.fromCharCode(e.charCode == undefined ? e.keyCode : e.charCode); return (chr < ' ' || chr == inst._get('dateFormat').charAt(3) || (chr >= '0' && chr <= '9')); // only allow numbers and separator }, /* Attach the calendar to an input field. */ _connectCalendar: function(target, inst) { var $input = $(target); var appendText = inst._get('appendText'); if (appendText) { $input.after('<span class="calendar_append">' + appendText + '</span>'); } var autoPopUp = inst._get('autoPopUp'); if (autoPopUp == 'focus' || autoPopUp == 'both') { // pop-up calendar when in the marked field $input.focus(this.showFor); } if (autoPopUp == 'button' || autoPopUp == 'both') { // pop-up calendar when button clicked var buttonText = inst._get('buttonText'); var buttonImage = inst._get('buttonImage'); var buttonImageOnly = inst._get('buttonImageOnly'); var trigger = $(buttonImageOnly ? '<img class="calendar_trigger" src="' + buttonImage + '" alt="' + buttonText + '" title="' + buttonText + '"/>' : '<button type="button" class="calendar_trigger">' + (buttonImage != '' ? '<img src="' + buttonImage + '" alt="' + buttonText + '" title="' + buttonText + '"/>' : buttonText) + '</button>'); $input.wrap('<span class="calendar_wrap"></span>').after(trigger); trigger.click(this.showFor); } $input.keydown(this._doKeyDown).keypress(this._doKeyPress); $input[0]._calId = inst._id; }, /* Attach an inline calendar to a div. */ _inlineCalendar: function(target, inst, defaultDate) { $(target).append(inst._calendarDiv); target._calId = inst._id; var date = defaultDate; inst._selectedDay = date.getDate(); inst._selectedMonth = date.getMonth(); inst._selectedYear = date.getFullYear(); popUpCal._adjustDate(inst); }, /* Pop-up the calendar in a "dialog" box. @param dateText string - the initial date to display (in the current format) @param onSelect function - the function(dateText) to call when a date is selected @param settings object - update the dialog calendar instance's settings (anonymous object) @param pos int[2] - coordinates for the dialog's position within the screen leave empty for default (screen centre) @return void */ dialogCalendar: function(dateText, onSelect, settings, pos) { var inst = this._dialogInst; // internal instance if (!inst) { inst = this._dialogInst = new PopUpCalInstance({}, false); this._dialogInput = $('<input type="text" size="1" style="position: absolute; top: -100px;"/>'); this._dialogInput.keydown(this._doKeyDown); $('body').append(this._dialogInput); this._dialogInput[0]._calId = inst._id; } $.extend(inst._settings, settings || {}); this._dialogInput.val(dateText); /* Cross Browser Positioning */ if (self.innerHeight) { // all except Explorer windowWidth = self.innerWidth; windowHeight = self.innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight; } else if (document.body) { // other Explorers windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight; } this._pos = pos || // should use actual width/height below [(windowWidth / 2) - 100, (windowHeight / 2) - 100]; // move input on screen for focus, but hidden behind dialog this._dialogInput.css('left', this._pos[0] + 'px').css('top', this._pos[1] + 'px'); inst._settings.onSelect = onSelect; this._inDialog = true; this._calendarDiv.addClass('calendar_dialog'); this.showFor(this._dialogInput[0]); if ($.blockUI) { $.blockUI(this._calendarDiv); } }, /* Enable the input field(s) for entry. @param inputs element/object - single input field or jQuery collection of input fields @return void */ enableFor: function(inputs) { inputs = (inputs.jquery ? inputs : $(inputs)); inputs.each(function() { this.disabled = false; $('../button.calendar_trigger', this).each(function() { this.disabled = false; }); $('../img.calendar_trigger',this).css({opacity:'1.0',cursor:''}); var $this = this; popUpCal._disabledInputs = $.map(popUpCal._disabledInputs, function(value) { return (value == $this ? null : value); }); // delete entry }); }, /* Disable the input field(s) from entry. @param inputs element/object - single input field or jQuery collection of input fields @return void */ disableFor: function(inputs) { inputs = (inputs.jquery ? inputs : $(inputs)); inputs.each(function() { this.disabled = true; $('../button.calendar_trigger', this).each(function() { this.disabled = true; }); $('../img.calendar_trigger', this).css({opacity:'0.5',cursor:'default'}); var $this = this; popUpCal._disabledInputs = $.map(popUpCal._disabledInputs, function(value) { return (value == $this ? null : value); }); // delete entry popUpCal._disabledInputs[popUpCal._disabledInputs.length] = this; }); }, /* Update the settings for a calendar attached to an input field or division. @param control element - the input field or div/span attached to the calendar @param settings object - the new settings to update @return void */ reconfigureFor: function(control, settings) { var inst = this._getInst(control._calId); if (inst) { $.extend(inst._settings, settings || {}); this._updateCalendar(inst); } }, /* Set the date for a calendar attached to an input field or division. @param control element - the input field or div/span attached to the calendar @param date Date - the new date @return void */ setDateFor: function(control, date) { var inst = this._getInst(control._calId); if (inst) { inst._setDate(date); } }, /* Retrieve the date for a calendar attached to an input field or division. @param control element - the input field or div/span attached to the calendar @return Date - the current date */ getDateFor: function(control) { var inst = this._getInst(control._calId); return (inst ? inst._getDate() : null); },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -