UNPKG

gantt-source_management

Version:

Gantt, Schedule,

13 lines 2.82 kB
/** * CalendarScroll plugin * * @copyright NEURONET - Rafal Pospiech * @author Rafal Pospiech <neuronet.io@gmail.com> * @module gantt-schedule-timeline-calendar * @link https://github.com/neuronetio/gantt-schedule-timeline-calendar * @link https://gantt-schedule-timeline-calendar.neuronet.io * @version 3.37.5 * @released 2024-07-19 * @license SEE LICENSE IN LICENSE FILE */ const t={enabled:!0,bodyClassName:"gstc-scrolling"};function e(e=t){let i,n,o,s=e.enabled;class r{constructor(t){this.moving=!1,this.initialDataIndex={x:0,y:0},this.pointerDown=this.pointerDown.bind(this),this.pointerUp=this.pointerUp.bind(this),this.destroy=this.destroy.bind(this),this.pointerMove=i.schedule(this.pointerMove.bind(this)),t.addEventListener("pointerdown",this.pointerDown),document.addEventListener("pointermove",this.pointerMove,{passive:!0}),document.addEventListener("pointerup",this.pointerUp),t.style.cursor="grab"}destroy(t){t.removeEventListener("pointerdown",this.pointerDown),document.removeEventListener("pointermove",this.pointerMove),document.removeEventListener("pointerup",this.pointerUp)}resetInitialPoint(t){this.initialPoint={x:t.screenX,y:t.screenY}}pointerDown(t){if(!s)return;document.body.classList.add(e.bodyClassName),this.moving=!0,this.resetInitialPoint(t);const i=o.get("$data.scroll");this.initialDataIndex={x:i.horizontal.dataIndex||0,y:i.vertical.dataIndex||0}}pointerUp(){s&&(document.body.classList.remove(e.bodyClassName),this.moving&&(this.moving=!1))}handleHorizontalMovement(t,e){const i=o.get("$data.chart.time"),s=o.get("config.scroll.horizontal");if(t.x>0){if(0===this.initialDataIndex.x)return this.resetInitialPoint(e);const o=i.allDates[i.level];let r,l=this.initialDataIndex.x-1,a=0;for(;l>0&&(r=o[l],a+=r.width,!(a>=t.x));l--);let d=0;r&&(d=r.leftPx),s.byPixels?n.setScrollLeft(d):n.setScrollLeft(l)}else if(t.x<0){let r=this.initialDataIndex.x;const l=o.get("$data.scroll.horizontal"),a=i.allDates[i.level];if(r-1>=a.length-l.lastPageCount)return this.resetInitialPoint(e);let d,c=0;for(let e=a.length;r<e&&(d=a[r],c+=d.width,!(-c<=t.x));r++);if(r-1>=a.length-l.lastPageCount)return;let h=0;d&&(h=d.leftPx),s.byPixels?n.setScrollLeft(h):n.setScrollLeft(r)}}pointerMove(t){if(!s||!this.moving)return;const e={x:t.screenX-this.initialPoint.x,y:t.screenY-this.initialPoint.y};this.handleHorizontalMovement(e,t)}}return function(t){i=t,n=i.api,o=i.state;const l="config.plugin.CalendarScroll",a=t.state.get(l);return a&&(e=n.mergeDeep({},e,a)),o.update(l,e),o.subscribe("config.plugin.CalendarScroll.enabled",(t=>s=t)),o.update("config.actions.chart-calendar",(t=>(t.push(r),t))),n.pluginInitialized("CalendarScroll"),function(){o.update("config.actions.chart-calendar",(t=>t.filter((t=>t!==r)))),n.pluginDestroyed("CalendarScroll")}}}export{e as Plugin};