UNPKG

gantt-source_management

Version:

Gantt, Schedule,

13 lines 6.95 kB
/** * TimelinePointer 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="chart-timeline-grid-row-cell",i="chart-timeline-items-row-item";function e(t={}){const i={enabled:!0,isMoving:!1,pointerState:"up",currentTarget:null,realTarget:null,targetType:"",targetData:null,captureEvents:{down:!1,up:!1,move:!1},initialPosition:{x:0,y:0},currentPosition:{x:0,y:0},movement:{x:0,y:0},events:{down:null,move:null,up:null}};return t.captureEvents&&(i.captureEvents=Object.assign(Object.assign({},i.captureEvents),t.captureEvents)),i}const a="TimelinePointer",s=`config.plugin.${a}`;class n{constructor(a,n){this.locked={up:!1,move:!1,down:!1},this.initialScrollPosPx={horizontal:0,vertical:0},this.onDestroy=[],this.classNames={cell:"",item:""},this.api=n.api,this.state=n.state,this.apiPointerListeners={down:new Set,move:new Set,up:new Set},this.pointerDown=this.pointerDown.bind(this),this.pointerMove=this.pointerMove.bind(this),this.pointerMove=n.schedule(this.pointerMove),this.pointerUp=this.pointerUp.bind(this),this.onDestroy.push(this.state.subscribe("$data.elements.chart-timeline",(t=>{t&&(this.element=t,this.element.removeEventListener("pointerdown",this.pointerDown),this.element.addEventListener("pointerdown",this.pointerDown),document.removeEventListener("pointerup",this.pointerUp),document.addEventListener("pointerup",this.pointerUp),document.removeEventListener("pointermove",this.pointerMove),document.addEventListener("pointermove",this.pointerMove))}))),this.apiLock=this.apiLock.bind(this),this.apiUnlock=this.apiUnlock.bind(this),this.apiIsLocked=this.apiIsLocked.bind(this),this.apiAddPointerListener=this.apiAddPointerListener.bind(this),this.apiRemovePointerListener=this.apiRemovePointerListener.bind(this),this.getRealPosition=this.getRealPosition.bind(this),this.data=e(a),this.classNames.cell=this.api.getClass(t),this.classNames.item=this.api.getClass(i),this.destroy=this.destroy.bind(this),this.api.plugins.TimelinePointer={lock:this.apiLock,unlock:this.apiUnlock,isLocked:this.apiIsLocked,addPointerListener:this.apiAddPointerListener,removePointerListener:this.apiRemovePointerListener},this.onDestroy.push(this.state.subscribe(s,(t=>this.data=t)))}destroy(){this.onDestroy.forEach((t=>t())),this.element.removeEventListener("pointerdown",this.pointerDown),document.removeEventListener("pointerup",this.pointerUp),document.removeEventListener("pointermove",this.pointerMove),this.api.pluginDestroyed(a)}updateData(){this.state.update(s,(()=>Object.assign({},this.data)))}apiLock(t,i=!0){this.locked[t]=i}apiUnlock(t){this.locked[t]=!1}apiIsLocked(t){return this.locked[t]}apiAddPointerListener(t,i){this.apiPointerListeners[t].add(i)}apiRemovePointerListener(t,i){this.apiPointerListeners[t].delete(i)}apiTriggerPointerListener(t,i){this.apiPointerListeners[t].forEach((t=>t(i)))}getRealTarget(e){let a=e.target.closest("."+this.classNames.item);return a||(a=e.target.closest("."+this.classNames.cell),a||(a=e.target.closest(`[data-type="${i}"]`),a||(a=e.target.closest(`[data-type="${t}"]`),a||null)))}getRealPosition(t){const i={x:0,y:0};if(this.element){const e=this.element.getBoundingClientRect();i.x=t.clientX-e.x,i.y=t.clientY-e.y;const a=this.state.get("$data.scroll.vertical.preciseOffset")||0;i.y-=a}return i}pointerDown(e){if(!this.data.enabled)return;this.initialScrollPosPx.horizontal=this.state.get("$data.scroll.horizontal.handlePosPx"),this.initialScrollPosPx.vertical=this.state.get("$data.scroll.vertical.handlePosPx"),this.data.pointerState="down",this.data.currentTarget=e.target;const a=this.getRealTarget(e);if(this.data.realTarget=a,this.data.targetType="",this.data.targetData=null,this.data.realTarget)if(this.data.realTarget.classList.contains(this.classNames.item))this.data.targetType=i,this.data.targetData=this.data.realTarget.vido.item;else if(this.data.realTarget.classList.contains(this.classNames.cell))this.data.targetType=t,this.data.targetData=this.data.realTarget.vido;else if(this.data.realTarget.dataset.type===i){this.data.targetType=i;const t=this.data.realTarget.dataset.gstcid;this.data.targetData=this.api.getItem(t)}else if(this.data.realTarget.dataset.type===t){this.data.targetType=t;const i=this.data.realTarget.dataset.gstcid;this.data.targetData=this.state.get(`$data.chart.grid.cells.${i}`)}this.data.isMoving=!!this.data.targetType,this.data.events.down=e,this.data.events.move=e;const s=this.getRealPosition(e);this.data.initialPosition=s,this.data.currentPosition=s;const n={type:this.data.pointerState,originalEvent:e,targetElement:a,targetData:this.data.targetData,targetType:this.data.targetType,initialPosition:this.data.initialPosition,currentPosition:this.data.currentPosition,movement:{x:0,y:0},initialScrollPosPx:this.initialScrollPosPx,isMoving:this.data.isMoving,allEvents:this.data.events};this.apiTriggerPointerListener("down",n),this.updateData()}pointerUp(t){if(!this.data.enabled)return;this.data.pointerState="up",this.data.isMoving=!1,this.data.events.up=t,this.data.currentPosition=this.getRealPosition(t);const i={type:this.data.pointerState,originalEvent:t,targetElement:this.data.realTarget,targetData:this.data.targetData,targetType:this.data.targetType,initialPosition:this.data.initialPosition,currentPosition:this.data.currentPosition,movement:this.data.movement,initialScrollPosPx:this.initialScrollPosPx,isMoving:this.data.isMoving,allEvents:this.data.events};this.apiTriggerPointerListener("up",i),this.data.realTarget=null,this.data.targetData=null,this.updateData()}pointerMove(t){if(!this.data.enabled||!this.data.isMoving)return;const i=this.state.get("$data.scroll.horizontal.handlePosPx");this.data.pointerState="move",this.data.events.move=t,this.data.currentPosition=this.getRealPosition(t),this.data.movement.x=this.data.currentPosition.x-this.data.initialPosition.x,this.data.movement.y=this.data.currentPosition.y-this.data.initialPosition.y,this.data.movement.x+=i-this.initialScrollPosPx.horizontal;const e={type:this.data.pointerState,originalEvent:t,targetElement:this.data.realTarget,targetData:this.data.targetData,targetType:this.data.targetType,initialPosition:this.data.initialPosition,currentPosition:this.data.currentPosition,movement:this.data.movement,initialScrollPosPx:this.initialScrollPosPx,isMoving:this.data.isMoving,allEvents:this.data.events};this.apiTriggerPointerListener("move",e),this.updateData()}}function o(t={}){return function(i){const a=i.api,o=i.api.mergeDeep,r=i.state.get(s);r&&(t=o(o({},t),r));const h=e(t);i.state.update(s,h);const l=new n(t,i);return a.pluginInitialized("TimelinePointer"),l.destroy}}export{t as CELL,i as ITEM,o as Plugin};