+++ /dev/null
-/* jQuery Calendar v2.7\r
- Written by Marc Grabanski (m@marcgrabanski.com) and enhanced by Keith Wood (kbwood@iprimus.com.au).\r
-\r
- Copyright (c) 2007 Marc Grabanski (http://marcgrabanski.com/code/jquery-calendar)\r
- Dual licensed under the GPL (http://www.gnu.org/licenses/gpl-3.0.txt) and \r
- CC (http://creativecommons.org/licenses/by/3.0/) licenses. "Share or Remix it but please Attribute the authors."\r
- Date: 09-03-2007 */\r
-\r
-/* PopUp Calendar manager.\r
- Use the singleton instance of this class, popUpCal, to interact with the calendar.\r
- Settings for (groups of) calendars are maintained in an instance object\r
- (PopUpCalInstance), allowing multiple different settings on the same page. */\r
-function PopUpCal() {\r
- this._nextId = 0; // Next ID for a calendar instance\r
- this._inst = []; // List of instances indexed by ID\r
- this._curInst = null; // The current instance in use\r
- this._disabledInputs = []; // List of calendar inputs that have been disabled\r
- this._popUpShowing = false; // True if the popup calendar is showing , false if not\r
- this._inDialog = false; // True if showing within a "dialog", false if not\r
- this.regional = []; // Available regional settings, indexed by language code\r
- this.regional[''] = { // Default regional settings\r
- clearText: 'Clear', // Display text for clear link\r
- closeText: 'Close', // Display text for close link\r
- prevText: '<Prev', // Display text for previous month link\r
- nextText: 'Next>', // Display text for next month link\r
- currentText: 'Today', // Display text for current month link\r
- dayNames: ['Su','Mo','Tu','We','Th','Fr','Sa'], // Names of days starting at Sunday\r
- monthNames: ['January','February','March','April','May','June',\r
- 'July','August','September','October','November','December'], // Names of months\r
- dateFormat: 'DMY/' // First three are day, month, year in the required order,\r
- // fourth (optional) is the separator, e.g. US would be 'MDY/', ISO would be 'YMD-'\r
- };\r
- this._defaults = { // Global defaults for all the calendar instances\r
- autoPopUp: 'focus', // 'focus' for popup on focus,\r
- // 'button' for trigger button, or 'both' for either\r
- defaultDate: null, // Used when field is blank: actual date,\r
- // +/-number for offset from today, null for today\r
- appendText: '', // Display text following the input box, e.g. showing the format\r
- buttonText: '...', // Text for trigger button\r
- buttonImage: '', // URL for trigger button image\r
- buttonImageOnly: false, // True if the image appears alone, false if it appears on a button\r
- closeAtTop: true, // True to have the clear/close at the top,\r
- // false to have them at the bottom\r
- hideIfNoPrevNext: false, // True to hide next/previous month links\r
- // if not applicable, false to just disable them\r
- changeMonth: true, // True if month can be selected directly, false if only prev/next\r
- changeYear: true, // True if year can be selected directly, false if only prev/next\r
- yearRange: '-10:+10', // Range of years to display in drop-down,\r
- // either relative to current year (-nn:+nn) or absolute (nnnn:nnnn)\r
- firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ...\r
- changeFirstDay: true, // True to click on day name to change, false to remain as set\r
- showOtherMonths: false, // True to show dates in other months, false to leave blank\r
- minDate: null, // The earliest selectable date, or null for no limit\r
- maxDate: null, // The latest selectable date, or null for no limit\r
- speed: 'medium', // Speed of display/closure\r
- customDate: null, // Function that takes a date and returns an array with\r
- // [0] = true if selectable, false if not,\r
- // [1] = custom CSS class name(s) or '', e.g. popUpCal.noWeekends\r
- fieldSettings: null, // Function that takes an input field and\r
- // returns a set of custom settings for the calendar\r
- onSelect: null // Define a callback function when a date is selected\r
- };\r
- $.extend(this._defaults, this.regional['']);\r
- this._calendarDiv = $('<div id="calendar_div"></div>');\r
- $(document.body).append(this._calendarDiv);\r
- $(document.body).mousedown(this._checkExternalClick);\r
-}\r
-\r
-$.extend(PopUpCal.prototype, {\r
- /* Class name added to elements to indicate already configured with a calendar. */\r
- markerClassName: 'hasCalendar',\r
- \r
- /* Register a new calendar instance - with custom settings. */\r
- _register: function(inst) {\r
- var id = this._nextId++;\r
- this._inst[id] = inst;\r
- return id;\r
- },\r
-\r
- /* Retrieve a particular calendar instance based on its ID. */\r
- _getInst: function(id) {\r
- return this._inst[id] || id;\r
- },\r
-\r
- /* Override the default settings for all instances of the calendar. \r
- @param settings object - the new settings to use as defaults (anonymous object)\r
- @return void */\r
- setDefaults: function(settings) {\r
- extendRemove(this._defaults, settings || {});\r
- },\r
-\r
- /* Handle keystrokes. */\r
- _doKeyDown: function(e) {\r
- var inst = popUpCal._getInst(this._calId);\r
- if (popUpCal._popUpShowing) {\r
- switch (e.keyCode) {\r
- case 9: popUpCal.hideCalendar(inst, '');\r
- break; // hide on tab out\r
- case 13: popUpCal._selectDate(inst);\r
- break; // select the value on enter\r
- case 27: popUpCal.hideCalendar(inst, inst._get('speed'));\r
- break; // hide on escape\r
- case 33: popUpCal._adjustDate(inst, -1, (e.ctrlKey ? 'Y' : 'M'));\r
- break; // previous month/year on page up/+ ctrl\r
- case 34: popUpCal._adjustDate(inst, +1, (e.ctrlKey ? 'Y' : 'M'));\r
- break; // next month/year on page down/+ ctrl\r
- case 35: if (e.ctrlKey) popUpCal._clearDate(inst);\r
- break; // clear on ctrl+end\r
- case 36: if (e.ctrlKey) popUpCal._gotoToday(inst);\r
- break; // current on ctrl+home\r
- case 37: if (e.ctrlKey) popUpCal._adjustDate(inst, -1, 'D');\r
- break; // -1 day on ctrl+left\r
- case 38: if (e.ctrlKey) popUpCal._adjustDate(inst, -7, 'D');\r
- break; // -1 week on ctrl+up\r
- case 39: if (e.ctrlKey) popUpCal._adjustDate(inst, +1, 'D');\r
- break; // +1 day on ctrl+right\r
- case 40: if (e.ctrlKey) popUpCal._adjustDate(inst, +7, 'D');\r
- break; // +1 week on ctrl+down\r
- }\r
- }\r
- else if (e.keyCode == 36 && e.ctrlKey) { // display the calendar on ctrl+home\r
- popUpCal.showFor(this);\r
- }\r
- },\r
-\r
- /* Filter entered characters. */\r
- _doKeyPress: function(e) {\r
- var inst = popUpCal._getInst(this._calId);\r
- var chr = String.fromCharCode(e.charCode == undefined ? e.keyCode : e.charCode);\r
- return (chr < ' ' || chr == inst._get('dateFormat').charAt(3) ||\r
- (chr >= '0' && chr <= '9')); // only allow numbers and separator\r
- },\r
-\r
- /* Attach the calendar to an input field. */\r
- _connectCalendar: function(target, inst) {\r
- var input = $(target);\r
- if (this._hasClass(input, this.markerClassName)) {\r
- return;\r
- }\r
- var appendText = inst._get('appendText');\r
- if (appendText) {\r
- input.after('<span class="calendar_append">' + appendText + '</span>');\r
- }\r
- var autoPopUp = inst._get('autoPopUp');\r
- if (autoPopUp == 'focus' || autoPopUp == 'both') { // pop-up calendar when in the marked field\r
- input.focus(this.showFor);\r
- }\r
- if (autoPopUp == 'button' || autoPopUp == 'both') { // pop-up calendar when button clicked\r
- var buttonText = inst._get('buttonText');\r
- var buttonImage = inst._get('buttonImage');\r
- var buttonImageOnly = inst._get('buttonImageOnly');\r
- var trigger = $(buttonImageOnly ? '<img class="calendar_trigger" src="' +\r
- buttonImage + '" alt="' + buttonText + '" title="' + buttonText + '"/>' :\r
- '<button type="button" class="calendar_trigger">' + (buttonImage != '' ?\r
- '<img src="' + buttonImage + '" alt="' + buttonText + '" title="' + buttonText + '"/>' :\r
- buttonText) + '</button>');\r
- input.wrap('<span class="calendar_wrap"></span>').after(trigger);\r
- trigger.click(this.showFor);\r
- }\r
- input.addClass(this.markerClassName).keydown(this._doKeyDown).keypress(this._doKeyPress);\r
- input[0]._calId = inst._id;\r
- },\r
-\r
- /* Attach an inline calendar to a div. */\r
- _inlineCalendar: function(target, inst) {\r
- var input = $(target);\r
- if (this._hasClass(input, this.markerClassName)) {\r
- return;\r
- }\r
- input.addClass(this.markerClassName).append(inst._calendarDiv);\r
- input[0]._calId = inst._id;\r
- },\r
-\r
- /* Does this element have a particular class? */\r
- _hasClass: function(element, className) {\r
- var classes = element.attr('class');\r
- return (classes && classes.indexOf(className) > -1);\r
- },\r
-\r
- /* Pop-up the calendar in a "dialog" box.\r
- @param dateText string - the initial date to display (in the current format)\r
- @param onSelect function - the function(dateText) to call when a date is selected\r
- @param settings object - update the dialog calendar instance's settings (anonymous object)\r
- @param pos int[2] - coordinates for the dialog's position within the screen\r
- leave empty for default (screen centre)\r
- @return void */\r
- dialogCalendar: function(dateText, onSelect, settings, pos) {\r
- var inst = this._dialogInst; // internal instance\r
- if (!inst) {\r
- inst = this._dialogInst = new PopUpCalInstance({}, false);\r
- this._dialogInput = $('<input type="text" size="1" style="position: absolute; top: -100px;"/>');\r
- this._dialogInput.keydown(this._doKeyDown);\r
- $('body').append(this._dialogInput);\r
- this._dialogInput[0]._calId = inst._id;\r
- }\r
- extendRemove(inst._settings, settings || {});\r
- this._dialogInput.val(dateText);\r
- \r
- /* Cross Browser Positioning */\r
- if (self.innerHeight) { // all except Explorer\r
- windowWidth = self.innerWidth;\r
- windowHeight = self.innerHeight;\r
- } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode\r
- windowWidth = document.documentElement.clientWidth;\r
- windowHeight = document.documentElement.clientHeight;\r
- } else if (document.body) { // other Explorers\r
- windowWidth = document.body.clientWidth;\r
- windowHeight = document.body.clientHeight;\r
- } \r
- this._pos = pos || // should use actual width/height below\r
- [(windowWidth / 2) - 100, (windowHeight / 2) - 100];\r
-\r
- // move input on screen for focus, but hidden behind dialog\r
- this._dialogInput.css('left', this._pos[0] + 'px').css('top', this._pos[1] + 'px');\r
- inst._settings.onSelect = onSelect;\r
- this._inDialog = true;\r
- this._calendarDiv.addClass('calendar_dialog');\r
- this.showFor(this._dialogInput[0]);\r
- if ($.blockUI) {\r
- $.blockUI(this._calendarDiv);\r
- }\r
- },\r
-\r
- /* Enable the input field(s) for entry.\r
- @param inputs element/object - single input field or jQuery collection of input fields\r
- @return void */\r
- enableFor: function(inputs) {\r
- inputs = (inputs.jquery ? inputs : $(inputs));\r
- inputs.each(function() {\r
- this.disabled = false;\r
- $('../button.calendar_trigger', this).each(function() { this.disabled = false; });\r
- $('../img.calendar_trigger', this).css({opacity:'1.0',cursor:''});\r
- var $this = this;\r
- popUpCal._disabledInputs = $.map(popUpCal._disabledInputs,\r
- function(value) { return (value == $this ? null : value); }); // delete entry\r
- });\r
- },\r
-\r
- /* Disable the input field(s) from entry.\r
- @param inputs element/object - single input field or jQuery collection of input fields\r
- @return void */\r
- disableFor: function(inputs) {\r
- inputs = (inputs.jquery ? inputs : $(inputs));\r
- inputs.each(function() {\r
- this.disabled = true;\r
- $('../button.calendar_trigger', this).each(function() { this.disabled = true; });\r
- $('../img.calendar_trigger', this).css({opacity:'0.5',cursor:'default'});\r
- var $this = this;\r
- popUpCal._disabledInputs = $.map(popUpCal._disabledInputs,\r
- function(value) { return (value == $this ? null : value); }); // delete entry\r
- popUpCal._disabledInputs[popUpCal._disabledInputs.length] = this;\r
- });\r
- },\r
-\r
- /* Update the settings for a calendar attached to an input field or division.\r
- @param control element - the input field or div/span attached to the calendar or\r
- string - the ID or other jQuery selector of the input field\r
- @param settings object - the new settings to update\r
- @return void */\r
- reconfigureFor: function(control, settings) {\r
- control = (typeof control == 'string' ? $(control)[0] : control);\r
- var inst = this._getInst(control._calId);\r
- if (inst) {\r
- extendRemove(inst._settings, settings || {});\r
- this._updateCalendar(inst);\r
- }\r
- },\r
-\r
- /* Set the date for a calendar attached to an input field or division.\r
- @param control element - the input field or div/span attached to the calendar\r
- @param date Date - the new date\r
- @return void */\r
- setDateFor: function(control, date) {\r
- var inst = this._getInst(control._calId);\r
- if (inst) {\r
- inst._setDate(date);\r
- }\r
- },\r
-\r
- /* Retrieve the date for a calendar attached to an input field or division.\r
- @param control element - the input field or div/span attached to the calendar\r
- @return Date - the current date */\r
- getDateFor: function(control) {\r
- var inst = this._getInst(control._calId);\r
- return (inst ? inst._getDate() : null);\r
- },\r
-\r
- /* Pop-up the calendar for a given input field.\r
- @param target element - the input field attached to the calendar\r
- @return void */\r
- showFor: function(target) {\r
- var input = (target.nodeName && target.nodeName.toLowerCase() == 'input' ? target : this);\r
- if (input.nodeName.toLowerCase() != 'input') { // find from button/image trigger\r
- input = $('input', input.parentNode)[0];\r
- }\r
- if (popUpCal._lastInput == input) { // already here\r
- return;\r
- }\r
- for (var i = 0; i < popUpCal._disabledInputs.length; i++) { // check not disabled\r
- if (popUpCal._disabledInputs[i] == input) {\r
- return;\r
- }\r
- }\r
- var inst = popUpCal._getInst(input._calId);\r
- var fieldSettings = inst._get('fieldSettings');\r
- extendRemove(inst._settings, (fieldSettings ? fieldSettings(input) : {}));\r
- popUpCal.hideCalendar(inst, '');\r
- popUpCal._lastInput = input;\r
- inst._setDateFromField(input);\r
- if (popUpCal._inDialog) { // hide cursor\r
- input.value = '';\r
- }\r
- if (!popUpCal._pos) { // position below input\r
- popUpCal._pos = popUpCal._findPos(input);\r
- popUpCal._pos[1] += input.offsetHeight;\r
- }\r
- inst._calendarDiv.css('position', (popUpCal._inDialog && $.blockUI ? 'static' : 'absolute')).\r
- css('left', popUpCal._pos[0] + 'px').css('top', popUpCal._pos[1] + 'px');\r
- popUpCal._pos = null;\r
- popUpCal._showCalendar(inst);\r
- },\r
-\r
- /* Construct and display the calendar. */\r
- _showCalendar: function(id) {\r
- var inst = this._getInst(id);\r
- popUpCal._updateCalendar(inst);\r
- if (!inst._inline) {\r
- var speed = inst._get('speed');\r
- inst._calendarDiv.show(speed, function() {\r
- popUpCal._popUpShowing = true;\r
- popUpCal._afterShow(inst);\r
- });\r
- if (speed == '') {\r
- popUpCal._popUpShowing = true;\r
- popUpCal._afterShow(inst);\r
- }\r
- if (inst._input[0].type != 'hidden') {\r
- inst._input[0].focus();\r
- }\r
- this._curInst = inst;\r
- }\r
- },\r
-\r
- /* Generate the calendar content. */\r
- _updateCalendar: function(inst) {\r
- inst._calendarDiv.empty().append(inst._generateCalendar());\r
- if (inst._input && inst._input[0].type != 'hidden') {\r
- inst._input[0].focus();\r
- }\r
- },\r
-\r
- /* Tidy up after displaying the calendar. */\r
- _afterShow: function(inst) {\r
- if ($.browser.msie) { // fix IE < 7 select problems\r
- $('#calendar_cover').css({width: inst._calendarDiv[0].offsetWidth + 4,\r
- height: inst._calendarDiv[0].offsetHeight + 4});\r
- }\r
- // re-position on screen if necessary\r
- var calDiv = inst._calendarDiv[0];\r
- var pos = popUpCal._findPos(inst._input[0]);\r
- // Get browser width and X value (IE6+, FF, Safari, Opera)\r
- if( typeof( window.innerWidth ) == 'number' ) {\r
- browserWidth = window.innerWidth;\r
- } else {\r
- browserWidth = document.documentElement.clientWidth;\r
- }\r
- if ( document.documentElement && (document.documentElement.scrollLeft)) {\r
- browserX = document.documentElement.scrollLeft; \r
- } else {\r
- browserX = document.body.scrollLeft;\r
- }\r
- // Reposition calendar if outside the browser window.\r
- if ((calDiv.offsetLeft + calDiv.offsetWidth) >\r
- (browserWidth + browserX) ) {\r
- inst._calendarDiv.css('left', (pos[0] + inst._input[0].offsetWidth - calDiv.offsetWidth) + 'px');\r
- }\r
- // Get browser height and Y value (IE6+, FF, Safari, Opera)\r
- if( typeof( window.innerHeight ) == 'number' ) {\r
- browserHeight = window.innerHeight;\r
- } else {\r
- browserHeight = document.documentElement.clientHeight;\r
- }\r
- if ( document.documentElement && (document.documentElement.scrollTop)) {\r
- browserTopY = document.documentElement.scrollTop;\r
- } else {\r
- browserTopY = document.body.scrollTop;\r
- }\r
- // Reposition calendar if outside the browser window.\r
- if ((calDiv.offsetTop + calDiv.offsetHeight) >\r
- (browserTopY + browserHeight) ) {\r
- inst._calendarDiv.css('top', (pos[1] - calDiv.offsetHeight) + 'px');\r
- }\r
- },\r
-\r
- /* Hide the calendar from view.\r
- @param id string/object - the ID of the current calendar instance,\r
- or the instance itself\r
- @param speed string - the speed at which to close the calendar\r
- @return void */\r
- hideCalendar: function(id, speed) {\r
- var inst = this._getInst(id);\r
- if (popUpCal._popUpShowing) {\r
- speed = (speed != null ? speed : inst._get('speed'));\r
- inst._calendarDiv.hide(speed, function() {\r
- popUpCal._tidyDialog(inst);\r
- });\r
- if (speed == '') {\r
- popUpCal._tidyDialog(inst);\r
- }\r
- popUpCal._popUpShowing = false;\r
- popUpCal._lastInput = null;\r
- inst._settings.prompt = null;\r
- if (popUpCal._inDialog) {\r
- popUpCal._dialogInput.css('position', 'absolute').\r
- css('left', '0px').css('top', '-100px');\r
- if ($.blockUI) {\r
- $.unblockUI();\r
- $('body').append(this._calendarDiv);\r
- }\r
- }\r
- popUpCal._inDialog = false;\r
- }\r
- popUpCal._curInst = null;\r
- },\r
-\r
- /* Tidy up after a dialog display. */\r
- _tidyDialog: function(inst) {\r
- inst._calendarDiv.removeClass('calendar_dialog');\r
- $('.calendar_prompt', inst._calendarDiv).remove();\r
- },\r
-\r
- /* Close calendar if clicked elsewhere. */\r
- _checkExternalClick: function(event) {\r
- if (!popUpCal._curInst) {\r
- return;\r
- }\r
- var target = $(event.target);\r
- if( (target.parents("#calendar_div").length == 0)\r
- && (target.attr('class') != 'calendar_trigger')\r
- && popUpCal._popUpShowing \r
- && !(popUpCal._inDialog && $.blockUI) )\r
- {\r
- popUpCal.hideCalendar(popUpCal._curInst, '');\r
- }\r
- },\r
-\r
- /* Adjust one of the date sub-fields. */\r
- _adjustDate: function(id, offset, period) {\r
- var inst = this._getInst(id);\r
- inst._adjustDate(offset, period);\r
- this._updateCalendar(inst);\r
- },\r
-\r
- /* Action for current link. */\r
- _gotoToday: function(id) {\r
- var date = new Date();\r
- var inst = this._getInst(id);\r
- inst._selectedDay = date.getDate();\r
- inst._selectedMonth = date.getMonth();\r
- inst._selectedYear = date.getFullYear();\r
- this._adjustDate(inst);\r
- },\r
-\r
- /* Action for selecting a new month/year. */\r
- _selectMonthYear: function(id, select, period) {\r
- var inst = this._getInst(id);\r
- inst._selectingMonthYear = false;\r
- inst[period == 'M' ? '_selectedMonth' : '_selectedYear'] =\r
- select.options[select.selectedIndex].value - 0;\r
- this._adjustDate(inst);\r
- },\r
-\r
- /* Restore input focus after not changing month/year. */\r
- _clickMonthYear: function(id) {\r
- var inst = this._getInst(id);\r
- if (inst._input && inst._selectingMonthYear && !$.browser.msie) {\r
- inst._input[0].focus();\r
- }\r
- inst._selectingMonthYear = !inst._selectingMonthYear;\r
- },\r
-\r
- /* Action for changing the first week day. */\r
- _changeFirstDay: function(id, a) {\r
- var inst = this._getInst(id);\r
- var dayNames = inst._get('dayNames');\r
- var value = a.firstChild.nodeValue;\r
- for (var i = 0; i < 7; i++) {\r
- if (dayNames[i] == value) {\r
- inst._settings.firstDay = i;\r
- break;\r
- }\r
- }\r
- this._updateCalendar(inst);\r
- },\r
-\r
- /* Action for selecting a day. */\r
- _selectDay: function(id, td) {\r
- var inst = this._getInst(id);\r
- inst._selectedDay = $("a", td).html();\r
- this._selectDate(id);\r
- },\r
-\r
- /* Erase the input field and hide the calendar. */\r
- _clearDate: function(id) {\r
- this._selectDate(id, '');\r
- },\r
-\r
- /* Update the input field with the selected date. */\r
- _selectDate: function(id, dateStr) {\r
- var inst = this._getInst(id);\r
- dateStr = (dateStr != null ? dateStr : inst._formatDate());\r
- if (inst._input) {\r
- inst._input.val(dateStr);\r
- }\r
- var onSelect = inst._get('onSelect');\r
- if (onSelect) {\r
- onSelect(dateStr, inst); // trigger custom callback\r
- }\r
- else {\r
- inst._input.trigger('change'); // fire the change event\r
- }\r
- if (inst._inline) {\r
- this._updateCalendar(inst);\r
- }\r
- else {\r
- this.hideCalendar(inst, inst._get('speed'));\r
- }\r
- },\r
-\r
- /* Set as customDate function to prevent selection of weekends.\r
- @param date Date - the date to customise\r
- @return [boolean, string] - is this date selectable?, what is its CSS class? */\r
- noWeekends: function(date) {\r
- var day = date.getDay();\r
- return [(day > 0 && day < 6), ''];\r
- },\r
-\r
- /* Find an object's position on the screen. */\r
- _findPos: function(obj) {\r
- while (obj && (obj.type == 'hidden' || obj.nodeType != 1)) {\r
- obj = obj.nextSibling;\r
- }\r
- var curleft = curtop = 0;\r
- if (obj && obj.offsetParent) {\r
- curleft = obj.offsetLeft;\r
- curtop = obj.offsetTop;\r
- while (obj = obj.offsetParent) {\r
- var origcurleft = curleft;\r
- curleft += obj.offsetLeft;\r
- if (curleft < 0) {\r
- curleft = origcurleft;\r
- }\r
- curtop += obj.offsetTop;\r
- }\r
- }\r
- return [curleft,curtop];\r
- }\r
-});\r
-\r
-/* Individualised settings for calendars applied to one or more related inputs.\r
- Instances are managed and manipulated through the PopUpCal manager. */\r
-function PopUpCalInstance(settings, inline) {\r
- this._id = popUpCal._register(this);\r
- this._selectedDay = 0;\r
- this._selectedMonth = 0; // 0-11\r
- this._selectedYear = 0; // 4-digit year\r
- this._input = null; // The attached input field\r
- this._inline = inline; // True if showing inline, false if used in a popup\r
- this._calendarDiv = (!inline ? popUpCal._calendarDiv :\r
- $('<div id="calendar_div_' + this._id + '" class="calendar_inline"></div>'));\r
- // customise the calendar object - uses manager defaults if not overridden\r
- this._settings = extendRemove({}, settings || {}); // clone\r
- if (inline) {\r
- this._setDate(this._getDefaultDate());\r
- }\r
-}\r
-\r
-$.extend(PopUpCalInstance.prototype, {\r
- /* Get a setting value, defaulting if necessary. */\r
- _get: function(name) {\r
- return (this._settings[name] != null ? this._settings[name] : popUpCal._defaults[name]);\r
- },\r
-\r
- /* Parse existing date and initialise calendar. */\r
- _setDateFromField: function(input) {\r
- this._input = $(input);\r
- var dateFormat = this._get('dateFormat');\r
- var currentDate = this._input.val().split(dateFormat.charAt(3));\r
- if (currentDate.length == 3) {\r
- this._currentDay = parseInt(currentDate[dateFormat.indexOf('D')], 10);\r
- this._currentMonth = parseInt(currentDate[dateFormat.indexOf('M')], 10) - 1;\r
- this._currentYear = parseInt(currentDate[dateFormat.indexOf('Y')], 10);\r
- }\r
- else {\r
- var date = this._getDefaultDate();\r
- this._currentDay = date.getDate();\r
- this._currentMonth = date.getMonth();\r
- this._currentYear = date.getFullYear();\r
- }\r
- this._selectedDay = this._currentDay;\r
- this._selectedMonth = this._currentMonth;\r
- this._selectedYear = this._currentYear;\r
- this._adjustDate();\r
- },\r
- \r
- /* Retrieve the default date shown on opening. */\r
- _getDefaultDate: function() {\r
- var offsetDate = function(offset) {\r
- var date = new Date();\r
- date.setDate(date.getDate() + offset);\r
- return date;\r
- };\r
- var defaultDate = this._get('defaultDate');\r
- return (defaultDate == null ? new Date() :\r
- (typeof defaultDate == 'number' ? offsetDate(defaultDate) : defaultDate));\r
- },\r
-\r
- /* Set the date directly. */\r
- _setDate: function(date) {\r
- this._selectedDay = this._currentDay = date.getDate();\r
- this._selectedMonth = this._currentMonth = date.getMonth();\r
- this._selectedYear = this._currentYear = date.getFullYear();\r
- this._adjustDate();\r
- },\r
-\r
- /* Retrieve the date directly. */\r
- _getDate: function() {\r
- return new Date(this._currentYear, this._currentMonth, this._currentDay);\r
- },\r
-\r
- /* Generate the HTML for the current state of the calendar. */\r
- _generateCalendar: function() {\r
- var today = new Date();\r
- today = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // clear time\r
- // build the calendar HTML\r
- var controls = '<div class="calendar_control">' +\r
- '<a class="calendar_clear" onclick="popUpCal._clearDate(' + this._id + ');">' +\r
- this._get('clearText') + '</a>' +\r
- '<a class="calendar_close" onclick="popUpCal.hideCalendar(' + this._id + ');">' +\r
- this._get('closeText') + '</a></div>';\r
- var prompt = this._get('prompt');\r
- var closeAtTop = this._get('closeAtTop');\r
- var hideIfNoPrevNext = this._get('hideIfNoPrevNext');\r
- // controls and links\r
- var html = (prompt ? '<div class="calendar_prompt">' + prompt + '</div>' : '') +\r
- (closeAtTop && !this._inline ? controls : '') + '<div class="calendar_links">' +\r
- (this._canAdjustMonth(-1) ? '<a class="calendar_prev" ' +\r
- 'onclick="popUpCal._adjustDate(' + this._id + ', -1, \'M\');">' + this._get('prevText') + '</a>' :\r
- (hideIfNoPrevNext ? '' : '<label class="calendar_prev">' + this._get('prevText') + '</label>')) +\r
- (this._isInRange(today) ? '<a class="calendar_current" ' +\r
- 'onclick="popUpCal._gotoToday(' + this._id + ');">' + this._get('currentText') + '</a>' : '') +\r
- (this._canAdjustMonth(+1) ? '<a class="calendar_next" ' +\r
- 'onclick="popUpCal._adjustDate(' + this._id + ', +1, \'M\');">' + this._get('nextText') + '</a>' :\r
- (hideIfNoPrevNext ? '' : '<label class="calendar_next">' + this._get('nextText') + '</label>')) +\r
- '</div><div class="calendar_header">';\r
- var minDate = this._get('minDate');\r
- var maxDate = this._get('maxDate');\r
- // month selection\r
- var monthNames = this._get('monthNames');\r
- if (!this._get('changeMonth')) {\r
- html += monthNames[this._selectedMonth] + ' ';\r
- }\r
- else {\r
- var inMinYear = (minDate && minDate.getFullYear() == this._selectedYear);\r
- var inMaxYear = (maxDate && maxDate.getFullYear() == this._selectedYear);\r
- html += '<select class="calendar_newMonth" ' +\r
- 'onchange="popUpCal._selectMonthYear(' + this._id + ', this, \'M\');" ' +\r
- 'onclick="popUpCal._clickMonthYear(' + this._id + ');">';\r
- for (var month = 0; month < 12; month++) {\r
- if ((!inMinYear || month >= minDate.getMonth()) &&\r
- (!inMaxYear || month <= maxDate.getMonth())) {\r
- html += '<option value="' + month + '"' +\r
- (month == this._selectedMonth ? ' selected="selected"' : '') +\r
- '>' + monthNames[month] + '</option>';\r
- }\r
- }\r
- html += '</select>';\r
- }\r
- // year selection\r
- if (!this._get('changeYear')) {\r
- html += this._selectedYear;\r
- }\r
- else {\r
- // determine range of years to display\r
- var years = this._get('yearRange').split(':');\r
- var year = 0;\r
- var endYear = 0;\r
- if (years.length != 2) {\r
- year = this._selectedYear - 10;\r
- endYear = this._selectedYear + 10;\r
- }\r
- else if (years[0].charAt(0) == '+' || years[0].charAt(0) == '-') {\r
- year = this._selectedYear + parseInt(years[0], 10);\r
- endYear = this._selectedYear + parseInt(years[1], 10);\r
- }\r
- else {\r
- year = parseInt(years[0], 10);\r
- endYear = parseInt(years[1], 10);\r
- }\r
- year = (minDate ? Math.max(year, minDate.getFullYear()) : year);\r
- endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);\r
- html += '<select class="calendar_newYear" onchange="popUpCal._selectMonthYear(' +\r
- this._id + ', this, \'Y\');" ' + 'onclick="popUpCal._clickMonthYear(' +\r
- this._id + ');">';\r
- for (; year <= endYear; year++) {\r
- html += '<option value="' + year + '"' +\r
- (year == this._selectedYear ? ' selected="selected"' : '') +\r
- '>' + year + '</option>';\r
- }\r
- html += '</select>';\r
- }\r
- html += '</div><table class="calendar" cellpadding="0" cellspacing="0"><thead>' +\r
- '<tr class="calendar_titleRow">';\r
- var firstDay = this._get('firstDay');\r
- var changeFirstDay = this._get('changeFirstDay');\r
- var dayNames = this._get('dayNames');\r
- for (var dow = 0; dow < 7; dow++) { // days of the week\r
- html += '<td>' + (!changeFirstDay ? '' : '<a onclick="popUpCal._changeFirstDay(' +\r
- this._id + ', this);">') + dayNames[(dow + firstDay) % 7] +\r
- (changeFirstDay ? '</a>' : '') + '</td>';\r
- }\r
- html += '</tr></thead><tbody>';\r
- var daysInMonth = this._getDaysInMonth(this._selectedYear, this._selectedMonth);\r
- this._selectedDay = Math.min(this._selectedDay, daysInMonth);\r
- var leadDays = (this._getFirstDayOfMonth(this._selectedYear, this._selectedMonth) - firstDay + 7) % 7;\r
- var currentDate = new Date(this._currentYear, this._currentMonth, this._currentDay);\r
- var selectedDate = new Date(this._selectedYear, this._selectedMonth, this._selectedDay);\r
- var printDate = new Date(this._selectedYear, this._selectedMonth, 1 - leadDays);\r
- var numRows = Math.ceil((leadDays + daysInMonth) / 7); // calculate the number of rows to generate\r
- var customDate = this._get('customDate');\r
- var showOtherMonths = this._get('showOtherMonths');\r
- for (var row = 0; row < numRows; row++) { // create calendar rows\r
- html += '<tr class="calendar_daysRow">';\r
- for (var dow = 0; dow < 7; dow++) { // create calendar days\r
- var customSettings = (customDate ? customDate(printDate) : [true, '']);\r
- var otherMonth = (printDate.getMonth() != this._selectedMonth);\r
- var unselectable = otherMonth || !customSettings[0] ||\r
- (minDate && printDate < minDate) || (maxDate && printDate > maxDate);\r
- html += '<td class="calendar_daysCell' +\r
- ((dow + firstDay + 6) % 7 >= 5 ? ' calendar_weekEndCell' : '') + // highlight weekends\r
- (otherMonth ? ' calendar_otherMonth' : '') + // highlight days from other months\r
- (printDate.getTime() == selectedDate.getTime() ? ' calendar_daysCellOver' : '') + // highlight selected day\r
- (unselectable ? ' calendar_unselectable' : '') + // highlight unselectable days\r
- (otherMonth && !showOtherMonths ? '' : ' ' + customSettings[1] + // highlight custom dates\r
- (printDate.getTime() == currentDate.getTime() ? ' calendar_currentDay' : // highlight current day\r
- (printDate.getTime() == today.getTime() ? ' calendar_today' : ''))) + '"' + // highlight today (if different)\r
- (unselectable ? '' : ' onmouseover="$(this).addClass(\'calendar_daysCellOver\');"' +\r
- ' onmouseout="$(this).removeClass(\'calendar_daysCellOver\');"' +\r
- ' onclick="popUpCal._selectDay(' + this._id + ', this);"') + '>' + // actions\r
- (otherMonth ? (showOtherMonths ? printDate.getDate() : ' ') : // display for other months\r
- (unselectable ? printDate.getDate() : '<a>' + printDate.getDate() + '</a>')) + '</td>'; // display for this month\r
- printDate.setDate(printDate.getDate() + 1);\r
- }\r
- html += '</tr>';\r
- }\r
- html += '</tbody></table>' + (!closeAtTop && !this._inline ? controls : '') +\r
- '<div style="clear: both;"></div>' + (!$.browser.msie ? '' :\r
- '<!--[if lte IE 6.5]><iframe src="javascript:false;" class="calendar_cover"></iframe><![endif]-->');\r
- return html;\r
- },\r
-\r
- /* Adjust one of the date sub-fields. */\r
- _adjustDate: function(offset, period) {\r
- var date = new Date(this._selectedYear + (period == 'Y' ? offset : 0),\r
- this._selectedMonth + (period == 'M' ? offset : 0),\r
- this._selectedDay + (period == 'D' ? offset : 0));\r
- // ensure it is within the bounds set\r
- var minDate = this._get('minDate');\r
- var maxDate = this._get('maxDate');\r
- date = (minDate && date < minDate ? minDate : date);\r
- date = (maxDate && date > maxDate ? maxDate : date);\r
- this._selectedDay = date.getDate();\r
- this._selectedMonth = date.getMonth();\r
- this._selectedYear = date.getFullYear();\r
- },\r
-\r
- /* Find the number of days in a given month. */\r
- _getDaysInMonth: function(year, month) {\r
- return 32 - new Date(year, month, 32).getDate();\r
- },\r
-\r
- /* Find the day of the week of the first of a month. */\r
- _getFirstDayOfMonth: function(year, month) {\r
- return new Date(year, month, 1).getDay();\r
- },\r
-\r
- /* Determines if we should allow a "next/prev" month display change. */\r
- _canAdjustMonth: function(offset) {\r
- var date = new Date(this._selectedYear, this._selectedMonth + offset, 1);\r
- if (offset < 0) {\r
- date.setDate(this._getDaysInMonth(date.getFullYear(), date.getMonth()));\r
- }\r
- return this._isInRange(date);\r
- },\r
-\r
- /* Is the given date in the accepted range? */\r
- _isInRange: function(date) {\r
- var minDate = this._get('minDate');\r
- var maxDate = this._get('maxDate');\r
- return ((!minDate || date >= minDate) && (!maxDate || date <= maxDate));\r
- },\r
-\r
- /* Format the given date for display. */\r
- _formatDate: function() {\r
- var day = this._currentDay = this._selectedDay;\r
- var month = this._currentMonth = this._selectedMonth;\r
- var year = this._currentYear = this._selectedYear;\r
- month++; // adjust javascript month\r
- var dateFormat = this._get('dateFormat');\r
- var dateString = '';\r
- for (var i = 0; i < 3; i++) {\r
- dateString += dateFormat.charAt(3) +\r
- (dateFormat.charAt(i) == 'D' ? (day < 10 ? '0' : '') + day :\r
- (dateFormat.charAt(i) == 'M' ? (month < 10 ? '0' : '') + month :\r
- (dateFormat.charAt(i) == 'Y' ? year : '?')));\r
- }\r
- return dateString.substring(dateFormat.charAt(3) ? 1 : 0);\r
- }\r
-});\r
-\r
-/* jQuery extend now ignores nulls! */\r
-function extendRemove(target, props) {\r
- $.extend(target, props);\r
- for (var name in props) {\r
- if (props[name] == null) {\r
- target[name] = null;\r
- }\r
- }\r
- return target;\r
-}\r
-\r
-/* Attach the calendar to a jQuery selection.\r
- @param settings object - the new settings to use for this calendar instance (anonymous)\r
- @return jQuery object - for chaining further calls */\r
-$.fn.calendar = function(settings) {\r
- return this.each(function() {\r
- // check for settings on the control itself - in namespace 'cal:'\r
- var inlineSettings = null;\r
- for (attrName in popUpCal._defaults) {\r
- var attrValue = this.getAttribute('cal:' + attrName);\r
- if (attrValue) {\r
- inlineSettings = inlineSettings || {};\r
- try {\r
- inlineSettings[attrName] = eval(attrValue);\r
- }\r
- catch (err) {\r
- inlineSettings[attrName] = attrValue;\r
- }\r
- }\r
- }\r
- var nodeName = this.nodeName.toLowerCase();\r
- if (nodeName == 'input') {\r
- var instSettings = (inlineSettings ? $.extend($.extend({}, settings || {}),\r
- inlineSettings || {}) : settings); // clone and customise\r
- var inst = (inst && !inlineSettings ? inst :\r
- new PopUpCalInstance(instSettings, false));\r
- popUpCal._connectCalendar(this, inst);\r
- } \r
- else if (nodeName == 'div' || nodeName == 'span') {\r
- var instSettings = $.extend($.extend({}, settings || {}),\r
- inlineSettings || {}); // clone and customise\r
- var inst = new PopUpCalInstance(instSettings, true);\r
- popUpCal._inlineCalendar(this, inst);\r
- }\r
- });\r
-};\r
-\r
-/* Initialise the calendar. */\r
-$(document).ready(function() {\r
- popUpCal = new PopUpCal(); // singleton instance\r
-});\r