1 |
- !function(t){var e="undefined"!=typeof window?window:this;"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e):function(e){if(!e.document)throw new Error("document is a undefined");return t(e)}:t(e)}(function(t){function e(){var t=new Date;this.past=!1,this.hours=!1,this.hoursPast=!1,this.currentNode=null,this.minDate=null,this.maxDate=null,this.shield="[]",this.startDate="",this.startJSON={},this.fixDate={y:t.getFullYear(),m:t.getMonth()+1,d:0},this.init()}function a(t,e){function a(a){function n(t){if(w)return!1;var a=t.targetTouches?t.targetTouches[0]:t||event;return s=a.pageX-o,Math.abs(s)>=c&&(w=!0,e&&e(d,s)),a.preventDefault&&a.preventDefault(),!1}function i(){this.onmousemove&&(this.onmousemove=null),this.onmouseup&&(this.onmouseup=null),this.removeEventListener("touchmove",n,!1),this.removeEventListener("touchend",i,!1),w=!1}var s,r=a.targetTouches?a.targetTouches[0]:a||event,o=r.pageX,c=parseInt(document.documentElement.clientWidth/5,10),d=this;this.onmousemove=n,this.onmouseup=i,t.addEventListener("touchmove",n,!1),t.addEventListener("touchend",i,!1)}t.onmousedown=a,t.addEventListener("touchstart",a,!1)}function n(t,e,a){return void 0===t||void 0===e||void 0===a?null:new Date(t,e,a,23,59,59).getTime()}function i(t,e){switch(e.charAt(0)){case"#":return t.querySelector(e);case".":return t.querySelectorAll(e);default:return t.querySelectorAll(e)}}function s(t,e,a){if(t){e=e||{},a=a||"";var n=document.createElement(t);for(var i in e)n.setAttribute(i,e[i]);return n.innerHTML=a,n}}function r(){L.classList.add("close"),setTimeout(function(){L.classList.remove("active","close")},290)}function o(t,e){if(!t)return!1;var a=e||"y/m/d",n=t.split("/"),i=a.match(/[^a-z\d]/i)[0],s={y:n[0],m:d(n[1]),d:d(n[2])};return a.split(/[^a-z]/i).map(function(t){return s[t.charAt(0).toLocaleLowerCase()]}).join(i)}function c(t){if(!t)return[];var e=[];return/^\[|\]$/.test(t)?e=JSON.parse(t.replace(/\'/g,'"')):/^\d+[\/-]\d+[\/-]\d+$/.test(t)&&(e=[t]),e.map(function(t){var e=new Date(t+" 23:59:59");return{y:e.getFullYear(),m:e.getMonth()+1,d:e.getDate()}})}function d(t){return Number(t)<10?"0"+t:t}function l(t,e,a){switch(arguments.length){case 3:t.setAttribute(e,a);break;case 2:return t.getAttribute(e)}}var h,u,p,v,f,m,y,g=0,D=0,w=!1,L=s("div",{"class":"calendar"}),x=s("div",{"class":"calendar-mask"}),C=s("div",{"class":"calendar-content"}),N=s("div",{"class":"calendar-list"});return e.prototype.init=function(){var t=this,e=document.querySelectorAll(".calendars");if(e.length){L.appendChild(x),L.appendChild(C),document.body.appendChild(L),this.createHeader(function(){this.createWeek(),C.appendChild(N),a(N,function(t,e){e>0?g--:g++,this.startJSON.prev.m=g-1,this.startJSON.now.m=g,this.startJSON.next.m=g+1,this.transitions(t,e)}.bind(this)),h.forEach(function(t){t.onclick=function(){this.classList.contains("calendar-month-txt")?(y.classList.toggle("active"),m.show&&(m.show=!1,m.classList.remove("active")),u.forEach(function(t){l(t,"data-value")===l(this,"data-value")?t.classList.add("active"):t.classList.remove("active")}.bind(this)),y.show=!y.show):this.classList.contains("calendar-year-txt")&&(m.classList.toggle("active"),y.show&&(y.classList.remove("active"),y.show=!1),p.forEach(function(t){var e=t.classList;l(t,"data-value")===l(this,"data-value")?e.add("active"):e.remove("active")}.bind(this)),m.show=!m.show)}})}.bind(this)),this.createDate({},function(e){for(var a=0;a<u.length;a++)e[a].onclick=function(){for(var a=0;a<e.length;a++)e[a].classList.remove("active");g+=l(this,"data-value")-l(f,"data-value"),t.selectDate(this,y,"m",g)}});for(var n=0;n<e.length;n++)l(e[n],"readonly","true"),e[n].addEventListener("focus",function(){if(null==l(this,"disabled")){var e=Number(l(this,"start-year"))||1915,n=Number(l(this,"end-year"))||2050;t.hours=!(null==l(this,"hours")),t.hoursPast=!(null==l(this,"hours-past")),t.past=!(null==l(this,"past"))||t.hoursPast,t.minDate=c(l(this,"min-date")||"")[0],t.maxDate=c(l(this,"max-date")||"")[0],t.shield=c(l(this,"shield")||""),t.startDate=c(l(this,"start-date")||"");var i,s,r,o=new Date;if(t.startDate instanceof Array&&t.startDate.length){var d=t.startDate[0];D=d.y-o.getFullYear(),g=d.m-(o.getMonth()+1);for(var h in d)t.fixDate[h]=d[h];i={y:D,m:g-1,d:d.d},s={y:D,m:g,d:d.d},r={y:D,m:g+1,d:d.d},t.startJSON={prev:i,now:s,next:r}}else t.fixDate.y=o.getFullYear(),t.fixDate.m=o.getMonth()+1,t.fixDate.d=0;t.currentNode!=this&&((!t.startDate instanceof Array||!t.startDate)&&(D=g=0,t.startJSON.prev={y:D,m:g-1},t.startJSON.now={y:D,m:g},t.startJSON.next={y:D,m:g+1}),t.appendList(t.startJSON,t.addEvent.bind(t)),t.createDate({start:e,end:n,type:"year"},function(e){for(var n=0;n<e.length;n++)e[n].onclick=function(){for(var a=0;a<e.length;a++)e[a].classList.remove("active");D+=l(this,"data-value")-l(v,"data-value"),t.selectDate(this,m,"y",D)};a(m,function(t,e){m.index=m.index||0;var a=m.children.length;e>0?(m.index++,m.index>=0&&(m.index=0)):(m.index--,m.index<=-a&&(m.index=-(a-1)));var n="translate3D("+m.index*(100/a)+"%, 0, 0)";m.style.WebkitTransform=n,m.style.transform=n})})),L.classList.add("active"),t.currentNode=this}});C.onclick=function(t){var e=t.targetTouches?t.targetTouches[0]:t||event;e.cancelBubble=!0},x.onclick=r}},e.prototype.createCalenList=function(t,e){function a(t,e,a){if(!i.shield)return!1;for(var n=0;n<i.shield.length;n++)if(i.shield[n].y=i.shield[n].y||p.getFullYear(),i.shield[n].m=i.shield[n].m||p.getMonth()+1,i.shield[n].d=i.shield[n].d||p.getDate(),t==i.shield[n].y&&e==i.shield[n].m&&a==i.shield[n].d)return!0;return!1}var i=this,r=document.createElement("div"),o=0,c=t.y||0,d=t.m||0,h=(t.d,this.minDate||{}),u=this.maxDate||{},p=new Date;p.setFullYear(p.getFullYear()+c,p.getMonth()+d+1,1),p.setDate(0);var m=p.getDate();p.setDate(1);var y=p.getDay(),p=new Date,g=p.getDate();p.setFullYear(p.getFullYear()+c,p.getMonth()+d,1);var D=p.getFullYear(),w=p.getMonth()+1;p.setDate(0);for(var L=p.getDate(),x=[],C=L;C>0;C--)x.push(C);e&&(v.innerHTML=D,f.innerHTML=w<10?"0"+w:w,l(v,"data-value",D),l(f,"data-value",w-1));var N=y+7;N=N>=10?N-7:N;for(var C=0;C<N;C++){var b=s("span"),S=s("a",{"data-calen":[D,w-1,x[C]].join("/"),"class":"prev-m prev-to-month pasted",href:"javascript:;"},x[C]);b.appendChild(S),0!==r.children.length?r.insertBefore(b,r.children[0]):r.appendChild(b),o++}for(var j=n(D,w,g),k=n(h.y,h.m,h.d),J=n(u.y,u.m,u.d),C=0;C<m;C++){o++;var O=C+1,b=s("span"),S=s("a",{"data-calen":[D,w,O].join("/"),href:"javascript:;"},O);new Date;o%7!==0&&o%7!==1||S.classList.add("weekend");var T=n(c+D,d+w,O),E=n(D,w,O);(i.past&&T<j||k&&E<k||J&&E>J)&&S.classList.add("expire","pasted"),(T===j||i.fixDate.y===D&&i.fixDate.m===w&&i.fixDate.d===O)&&S.classList.add("today"),a(D,w,O)&&S.classList.add("pasted","shield"),b.appendChild(S),r.appendChild(b)}for(var M=42-r.children.length,C=0;C<M;C++){var O=C+1,b=s("span"),S=s("a",{"data-calen":[D,w+1,O].join("/"),"class":"next-m next-to-month pasted",href:"javascript:;"},O);b.appendChild(S),r.appendChild(b)}return r},e.prototype.createDate=function(t,e){t=t||{},t.start=t.start||1,t.end=t.end||12,t.type=t.type||"month";for(var a=s("div",{"class":"month"==t.type?"calendar-months":"calendar-years"}),n=s("div"),i=[],r=0,o=0,c=0,d=(new Date).getFullYear(),l=t.start;l<=t.end;l++){var h=s("span"),v=s("a",{"data-value":"year"==t.type?l:l-1,href:"javascript:;"},l<10?"0"+l:l);i.push(v),"year"==t.type?(r>=12&&(a.appendChild(n),n=s("div"),r=0,o++),l==d&&(c=o),h.appendChild(v),n.appendChild(h)):(h.appendChild(v),a.appendChild(h)),r++}if("year"==t.type){m&&C&&C.removeChild(m),a.appendChild(n),m=a,p=i,r&&o++,a.style.width=100*o+"%";for(var l=0;l<o;l++)a.children[l].style.width=100/o+"%";a.style.WebkitTransform="translate3D(-"+c*(100/o)+"%, 0, 0)",a.style.transform="translate3D(-"+c*(100/o)+"%, 0, 0)",m.index=-c}else y&&C&&C.removeChild(y),y=a,u=i;C.appendChild(a),e&&e(i)},e.prototype.createTime=function(t,e,a){var n=i(C,".calendar-time"),o=[],c=new Date,d=c.getDate(),h=c.getHours(),u=this;if(n.length){n=n[0];for(var p=i(n,"a"),v=0;v<p.length;v++)o.push({obj:p[v],time:parseInt(l(p[v],"data-time"),10)})}else{n=s("div",{"class":"calendar-time"});for(var v=0;v<24;v++){var f=v<10?"0"+v:v;f+=":00";var m=s("span"),y=s("a",{href:"javascript:;","data-time":f},f);m.appendChild(y),n.appendChild(m),o.push({obj:y,time:parseInt(f,10)})}}n.classList.add("active"),o.forEach(function(i){u.hoursPast&&(g<0&&D<=0||a==d&&i.time<=h||g<=0&&D<=0&&a<d)?(i.obj.classList.add("expire","pasted"),i.obj.active=!1):(i.obj.classList.remove("expire","pasted"),i.obj.active=!0),i.obj.onclick=function(){if(this.active){var a=e+" "+(i.time<10?"0"+i.time:i.time)+":00";null!==t.value?t.value=a:null!=t.innerHTML&&(t.innerHTML=a),r(),u.changes()}n.classList.remove("active")}}),C.appendChild(n)},e.prototype.createHeader=function(t){h=h||[];var e=this,a=s("div",{"class":"calendar-header"}),n=s("div",{"class":"calendar-year"}),i=s("a",{"class":"year-prev switch-btn",href:"javascript:;"},"<"),r=s("a",{"class":"year-next switch-btn",href:"javascript:;"},">"),o=s("a",{"class":"calendar-year-txt calendar-title",href:"javascript:;"});n.appendChild(i),n.appendChild(o),n.appendChild(r);var c=s("div",{"class":"calendar-month"}),d=s("a",{"class":"month-prev switch-btn",href:"javascript:;"},"<"),l=s("a",{"class":"month-next switch-btn",href:"javascript:;"},">"),u=s("a",{"class":"calendar-month-txt calendar-title",href:"javascript:;"});c.appendChild(d),c.appendChild(u),c.appendChild(l),a.appendChild(n),a.appendChild(c),h.push(o,u),f=u,v=o,d.onclick=function(){e.switchDate(-1)},l.onclick=function(){e.switchDate(1)},i.onclick=function(){e.switchDate(-1,"year")},r.onclick=function(){e.switchDate(1,"year")},C.children.length?C.insertBefore(a,C.children[0]):C.appendChild(a);for(var p=0;p<a.children.length;p++)a.children[p].ontouchstart=function(){this.classList.add("active")},a.children[p].ontouchend=function(){this.classList.remove("active")};t&&t()},e.prototype.createWeek=function(){for(var t=s("div",{"class":"calendar-week"}),e="日一二三四五六",a=0;a<7;a++){var n=a+1,i={};n%7!=1&&n%7!=0||(i["class"]="weekend"),t.appendChild(s("span",i,e.charAt(a)))}C.appendChild(t)},e.prototype.appendList=function(t,e){t=t||{},t.prev=t.prev||{m:g-1,y:D},t.now=t.now||{m:g,y:D},t.next=t.next||{m:g+1,y:D},N.innerHTML="",N.appendChild(this.createCalenList(t.prev)),N.appendChild(this.createCalenList(t.now,!0)),N.appendChild(this.createCalenList(t.next)),e&&e()},e.prototype.addEvent=function(){var t=this;Array.prototype.forEach.call(N.querySelectorAll("a"),function(e){e.onclick=function(){var e=this.classList;if(e.contains("prev-to-month"))t.switchDate(-1);else if(e.contains("next-to-month"))t.switchDate(1);else if(!e.contains("pasted")&&!e.contains("shield")){var a=l(this,"data-calen"),n=o(a,l(t.currentNode,"format")||!1);if(t.hours){var i=this.innerHTML;t.createTime(t.currentNode,n,i)}else t.currentNode&&(void 0===t.currentNode.value?t.currentNode.innerHTML=n:t.currentNode.oldValue!==n&&(t.currentNode.value=n,t.currentNode.oldValue=n,t.changes())),r()}}})},e.prototype.switchDate=function(t,e){var a=this;switch(e=e||"month"){case"month":t>0?g++:g--,a.startJSON.prev.m=g-1,a.startJSON.now.m=g,a.startJSON.next.m=g+1,a.transitions(N,t>0?-1:1);break;case"year":a.appendList({prev:{m:g,y:D-1},next:{m:g,y:D+1}},function(){t>0?D++:D--,a.startJSON.prev.y=D,a.startJSON.now.y=D,a.startJSON.next.y=D,a.transitions(N,t>0?-1:1)})}},e.prototype.transitions=function(t,e){t.classList.add("silde",e>0?"prev-to":"next-to"),setTimeout(function(){this.appendList(this.startJSON,function(){t.classList.remove("silde","prev-to","next-to"),this.addEvent()}.bind(this))}.bind(this),500)},e.prototype.selectDate=function(t,e,a,n){var i=this;this.startJSON.prev[a]="m"==a?n-1:n,this.startJSON.now[a]=n,this.startJSON.next[a]="m"==a?n+1:n,this.appendList(this.startJSON,i.addEvent.bind(this)),t.classList.add("active"),e.classList.remove("active"),m.show=!1,y.show=!1},e.prototype.changes=function(){var e=t.jQuery||t.$||null;e?e(this.currentNode)&&e(this.currentNode).change&&e(this.currentNode).change():this.currentNode.onchange&&this.currentNode.onchange()},t.addEventListener("load",function(){new e},!1),e});
|