-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicky.min.js
12 lines (12 loc) · 5.73 KB
/
datepicky.min.js
1
2
3
4
5
6
7
8
9
10
11
12
var createDatepicky=function(){function j(a){return a.getFullYear()+"-"+("0"+(a.getMonth()+1)).slice(-2)+"-"+("0"+a.getDate()).slice(-2)}function e(a,b,c,d){a=document.createElement(a);b.appendChild(a);c&&(a.className=c);d&&(a.innerHTML=d);return a}function k(a,b,c){var d=[],g=1,e;do e=new Date(b,a-1,g++,0,0,0,0),d.push(e);while(e.getDate()==g-1);d.pop();g--;for(e=(c+6)%7;d[d.length-1].getDay()!=e;)d.push(new Date(b,a-1,g++,0,0,0,0));for(g=0;d[0].getDay()!=c;)d.unshift(new Date(b,a-1,g--,0,0,0,0));
for(a=[];d.length;){b=[];for(c=1;c<=7;c++)b.push(d.shift());a.push(b)}return a}function i(a){return typeof a=="string"?(a=new Date(a.replace(/-/g,"/")),a.getTime()?a:null):new Date(a.getFullYear(),a.getMonth(),a.getDate())}function f(a,b,c){this.processOptions(c);this.weeksOfDates=k(this.month,this.year,this.firstDayOfWeek);this.inputDateField=document.getElementById(a);h(this.inputDateField,l,this.inputDateFieldChangeHandler,this);a=document.getElementById(b);a.className="Datepicky";this.renderYearMonthSection(a);
this.renderDatesSection(a);this.inputDateField.value?this.inputDateFieldChangeHandler():this.defaultDate?(this.inputDateField.value=j(this.defaultDate),this.inputDateFieldChangeHandler()):this.renderDates()}var h=window.addEventListener?function(a,b,c,d){a.addEventListener(b,function(a){c.call(d,a)},!1)}:function(a,b,c,d){a.attachEvent("on"+b,function(a){c.call(d,a)})},l=function(){var a=document.createElement("input"),b="oninput"in a;b||(a.setAttribute("oninput","return;"),b=typeof a.oninput=="function");
return b}()?"input":"propertychange",m=["Su","Mo","Tu","We","Th","Fr","Sa"],n=[1,2,3,4,5,6,7,8,9,10,11,12];f.prototype.processOptions=function(a){var g;a||(a={});this.weekdayNames=a.weekdayNames||m;this.monthNames=a.monthNames||n;this.invalidDateClassName=a.invalidDateClassName||"";this.disabledDays=a.disabledDays||[];for(var b=a.disabledDates||[],c=[],d=b.length,e=0;e<d;e++){var f=i(b[e]);f&&c.push(f.getTime())}this.disabledTimes=c;this.firstDayOfWeek=typeof a.firstDayOfWeek=="undefined"?1:a.firstDayOfWeek;
if(a.minDate)this.minDate=i(a.minDate),b=this.minDate.getFullYear(),c=this.minDate.getMonth()+1,this.minMonths=(b-1)*12+c;if(a.maxDate)this.maxDate=i(a.maxDate),b=this.maxDate.getFullYear(),c=this.maxDate.getMonth()+1,this.maxMonths=(b-1)*12+c;a.defaultDate?(g=this.defaultDate=i(a.defaultDate),a=g):a.defaultMonth?typeof a.defaultMonth=="string"?(a=a.defaultMonth.split("-"),a=new Date(a[0],a[1]-1)):a=a.defaultMonth:a=new Date;this.month=a.getMonth()+1;this.year=a.getFullYear()};f.prototype.renderYearMonthSection=
function(a){a=e("table",a,"YearMonth");a=e("tbody",a);a=e("tr",a);this.yearDownCell=e("td",a,null,"<");h(this.yearDownCell,"click",function(){this.changeYearAndMonth(-1,0)},this);this.yearCell=e("td",a);this.yearUpCell=e("td",a,null,">");h(this.yearUpCell,"click",function(){this.changeYearAndMonth(1,0)},this);this.monthDownCell=e("td",a,null,"<");h(this.monthDownCell,"click",function(){this.changeYearAndMonth(0,-1)},this);this.monthCell=e("td",a);this.monthUpCell=e("td",a,null,">");h(this.monthUpCell,
"click",function(){this.changeYearAndMonth(0,1)},this);this.updateYearMonthSection()};f.prototype.updateYearMonthSection=function(){this.monthCell.innerHTML=this.monthNames[this.month-1];this.yearCell.innerHTML=this.year;var a=(this.year-1)*12+this.month;this.yearDownCell.className=!this.minDate||a>=this.minMonths+12?"Clickable":"Other";this.yearUpCell.className=!this.maxDate||a<=this.maxMonths-12?"Clickable":"Other";this.monthDownCell.className=!this.minDate||a>this.minMonths?"Clickable":"Other";
this.monthUpCell.className=!this.maxDate||a<this.maxMonths?"Clickable":"Other"};f.prototype.changeYearAndMonth=function(a,b){var c=this.year+a,d=this.month+b;d==13?(d=1,c++):d==0&&(d=12,c--);var e=(c-1)*12+d;if((!this.minDate||e>=this.minMonths)&&(!this.maxDate||e<=this.maxMonths))this.month=d,this.year=c,this.weeksOfDates=k(this.month,this.year,this.firstDayOfWeek),this.renderDates(),this.updateYearMonthSection()};f.prototype.renderDatesSection=function(a){for(var a=e("table",a),b=e("thead",a),b=
e("tr",b),c=0;c<7;c++)e("th",b,null,this.weekdayNames[this.weeksOfDates[0][c].getDay()]);this.tbody=e("tbody",a);h(this.tbody,"click",this.tbodyClickHandler,this)};f.prototype.renderDates=function(){for(var a=this.tbody.rows.length;a>0;a--)this.tbody.deleteRow(-1);for(var a=this.weeksOfDates.length,b=0;b<a;b++)for(var c=e("tr",this.tbody),d=0;d<7;d++){var g=this.weeksOfDates[b][d];e("td",c,this.dateIsSelectable(g)?"Clickable":"Other",g.getDate())}};f.prototype.dateIsSelectable=function(a){var b=a.getMonth()+
1==this.month;if(b)if(this.minDate>a)b=!1;else if(this.maxDate<a)b=!1;else{for(var c=a.getDay(),d=this.disabledDays.length-1;d>=0;d--)if(this.disabledDays[d]==c){b=!1;break}if(b){a=a.getTime();for(d=this.disabledTimes.length-1;d>=0;d--)if(this.disabledTimes[d]==a){b=!1;break}}}return b};f.prototype.inputDateFieldChangeHandler=function(){var a=i(this.inputDateField.value),b;if(a){var c=a.getTime();this.changeYearAndMonth(a.getFullYear()-this.year,a.getMonth()+1-this.month);if(b=this.dateIsSelectable(a)){for(var d=
0,e=0;c!=this.weeksOfDates[d][e].getTime();)e++,e==7&&(e=0,d++);this.selectedDayCell=this.tbody.rows[d].cells[e];this.selectedDayCell.className="Selected"}}if(this.invalidDateClassName)a=a&&b?"":this.invalidDateClassName,a!=this.inputDateField.className&&(this.inputDateField.className=a)};f.prototype.tbodyClickHandler=function(a){a=a.target||a.srcElement;if(a.className=="Clickable"){if(this.selectedDayCell)this.selectedDayCell.className="Clickable";this.selectedDayCell=a;this.selectedDayCell.className=
"Selected";for(var b=0,c=0;a!=this.tbody.rows[b].cells[c];)c++,c==7&&(c=0,b++);this.inputDateField.value=j(this.weeksOfDates[b][c]);if(this.invalidDateClassName)this.inputDateField.className=""}};return function(a,b,c){new f(a,b,c)}}();