-
Notifications
You must be signed in to change notification settings - Fork 4
/
fortnight.min.js
1 lines (1 loc) · 3.83 KB
/
fortnight.min.js
1
var fortnight=function(e,t,n){"use strict";function o(e){return Array.prototype.slice.call(e)}function u(e,t){e.appendChild(t)}function a(t,n,r,i){t.addEventListener(n,function(t){var n=t.target;if(n===e)return;if(r.name&&n.nodeName.toLowerCase()!==r.name)return;if(r.classes){var s=r.classes.split(/\s+/);for(var o=0;o<s.length;o++)if(!v(n,s[o]))return}i.call(this,t)})}function f(e){var t=e.split("."),n=t.shift(),r=document.createElement(n);return n=="a"&&(r.href="javascript:;"),r[s]=t.join(" "),r}function l(e,t){e.style.left=c(t)+"px",e.style.top=h(t)+t.offsetHeight+"px"}function c(e){return e.offsetParent?c(e.offsetParent)+e.offsetLeft:e.offsetLeft}function h(e){return e.offsetParent?h(e.offsetParent)+e.offsetTop:e.offsetTop}function p(e,t){if(!e||!e[s])return;d(e,t),e[s]+=" "+t}function d(e,t){if(!e||!e[s])return;var n=e[s].split(/\s+/),r=n.indexOf(t);r+1&&(n.splice(r,1),e[s]=n.join(" "))}function v(e,t){if(!e||!e[s])return!1;var n=e[s].split(/\s+/),r=n.indexOf(t);return!!(r+1)}function m(e){return e.getUTCFullYear()}function g(e){return e.getUTCMonth()}function y(e){return e.getUTCDate()}function b(e){var t=e.toString();return("0"+t).substr(-2)}function w(e){return[m(e),b(g(e)+1),b(y(e))].join("-")}function E(e,t,r,i){return t===n&&(t=m(e)),r===n&&(r=g(e)),i===n&&(i=y(e)),new Date(t,r,i)}function S(e,t,n,r){return E(e,m(e)+t,g(e)+n,y(e)+r)}function x(e){while(e.getUTCDay())e=C(e);return e}function T(e){while(y(e)>1)e=C(e);return e}function N(e){return S(e,0,0,1)}function C(e){return S(e,0,0,-1)}function k(e,t){if(!t)return;t=t.length===n?[t]:t;var r=!1;return t.forEach(function(t){t.length==2?L(t[0],t[1],e)&&(r=!0):w(t)==w(e)&&(r=!0)}),r}function L(e,t,n){return w(e)<=w(n)&&w(n)<=w(t)}function A(e,t){var n=g(e),r=y(e),s=x(T(e)),o=f("div.month"),a=f("div.label");a.textContent=i.months[n]+" "+m(e),u(o,a);var l=f("div.week"),c=s,h=!1;while(!h){var d=f("a.day");d.setAttribute("data-date",w(c)),d.textContent=y(c),g(c)!=n&&p(d,"badmonth"),k(c,t)&&p(d,"sel"),u(l,d),c=N(c),c.getUTCDay()||(u(o,l),l=f("div.week"),h=g(c)!=n&&s.getTime()<c.getTime())}return o}function O(){var e=f("div.controls"),t=f("a.prev"),n=f("a.next");return t.innerHTML=i.prev,n.innerHTML=i.next,u(e,t),u(e,n),e}function M(e){var e=e||{},t=this,n=e.span||1,i=e.cursor||r;t.selected=e.selected||[],t.multi=e.multi||!1,t.el=f("div.fortnight.calendar"),t.render=function(){t.el.innerHTML="";var r=S(i,0,-Math.floor(n/2),0);for(var s=0;s<n;s++)u(t.el,A(r,t.selected)),r=S(r,0,1,0)},t.setCursor=function(n){i=n,t.render()},t.getCursor=function(t){return i},t.render()}function _(t){t=t||{};var i=this,s,o=r,c=!1,h=!!t.el,v=t.el,m,g;i.cal=new M,i.el=f("div.fortnight.picker"),u(i.el,i.cal.el),u(i.el,O()),i.show=function(){p(i.el,"show"),c=!0},i.hide=function(){d(i.el,"show"),v=!1,c=!1},i.selectDate=function(t){var n=t.target.getAttribute("data-date");s=new Date(n),i.cal.selected=s,i.cal.render(),v&&(v.value=n)},i.mount=function(t){if(c&&v===t)return;v=t,l(i.el,t);var u=new Date(t.value);o=r,y(u)?(s=u,o=u):s=n,i.show()},i.nextMonth=function(t){i.cal.setCursor(S(i.cal.getCursor(),0,1,0))},i.prevMonth=function(t){i.cal.setCursor(S(i.cal.getCursor(),0,-1,0))},e.addEventListener("click",function(t){var n=t.target;if(n===e)return;while(n.parentNode){if(n===i.el||n===v)return;n=n.parentNode}c&&i.hide()}),a(i.el,"click",{classes:"day"},i.selectDate),a(i.el,"click",{classes:"next"},i.nextMonth),a(i.el,"click",{classes:"prev"},i.prevMonth)}function D(n){var r=new _;u(t.body,r.el),e.addEventListener("focus",function(e){var i=e.target;if(i===window)return;var s=t.querySelectorAll(n);n&&o(s).indexOf(i)>-1&&r.mount(i)},!0)}var r=new Date,i={prev:"<",next:">",months:["January","February","March","April","May","June","July","August","September","October","November","December"]},s="className";return M.prototype={getSelected:function(){return this.mutli?selected:selected[0]}},{Picker:_,Calendar:M,watch:D}}(window,document);