-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.datePicker.min.js
8 lines (8 loc) · 15.7 KB
/
jquery.datePicker.min.js
1
2
3
4
5
6
7
8
/**
* Copyright (c) 2008 Kelvin Luck (http://www.kelvinluck.com/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
* .
* $Id: jquery.datePicker.js 108 2011-11-17 21:19:57Z [email protected] $
**/
(function($){$.fn.extend({renderCalendar:function(s){var c=function(a){return document.createElement(a)};s=$.extend({},$.fn.datePicker.defaults,s);if(s.showHeader!=$.dpConst.SHOW_HEADER_NONE){var e=$(c('tr'));for(var i=Date.firstDayOfWeek;i<Date.firstDayOfWeek+7;i++){var f=i%7;var g=Date.dayNames[f];e.append(jQuery(c('th')).attr({'scope':'col','abbr':g,'title':g,'class':(f==0||f==6?'weekend':'weekday')}).html(s.showHeader==$.dpConst.SHOW_HEADER_SHORT?g.substr(0,1):g))}};var h=$(c('table')).attr({'cellspacing':2}).addClass('jCalendar').append((s.showHeader!=$.dpConst.SHOW_HEADER_NONE?$(c('thead')).append(e):c('thead')));var j=$(c('tbody'));var k=(new Date()).zeroTime();k.setHours(12);var l=s.month==undefined?k.getMonth():s.month;var m=s.year||k.getFullYear();var n=(new Date(m,l,1,12,0,0));var o=Date.firstDayOfWeek-n.getDay()+1;if(o>1)o-=7;var p=Math.ceil(((-1*o+1)+n.getDaysInMonth())/7);n.addDays(o-1);var q=function(b){return function(){if(s.hoverClass){var a=$(this);if(!s.selectWeek){a.addClass(s.hoverClass)}else if(b&&!a.is('.disabled')){a.parent().addClass('activeWeekHover')}}}};var t=function(){if(s.hoverClass){var a=$(this);a.removeClass(s.hoverClass);a.parent().removeClass('activeWeekHover')}};var w=0;while(w++<p){var r=jQuery(c('tr'));var u=s.dpController?n>s.dpController.startDate:false;for(var i=0;i<7;i++){var v=n.getMonth()==l;var d=$(c('td')).text(n.getDate()+'').addClass((v?'current-month ':'other-month ')+(n.isWeekend()?'weekend ':'weekday ')+(v&&n.getTime()==k.getTime()?'today ':'')).data('datePickerDate',n.asString()).hover(q(u),t);r.append(d);if(s.renderCallback){s.renderCallback(d,n,l,m)}n=new Date(n.getFullYear(),n.getMonth(),n.getDate()+1,12,0,0)}j.append(r)}h.append(j);return this.each(function(){$(this).empty().append(h)})},datePicker:function(s){if(!$.event._dpCache)$.event._dpCache=[];s=$.extend({},$.fn.datePicker.defaults,s);return this.each(function(){var c=$(this);var f=true;if(!this._dpId){this._dpId=$.guid++;$.event._dpCache[this._dpId]=new DatePicker(this);f=false}if(s.inline){s.createButton=false;s.displayClose=false;s.closeOnSelect=false;c.empty()}var g=$.event._dpCache[this._dpId];g.init(s);if(!f&&s.createButton){g.button=$('<a href="#" class="dp-choose-date" title="'+$.dpText.TEXT_CHOOSE_DATE+'"><img src="/cal.gif" /></a>').bind('click',function(){c.dpDisplay(this);this.blur();return false});c.after(g.button)}if(!f&&c.is(':text')){c.bind('dateSelected',function(e,a,b){this.value=a.asString()}).bind('change',function(){if(this.value==''){g.clearSelected()}else{var d=Date.fromString(this.value);if(d){g.setSelected(d,true,true)}}});if(s.clickInput){c.bind('click',function(){c.trigger('change');c.dpDisplay()})}var d=Date.fromString(this.value);if(this.value!=''&&d){g.setSelected(d,true,true)}}c.addClass('dp-applied')})},dpSetDisabled:function(s){return x.call(this,'setDisabled',s)},dpSetStartDate:function(d){return x.call(this,'setStartDate',d)},dpSetEndDate:function(d){return x.call(this,'setEndDate',d)},dpGetSelected:function(){var c=_getController(this[0]);if(c){return c.getSelected()}return null},dpSetSelected:function(d,v,m,e){if(v==undefined)v=true;if(m==undefined)m=true;if(e==undefined)e=true;return x.call(this,'setSelected',Date.fromString(d),v,m,e)},dpSetDisplayedMonth:function(m,y){return x.call(this,'setDisplayedMonth',Number(m),Number(y),true)},dpDisplay:function(e){return x.call(this,'display',e)},dpSetRenderCallback:function(a){return x.call(this,'setRenderCallback',a)},dpSetPosition:function(v,h){return x.call(this,'setPosition',v,h)},dpSetOffset:function(v,h){return x.call(this,'setOffset',v,h)},dpClose:function(){return x.call(this,'_closeCalendar',false,this[0])},dpRerenderCalendar:function(){return x.call(this,'_rerenderCalendar')},_dpDestroy:function(){}});var x=function(f,a,b,d,e){return this.each(function(){var c=_getController(this);if(c){c[f](a,b,d,e)}})};function DatePicker(a){this.ele=a;this.displayedMonth=null;this.displayedYear=null;this.startDate=null;this.endDate=null;this.showYearNavigation=null;this.closeOnSelect=null;this.displayClose=null;this.rememberViewedMonth=null;this.selectMultiple=null;this.numSelectable=null;this.numSelected=null;this.verticalPosition=null;this.horizontalPosition=null;this.verticalOffset=null;this.horizontalOffset=null;this.button=null;this.renderCallback=[];this.selectedDates={};this.inline=null;this.context='#dp-popup';this.settings={}};$.extend(DatePicker.prototype,{init:function(s){this.setStartDate(s.startDate);this.setEndDate(s.endDate);this.setDisplayedMonth(Number(s.month),Number(s.year));this.setRenderCallback(s.renderCallback);this.showYearNavigation=s.showYearNavigation;this.closeOnSelect=s.closeOnSelect;this.displayClose=s.displayClose;this.rememberViewedMonth=s.rememberViewedMonth;this.selectMultiple=s.selectMultiple;this.numSelectable=s.selectMultiple?s.numSelectable:1;this.numSelected=0;this.verticalPosition=s.verticalPosition;this.horizontalPosition=s.horizontalPosition;this.hoverClass=s.hoverClass;this.setOffset(s.verticalOffset,s.horizontalOffset);this.inline=s.inline;this.settings=s;if(this.inline){this.context=this.ele;this.display()}},setStartDate:function(d){if(d){if(d instanceof Date){this.startDate=d}else{this.startDate=Date.fromString(d)}}if(!this.startDate){this.startDate=(new Date()).zeroTime()}this.setDisplayedMonth(this.displayedMonth,this.displayedYear)},setEndDate:function(d){if(d){if(d instanceof Date){this.endDate=d}else{this.endDate=Date.fromString(d)}}if(!this.endDate){this.endDate=(new Date('12/31/2999'))}if(this.endDate.getTime()<this.startDate.getTime()){this.endDate=this.startDate}this.setDisplayedMonth(this.displayedMonth,this.displayedYear)},setPosition:function(v,h){this.verticalPosition=v;this.horizontalPosition=h},setOffset:function(v,h){this.verticalOffset=parseInt(v)||0;this.horizontalOffset=parseInt(h)||0},setDisabled:function(s){$e=$(this.ele);$e[s?'addClass':'removeClass']('dp-disabled');if(this.button){$but=$(this.button);$but[s?'addClass':'removeClass']('dp-disabled');$but.attr('title',s?'':$.dpText.TEXT_CHOOSE_DATE)}if($e.is(':text')){$e.attr('disabled',s?'disabled':'')}},setDisplayedMonth:function(m,y,a){if(this.startDate==undefined||this.endDate==undefined){return}var s=new Date(this.startDate.getTime());s.setDate(1);var e=new Date(this.endDate.getTime());e.setDate(1);var t;if((!m&&!y)||(isNaN(m)&&isNaN(y))){t=new Date().zeroTime();t.setDate(1)}else if(isNaN(m)){t=new Date(y,this.displayedMonth,1)}else if(isNaN(y)){t=new Date(this.displayedYear,m,1)}else{t=new Date(y,m,1)}if(t.getTime()<s.getTime()){t=s}else if(t.getTime()>e.getTime()){t=e}var b=this.displayedMonth;var c=this.displayedYear;this.displayedMonth=t.getMonth();this.displayedYear=t.getFullYear();if(a&&(this.displayedMonth!=b||this.displayedYear!=c)){this._rerenderCalendar();$(this.ele).trigger('dpMonthChanged',[this.displayedMonth,this.displayedYear])}},setSelected:function(d,v,a,b){if(d<this.startDate||d.zeroTime()>this.endDate.zeroTime()){return}var s=this.settings;if(s.selectWeek){d=d.addDays(-(d.getDay()-Date.firstDayOfWeek+7)%7);if(d<this.startDate){return}}if(v==this.isSelected(d)){return}if(this.selectMultiple==false){this.clearSelected()}else if(v&&this.numSelected==this.numSelectable){return}if(a&&(this.displayedMonth!=d.getMonth()||this.displayedYear!=d.getFullYear())){this.setDisplayedMonth(d.getMonth(),d.getFullYear(),true)}this.selectedDates[d.asString()]=v;this.numSelected+=v?1:-1;var c='td.'+(d.getMonth()==this.displayedMonth?'current-month':'other-month');var e;$(c,this.context).each(function(){if($(this).data('datePickerDate')==d.asString()){e=$(this);if(s.selectWeek){e.parent()[v?'addClass':'removeClass']('selectedWeek')}e[v?'addClass':'removeClass']('selected')}});$('td',this.context).not('.selected')[this.selectMultiple&&this.numSelected==this.numSelectable?'addClass':'removeClass']('unselectable');if(b){var s=this.isSelected(d);$e=$(this.ele);var f=Date.fromString(d.asString());$e.trigger('dateSelected',[f,e,s]);$e.trigger('change')}},isSelected:function(d){return this.selectedDates[d.asString()]},getSelected:function(){var r=[];for(var s in this.selectedDates){if(this.selectedDates[s]==true){r.push(Date.fromString(s))}}return r},clearSelected:function(){this.selectedDates={};this.numSelected=0;$('td.selected',this.context).removeClass('selected').parent().removeClass('selectedWeek')},display:function(d){if($(this.ele).is('.dp-disabled'))return;d=d||this.ele;var c=this;var f=$(d);var g=f.offset();var h;var i;var j;var k;if(c.inline){h=$(this.ele);i={'id':'calendar-'+this.ele._dpId,'class':'dp-popup dp-popup-inline'};$('.dp-popup',h).remove();k={}}else{h=$('body');i={'id':'dp-popup','class':'dp-popup'};k={'top':g.top+c.verticalOffset,'left':g.left+c.horizontalOffset};var l=function(e){var a=e.target;var b=$('#dp-popup')[0];while(true){if(a==b){return true}else if(a==document){c._closeCalendar();return false}else{a=$(a).parent()[0]}}};this._checkMouse=l;c._closeCalendar(true);$(document).bind('keydown.datepicker',function(a){if(a.keyCode==27){c._closeCalendar()}})}if(!c.rememberViewedMonth){var m=this.getSelected()[0];if(m){m=new Date(m);this.setDisplayedMonth(m.getMonth(),m.getFullYear(),false)}}h.append($('<div></div>').attr(i).css(k).append($('<h2></h2>'),$('<div class="dp-nav-prev"></div>').append($('<a class="dp-nav-prev-year" href="#" title="'+$.dpText.TEXT_PREV_YEAR+'"><<</a>').bind('click',function(){return c._displayNewMonth.call(c,this,0,-1)}),$('<a class="dp-nav-prev-month" href="#" title="'+$.dpText.TEXT_PREV_MONTH+'"><</a>').bind('click',function(){return c._displayNewMonth.call(c,this,-1,0)})),$('<div class="dp-nav-next"></div>').append($('<a class="dp-nav-next-year" href="#" title="'+$.dpText.TEXT_NEXT_YEAR+'">>></a>').bind('click',function(){return c._displayNewMonth.call(c,this,0,1)}),$('<a class="dp-nav-next-month" href="#" title="'+$.dpText.TEXT_NEXT_MONTH+'">></a>').bind('click',function(){return c._displayNewMonth.call(c,this,1,0)})),$('<div class="dp-calendar"></div>')).bgIframe());var n=this.inline?$('.dp-popup',this.context):$('#dp-popup');if(this.showYearNavigation==false){$('.dp-nav-prev-year, .dp-nav-next-year',c.context).css('display','none')}if(this.displayClose){n.append($('<a href="#" id="dp-close">'+$.dpText.TEXT_CLOSE+'</a>').bind('click',function(){c._closeCalendar();return false}))}c._renderCalendar();$(this.ele).trigger('dpDisplayed',n);if(!c.inline){if(this.verticalPosition==$.dpConst.POS_BOTTOM){n.css('top',g.top+f.height()-n.height()+c.verticalOffset)}if(this.horizontalPosition==$.dpConst.POS_RIGHT){n.css('left',g.left+f.width()-n.width()+c.horizontalOffset)}$(document).bind('mousedown.datepicker',this._checkMouse)}},setRenderCallback:function(a){if(a==null)return;if(a&&typeof(a)=='function'){a=[a]}this.renderCallback=this.renderCallback.concat(a)},cellRender:function(f,g,h,i){var c=this.dpController;var d=new Date(g.getTime());f.bind('click',function(){var a=$(this);if(!a.is('.disabled')){c.setSelected(d,!a.is('.selected')||!c.selectMultiple,false,true);if(c.closeOnSelect){if(c.settings.autoFocusNextInput){var b=c.ele;var e=false;$(':input',b.form).each(function(){if(e){$(this).focus();return false}if(this==b){e=true}})}else{c.ele.focus()}c._closeCalendar()}}});if(c.isSelected(d)){f.addClass('selected');if(c.settings.selectWeek){f.parent().addClass('selectedWeek')}}else if(c.selectMultiple&&c.numSelected==c.numSelectable){f.addClass('unselectable')}},_applyRenderCallbacks:function(){var c=this;$('td',this.context).each(function(){for(var i=0;i<c.renderCallback.length;i++){$td=$(this);c.renderCallback[i].apply(this,[$td,Date.fromString($td.data('datePickerDate')),c.displayedMonth,c.displayedYear])}});return},_displayNewMonth:function(a,m,y){if(!$(a).is('.disabled')){this.setDisplayedMonth(this.displayedMonth+m,this.displayedYear+y,true)}a.blur();return false},_rerenderCalendar:function(){this._clearCalendar();this._renderCalendar()},_renderCalendar:function(){$('h2',this.context).html((new Date(this.displayedYear,this.displayedMonth,1)).asString($.dpText.HEADER_FORMAT));$('.dp-calendar',this.context).renderCalendar($.extend({},this.settings,{month:this.displayedMonth,year:this.displayedYear,renderCallback:this.cellRender,dpController:this,hoverClass:this.hoverClass}));if(this.displayedYear==this.startDate.getFullYear()&&this.displayedMonth==this.startDate.getMonth()){$('.dp-nav-prev-year',this.context).addClass('disabled');$('.dp-nav-prev-month',this.context).addClass('disabled');$('.dp-calendar td.other-month',this.context).each(function(){var a=$(this);if(Number(a.text())>20){a.addClass('disabled')}});var d=this.startDate.getDate();$('.dp-calendar td.current-month',this.context).each(function(){var a=$(this);if(Number(a.text())<d){a.addClass('disabled')}})}else{$('.dp-nav-prev-year',this.context).removeClass('disabled');$('.dp-nav-prev-month',this.context).removeClass('disabled');var d=this.startDate.getDate();if(d>20){var c=this.startDate.getTime();var e=new Date(c);e.addMonths(1);if(this.displayedYear==e.getFullYear()&&this.displayedMonth==e.getMonth()){$('.dp-calendar td.other-month',this.context).each(function(){var a=$(this);if(Date.fromString(a.data('datePickerDate')).getTime()<c){a.addClass('disabled')}})}}}if(this.displayedYear==this.endDate.getFullYear()&&this.displayedMonth==this.endDate.getMonth()){$('.dp-nav-next-year',this.context).addClass('disabled');$('.dp-nav-next-month',this.context).addClass('disabled');$('.dp-calendar td.other-month',this.context).each(function(){var a=$(this);if(Number(a.text())<14){a.addClass('disabled')}});var d=this.endDate.getDate();$('.dp-calendar td.current-month',this.context).each(function(){var a=$(this);if(Number(a.text())>d){a.addClass('disabled')}})}else{$('.dp-nav-next-year',this.context).removeClass('disabled');$('.dp-nav-next-month',this.context).removeClass('disabled');var d=this.endDate.getDate();if(d<13){var f=new Date(this.endDate.getTime());f.addMonths(-1);if(this.displayedYear==f.getFullYear()&&this.displayedMonth==f.getMonth()){$('.dp-calendar td.other-month',this.context).each(function(){var a=$(this);var b=Number(a.text());if(b<13&&b>d){a.addClass('disabled')}})}}}this._applyRenderCallbacks()},_closeCalendar:function(a,b){if(!b||b==this.ele){$(document).unbind('mousedown.datepicker');$(document).unbind('keydown.datepicker');this._clearCalendar();$('#dp-popup a').unbind();$('#dp-popup').empty().remove();if(!a){$(this.ele).trigger('dpClosed',[this.getSelected()])}}},_clearCalendar:function(){$('.dp-calendar td',this.context).unbind();$('.dp-calendar',this.context).empty()}});$.dpConst={SHOW_HEADER_NONE:0,SHOW_HEADER_SHORT:1,SHOW_HEADER_LONG:2,POS_TOP:0,POS_BOTTOM:1,POS_LEFT:0,POS_RIGHT:1,DP_INTERNAL_FOCUS:'dpInternalFocusTrigger'};$.dpText={TEXT_PREV_YEAR:'去年',TEXT_PREV_MONTH:'上月',TEXT_NEXT_YEAR:'明年',TEXT_NEXT_MONTH:'下月',TEXT_CLOSE:'关闭',TEXT_CHOOSE_DATE:'选定日期',HEADER_FORMAT:'mmmm yyyy'};$.dpVersion='$Id: jquery.datePicker.js 108 2011-11-17 21:19:57Z [email protected] $';$.fn.datePicker.defaults={month:undefined,year:undefined,showHeader:$.dpConst.SHOW_HEADER_SHORT,startDate:undefined,endDate:undefined,inline:false,renderCallback:null,createButton:true,showYearNavigation:true,closeOnSelect:true,displayClose:false,selectMultiple:false,numSelectable:Number.MAX_VALUE,clickInput:false,rememberViewedMonth:true,selectWeek:false,verticalPosition:$.dpConst.POS_TOP,horizontalPosition:$.dpConst.POS_LEFT,verticalOffset:0,horizontalOffset:0,hoverClass:'dp-hover',autoFocusNextInput:false};function _getController(a){if(a._dpId)return $.event._dpCache[a._dpId];return false};if($.fn.bgIframe==undefined){$.fn.bgIframe=function(){return this}};$(window).bind('unload',function(){var a=$.event._dpCache||[];for(var i in a){$(a[i].ele)._dpDestroy()}})})(jQuery);