!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).WZoom=e()}(this,(function(){"use strict";function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=Array(e);n=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,s=!0,c=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){c=!0,r=t},f:function(){try{s||null==n.return||n.return()}finally{if(c)throw r}}}}function r(t,e,n){return(e=m(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function s(){return s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var o=function(t,e){for(;!{}.hasOwnProperty.call(t,e)&&null!==(t=c(t)););return t}(t,e);if(o){var i=Object.getOwnPropertyDescriptor(o,e);return i.get?i.get.call(arguments.length<3?t:n):i.value}},s.apply(null,arguments)}function c(t){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},c(t)}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&f(t,e)}function u(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(u=function(){return!!t})()}function l(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);e&&(o=o.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,o)}return n}function h(t){for(var e=1;e3&&void 0!==arguments[3]&&arguments[3];t.addEventListener(e,n,o)}function w(t,e,n){var o=arguments.length>3&&void 0!==arguments[3]&&arguments[3];t.removeEventListener(e,n,o)}function H(){return"ontouchstart"in window||navigator.MaxTouchPoints>0||navigator.msMaxTouchPoints>0}function T(t){return"wheel"===t.type||"pointerup"===t.type||"pointerdown"===t.type||"pointermove"===t.type||"mousedown"===t.type||"mousemove"===t.type||"mouseup"===t.type?t.clientX:t.changedTouches[0].clientX}function O(t){return"wheel"===t.type||"pointerup"===t.type||"pointerdown"===t.type||"pointermove"===t.type||"mousedown"===t.type||"mousemove"===t.type||"mouseup"===t.type?t.clientY:t.changedTouches[0].clientY}function S(t,e,n,o,i,r){var s=i/2-(t+e-n)+o;return s*r-s+o}function P(t,e,n,o,i){switch(t){case"left":o/2-i1)return!1;var e=this.coordinatesShift,n=this.coordinates;e.x=T(t)-n.x,e.y=O(t)-n.y,n.x=T(t),n.y=O(t),clearTimeout(this.moveTimer),this.moveTimer=setTimeout((function(){e.x=0,e.y=0}),50),t.data=h(h({},t.data||{}),{},{x:e.x,y:e.y}),this._run(x,t)}}])}(D),E="click",$="dblclick",z="wheel",C=function(t){function i(t){var o;return n(this,i),(o=e(this,i)).target=t,o.coordsOnDown=null,o.pressingTimeout=null,o.firstClick=!0,o.isTouch=H(),o.events=o.isTouch?{down:"touchstart",up:"touchend"}:{down:"mousedown",up:"mouseup"},o.events.options=!!o.isTouch&&{passive:!0},o._downHandler=o._downHandler.bind(o),o._upHandler=o._upHandler.bind(o),o._wheelHandler=o._wheelHandler.bind(o),_(o.target,o.events.down,o._downHandler,o.events.options),_(o.target,o.events.up,o._upHandler,o.events.options),_(o.target,z,o._wheelHandler),o}return a(i,t),o(i,[{key:"destroy",value:function(){w(this.target,this.events.down,this._downHandler,this.events.options),w(this.target,this.events.up,this._upHandler,this.events.options),w(this.target,z,this._wheelHandler,this.events.options),p(i,"destroy",this)([])}},{key:"_downHandler",value:function(t){this.coordsOnDown=null,(this.isTouch&&1===t.touches.length||1===t.buttons)&&(this.coordsOnDown={x:T(t),y:O(t)}),clearTimeout(this.pressingTimeout)}},{key:"_upHandler",value:function(t){var e=this,n=this.subscribes[$]?setTimeout:function(t,e){return t()};this.firstClick?(this.firstClick=!1,this.pressingTimeout=n((function(){e._isDetectedShift(t)||e._run(E,t),e.firstClick=!0}),200)):this.pressingTimeout=n((function(){e._isDetectedShift(t)||e._run($,t),e.firstClick=!0}),100)}},{key:"_wheelHandler",value:function(t){this._run(z,t)}},{key:"_isDetectedShift",value:function(t){return!(this.coordsOnDown&&this.coordsOnDown.x===T(t)&&this.coordsOnDown.y===O(t))}}])}(D),L="pinchtozoom",Y=function(t){function i(t){var o;return n(this,i),(o=e(this,i)).target=t,o.fingersHypot=null,o.zoomPinchWasDetected=!1,o._touchMoveHandler=o._touchMoveHandler.bind(o),o._touchEndHandler=o._touchEndHandler.bind(o),_(o.target,"touchmove",o._touchMoveHandler),_(o.target,"touchend",o._touchEndHandler),o}return a(i,t),o(i,[{key:"destroy",value:function(){w(this.target,"touchmove",this._touchMoveHandler),w(this.target,"touchend",this._touchEndHandler),p(i,"destroy",this)([])}},{key:"_touchMoveHandler",value:function(t){if(2===t.targetTouches.length){var e=t.targetTouches[0].clientX,n=t.targetTouches[0].clientY,o=t.targetTouches[1].clientX,i=t.targetTouches[1].clientY,r=Math.round(Math.sqrt(Math.pow(Math.abs(e-o),2)+Math.pow(Math.abs(n-i),2))),s=0;if(r>this.fingersHypot+5&&(s=-1),r1&&void 0!==arguments[1]?arguments[1]:{};if(this._init=this._init.bind(this),this._prepare=this._prepare.bind(this),this._computeScale=this._computeScale.bind(this),this._computePosition=this._computePosition.bind(this),this._transform=this._transform.bind(this),this.content={},"string"==typeof t){if(this.content.$element=document.querySelector(t),!this.content.$element)throw"WZoom: Element with selector `".concat(t,"` not found")}else{if(!(t instanceof HTMLElement))throw"WZoom: `selectorOrHTMLElement` must be selector or HTMLElement, and not ".concat({}.toString.call(t));this.content.$element=t}this.viewport={},this.viewport.$element=this.content.$element.parentElement,this.options=function(t,e){var n=Object.assign({},e,t);if(H())n.smoothTime=0,n.smoothTimeDrag=0;else{var o=Number(n.smoothTime),i=Number(n.smoothTimeDrag);n.smoothTime=isNaN(o)?k.smoothTime:o,n.smoothTimeDrag=isNaN(i)?n.smoothTime:i}return n}(e,k),this.isTouch=H(),this.direction=1,this.observers=[],"image"===this.options.type?this.content.$element.complete?this._init():_(this.content.$element,"load",this._init,{once:!0}):this._init()}return X.prototype={constructor:X,_init:function(){var t=this,e=this.viewport,n=this.content,o=this.options,i=this.observers;if(this._prepare(),this._destroyObservers(),!0===o.dragScrollable){var r=new M(n.$element);i.push(r),"function"==typeof o.onGrab&&r.on(W,(function(e){e.preventDefault(),o.onGrab(e,t)})),"function"==typeof o.onDrop&&r.on(j,(function(e){e.preventDefault(),o.onDrop(e,t)})),r.on(x,(function(i){i.preventDefault();var r=i.data,s=r.x,c=r.y,a=n.currentLeft+s,u=n.currentTop+c,l=(n.currentWidth-e.originalWidth)/2+n.correctX,h=(n.currentHeight-e.originalHeight)/2+n.correctY;Math.abs(a)<=l&&(n.currentLeft=a),Math.abs(u)<=h&&(n.currentTop=u),t._transform(o.smoothTimeDrag),"function"==typeof o.onMove&&o.onMove(i,t)}))}var s=new C(n.$element);if(i.push(s),!o.disableWheelZoom)if(this.isTouch){var c=new Y(n.$element);i.push(c),c.on(L,(function(e){var n=e.data,o=n.clientX,i=n.clientY,r=n.direction,s=t._computeScale(r);t._computePosition(s,o,i),t._transform()}))}else s.on(z,(function(e){e.preventDefault();var n=o.reverseWheelDirection?-e.deltaY:e.deltaY,i=t._computeScale(n);t._computePosition(i,T(e),O(e)),t._transform()}));if(o.zoomOnClick||o.zoomOnDblClick){var a=o.zoomOnDblClick?$:E;s.on(a,(function(e){var o=1===t.direction?n.maxScale:n.minScale;t._computePosition(o,T(e),O(e)),t._transform(),t.direction*=-1}))}},_prepare:function(){var t=this.viewport,e=this.content,n=this.options,o=g(t.$element),i=o.left,r=o.top;t.originalWidth=t.$element.offsetWidth,t.originalHeight=t.$element.offsetHeight,t.originalLeft=i,t.originalTop=r,"image"===n.type?(e.originalWidth=n.width||e.$element.naturalWidth,e.originalHeight=n.height||e.$element.naturalHeight):(e.originalWidth=n.width||e.$element.offsetWidth,e.originalHeight=n.height||e.$element.offsetHeight),e.maxScale=n.maxScale,e.minScale=n.minScale||Math.min(t.originalWidth/e.originalWidth,t.originalHeight/e.originalHeight,e.maxScale),e.currentScale=e.minScale,e.currentWidth=e.originalWidth*e.currentScale,e.currentHeight=e.originalHeight*e.currentScale;var s=function(t,e,n){var o=0,i=0;switch(n){case"top":i=(e.currentHeight-t.originalHeight)/2;break;case"right":o=(e.currentWidth-t.originalWidth)/2*-1;break;case"bottom":i=(e.currentHeight-t.originalHeight)/2*-1;break;case"left":o=(e.currentWidth-t.originalWidth)/2}return[o,i]}(t,e,n.alignContent),c=d(s,2);e.alignPointX=c[0],e.alignPointY=c[1],e.currentLeft=e.alignPointX,e.currentTop=e.alignPointY;var a=function(t,e,n){var o=Math.max(0,(t.originalWidth-e.currentWidth)/2),i=Math.max(0,(t.originalHeight-e.currentHeight)/2);switch(n){case"top":i=0;break;case"right":o=0;break;case"bottom":i*=2;break;case"left":o*=2}return[o,i]}(t,e,n.alignContent),u=d(a,2);e.correctX=u[0],e.correctY=u[1],"function"==typeof n.prepare&&n.prepare(this),this._transform()},_computeScale:function(t){this.direction=t<0?1:-1;var e=this.content,n=e.minScale,o=e.maxScale,i=e.currentScale*Math.pow(this.options.speed,this.direction);return i<=n?(this.direction=1,n):i>=o?(this.direction=-1,o):i},_computePosition:function(t,e,n){var o=this.viewport,i=this.content,r=this.options,s=this.direction,c=i.originalWidth*t,a=i.originalHeight*t,u=y(),l=b(),h=S(e,u,o.originalLeft,i.currentLeft,o.originalWidth,c/i.currentWidth),f=S(n,l,o.originalTop,i.currentTop,o.originalHeight,a/i.currentHeight);-1===s&&(h=P(r.alignContent,o.originalWidth,i.correctX,c,h),f=P(r.alignContent,o.originalHeight,i.correctY,a,f)),t===i.minScale&&(h=i.alignPointX,f=i.alignPointY),i.currentWidth=c,i.currentHeight=a,i.currentLeft=h,i.currentTop=f,i.currentScale=t},_transform:function(t){var e,n;void 0===t&&(t=this.options.smoothTime),e=this.content.$element,(n=t)?e.style.transition="transform ".concat(n,"s"):e.style.removeProperty("transition"),function(t,e,n,o){t.style.transform="translate(".concat(e,"px, ").concat(n,"px) scale(").concat(o,")")}(this.content.$element,this.content.currentLeft,this.content.currentTop,this.content.currentScale),"function"==typeof this.options.rescale&&this.options.rescale(this)},_zoom:function(t){var e,n,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};void 0!==o.x&&void 0!==o.y||(e=this.viewport,o={x:(n=g(e.$element)).left+e.originalWidth/2-y(),y:n.top+e.originalHeight/2-b()}),this._computePosition(t,o.x,o.y),this._transform()},_destroyObservers:function(){var t,e=i(this.observers);try{for(e.s();!(t=e.n()).done;){t.value.destroy()}}catch(t){e.e(t)}finally{e.f()}},prepare:function(){this._prepare()},transform:function(t,e,n){var o=this.content;o.currentWidth=o.originalWidth*n,o.currentHeight=o.originalHeight*n,o.currentLeft=e,o.currentTop=t,o.currentScale=n,this._transform()},zoomUp:function(){this._zoom(this._computeScale(-1))},zoomDown:function(){this._zoom(this._computeScale(1))},maxZoomUp:function(){this._zoom(this.content.maxScale)},maxZoomDown:function(){this._zoom(this.content.minScale)},zoomUpToPoint:function(t){this._zoom(this._computeScale(-1),t)},zoomDownToPoint:function(t){this._zoom(this._computeScale(1),t)},maxZoomUpToPoint:function(t){this._zoom(this.content.maxScale,t)},destroy:function(){for(var t in this.content.$element.style.removeProperty("transition"),this.content.$element.style.removeProperty("transform"),"image"===this.options.type&&w(this.content.$element,"load",this._init),this._destroyObservers(),this)this.hasOwnProperty(t)&&(this[t]=null)}},X.create=function(t){return new X(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{})},X}));