UNPKG

@scandit/web-datacapture-barcode

Version:

Scandit Data Capture SDK for the Web

1 lines 3.2 kB
import {a}from'./chunk-T2Y2C76P.js';import {Point}from'@scandit/web-datacapture-core';import {clamp}from'@scandit/web-datacapture-core/build/js/private/utils/clamp.js';import {Timeout}from'@scandit/web-datacapture-core/build/js/private/utils/timeout.js';var s=class extends Event{constructor(t,e){super(t),this.point=e;}},l=class{constructor(t,e){this.translate=new Point(0,0);this.draggable=false;this.pointerDownListener=this.onPointerDown.bind(this);this.pointerMoveListener=this.onPointerMove.bind(this);this.pointerUpListener=this.onPointerUp.bind(this);this.touchStartListener=this.onTouchStart.bind(this);this.resizeListener=this.onResize.bind(this);this.repositioningTimeout=new Timeout(500,this.repositionIfOutOfBounds.bind(this));this.margin=new Point(0,0);this.element=t,this.boundaryElement=e!=null&&e.clientWidth&&(e!=null&&e.clientHeight)?e:document.body;}get translateWithMargin(){return new Point(this.translate.x+this.margin.x,this.translate.y+this.margin.y)}connect(){this.render(),this.element.addEventListener("pointerdown",this.pointerDownListener),this.element.addEventListener("pointermove",this.pointerMoveListener),this.element.addEventListener("pointerup",this.pointerUpListener),this.element.addEventListener("touchstart",this.touchStartListener),window.addEventListener("resize",this.resizeListener);}disconnect(){this.element.removeEventListener("pointerdown",this.pointerDownListener),this.element.removeEventListener("pointermove",this.pointerMoveListener),this.element.removeEventListener("pointerup",this.pointerUpListener),this.element.removeEventListener("touchstart",this.touchStartListener),window.removeEventListener("resize",this.resizeListener),this.repositioningTimeout.stop();}getBoundedTranslate(){let{width:t,height:e}=this.element.getBoundingClientRect(),n=this.boundaryElement.clientWidth,c=this.boundaryElement.clientHeight,o=clamp(this.translate.x,Math.abs(this.margin.x),n-t-this.margin.x),r=clamp(this.translate.y,Math.abs(this.margin.y),c-e-this.margin.y);if(this.translate.x!==o||this.translate.y!==r)return new Point(o,r)}async translateTo(t,e){if(this.translate=t,e){let n=this.element.animate([{transform:`translate(${this.translate.x}px, ${this.translate.y}px)`}],{duration:200,easing:"ease"});n.play(),await n.finished;}this.render(),this.element.dispatchEvent(new s("drag",t));}render(){this.element.style.setProperty("transform",`translate(${this.translate.x}px, ${this.translate.y}px)`);}async repositionIfOutOfBounds(){let t=this.getBoundedTranslate();t&&await this.translateTo(t,true);}onPointerDown(t){t instanceof PointerEvent&&(this.offset=new Point(this.translate.x-t.clientX,this.translate.y-t.clientY),this.pointerDownEvent=t);}onPointerMove(t){t instanceof PointerEvent&&this.offset&&this.draggable&&this.translateTo(new Point(this.offset.x+t.clientX,this.offset.y+t.clientY),false);}async onPointerUp(t){t instanceof PointerEvent&&this.pointerDownEvent&&a(this.pointerDownEvent,t)&&(await this.repositionIfOutOfBounds(),this.offset=void 0,this.draggable=false,this.element.dispatchEvent(new s("dragend",this.translate)));}onTouchStart(t){t.returnValue=false;}onResize(){this.repositioningTimeout.start();}};export{s as a,l as b};