-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.date.min.js
10 lines (10 loc) · 15.1 KB
/
jquery.date.min.js
1
2
3
4
5
6
7
8
9
10
/*!
* Datepicker v0.4.0
* https://github.com/fengyuanchen/datepicker
*
* Copyright (c) 2014-2016 Fengyuan Chen
* Released under the MIT license
*
* Date: 2016-10-15T04:28:08.752Z
*/
!function(t){"function"==typeof define&&define.amd?define("datepicker",["jquery"],t):t("object"==typeof exports?require("jquery"):jQuery)}(function(t){"use strict";function e(t){return j.call(t).slice(8,-1).toLowerCase()}function i(t){return"string"==typeof t}function s(t){return"number"==typeof t&&!isNaN(t)}function a(t){return"undefined"==typeof t}function n(t){return"date"===e(t)}function r(t,e){var i=[];return Array.from?Array.from(t).slice(e||0):(s(e)&&i.push(e),i.slice.apply(t,i))}function h(t,e){var i=r(arguments,2);return function(){return t.apply(e,i.concat(r(arguments)))}}function o(t){return t%4===0&&t%100!==0||t%400===0}function l(t,e){return[31,o(t)?29:28,31,30,31,30,31,31,30,31,30,31][e]}function d(t){var e,i,s=String(t).toLowerCase(),a=s.match(x);if(!a||0===a.length)throw new Error("Invalid date format.");for(t={source:s,parts:a},e=a.length,i=0;i<e;i++)switch(a[i]){case"dd":case"d":t.hasDay=!0;break;case"mm":case"m":t.hasMonth=!0;break;case"yyyy":case"yy":t.hasYear=!0}return t}function u(e,i){i=t.isPlainObject(i)?i:{},i.language&&(i=t.extend({},u.LANGUAGES[i.language],i)),this.$element=t(e),this.options=t.extend({},u.DEFAULTS,i),this.isBuilt=!1,this.isShown=!1,this.isInput=!1,this.isInline=!1,this.initialValue="",this.initialDate=null,this.startDate=null,this.endDate=null,this.init()}var c=t(window),f=window.document,p=t(f),w=window.Number,y="datepicker",m="click."+y,g="keyup."+y,v="focus."+y,D="resize."+y,k="show."+y,b="hide."+y,$="pick."+y,x=/(y|m|d)+/g,C=/\d+/g,S=/^\d{2,4}$/,F=y+"-inline",V=y+"-dropdown",M=y+"-top-left",I=y+"-top-right",T=y+"-bottom-left",Y=y+"-bottom-right",A=[M,I,T,Y].join(" "),P=y+"-hide",N=Math.min,j=Object.prototype.toString;u.prototype={constructor:u,init:function(){var e=this.options,i=this.$element,s=e.startDate,a=e.endDate,n=e.date;this.$trigger=t(e.trigger||i),this.isInput=i.is("input")||i.is("textarea"),this.isInline=e.inline&&(e.container||!this.isInput),this.format=d(e.format),this.oldValue=this.initialValue=this.getValue(),n=this.parseDate(n||this.initialValue),s&&(s=this.parseDate(s),n.getTime()<s.getTime()&&(n=new Date(s)),this.startDate=s),a&&(a=this.parseDate(a),s&&a.getTime()<s.getTime()&&(a=new Date(s)),n.getTime()>a.getTime()&&(n=new Date(a)),this.endDate=a),this.date=n,this.viewDate=new Date(n),this.initialDate=new Date(this.date),this.bind(),(e.autoShow||this.isInline)&&this.show(),e.autoPick&&this.pick()},build:function(){var e,i=this.options,s=this.$element;this.isBuilt||(this.isBuilt=!0,this.$picker=e=t(i.template),this.$week=e.find('[data-view="week"]'),this.$yearsPicker=e.find('[data-view="years picker"]'),this.$yearsPrev=e.find('[data-view="years prev"]'),this.$yearsNext=e.find('[data-view="years next"]'),this.$yearsCurrent=e.find('[data-view="years current"]'),this.$years=e.find('[data-view="years"]'),this.$monthsPicker=e.find('[data-view="months picker"]'),this.$yearPrev=e.find('[data-view="year prev"]'),this.$yearNext=e.find('[data-view="year next"]'),this.$yearCurrent=e.find('[data-view="year current"]'),this.$months=e.find('[data-view="months"]'),this.$daysPicker=e.find('[data-view="days picker"]'),this.$monthPrev=e.find('[data-view="month prev"]'),this.$monthNext=e.find('[data-view="month next"]'),this.$monthCurrent=e.find('[data-view="month current"]'),this.$days=e.find('[data-view="days"]'),this.isInline?t(i.container||s).append(e.addClass(F)):(t(f.body).append(e.addClass(V)),e.addClass(P)),this.fillWeek())},unbuild:function(){this.isBuilt&&(this.isBuilt=!1,this.$picker.remove())},bind:function(){var e=this.options,i=this.$element;t.isFunction(e.show)&&i.on(k,e.show),t.isFunction(e.hide)&&i.on(b,e.hide),t.isFunction(e.pick)&&i.on($,e.pick),this.isInput&&(i.on(g,t.proxy(this.keyup,this)),e.trigger||i.on(v,t.proxy(this.show,this))),this.$trigger.on(m,t.proxy(this.show,this))},unbind:function(){var e=this.options,i=this.$element;t.isFunction(e.show)&&i.off(k,e.show),t.isFunction(e.hide)&&i.off(b,e.hide),t.isFunction(e.pick)&&i.off($,e.pick),this.isInput&&(i.off(g,this.keyup),e.trigger||i.off(v,this.show)),this.$trigger.off(m,this.show)},showView:function(t){var e=this.$yearsPicker,i=this.$monthsPicker,s=this.$daysPicker,a=this.format;if(a.hasYear||a.hasMonth||a.hasDay)switch(w(t)){case 2:case"years":i.addClass(P),s.addClass(P),a.hasYear?(this.fillYears(),e.removeClass(P)):this.showView(0);break;case 1:case"months":e.addClass(P),s.addClass(P),a.hasMonth?(this.fillMonths(),i.removeClass(P)):this.showView(2);break;default:e.addClass(P),i.addClass(P),a.hasDay?(this.fillDays(),s.removeClass(P)):this.showView(1)}},hideView:function(){this.options.autoHide&&this.hide()},place:function(){var t=this.options,e=this.$element,i=this.$picker,s=p.outerWidth(),a=p.outerHeight(),n=e.outerWidth(),r=e.outerHeight(),h=i.width(),o=i.height(),l=e.offset(),d=l.left,u=l.top,c=parseFloat(t.offset)||10,f=M;u>o&&u+r+o>a?(u-=o+c,f=T):u+=r+c,d+h>s&&(d=d+n-h,f=f.replace("left","right")),i.removeClass(A).addClass(f).css({top:u,left:d,zIndex:parseInt(t.zIndex,10)})},trigger:function(e,i){var s=t.Event(e,i);return this.$element.trigger(s),s},createItem:function(e){var i=this.options,s=i.itemTag,a={text:"",view:"",muted:!1,picked:!1,disabled:!1};return t.extend(a,e),"<"+s+" "+(a.disabled?'class="'+i.disabledClass+'"':a.picked?'class="'+i.pickedClass+'"':a.muted?'class="'+i.mutedClass+'"':"")+(a.view?' data-view="'+a.view+'"':"")+">"+a.text+"</"+s+">"},fillAll:function(){this.fillYears(),this.fillMonths(),this.fillDays()},fillWeek:function(){var e,i=this.options,s=parseInt(i.weekStart,10)%7,a=i.daysMin,n="";for(a=t.merge(a.slice(s),a.slice(0,s)),e=0;e<=6;e++)n+=this.createItem({text:a[e]});this.$week.html(n)},fillYears:function(){var e,i=this.options,s=i.disabledClass||"",a=i.yearSuffix||"",n=t.isFunction(i.filter)&&i.filter,r=this.startDate,h=this.endDate,o=this.viewDate,l=o.getFullYear(),d=o.getMonth(),u=o.getDate(),c=this.date,f=c.getFullYear(),p=!1,w=!1,y=!1,m=!1,g=!1,v="",D=-5,k=6;for(e=D;e<=k;e++)c=new Date(l+e,d,u),g=e===D||e===k,m=l+e===f,y=!1,r&&(y=c.getFullYear()<r.getFullYear(),e===D&&(p=y)),!y&&h&&(y=c.getFullYear()>h.getFullYear(),e===k&&(w=y)),!y&&n&&(y=n.call(this.$element,c)===!1),v+=this.createItem({text:l+e,view:y?"year disabled":m?"year picked":"year",muted:g,picked:m,disabled:y});this.$yearsPrev.toggleClass(s,p),this.$yearsNext.toggleClass(s,w),this.$yearsCurrent.toggleClass(s,!0).html(l+D+a+" - "+(l+k)+a),this.$years.html(v)},fillMonths:function(){var e,i=this.options,s=i.disabledClass||"",a=i.monthsShort,n=t.isFunction(i.filter)&&i.filter,r=this.startDate,h=this.endDate,o=this.viewDate,l=o.getFullYear(),d=o.getDate(),u=this.date,c=u.getFullYear(),f=u.getMonth(),p=!1,w=!1,y=!1,m=!1,g="";for(e=0;e<=11;e++)u=new Date(l,e,d),m=l===c&&e===f,y=!1,r&&(p=u.getFullYear()===r.getFullYear(),y=p&&u.getMonth()<r.getMonth()),!y&&h&&(w=u.getFullYear()===h.getFullYear(),y=w&&u.getMonth()>h.getMonth()),!y&&n&&(y=n.call(this.$element,u)===!1),g+=this.createItem({index:e,text:a[e],view:y?"month disabled":m?"month picked":"month",picked:m,disabled:y});this.$yearPrev.toggleClass(s,p),this.$yearNext.toggleClass(s,w),this.$yearCurrent.toggleClass(s,p&&w).html(l+i.yearSuffix||""),this.$months.html(g)},fillDays:function(){var e,i,s,a=this.options,n=a.disabledClass||"",r=a.yearSuffix||"",h=a.monthsShort,o=parseInt(a.weekStart,10)%7,d=t.isFunction(a.filter)&&a.filter,u=this.startDate,c=this.endDate,f=this.viewDate,p=f.getFullYear(),w=f.getMonth(),y=p,m=w,g=p,v=w,D=this.date,k=D.getFullYear(),b=D.getMonth(),$=D.getDate(),x=!1,C=!1,S=!1,F=!1,V=[],M=[],I=[],T=42;for(0===w?(y-=1,m=11):m-=1,e=l(y,m),D=new Date(p,w,1),s=D.getDay()-o,s<=0&&(s+=7),u&&(x=D.getTime()<=u.getTime()),i=e-(s-1);i<=e;i++)D=new Date(y,m,i),S=!1,u&&(S=D.getTime()<u.getTime()),!S&&d&&(S=d.call(this.$element,D)===!1),V.push(this.createItem({text:i,view:"day prev",muted:!0,disabled:S}));for(11===w?(g+=1,v=0):v+=1,e=l(p,w),s=T-(V.length+e),D=new Date(p,w,e),c&&(C=D.getTime()>=c.getTime()),i=1;i<=s;i++)D=new Date(g,v,i),S=!1,c&&(S=D.getTime()>c.getTime()),!S&&d&&(S=d.call(this.$element,D)===!1),M.push(this.createItem({text:i,view:"day next",muted:!0,disabled:S}));for(i=1;i<=e;i++)D=new Date(p,w,i),F=p===k&&w===b&&i===$,S=!1,u&&(S=D.getTime()<u.getTime()),!S&&c&&(S=D.getTime()>c.getTime()),!S&&d&&(S=d.call(this.$element,D)===!1),I.push(this.createItem({text:i,view:S?"day disabled":F?"day picked":"day",picked:F,disabled:S}));this.$monthPrev.toggleClass(n,x),this.$monthNext.toggleClass(n,C),this.$monthCurrent.toggleClass(n,x&&C).html(a.yearFirst?p+r+" "+h[w]:h[w]+" "+p+r),this.$days.html(V.join("")+I.join(" ")+M.join(""))},click:function(e){var i,s,a,n,r,h,o=t(e.target),l=this.viewDate;if(e.stopPropagation(),e.preventDefault(),!o.hasClass("disabled"))switch(i=l.getFullYear(),s=l.getMonth(),a=l.getDate(),h=o.data("view")){case"years prev":case"years next":i="years prev"===h?i-10:i+10,r=o.text(),n=S.test(r),n&&(i=parseInt(r,10),this.date=new Date(i,s,N(a,28))),this.viewDate=new Date(i,s,N(a,28)),this.fillYears(),n&&(this.showView(1),this.pick("year"));break;case"year prev":case"year next":i="year prev"===h?i-1:i+1,this.viewDate=new Date(i,s,N(a,28)),this.fillMonths();break;case"year current":this.format.hasYear&&this.showView(2);break;case"year picked":this.format.hasMonth?this.showView(1):this.hideView();break;case"year":i=parseInt(o.text(),10),this.date=new Date(i,s,N(a,28)),this.viewDate=new Date(i,s,N(a,28)),this.format.hasMonth?this.showView(1):this.hideView(),this.pick("year");break;case"month prev":case"month next":s="month prev"===h?s-1:"month next"===h?s+1:s,this.viewDate=new Date(i,s,N(a,28)),this.fillDays();break;case"month current":this.format.hasMonth&&this.showView(1);break;case"month picked":this.format.hasDay?this.showView(0):this.hideView();break;case"month":s=t.inArray(o.text(),this.options.monthsShort),this.date=new Date(i,s,N(a,28)),this.viewDate=new Date(i,s,N(a,28)),this.format.hasDay?this.showView(0):this.hideView(),this.pick("month");break;case"day prev":case"day next":case"day":s="day prev"===h?s-1:"day next"===h?s+1:s,a=parseInt(o.text(),10),this.date=new Date(i,s,a),this.viewDate=new Date(i,s,a),this.fillDays(),"day"===h&&this.hideView(),this.pick("day");break;case"day picked":this.hideView(),this.pick("day")}},clickDoc:function(t){for(var e,i=t.target,s=this.$trigger[0];i!==f;){if(i===s){e=!0;break}i=i.parentNode}e||this.hide()},keyup:function(){this.update()},getValue:function(){var t=this.$element,e="";return this.isInput?e=t.val():this.isInline?this.options.container&&(e=t.text()):e=t.text(),e},setValue:function(t){var e=this.$element;t=i(t)?t:"",this.isInput?e.val(t):this.isInline?this.options.container&&e.text(t):e.text(t)},show:function(){this.isBuilt||this.build(),this.isShown||this.trigger(k).isDefaultPrevented()||(this.isShown=!0,this.$picker.removeClass(P).on(m,t.proxy(this.click,this)),this.showView(this.options.startView),this.isInline||(c.on(D,this._place=h(this.place,this)),p.on(m,this._clickDoc=h(this.clickDoc,this)),this.place()))},hide:function(){this.isShown&&(this.trigger(b).isDefaultPrevented()||(this.isShown=!1,this.$picker.addClass(P).off(m,this.click),this.isInline||(c.off(D,this._place),p.off(m,this._clickDoc))))},update:function(){var t=this.getValue();t!==this.oldValue&&(this.setDate(t,!0),this.oldValue=t)},pick:function(t){var e=this.$element,i=this.date;this.trigger($,{view:t||"",date:i}).isDefaultPrevented()||(this.setValue(i=this.formatDate(this.date)),this.isInput&&e.trigger("change"))},reset:function(){this.setDate(this.initialDate,!0),this.setValue(this.initialValue),this.isShown&&this.showView(this.options.startView)},getMonthName:function(e,i){var n=this.options,r=n.months;return t.isNumeric(e)?e=w(e):a(i)&&(i=e),i===!0&&(r=n.monthsShort),r[s(e)?e:this.date.getMonth()]},getDayName:function(e,i,n){var r=this.options,h=r.days;return t.isNumeric(e)?e=w(e):(a(n)&&(n=i),a(i)&&(i=e)),h=n===!0?r.daysMin:i===!0?r.daysShort:h,h[s(e)?e:this.date.getDay()]},getDate:function(t){var e=this.date;return t?this.formatDate(e):new Date(e)},setDate:function(e,s){var a=this.options.filter;if(n(e)||i(e)){if(e=this.parseDate(e),t.isFunction(a)&&a.call(this.$element,e)===!1)return;this.date=e,this.viewDate=new Date(e),s||this.pick(),this.isBuilt&&this.fillAll()}},setStartDate:function(t){(n(t)||i(t))&&(this.startDate=this.parseDate(t),this.isBuilt&&this.fillAll())},setEndDate:function(t){(n(t)||i(t))&&(this.endDate=this.parseDate(t),this.isBuilt&&this.fillAll())},parseDate:function(t){var e,s,a,r,h,o,l=this.format,d=[];if(n(t))return new Date(t.getFullYear(),t.getMonth(),t.getDate());if(i(t)&&(d=t.match(C)||[]),t=new Date,s=t.getFullYear(),a=t.getDate(),r=t.getMonth(),e=l.parts.length,d.length===e)for(o=0;o<e;o++)switch(h=parseInt(d[o],10)||1,l.parts[o]){case"dd":case"d":a=h;break;case"mm":case"m":r=h-1;break;case"yy":s=2e3+h;break;case"yyyy":s=h}return new Date(s,r,a)},formatDate:function(t){var e,i,s,a,r,h=this.format,o="";if(n(t))for(o=h.source,i=t.getFullYear(),a={d:t.getDate(),m:t.getMonth()+1,yy:i.toString().substring(2),yyyy:i},a.dd=(a.d<10?"0":"")+a.d,a.mm=(a.m<10?"0":"")+a.m,e=h.parts.length,r=0;r<e;r++)s=h.parts[r],o=o.replace(s,a[s]);return o},destroy:function(){this.unbind(),this.unbuild(),this.$element.removeData(y)}},u.LANGUAGES={},u.DEFAULTS={autoShow:!1,autoHide:!1,autoPick:!1,inline:!1,container:null,trigger:null,language:"",format:"mm/dd/yyyy",date:null,startDate:null,endDate:null,startView:0,weekStart:0,yearFirst:!1,yearSuffix:"",days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],daysShort:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],daysMin:["Su","Mo","Tu","We","Th","Fr","Sa"],months:["January","February","March","April","May","June","July","August","September","October","November","December"],monthsShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],itemTag:"li",mutedClass:"muted",pickedClass:"picked",disabledClass:"disabled",template:'<div class="datepicker-container"><div class="datepicker-panel" data-view="years picker"><ul><li data-view="years prev">‹</li><li data-view="years current"></li><li data-view="years next">›</li></ul><ul data-view="years"></ul></div><div class="datepicker-panel" data-view="months picker"><ul><li data-view="year prev">‹</li><li data-view="year current"></li><li data-view="year next">›</li></ul><ul data-view="months"></ul></div><div class="datepicker-panel" data-view="days picker"><ul><li data-view="month prev">‹</li><li data-view="month current"></li><li data-view="month next">›</li></ul><ul data-view="week"></ul><ul data-view="days"></ul></div></div>',offset:10,zIndex:1e3,filter:null,show:null,hide:null,pick:null},u.setDefaults=function(e){e=t.isPlainObject(e)?e:{},e.language&&(e=t.extend({},u.LANGUAGES[e.language],e)),t.extend(u.DEFAULTS,e)},u.other=t.fn.datepicker,t.fn.datepicker=function(e){var s,n=r(arguments,1);return this.each(function(){var a,r,h=t(this),o=h.data(y);if(!o){if(/destroy/.test(e))return;a=t.extend({},h.data(),t.isPlainObject(e)&&e),h.data(y,o=new u(this,a))}i(e)&&t.isFunction(r=o[e])&&(s=r.apply(o,n))}),a(s)?this:s},t.fn.datepicker.Constructor=u,t.fn.datepicker.languages=u.LANGUAGES,t.fn.datepicker.setDefaults=u.setDefaults,t.fn.datepicker.noConflict=function(){return t.fn.datepicker=u.other,this}});