📄 jquery-calendar.js
字号:
/* Pop-up the calendar for a given input field. @param target element - the input field attached to the calendar @return void */ showFor: function(target) { var input = (target.nodeName && target.nodeName.toLowerCase() == 'input' ? target : this); if (input.nodeName.toLowerCase() != 'input') { // find from button/image trigger input = $('input', input.parentNode)[0]; } if (popUpCal._lastInput == input) { // already here return; } for (var i = 0; i < popUpCal._disabledInputs.length; i++) { // check not disabled if (popUpCal._disabledInputs[i] == input) { return; } } var inst = popUpCal._getInst(input._calId); popUpCal.hideCalendar(inst, ''); popUpCal._lastInput = input; inst._setDateFromField(input); if (popUpCal._inDialog) { // hide cursor input.value = ''; } if (!popUpCal._pos) { // position below input popUpCal._pos = popUpCal._findPos(input); popUpCal._pos[1] += input.offsetHeight; } inst._calendarDiv.css('position', (popUpCal._inDialog && $.blockUI ? 'static' : 'absolute')). css('left', popUpCal._pos[0] + 'px').css('top', popUpCal._pos[1] + 'px'); popUpCal._pos = null; var fieldSettings = inst._get('fieldSettings'); $.extend(inst._settings, (fieldSettings ? fieldSettings(input) : {})); popUpCal._showCalendar(inst); }, /* Construct and display the calendar. */ _showCalendar: function(id) { var inst = this._getInst(id); popUpCal._updateCalendar(inst); if (!inst._inline) { var speed = inst._get('speed'); inst._calendarDiv.show(speed, function() { popUpCal._popUpShowing = true; popUpCal._afterShow(inst); }); if (speed == '') { popUpCal._popUpShowing = true; popUpCal._afterShow(inst); } if (inst._input[0].type != 'hidden') { inst._input[0].focus(); } this._curInst = inst; } }, /* Generate the calendar content. */ _updateCalendar: function(inst) { inst._calendarDiv.empty().append(inst._generateCalendar()); if (inst._input && inst._input != 'hidden') { inst._input[0].focus(); } }, /* Tidy up after displaying the calendar. */ _afterShow: function(inst) { if ($.browser.msie) { // fix IE < 7 select problems $('#calendar_cover').css({width: inst._calendarDiv[0].offsetWidth + 4, height: inst._calendarDiv[0].offsetHeight + 4}); } /*// re-position on screen if necessary var calDiv = inst._calendarDiv[0]; var pos = popUpCal._findPos(inst._input[0]); if ((calDiv.offsetLeft + calDiv.offsetWidth) > (document.body.clientWidth + document.body.scrollLeft)) { inst._calendarDiv.css('left', (pos[0] + inst._input[0].offsetWidth - calDiv.offsetWidth) + 'px'); } if ((calDiv.offsetTop + calDiv.offsetHeight) > (document.body.clientHeight + document.body.scrollTop)) { inst._calendarDiv.css('top', (pos[1] - calDiv.offsetHeight) + 'px'); }*/ }, /* Hide the calendar from view. @param id string/object - the ID of the current calendar instance, or the instance itself @param speed string - the speed at which to close the calendar @return void */ hideCalendar: function(id, speed) { var inst = this._getInst(id); if (popUpCal._popUpShowing) { speed = (speed != null ? speed : inst._get('speed')); inst._calendarDiv.hide(speed, function() { popUpCal._tidyDialog(inst); }); if (speed == '') { popUpCal._tidyDialog(inst); } popUpCal._popUpShowing = false; popUpCal._lastInput = null; inst._settings.prompt = null; if (popUpCal._inDialog) { popUpCal._dialogInput.css('position', 'absolute'). css('left', '0px').css('top', '-100px'); if ($.blockUI) { $.unblockUI(); $('body').append(this._calendarDiv); } } popUpCal._inDialog = false; } popUpCal._curInst = null; }, /* Tidy up after a dialog display. */ _tidyDialog: function(inst) { inst._calendarDiv.removeClass('calendar_dialog'); $('.calendar_prompt', inst._calendarDiv).remove(); }, /* Close calendar if clicked elsewhere. */ _checkExternalClick: function(event) { if (!popUpCal._curInst) { return; } var target = $(event.target); if( (target.parents("#calendar_div").length == 0) && (target.attr('class') != 'calendar_trigger') && popUpCal._popUpShowing && !(popUpCal._inDialog && $.blockUI) ) { popUpCal.hideCalendar(popUpCal._curInst, ''); } }, /* Adjust one of the date sub-fields. */ _adjustDate: function(id, offset, period) { var inst = this._getInst(id); inst._adjustDate(offset, period); this._updateCalendar(inst); }, /* Action for current link. */ _gotoToday: function(id) { var date = new Date(); var inst = this._getInst(id); inst._selectedDay = date.getDate(); inst._selectedMonth = date.getMonth(); inst._selectedYear = date.getFullYear(); this._adjustDate(inst); }, /* Action for selecting a new month/year. */ _selectMonthYear: function(id, select, period) { var inst = this._getInst(id); inst._selectingMonthYear = false; inst[period == 'M' ? '_selectedMonth' : '_selectedYear'] = select.options[select.selectedIndex].value - 0; this._adjustDate(inst); }, /* Restore input focus after not changing month/year. */ _clickMonthYear: function(id) { var inst = this._getInst(id); if (inst._input && inst._selectingMonthYear && !$.browser.msie) { inst._input[0].focus(); } inst._selectingMonthYear = !inst._selectingMonthYear; }, /* Action for changing the first week day. */ _changeFirstDay: function(id, a) { var inst = this._getInst(id); var dayNames = inst._get('dayNames'); var value = a.firstChild.nodeValue; for (var i = 0; i < 7; i++) { if (dayNames[i] == value) { inst._settings.firstDay = i; break; } } this._updateCalendar(inst); }, /* Action for selecting a day. */ _selectDay: function(id, td) { var inst = this._getInst(id); inst._selectedDay = $("a", td).html(); this._selectDate(id); }, /* Erase the input field and hide the calendar. */ _clearDate: function(id) { this._selectDate(id, ''); }, /* Update the input field with the selected date. */ _selectDate: function(id, dateStr) { var inst = this._getInst(id); dateStr = (dateStr != null ? dateStr : inst._formatDate()); if (inst._input) { inst._input.val(dateStr); } var onSelect = inst._get('onSelect'); if (onSelect) { onSelect(dateStr); // trigger custom callback } else { inst._input.trigger('change'); // fire the change event } if (inst._inline) { this._updateCalendar(inst); } else { this.hideCalendar(inst, inst._get('speed')); } }, /* Set as customDate function to prevent selection of weekends. @param date Date - the date to customise @return [boolean, string] - is this date selectable?, what is its CSS class? */ noWeekends: function(date) { var day = date.getDay(); return [(day > 0 && day < 6), '']; }, /* Find an object's position on the screen. */ _findPos: function(obj) { if (obj.type == 'hidden') { obj = obj.nextSibling; } var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft; curtop = obj.offsetTop; while (obj = obj.offsetParent) { var origcurleft = curleft; curleft += obj.offsetLeft; if (curleft < 0) { curleft = origcurleft; } curtop += obj.offsetTop; } } return [curleft,curtop]; }});/* Individualised settings for calendars applied to one or more related inputs. Instances are managed and manipulated through the PopUpCal manager. */function PopUpCalInstance(settings, inline, defaultDate) { this._id = popUpCal._register(this); this._selectedDay = 0; this._selectedMonth = 0; // 0-11 this._selectedYear = 0; // 4-digit year this._input = null; // The attached input field this._inline = inline; // True if showing inline, false if used in a popup this._calendarDiv = (!inline ? popUpCal._calendarDiv : $('<div id="calendar_div_' + this._id + '" class="calendar_inline"></div>')); if (inline) { var date = defaultDate; this._currentDate = defaultDate; this._currentDay = date.getDate(); this._currentMonth = date.getMonth(); this._currentYear = date.getFullYear(); } // customise the calendar object - uses manager defaults if not overridden this._settings = $.extend({}, settings || {}); // clone}$.extend(PopUpCalInstance.prototype, {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -