UNPKG

@scandit/web-datacapture-barcode

Version:

Scandit Data Capture SDK for the Web

1 lines 3.69 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 {safeAwaitAnimation}from'@scandit/web-datacapture-core/build/js/private/utils/safeCancelAnimation.js';import {Timeout}from'@scandit/web-datacapture-core/build/js/private/utils/timeout.js';var n=class extends Event{constructor(t,e){super(t),this.point=e;}},m=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.useViewportBounds=false;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(){var h,l,p,d;let{width:t,height:e}=this.element.getBoundingClientRect();if(t===0||e===0)return;let o=this.useViewportBounds?(l=(h=window.visualViewport)==null?void 0:h.width)!=null?l:window.innerWidth:this.boundaryElement.clientWidth,s=this.useViewportBounds?(d=(p=window.visualViewport)==null?void 0:p.height)!=null?d:window.innerHeight:this.boundaryElement.clientHeight,r=clamp(this.translate.x,Math.abs(this.margin.x),o-t-this.margin.x),a=clamp(this.translate.y,Math.abs(this.margin.y),s-e-this.margin.y);if(this.translate.x!==r||this.translate.y!==a)return new Point(r,a)}async translateTo(t,e,o=false){if(!Point.areEquals(this.translate,t)){if(this.translate=t,e){let s=this.element.animate([{transform:`translate(${this.translate.x}px, ${this.translate.y}px)`}],{duration:200,easing:"ease"});s.play(),await safeAwaitAnimation(s);}this.render(),this.element.dispatchEvent(new n("drag",t)),o||this.element.dispatchEvent(new n("dragend",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,true);}async onPointerUp(t){t instanceof PointerEvent&&(this.pointerDownEvent&&a(this.pointerDownEvent,t)&&(await this.repositionIfOutOfBounds(),this.element.dispatchEvent(new n("dragend",this.translate))),this.offset=void 0,this.draggable=false);}onTouchStart(t){t.returnValue=false;}onResize(){this.repositioningTimeout.start();}};export{n as a,m as b};