UNPKG

table-schedule

Version:
7 lines (5 loc) 25 kB
/** * TableSchedule.js v0.4.0 by Monkey-D-Pixel * git@github.com:Monkey-D-Pixel/table-schedule.git */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).TableSchedule=e()}(this,(function(){"use strict";function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(e)}var e,n,r,s,a,i,o,l,h=function e(n){if(!n||!n.tagName)return null;var r=document.createElement(n.tagName);if(n.attr&&Object.keys(n.attr).length)for(var s in n.attr)null!==n.attr[s]&&void 0!==n.attr[s]&&r.setAttribute(s,String(n.attr[s]));n.id&&"string"==typeof n.id&&(r.id=n.id),n.className&&"string"==typeof n.className&&(r.className=n.className),n.style&&"object"===t(n.style)&&Object.keys(n.style).length&&Object.assign(r.style,n.style);var a=n.children;if(a&&a instanceof Array&&a.length)for(var i=0;i<a.length;i++){var o=e(a[i]);o&&r.appendChild(o)}else null!=a&&(n.html?r.innerHTML=String(a):r.innerText=String(a));return r},d=function(t){return t<10?"0"+t:t.toString()},c=function(t){return t.getFullYear()+"/"+d(t.getMonth()+1)+"/"+d(t.getDate())},u=function(t){t=(t||"").replace(/-/g,"/");var e=Date.parse(t);return new Date(e)},v=function(t){var e=parseInt(t/60),n=parseInt(t%60);return e<10&&(e="0"+e),n<10&&(n="0"+n),e+":"+n},f=function(t){var e=(t=t||"").split(":"),n=parseInt(e[0]),r=parseInt(e[1]);return isNaN(n)||isNaN(r)?0:60*n+r},p=function(t,e){for(var n=t.parentElement;n&&!n.matches(e);)n=n.parentElement;return n},m=function(t,e){for(var n=t.target;n&&!n.matches(e);){if(n===t.currentTarget){n=null;break}n=n.parentElement}return n},g=/(auto|scroll)/,y=function(t,e){return getComputedStyle(t,null).getPropertyValue(e)},_=function(t){return g.test(function(t){return y(t,"overflow")+y(t,"overflow-y")+y(t,"overflow-x")}(t))},E=function(t){for(var e=t.parentElement;e&&!_(e);)e=e.parentElement;return e||document.scrollingElement||document.documentElement},x=function(t){return t.getBoundingClientRect()},b=function(t,e){e=e||E(t);var n=t.getBoundingClientRect().toJSON();return n.top+=e.scrollTop,n.bottom+=e.scrollTop,n.left+=e.scrollLeft,n.right+=e.scrollLeft,n},T=function(t,e){e=e||t.parentElement;var n=x(t),r=x(e);return{top:n.top-r.top,left:n.left-r.left,width:n.width,height:n.height}},C=function(t,e,n){var r;return"function"==typeof Event&&"function"==typeof CustomEvent?r=new CustomEvent(e,{detail:n,bubbles:!0,cancelable:!0}):(r=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,n),t.dispatchEvent(r)},N=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},D=function(t,e){if(!Array.isArray(t)||!t.length)return!1;var n=[];Array.isArray(e)?n=e:n.push(e);for(var r=0;r<n.length;r++)if(t.indexOf(n[r])>-1)return!0;return!1},L={startDate:new Date,num:7,dayStart:6,dayEnd:22,gap:10,quantizing:!0,createThreshold:10,stretchThreshold:5,moveYThreshold:5,dateFormat:function(t){return c(t).substring(5)},directChange:!1,extraDataset:null,labelGroups:!1,groupHeaderText:null},I="create",S="modify",w="remove",A=null,H={_genDates:function(t){this.dates=[];for(var e=new Date(this.config.startDate),n=0;n<this.config.num;n++)this.dates.push(new Date(e)),t||(this.events.push([]),this._events.push([])),e.setDate(e.getDate()+1)},_genThead:function(){for(var t=this.config,e=this.dates,n={tagName:"THEAD",props:{},children:[{tagName:"TR",children:[{tagName:"TH"}]}]},r=0;r<e.length;r++)n.children[0].children.push({tagName:"TH",children:[{tagName:"div",children:t.dateFormat(e[r]),html:!0},{tagName:"table",className:"group-header",children:[{tagName:"thead",children:[{tagName:"tr"}]}]}]});return n},_renderOuter:function(){var t=this.config,e=this.dates,n=this._genThead(),r={tagName:"TABLE",className:"time-line",children:[{tagName:"TBODY",children:[]}]},s=new Date;s.setHours(t.dayStart,0,0,0);for(var a,i=Math.ceil(60*(t.dayEnd-t.dayStart)/t.gap),o=0;o<i;o++)r.children[0].children.push({tagName:"TR",children:[{tagName:"TH",children:[{tagName:"div",children:(a=s,d(a.getHours())+":"+d(a.getMinutes()))}]}]}),s.setMinutes(s.getMinutes()+t.gap);for(var l={tagName:"TBODY",children:[{tagName:"TR",children:[{tagName:"TD",className:"time-line-wrapper",children:[r]}]}]},c=0;c<e.length;c++)l.children[0].children.push({tagName:"TD",className:"day-col"});this.el.root.appendChild(h(n)),this.el.root.appendChild(h(l))},_setGrid:function(t,e){e=e||1;for(var n=this.config,r=Math.ceil(60*(n.dayEnd-n.dayStart)/n.gap),s=this.el.dayGrids[t].querySelector("tbody"),a={tagName:"TR",children:[]},i=0;i<e;i++)a.children.push({tagName:"TD",className:"grid-cell"});s.innerHTML="";for(var o=0;o<r;o++)s.appendChild(h(a))},_renderInner:function(t){var e=this.el.dayCols,n={tagName:"DIV",className:"events-container",attr:{"data-index":0},children:[{tagName:"DIV",className:"events-col"}]},r={tagName:"TABLE",className:"day-grid",children:[{tagName:"TBODY",children:[]}]};if("number"==typeof t&&e[t])e[t].innerHTML="",e[t].appendChild(h(r)),this._setGrid(t),n.attr["data-index"]=t,e[t].appendChild(h(n));else for(var s=0;s<e.length;s++)e[s].innerHTML="",e[s].appendChild(h(r)),this._setGrid(s),n.attr["data-index"]=s,e[s].appendChild(h(n))},_renderEvent:function(e){var n=this.config,r=e.dateIndex,s=(e.startm-60*n.dayStart)/n.gap*20,a=(e.endm-e.startm)/n.gap*20,i=e.style&&"object"===t(e.style)?e.style:{},o=e.className?String(e.className).trim():"",l={"data-startm":e.startm,"data-endm":e.endm,"data-nr":e.nr};if(n.extraDataset&&Object.keys(n.extraDataset).length)for(var d in n.extraDataset)!N(l,"data-"+d)&&N(e,n.extraDataset[d])&&(l["data-"+d]=e[n.extraDataset[d]]);var c=h({tagName:"DIV",className:"event"+(o?" "+o:""),style:Object.assign({},i,{top:s+"px",height:a+"px"}),attr:l,children:[{tagName:"DIV",className:"event-content-wrapper",children:[{tagName:"DIV",className:"event-title",children:e.title||""},{tagName:"DIV",className:"event-content",children:e.content,html:!0}]},{tagName:"DIV",className:"event-bar-wrapper",children:[{tagName:"SPAN",className:"event-bar"}]}]});this._insertIntoDate(c,r)},_insertIntoDate:function(t,e){for(var n=this.el.eventsContainers[e].children,r=!1,s=t.dataset.nr,a=this.events[e][s],i=0;i<n.length&&!(r=this._insertIntoCol(t,n[i]));i++);if(!r){var o=this._addCol(e,a.group);this._insertIntoCol(t,n[o])}this._setGroupHeader(e)},_setGroupHeader:function(t){var e=this,n=this.config,r=function(t){var r=null,s=e.getEvent(t.children[0]);return s&&(r=s.group,n.groupHeaderText&&("string"==typeof n.groupHeaderText?s&&N(s,n.groupHeaderText)&&(r=s[n.groupHeaderText]||""):"function"==typeof n.groupHeaderText&&(r=n.groupHeaderText(s.group)))),r},s=this.el.groupHeaders[t].querySelector("tr");if(s.innerHTML="",n.labelGroups){var a=this.el.eventsContainers[t].children,i=1,o=a[0]&&a[0].dataset.group,l=r(a[0]),d={tagName:"th",style:{},children:[{tagName:"div",attr:{title:l},children:l}]};if(a.length>1){for(var c=1;c<a.length;c++){var u=a[c];if(u.dataset.group!==o){d.style.width=i/a.length*100+"%";var v=r(a[c-1]);d.children[0].children=v,d.children[0].attr.title=v,s.appendChild(h(d)),o=u.dataset.group,i=1}else i+=1}d.style.width=i/a.length*100+"%";var f=r(a[a.length-1]);d.children[0].children=f,d.children[0].attr.title=f,s.appendChild(h(d))}else void 0!==d.children[0].children&&null!==d.children[0].children&&s.appendChild(h(d));clearTimeout(A),A=setTimeout((function(){clearTimeout(A),A=null,e._coords.grid=b(e.el.dayGrids[0],e.el.scroll)}),0)}},_arrangeEvents:function(t){for(var e=this.el.eventsContainers[t],n=e.children,r=0;r<n.length;r++)0===n[r].childElementCount&&(0===r?n[r].removeAttribute("data-group"):(e.removeChild(n[r]),r-=1));if(n.length>1){for(var s=1;s<n.length;s++)for(var a=n[s].children,i=0;i<a.length;i++){for(var o=!1,l=0;!o&&l<s;)o=this._insertIntoCol(a[i],n[l]),l+=1;o&&(i-=1)}if(n.length>1)for(var h=0;h<n.length;h++)0===n[h].childElementCount&&(e.removeChild(n[h]),h-=1)}this._setGrid(t,n.length),this._setGroupHeader(t)},_addCol:function(t,e){var n=h({tagName:"DIV",className:"events-col"}),r=-1,s=this.el.eventsContainers[t];if(void 0!==e){e=String(e),n.dataset.group=e;var a=s.querySelectorAll('[data-group="'+e+'"]');a.length?(a[a.length-1].insertAdjacentElement("afterEnd",n),r=[].indexOf.call(s.children,n)):(s.appendChild(n),r=s.children.length-1)}else s.appendChild(n),r=s.children.length-1;return this._setGrid(t,s.children.length),r},_insertIntoCol:function(t,e){var n=t.dataset.nr,r=e.parentElement.dataset.index,s=this._events[r][n],a=s.group;void 0!==a&&(a=String(a));var i=s.startm,o=s.endm,l=e.children;if(l.length){if(e.dataset.group!==a)return!1;for(var h=!0,d=0;d<l.length;d++)if(l[d]!==t){var c=this._posToMinute(l[d]);if(!(c.startm>=o||c.endm<=i)){h=!1;break}}return!!h&&(e.appendChild(t),!0)}return void 0!==a&&(e.dataset.group=String(a)),e.appendChild(t),!0},_clearDom:function(t){var e=this;this._toDateIndexList(t).forEach((function(t){for(var n=e.el.eventsContainers[t].getElementsByClassName("event"),r=0;r<n.length;r++)n[r].parentElement.removeChild(n[r]),r-=1;e._renderInner(t),e._setGroupHeader(t)}))}},M={addEvent:function(t){var e=this._tsItem(t);if(e){for(var n=0,r=this._events[e.dateIndex],s=this.events[e.dateIndex];n<r.length&&r[n].startm<=e.startm;)n+=1;for(e.nr=n,n=r.length-1;n>=e.nr;)this.el.dayCols[e.dateIndex].querySelector('[data-nr="'+r[n].nr+'"]').dataset.nr=r[n].nr+1,r[n].nr+=1,n-=1;r.splice(e.nr,0,e),s.splice(e.nr,0,t),this._renderEvent(e)}return this},updateEvent:function(t,e){var n=this.config,r=Array.isArray(t)&&t.length>=2&&t[0]>=0&&t[0]<n.num&&t[1]>=0&&t[1]<this.events[t[0]].length,s=this._tsItem(e);if(r&&s){var a=t[0],i=t[1];if(s.dateIndex!==a)this.deleteEvent(t),this.addEvent(e),this._arrangeEvents(s.dateIndex);else{s.nr=i,this.events[a][i]=e,this._events[a][i]=s;var o=this.el.eventsContainers[a].querySelector('[data-nr="'+i+'"]'),l=this._minuteToTop(s.startm);o&&l>=0&&(Object.assign(o.style,{top:l+"px",height:this._minuteToTop(s.endm)-l+"px"}),this._insertIntoDate(o,a),this._arrangeEvents(a))}}return this},deleteEvent:function(t){var e=this.config;if(Array.isArray(t)&&t.length>=2&&t[0]>=0&&t[0]<e.num&&t[1]>=0&&t[1]<this.events[t[0]].length){var n=t[0],r=t[1],s=this._events[n],a=this.events[n];s.splice(r,1),a.splice(r,1);var i=this.el.dayCols[n].querySelector('[data-nr="'+r+'"]');i.parentElement.removeChild(i);for(var o=r;o<s.length;)this.el.dayCols[n].querySelector('[data-nr="'+s[o].nr+'"]').dataset.nr=s[o].nr-1,s[o].nr-=1,o+=1;this._arrangeEvents(n)}return this},clear:function(t){var e=this;return this._clearDom(t),this._toDateIndexList(t).forEach((function(t){e.events[t]=[],e._events[t]=[]})),this},rerender:function(){var t=this,e=this.config;this._clearDom();for(var n=0;n<e.num;n++)this._events[n].forEach((function(e){t._renderEvent(e)})),this._arrangeEvents(n);return this},changeStartDate:function(t){var e=this,n=this.config;return t instanceof Date&&"Invalid Date"!==t.toString()&&function(){var r=new Date(t),s=new Date(n.startDate);r.setHours(0,0,0,0),s.setHours(0,0,0,0);var a=(r.getTime()-s.getTime())/1e3/60/60/24,i=e.el.root.querySelector("thead");if(0!==a&&i){n.startDate=r,e._genDates(!0);var o=h(e._genThead());if(i.innerHTML=o.innerHTML,a<n.num&&a>0){for(var l=a;l<n.num;l++)e._events[l].forEach((function(t){t.dateIndex-=a}));e.events.splice(0,a),e._events.splice(0,a);for(var d=0;d<a;d++)e.events.push([]),e._events.push([]);e.rerender()}else if(a<0&&a>-n.num){for(var c=0;c<n.num+a;c++)e._events[c].forEach((function(t){t.dateIndex-=a}));e.events.splice(n.num+a,-a),e._events.splice(n.num+a,-a);for(var u=0;u<-a;u++)e.events.unshift([]),e._events.unshift([]);e.rerender()}else e.clear()}}(),this},destroy:function(){this._unbind();for(var t=this.el.root.children,e=0;e<t.length;e++)this.el.root.removeChild(t[e]),e-=1;for(var n in this.el)this.el[n]=null;this.dates=[],this.events=[],this._events=[]},getEvent:function(t){if(t&&t instanceof HTMLElement){var e=this._getEventCoords(t);return this.events[e[0]][e[1]]}return null},getElem:function(t){if(t)for(var e=0;e<this.events.length;e++){var n=this.events[e].indexOf(t);if(n>-1)return this.el.dayCols[e].querySelector('[data-nr="'+n+'"]')}return null},updateRect:function(){return this._coords.grid=b(this.el.dayGrids[0]),this._coords.scroll=b(this.el.scroll),this}},q=/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/,O={_posToMinute:function(t,e){if(!e&&t.dataset.startm&&t.dataset.endm)return{startm:parseInt(t.dataset.startm),endm:parseInt(t.dataset.endm)};var n=this.config,r=t.offsetTop,s=t.offsetHeight,a=60*n.dayStart+r/20*n.gap;return{startm:a,endm:a+s/20*n.gap}},_minuteToTop:function(t){var e=-1;if("number"==typeof(t=parseInt(t))&&!isNaN(t)){var n=this.config;e=(t-60*n.dayStart)/n.gap*20}return e},_quantizeH:function(t){return 20*Math.round(t/20)},_quantize:function(t){var e=this._coords.grid.top,n=this.el.scroll.scrollTop,r=t-e+n;return this._quantizeH(r)+e-n},_quantizeTop:function(t){var e=this._coords.grid.top-this.el.scroll.scrollTop;return this._quantize(t+e)-e},_getEventCoords:function(t){var e=parseInt(t.dataset.nr);return[parseInt(p(t,".events-container").dataset.index),e]},_tsItem:function(t){if(!t)return null;var e=!0;if(e&=t.date&&"Invalid Date"!==u(t.date).toString(),e&=t.start&&q.test(t.start)&&t.end&&q.test(t.end)){for(var n=this.config,r=60*n.dayStart,s=60*n.dayEnd,a=u(t.date),i=c(a),o=f(t.start),l=f(t.end),h=-1,d=0;d<this.dates.length;d++)if(i===c(this.dates[d])){h=d;break}return h>-1&&o<l&&o>=r&&l<=s?Object.assign({},t,{parsedDate:a,ymd:i,startm:o,endm:l,dateIndex:h}):null}return null},_toDateIndex:function(e){var n=this.config;if("number"==typeof e)return e<n.num&&e>-1?e:-1;if(!e)return-1;if("string"==typeof e){var r=u(e);if("Invalid Date"!==r.toString()){for(var s=-1,a=0;a<this.dates.length;a++)if(c(r)===c(this.dates[a])){s=a;break}return s}return-1}if("object"===t(e)&&e instanceof Date&&"Invalid Date"!==e.toString()){for(var i=-1,o=0;o<this.dates.length;o++)if(c(e)===c(this.dates[o])){i=o;break}return i}return-1},_toDateIndexList:function(t){var e=this,n=this.config,r=[];if(void 0===t)for(var s=0;s<n.num;s++)r.push(s);else if(Array.isArray(t)){t.map((function(t){return e._toDateIndex(t)})).forEach((function(t){t>-1&&-1===r.indexOf(t)&&r.push(t)}))}else{var a=this._toDateIndex(t);a>-1&&r.push(a)}return r}},j=null,k={startY:0,startX:0,yArr:[],xArr:[],timestamp:Date.now()},Y=null,z=null,G=null,B=0,V=0,F=!1,R=null,X=[],P={top:-1/0,right:1/0,bottom:1/0,left:-1/0},K=(r={h:null,v:null},s={h:0,v:0},a={h:0,v:0},i=window.requestAnimationFrame&&"function"==typeof requestAnimationFrame,{set:function(t,e){var n=this.el.scroll;s[t]=e;var a=function e(){"h"===t?n.scrollLeft+=s[t]:n.scrollTop+=s[t],i&&(r[t]=requestAnimationFrame(e))};0===e?o.call(this,t):r[t]||(r[t]=i?requestAnimationFrame(a):setInterval(a,16))},reset:o=function(t){var e=[];t?e.push(t):e=["h","v"];var n={h:this.el.scroll.scrollLeft-a.h,v:this.el.scroll.scrollTop-a.v};return e.forEach((function(t){i?cancelAnimationFrame(r[t]):clearInterval(r[t]),r[t]=null,s[t]=0})),l.call(this,t),n},scrolling:function(t){return 0!==s[t]},record:l=function(t){t&&"h"!==t||(a.h=this.el.scroll.scrollLeft),t&&"v"!==t||(a.v=this.el.scroll.scrollTop)},initScroll:a}),W=function(){k.startY=0,k.yArr=[],k.startX=0,k.xArr=[],k.timestamp=Date.now(),j=null,z=null,G=null,R=null,F=!1,B=0,V=0,P={top:-1/0,right:1/0,bottom:1/0,left:-1/0}},J=function(t){return/mouse/.test(t.type)?{x:t.clientX,y:t.clientY}:{x:t.touches[0].clientX,y:t.touches[0].clientY}},$=function(t){t.stopImmediatePropagation()},Q=function(t){return t.preventDefault(),t.stopPropagation(),!1},U=function(){clearTimeout(Y),Y=null},Z={touchstart:function(t){var r=this.config,s=J(t),a=s.y;r.quantizing^t.shiftKey&&(a=this._quantize(a));for(var i=function(){k.timestamp=Date.now(),k.startY=s.y,k.yArr.push(a);var t=s.x;k.startX=t,k.xArr.push(t),document.addEventListener("mousemove",this._handlers.touchmove,{passive:!1}),document.addEventListener("touchmove",this._handlers.touchmove,{passive:!1}),document.addEventListener("mouseup",this._handlers.touchend),document.addEventListener("touchend",this._handlers.touchend)}.bind(this),o=[[".event-bar",function(t,e){i(),j=e,e.style.opacity=1,G=p(e,".event");var n=T(G);B=n.height,V=n.top,P.top=this._coords.grid.top,P.bottom=this._coords.scroll.top+T(this.el.scroll).height,G.removeEventListener("click",$)}],[".event",function(t,r){i(),j=r;var a=this.el.root,o=this.el.scroll,l=T(G=r);V=l.top,B=l.height,e=b(r,o),X=[].map.call(this.el.dayCols,(function(t){return b(t,o).left})),n=-1,K.record.call(this);var h=this._coords.scroll,d=x(r),c=T(o),u=T(j);P={top:s.y-d.top+h.top+T(a.querySelector("thead")).height,right:h.left+c.width-(u.width-(s.x-d.left)),bottom:h.top+c.height-(u.height-(s.y-d.top)),left:s.x-d.left+h.left+T(a.querySelector(".time-line")).width},j.removeEventListener("click",$)}],[".events-col",function(t,e){var n=this;if(e===t.target){i();var r=function(){j=e;var t=a-n._coords.grid.top+n.el.scroll.scrollTop;z=h({tagName:"DIV",className:"event drawing",style:{top:t+"px"}}),e.appendChild(z)};"touchstart"===t.type?(Y=setTimeout((function(){r()}),500),window.addEventListener("contextmenu",Q),this.el.scroll.addEventListener("scroll",U)):r()}else"event-bar"!==t.target.className&&e.contains(t.target)&&t.stopPropagation()}]],l=0;l<o.length;l++){var d=o[l][0],c=o[l][1].bind(this),u=m(t,d);if(u){this.el.root.style.userSelect="none",c(t,u);break}}},touchmove:function(t){var r=this.config,s=J(t),a=s.y,i=k.startY,o=r.quantizing^t.shiftKey;o&&(a=this._quantize(a),i=this._quantize(i)),k.yArr.push(a);var l=s.x,h=k.startX;k.xArr.push(l);var d=a-i,c=l-h;if(j)if(t.preventDefault(),"events-col"===j.className&&z&&d>0)z.style.height=d+"px";else if("event-bar"===j.className&&G){if(Math.abs(d)>=r.stretchThreshold/r.gap*20||F){var u;if(F||G.addEventListener("click",$),F=!0,s.y>=P.top&&s.y<=P.bottom)K.scrolling("v")&&K.reset.call(this,"v"),u=a-this._coords.grid.top-V+this.el.scroll.scrollTop,o||(u=B+d);else s.y<P.top&&K.set.call(this,"v",s.y-P.top),s.y>P.bottom&&K.set.call(this,"v",s.y-P.bottom);u>-.1&&V+u<=60*(r.dayEnd-r.dayStart)/r.gap*20&&(G.style.height=Math.max(u,0)+"px",Math.round(u)>=r.createThreshold/r.gap*20?G.classList.remove("drawing"):G.classList.add("drawing"))}}else if(j.classList.contains("event")&&G){var v=this.el.root,f=this.el.scroll;if(Math.abs(c)>40){var m=e.top-f.scrollTop,g=e.left-f.scrollLeft;if(R||(R=j.cloneNode(!0),Object.assign(R.style,{cursor:"move",position:"fixed",top:m+"px",left:g+"px",width:j.offsetWidth+"px",opacity:.5}),v.appendChild(R)),s.x>P.left&&s.x<P.right){var y=g+c;if(K.scrolling("h")){var _=K.reset.call(this,"h").h;k.startX-=_,y+=_}R.style.left=y+"px"}else s.x<P.left&&K.set.call(this,"h",s.x-P.left),s.x>P.right&&K.set.call(this,"h",s.x-P.right);if(s.y>P.top&&s.y<P.bottom){var E=(o?this._quantize(m):m)+d;if(K.scrolling("v")){var x=K.reset.call(this,"v").v;k.startY-=x,E+=x}R.style.top=E+"px"}else s.y<P.top&&K.set.call(this,"v",s.y-P.top),s.y>P.bottom&&K.set.call(this,"v",s.y-P.bottom);n=-1;for(var b=parseInt(p(j,".events-container").dataset.index),T=X.length-1;T>=0;T--)if(X[T]<l+f.scrollLeft){T!==b&&(n=T);break}[].forEach.call(this.el.dayCols,(function(t,e){e===n?t.classList.add("drop-target"):t.classList.remove("drop-target")})),n>-1&&this.el.dayCols[n].classList.add("drop-target")}else if([].forEach.call(this.el.dayCols,(function(t){t.classList.remove("drop-target")})),R&&(this.el.root.removeChild(R),R=null),Math.abs(d)>=r.moveYThreshold/r.gap*20||F)if(F||j.addEventListener("click",$),F=!0,j.style.cursor="move",s.y>P.top&&s.y<P.bottom)if(Object.assign(j.style,{position:"",left:"",width:""}),o){var C=this._quantizeTop(V)+d;if(K.scrolling("v")){var N=K.reset.call(this,"v").v;k.startY-=N,C+=N}C>=0&&C+B<=60*(r.dayEnd-r.dayStart)/r.gap*20&&(j.style.top=C+"px")}else j.style.top=V+d+"px";else{var D=e.top-K.initScroll.v,L=e.left-K.initScroll.h;Object.assign(j.style,{position:"fixed",left:L+"px",width:j.offsetWidth+"px"}),s.y<P.top&&(K.set.call(this,"v",s.y-P.top),j.style.top=P.top-(k.startY-D)+"px"),s.y>P.bottom&&(K.set.call(this,"v",s.y-P.bottom),j.style.top=P.bottom-(k.startY-D)+"px")}}},touchend:function(t){if(this.el.root.style.userSelect="",clearTimeout(Y),Y=null,window.removeEventListener("contextmenu",Q),this.el.scroll.removeEventListener("scroll",U),j){var e=this.config,r=e.quantizing^t.shiftKey,s=k.yArr[k.yArr.length-1];if("events-col"===j.className&&z){var a=this._posToMinute(z);if(j.removeChild(z),a.endm-a.startm>=e.createThreshold){var i=parseInt(j.parentElement.dataset.index),o={date:c(this.dates[i]),start:v(a.startm),end:v(a.endm)};C(this.el.root,I,{item:o}),(!0===e.directChange||D(e.directChange,I))&&this.addEvent(o)}W()}else if("event-bar"===j.className&&G){if(j.style.opacity="",G.classList.remove("drawing"),Math.abs(G.offsetHeight-B)>0){var l=this._posToMinute(G,!0),h=this._getEventCoords(G),d=this.events[h[0]][h[1]],u={type:"end",date:d.date,start:d.start,end:v(l.endm)};G.offsetHeight>=e.createThreshold/e.gap*20?(G.style.height=B+"px",C(this.el.root,S,{item:d,coords:h,mod:u}),(!0===e.directChange||D(e.directChange,[S,"end"]))&&(d.end=u.end,this.updateEvent(h,d),G.dataset.startm=l.startm,G.dataset.endm=l.endm)):(G.style.height=B+"px",C(this.el.root,w,{item:d,coords:h}),(!0===e.directChange||D(e.directChange,w))&&this.deleteEvent(h))}else G.style.height=B+"px";W(),K.reset.call(this)}else if(j.classList.contains("event")){if(R&&G){if(n>-1){var f=Math.round(parseFloat(R.style.top)+this.el.scroll.scrollTop-this._coords.grid.top);f=Math.max(f,0),f=Math.min(f,60*(e.dayEnd-e.dayStart)/e.gap*20-B),r&&(f=this._quantizeTop(f));var p=60*e.dayStart+f/20*e.gap,m=p+Math.round(B/20*e.gap),g=this._getEventCoords(G),y=this.events[g[0]][g[1]],_=p==this._events[g[0]][g[1]].startm?"date":"datetime",E={type:_,date:c(this.dates[n]),start:v(p),end:v(m)};C(this.el.root,S,{item:y,coords:g,mod:E}),(!0===e.directChange||D(e.directChange,[S,_]))&&(Object.assign(y,{date:E.date,start:E.start,end:E.end}),this.updateEvent(g,y))}Object.assign(G.style,{top:V+"px",height:B}),this.el.root.removeChild(R),[].forEach.call(this.el.dayCols,(function(t){t.classList.remove("drop-target")}))}else if(G){var x=T(G);if(Math.abs(x.top-V)>0){if("fixed"===G.style.position){var b,N=K.reset.call(this,"v").v;Object.assign(G.style,{position:"",left:"",width:""}),s<=P.top?b=V-(k.startY-P.top)+N:s>=P.bottom&&(b=V+(P.bottom-k.startY)+N),b=Math.max(b,0),b=Math.min(b,60*(e.dayEnd-e.dayStart)/e.gap*20-B),r&&(b=this._quantizeTop(b)),G.style.top=b+"px"}var L=this._posToMinute(G,!0),A=this._getEventCoords(G),H=this.events[A[0]][A[1]],M={type:"start",date:H.date,start:v(L.startm),end:v(L.endm)};Object.assign(G.style,{top:V+"px",height:B+"px"}),C(this.el.root,S,{item:H,coords:A,mod:M}),(!0===e.directChange||D(e.directChange,[S,"start"]))&&(H.start=M.start,H.end=M.end,this.updateEvent(A,H),G.dataset.startm=L.startm,G.dataset.endm=L.endm)}}j.style.cursor="",W(),K.reset.call(this)}document.removeEventListener("mousemove",this._handlers.touchmove,{passive:!1}),document.removeEventListener("touchmove",this._handlers.touchmove,{passive:!1}),document.removeEventListener("mouseup",this._handlers.touchend),document.removeEventListener("touchend",this._handlers.touchend)}},keydown:function(t){"Escape"!==t.key&&"Esc"!==t.key||(z&&z.parentElement.removeChild(z),G&&Object.assign(G.style,{top:V+"px",height:B+"px",position:"",width:"",left:""}),R&&(R.parentElement.removeChild(R),[].forEach.call(this.el.dayCols,(function(t){t.classList.remove("drop-target")}))),W(),K.reset.call(this),document.removeEventListener("mousemove",this._handlers.touchmove,{passive:!1}),document.removeEventListener("touchmove",this._handlers.touchmove,{passive:!1}))}},tt={_bind:function(){for(var t in Z)this._handlers[t]=Z[t].bind(this);this.el.root.addEventListener("mousedown",this._handlers.touchstart),this.el.root.addEventListener("touchstart",this._handlers.touchstart),document.addEventListener("keydown",this._handlers.keydown)},_unbind:function(){this.el.root.removeEventListener("mousedown",this._handlers.touchstart),this.el.root.removeEventListener("touchstart",this._handlers.touchstart),document.removeEventListener("keydown",this._handlers.keydown)}},et=function(t,e){var n;if(!(n="string"==typeof t?document.querySelector(t):t)||"TABLE"!==n.tagName)throw new TypeError("el must be a <TABLE> element or a valid selector");this.config=Object.assign({},L,e),this.dates=[],this.el={root:n,dayCols:n.getElementsByClassName("day-col"),dayGrids:n.getElementsByClassName("day-grid"),eventsContainers:n.getElementsByClassName("events-container"),groupHeaders:n.getElementsByClassName("group-header"),scroll:E(n)},this.events=[],this._events=[],this._handlers={},this._init(),this._coords={grid:b(this.el.dayGrids[0]),scroll:b(this.el.scroll)}};return et.prototype._init=function(){this.el.root.classList.add("table-schedule"),this._genDates(),this._renderOuter(),this._renderInner(),this._bind()},Object.assign(et.prototype,H,M,O,tt),et}));