diff --git a/dist/fDatepicker.js b/dist/fDatepicker.js index 636c9a9..dbdac6f 100644 --- a/dist/fDatepicker.js +++ b/dist/fDatepicker.js @@ -1,4 +1,13 @@ /* ======================================================= + * + * ______ _ + * / ___/ | | + * | / _ __ _____ _ __ ___ / | + * | |_ _ | |/ / / ___ \ | \/ _ \ / __ | + * | _ _/ | _ / | /___\_\ | | | | | | | | + * | | | | | |_____ | | | | | |___| | + * |__/ |_/ \_ _ _/ |_/ \_| \ ____ / + * * * @name datepicker.js * @author Frend @@ -54,12 +63,20 @@ this.i18n = options.i18n; this.selectCallback = options.selectCallback; + // cache after props set + var self = this; + // init scroll event $(window).on('scroll', $.proxy(this.scrollLoad, this)); // init select event this.$container.on('click', 'span[data-date]', $.proxy(this.initEvents, this)); + // init month exchange event + this.$container.on('click', '#prevBtn, #nextBtn', function(event) { + UTILS.monthExchange(event.target, self.tmpYear || self.getInitYear() || self.getStartYear(), (self.tmpYear != 0 && self.tmpMonth >= 0) ? self.tmpMonth : (self.getInitMonth() || self.getStartMonth()), self); + }); + // init datepicker this.init(); } @@ -70,8 +87,8 @@ init: function() { var self = this, - startYear = this.getStartYear(), - startMonth = this.getStartMonth(), + startYear = this.singleFrame ? (this.getInitYear() ? this.getInitYear() : this.getStartYear()) : this.getStartYear(), + startMonth = this.singleFrame ?(this.getInitMonth() ? this.getInitMonth() : this.getStartMonth()) : this.getStartMonth(), endYear = this.getEndYear(), endMonth = this.getEndMonth(); @@ -116,6 +133,14 @@ this.selectCallback.call(this, $this.data('date')); }, + getInitYear: function() { + return this.initDate.getFullYear(); + }, + + getInitMonth: function() { + return this.initDate.getMonth(); + }, + getStartYear: function() { return this.startDate.getFullYear(); }, @@ -132,6 +157,10 @@ return this.endDate.getMonth(); }, + getEndDate: function() { + return this.endDate; + }, + scrollLoad: function() { // when the restFrames is not empty, trigger scrolling to load if (this.restFrames <= 0) return; @@ -197,24 +226,70 @@ return arr; }, + monthExchange: function(target, year, month, datepickerObj) { + var $this = $(target), + // year = year, + // month = month, + className = $this[0].className; + + if (className.indexOf('disable-btn') != -1) return; + // prev month + if (className.indexOf('prev-btn') != -1) { + (month - 1 >= 0) ? (function() { + datepickerObj.tmpYear = year; + datepickerObj.tmpMonth = month - 1; + })() : (function() { + datepickerObj.tmpYear = year - 1; + datepickerObj.tmpMonth = 11; + })(); + datepickerObj.$container.empty().append(this.renderSinglePicker(datepickerObj.tmpYear, datepickerObj.tmpMonth, datepickerObj)); + } + // next month + if (className.indexOf('next-btn') != -1) { + (month + 1 <= 11) ? (function() { + datepickerObj.tmpYear = year; + datepickerObj.tmpMonth = month + 1; + })() : (function() { + datepickerObj.tmpYear = year + 1; + datepickerObj.tmpMonth = 0; + })(); + datepickerObj.$container.empty().append(this.renderSinglePicker(datepickerObj.tmpYear, datepickerObj.tmpMonth, datepickerObj)); + } + }, + renderSinglePicker: function(year, month, datepickerObj) { + var arr = this.fillArr(year, month), + currentDate = this.getCurrentDate(), + endDate = datepickerObj.getEndDate(), + initDate = datepickerObj.initDate.getTime(), + $tpl = this.renderPickerHead(year, month, datepickerObj); + + return $tpl.append(this.renderPickerBody(arr, currentDate, endDate, initDate)); + }, + + renderPickerHead: function(year, month, datepickerObj) { var weeksMap = datepickerObj.i18n ? this.weeksi18n : this.weeks, ym = datepickerObj.i18n ? (this.monthsi18n[month] + ' ' + year) : (year + '年 ' + this.months[month]), + prev = datepickerObj.singleFrame ? '<' : '', + next = datepickerObj.singleFrame ? '>' : '', + hd = prev + ym + next, $tpl = $('
' + - '

' + ym + '

' + - (function() { - var th = ''; - - for (var i = 0; i < weeksMap.length; i++) { - th += '' + weeksMap[i] + ''; - } - return th; - })() + - '
'), - arr = this.fillArr(year, month), - currentDate = this.getCurrentDate(), - initDate = datepickerObj.initDate.getTime(), - tmp = ''; + '

' + hd + '

' + + (function() { + var th = ''; + + for (var i = 0; i < weeksMap.length; i++) { + th += '' + weeksMap[i] + ''; + } + return th; + })() + + ''); + + return $tpl; + }, + + renderPickerBody: function(arr, currentDate, endDate, initDate) { + var tmp = ''; for (var i = 0; i < arr.length; i++) { arr[i] == undefined ? @@ -227,7 +302,7 @@ className = ''; // is out of date - className += new Date(arr[i]) < currentDate ? 'is-outdate ' : ''; + className += (new Date(arr[i]) < currentDate || new Date(arr[i]) > endDate ) ? 'is-outdate ' : ''; // is today className += (currentDate.getFullYear() == itemYear && parseInt(currentDate.getMonth()) == itemMonth && parseInt(currentDate.getDate()) == itemDate) ? 'is-today ' : ''; // is init selected @@ -241,7 +316,7 @@ })(i); } - return $tpl.append(tmp); + return tmp; }, renderMutiplePicker: function(startYear, startMonth, endYear, endMonth, datepickerObj) { diff --git a/dist/fDatepicker.min.js b/dist/fDatepicker.min.js index 8cf77ac..eb9f0f5 100644 --- a/dist/fDatepicker.min.js +++ b/dist/fDatepicker.min.js @@ -1 +1 @@ -!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b()}):"undefined"!=typeof module&&module.exports?module.exports=b():window[a]=b()}("Datepicker",function(){"use strict";function a(b){var b=$.extend(a.DEFAULT,b);this.$container=$(b.container),this.initDate="string"==typeof b.initDate?new Date(b.initDate):b.initDate,this.startDate="string"==typeof b.startDate?new Date(b.startDate):b.startDate,this.endDate="string"==typeof b.endDate?new Date(b.endDate):b.endDate,this.initFrames=b.initFrame,this.restFrames=0,this.loadFrames=b.loadFrames,this.tmpYear=0,this.tmpMonth=0,this.loadOffset=b.loadOffset,this.singleFrame=b.singleFrame,this.i18n=b.i18n,this.selectCallback=b.selectCallback,$(window).on("scroll",$.proxy(this.scrollLoad,this)),this.$container.on("click","span[data-date]",$.proxy(this.initEvents,this)),this.init()}a.VERSION="1.0.0",a.DEFAULT={container:"",initDate:"",startDate:"",endDate:"",singleFrame:!1,initFrames:3,loadFrames:3,loadOffset:100,i18n:!1,selectCallback:$.noop},a.prototype={constructor:a,init:function(){var a=this,c=this.getStartYear(),d=this.getStartMonth(),e=this.getEndYear(),f=this.getEndMonth();if(!(c>e||c==e&&d>f)){if(this.singleFrame||c==e&&d==f)return void this.$container.append(b.renderSinglePicker(c,d,a));if(!this.singleFrame)if(this.restFrames=12*(e-c)+f-d-this.initFrames,this.restFrames>0){var g=this.tmpYear=c+(d+this.initFrames>11?1:0),h=this.tmpMonth=(d+this.initFrames-1)%12;this.$container.append(b.renderMutiplePicker(c,d,g,h,a))}else this.$container.append(b.renderMutiplePicker(c,d,e,f,a))}},initEvents:function(a){var b="span"==a.target.tagName.toLowerCase()?$(a.target):$(a.target).parents("span");b.is(".is-outdate")||b.is(".is-today")||(this.$container.find(".selected").removeClass("selected"),b.addClass("selected"),this.initDate=new Date(b.data("date")),this.selectCallback.call(this,b.data("date")))},getStartYear:function(){return this.startDate.getFullYear()},getStartMonth:function(){return this.startDate.getMonth()},getEndYear:function(){return this.endDate.getFullYear()},getEndMonth:function(){return this.endDate.getMonth()},scrollLoad:function(){if(!(this.restFrames<=0)){var a,c,d=this,e=$(window),f=$(document),g=this.tmpYear,h=this.tmpMonth+1;f.height()-e.height()-e.scrollTop()11?1:0),c=this.tmpMonth=(h+this.loadFrames-1)%12,this.$container.append(b.renderMutiplePicker(g,h,a,c,d))))}}};var b={weeks:["日","一","二","三","四","五","六"],weeksi18n:["Sun","Mon","Tues","Wed","Thur","Fri","Sat"],months:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],monthsi18n:["January","February","March","April","May","June","July","August","September","October","November","December"],getCurrentDate:function(){return new Date},getMonthFirstDay:function(a,b){return new Date(a,b,1).getDay()},getMonthLastDay:function(a,b){return new Date(a,b+1,0).getDate()},fillArr:function(a,b){for(var c=this.getMonthFirstDay(a,b),d=this.getMonthLastDay(a,b),e=d,f=c+d>36?new Array(42):c+d>28?new Array(35):new Array(28),g=c,h=0;e>h;h++,g++)f[g]=a+"/"+(b+1)+"/"+(h+1);return f},renderSinglePicker:function(a,b,c){for(var d=c.i18n?this.weeksi18n:this.weeks,e=c.i18n?this.monthsi18n[b]+" "+a:a+"年 "+this.months[b],f=$('

'+e+"

"+function(){for(var a="",b=0;b'+d[b]+"";return a}()+"
"),g=this.fillArr(a,b),h=this.getCurrentDate(),i=c.initDate.getTime(),j="",k=0;k":function(a){var b=g[a].split("/"),c=parseInt(b[0]),d=parseInt(b[1])-1,e=parseInt(b[2]),f="";f+=new Date(g[a])'+e+""}(k);return f.append(j)},renderMutiplePicker:function(a,b,c,d,e){for(var f=c-a,g=12*f+d-b,h=$("
"),i=0;g>=i;i++){var j=(b+i)%12,k=b+i>=12?a+1:a;h.append(this.renderSinglePicker(k,j,e))}return h}};return a}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b()}):"undefined"!=typeof module&&module.exports?module.exports=b():window[a]=b()}("Datepicker",function(){"use strict";function a(c){var c=$.extend(a.DEFAULT,c);this.$container=$(c.container),this.initDate="string"==typeof c.initDate?new Date(c.initDate):c.initDate,this.startDate="string"==typeof c.startDate?new Date(c.startDate):c.startDate,this.endDate="string"==typeof c.endDate?new Date(c.endDate):c.endDate,this.initFrames=c.initFrame,this.restFrames=0,this.loadFrames=c.loadFrames,this.tmpYear=0,this.tmpMonth=0,this.loadOffset=c.loadOffset,this.singleFrame=c.singleFrame,this.i18n=c.i18n,this.selectCallback=c.selectCallback;var d=this;$(window).on("scroll",$.proxy(this.scrollLoad,this)),this.$container.on("click","span[data-date]",$.proxy(this.initEvents,this)),this.$container.on("click","#prevBtn, #nextBtn",function(a){b.monthExchange(a.target,d.tmpYear||d.getInitYear()||d.getStartYear(),0!=d.tmpYear&&d.tmpMonth>=0?d.tmpMonth:d.getInitMonth()||d.getStartMonth(),d)}),this.init()}a.VERSION="1.0.0",a.DEFAULT={container:"",initDate:"",startDate:"",endDate:"",singleFrame:!1,initFrames:3,loadFrames:3,loadOffset:100,i18n:!1,selectCallback:$.noop},a.prototype={constructor:a,init:function(){var a=this,c=this.singleFrame&&this.getInitYear()?this.getInitYear():this.getStartYear(),d=this.singleFrame&&this.getInitMonth()?this.getInitMonth():this.getStartMonth(),e=this.getEndYear(),f=this.getEndMonth();if(!(c>e||c==e&&d>f)){if(this.singleFrame||c==e&&d==f)return void this.$container.append(b.renderSinglePicker(c,d,a));if(!this.singleFrame)if(this.restFrames=12*(e-c)+f-d-this.initFrames,this.restFrames>0){var g=this.tmpYear=c+(d+this.initFrames>11?1:0),h=this.tmpMonth=(d+this.initFrames-1)%12;this.$container.append(b.renderMutiplePicker(c,d,g,h,a))}else this.$container.append(b.renderMutiplePicker(c,d,e,f,a))}},initEvents:function(a){var b="span"==a.target.tagName.toLowerCase()?$(a.target):$(a.target).parents("span");b.is(".is-outdate")||b.is(".is-today")||(this.$container.find(".selected").removeClass("selected"),b.addClass("selected"),this.initDate=new Date(b.data("date")),this.selectCallback.call(this,b.data("date")))},getInitYear:function(){return this.initDate.getFullYear()},getInitMonth:function(){return this.initDate.getMonth()},getStartYear:function(){return this.startDate.getFullYear()},getStartMonth:function(){return this.startDate.getMonth()},getEndYear:function(){return this.endDate.getFullYear()},getEndMonth:function(){return this.endDate.getMonth()},getEndDate:function(){return this.endDate},scrollLoad:function(){if(!(this.restFrames<=0)){var a,c,d=this,e=$(window),f=$(document),g=this.tmpYear,h=this.tmpMonth+1;f.height()-e.height()-e.scrollTop()11?1:0),c=this.tmpMonth=(h+this.loadFrames-1)%12,this.$container.append(b.renderMutiplePicker(g,h,a,c,d))))}}};var b={weeks:["日","一","二","三","四","五","六"],weeksi18n:["Sun","Mon","Tues","Wed","Thur","Fri","Sat"],months:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],monthsi18n:["January","February","March","April","May","June","July","August","September","October","November","December"],getCurrentDate:function(){return new Date},getMonthFirstDay:function(a,b){return new Date(a,b,1).getDay()},getMonthLastDay:function(a,b){return new Date(a,b+1,0).getDate()},fillArr:function(a,b){for(var c=this.getMonthFirstDay(a,b),d=this.getMonthLastDay(a,b),e=d,f=c+d>36?new Array(42):c+d>28?new Array(35):new Array(28),g=c,h=0;e>h;h++,g++)f[g]=a+"/"+(b+1)+"/"+(h+1);return f},monthExchange:function(a,b,c,d){var e=$(a),f=e[0].className;-1==f.indexOf("disable-btn")&&(-1!=f.indexOf("prev-btn")&&(c-1>=0?function(){d.tmpYear=b,d.tmpMonth=c-1}():function(){d.tmpYear=b-1,d.tmpMonth=11}(),d.$container.empty().append(this.renderSinglePicker(d.tmpYear,d.tmpMonth,d))),-1!=f.indexOf("next-btn")&&(11>=c+1?function(){d.tmpYear=b,d.tmpMonth=c+1}():function(){d.tmpYear=b+1,d.tmpMonth=0}(),d.$container.empty().append(this.renderSinglePicker(d.tmpYear,d.tmpMonth,d))))},renderSinglePicker:function(a,b,c){var d=this.fillArr(a,b),e=this.getCurrentDate(),f=c.getEndDate(),g=c.initDate.getTime(),h=this.renderPickerHead(a,b,c);return h.append(this.renderPickerBody(d,e,f,g))},renderPickerHead:function(a,b,c){var d=c.i18n?this.weeksi18n:this.weeks,e=c.i18n?this.monthsi18n[b]+" "+a:a+"年 "+this.months[b],f=c.singleFrame?'<':"",g=c.singleFrame?'>':"",h=f+e+g,i=$('

'+h+"

"+function(){for(var a="",b=0;b'+d[b]+"";return a}()+"
");return i},renderPickerBody:function(a,b,c,d){for(var e="",f=0;f":function(f){var g=a[f].split("/"),h=parseInt(g[0]),i=parseInt(g[1])-1,j=parseInt(g[2]),k="";k+=new Date(a[f])c?"is-outdate ":"",k+=b.getFullYear()==h&&parseInt(b.getMonth())==i&&parseInt(b.getDate())==j?"is-today ":"",k+=-1!=k.indexOf("is-outdate")?"":-1!=k.indexOf("is-today")?"":new Date(a[f]).getTime()==d?"selected":"",(f%7==0||f%7==6)&&(k+=" is-weekend"),e+=''+j+""}(f);return e},renderMutiplePicker:function(a,b,c,d,e){for(var f=c-a,g=12*f+d-b,h=$("
"),i=0;g>=i;i++){var j=(b+i)%12,k=b+i>=12?a+1:a;h.append(this.renderSinglePicker(k,j,e))}return h}};return a}); \ No newline at end of file