@scandit/web-datacapture-barcode
Version:
Scandit Data Capture SDK for the Web
1 lines • 3.63 kB
JavaScript
import{didDrag as e}from"../utils/didDrag.js";import{Point as t}from"@scandit/web-datacapture-core";import{clamp as n}from"@scandit/web-datacapture-core/build/js/private/utils/clamp.js";import{safeAwaitAnimation as r}from"@scandit/web-datacapture-core/build/js/private/utils/safeCancelAnimation.js";import{Timeout as i}from"@scandit/web-datacapture-core/build/js/private/utils/timeout.js";var a=class extends Event{constructor(e,t){super(e),this.point=t}},o=class{constructor(e,n){this.translate=new t(0,0),this.draggable=!1,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 i(500,this.repositionIfOutOfBounds.bind(this)),this.margin=new t(0,0),this.useViewportBounds=!1,this.element=e,this.boundaryElement=n!=null&&n.clientWidth&&n!=null&&n.clientHeight?n:document.body}get translateWithMargin(){return new t(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 e,r;let{width:i,height:a}=this.element.getBoundingClientRect();if(i===0||a===0)return;let o=this.useViewportBounds?((e=window.visualViewport)==null?void 0:e.width)??window.innerWidth:this.boundaryElement.clientWidth,s=this.useViewportBounds?((r=window.visualViewport)==null?void 0:r.height)??window.innerHeight:this.boundaryElement.clientHeight,c=n(this.translate.x,Math.abs(this.margin.x),o-i-this.margin.x),l=n(this.translate.y,Math.abs(this.margin.y),s-a-this.margin.y);if(this.translate.x!==c||this.translate.y!==l)return new t(c,l)}async translateTo(e,n,i=!1){if(!t.areEquals(this.translate,e)){if(this.translate=e,n){let e=this.element.animate([{transform:`translate(${this.translate.x}px, ${this.translate.y}px)`}],{duration:200,easing:`ease`});e.play(),await r(e)}this.render(),this.element.dispatchEvent(new a(`drag`,e)),i||this.element.dispatchEvent(new a(`dragend`,e))}}render(){this.element.style.setProperty(`transform`,`translate(${this.translate.x}px, ${this.translate.y}px)`)}async repositionIfOutOfBounds(){let e=this.getBoundedTranslate();e&&await this.translateTo(e,!0)}onPointerDown(e){e instanceof PointerEvent&&(this.offset=new t(this.translate.x-e.clientX,this.translate.y-e.clientY),this.pointerDownEvent=e)}onPointerMove(e){e instanceof PointerEvent&&this.offset&&this.draggable&&this.translateTo(new t(this.offset.x+e.clientX,this.offset.y+e.clientY),!1,!0)}async onPointerUp(t){t instanceof PointerEvent&&(this.pointerDownEvent&&e(this.pointerDownEvent,t)&&(await this.repositionIfOutOfBounds(),this.element.dispatchEvent(new a(`dragend`,this.translate))),this.offset=void 0,this.draggable=!1)}onTouchStart(e){e.returnValue=!1}onResize(){this.repositioningTimeout.start()}};export{o as DragController,a as DragControllerEvent};