UNPKG

@purtuga/interactions

Version:

Re-usable UI interactions.

2 lines 13.3 kB
!function(e,t,n,r,i,o,s,a,l,c,u,d,f,h,p,m,v,y,b,w,g,k,_,E,x){!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Interactions=t():e.Interactions=t()}(e,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||c.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&c.defineProperty(e,Symbol.toStringTag,{value:"Module"}),c.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=c.create(null);if(n.r(r),c.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return c.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(t,n){t.exports=e.WeakMap.WeakMap=e.WeakMap},function(e,n,r){"use strict";function i(e){return(i=c.setPrototypeOf?c.getPrototypeOf:function(e){return e.__proto__||c.getPrototypeOf(e)})(e)}function o(e,t,n){return(o="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var r=function(e,t){for(;!c.prototype.hasOwnProperty.call(e,t)&&null!==(e=i(e)););return e}(e,t);if(r){var o=c.getOwnPropertyDescriptor(r,t);return o.get?o.get.call(n):o.value}})(e,t,n||e)}function d(e){return function(e){if(l.isArray(e))return e}(e)||function(e){if(Symbol.iterator in c(e)||"[object Arguments]"===c.prototype.toString.call(e))return l.from(e)}(e)||function(){throw new m("Invalid attempt to destructure non-iterable instance")}()}r.r(n);const f=u.bind.call.bind(u.bind)(u.call.bind,u.call),h=f(c.prototype.toString),p=e=>"[object Object]"===h(e),y=c.defineProperty,b=(c.defineProperties,c.keys,(e,t,n,r,i,o=!0,s=!1,a=!0)=>{const l={configurable:o,enumerable:s};return r||i?(l.get=r,l.set=i):(l.writable=a,l.value=n),y(e,t,l),e}),w=[],g=(l.isArray,f(w.forEach),f(w.indexOf),f(w.splice),f(w.slice),E.log),k=E.error||g,_=(E.warn,"undefined"!=typeof Symbol&&Symbol.iterator&&Symbol.iterator,HTMLElement.prototype),H=t;H.head,f(_.appendChild),f(_.insertBefore),f(_.hasAttribute),f(_.getAttribute),f(_.setAttribute),f(_.removeAttribute);function M(e,...t){let n,r,i=e||{},o=t.length,s=!1;for("boolean"==typeof e&&(s=e,i=t.shift()||{},o=t.length),n=0;n<o;n++)if(t[n])for(r in t[n])t[n].hasOwnProperty(r)&&(s&&p(i[r])&&p(t[n][r])?i[r]=M(!0,i[r],t[n][r]):i[r]=t[n][r]);return i}var P=r(0),S={stash:new P.WeakMap,create:function(){return new P.WeakMap}};const A=Symbol("state");class z{on(e,t){return O(this,e).add(t),()=>O(this,e).delete(t)}emit(e,t){O(this,e).forEach(e=>e(t))}clear(){O(this).clear()}}function O(e,t){return e[A]||(e[A]=new Map),t?(e[A].has(t)||e[A].set(t,new Set),e[A].get(t)):e[A]}const D=["on","emit","clear"];function T(e){return e.key&&e.kind?W(e):W}function W(e){const{kind:t,key:n,descriptor:r}=e;if("method"===t&&r.value){let e=!1;const t=r.value;delete r.value,delete r.writable,r.get=function(){if(function(e){if(e.constructor&&e===e.constructor.prototype)throw new v("Can not be accessed directly on constructor.prototype!")}(this),e)return;e=!0;const r=t.bind(this);return b(this,n,r),e=!1,r}}return e}function C(e,t,n,r){let i=t.split(/\s+/),o={};return i.forEach(t=>{e.addEventListener(t,n,r),o[t]={remove:()=>e.removeEventListener(t,n,r)}}),c.create({listeners:o,remove:function(){i.forEach(e=>o[e].remove())}})}const R=new WeakMap;class j{constructor(){this.init&&this.init(...arguments)}onDestroy(e){R.has(this)||R.set(this,new Set),R.get(this).add(e)}destroy(){const e=R.get(this);e&&(e.forEach(e=>{try{e()}catch(e){k(e)}}),e.clear())}}function I(e,t,n){var r=t(function(e){!function(e,t){["method","field"].forEach(function(n){t.forEach(function(t){t.kind===n&&"own"===t.placement&&U(e,t)})})}(e,i.elements)},n),i=function(e,t){var n=[],r=[],i={static:[],prototype:[],own:[]};if(e.forEach(function(e){X(e,i)}),e.forEach(function(e){if(!L(e))return n.push(e);var t=function(e,t){for(var n=[],r=[],i=e.decorators,o=i.length-1;o>=0;o--){var s=t[e.placement];s.splice(s.indexOf(e.key),1);var a=Y(e),l=J((0,i[o])(a)||a);X(e=l.element,t),l.finisher&&r.push(l.finisher);var c=l.extras;if(c){for(var u=0;u<c.length;u++)X(c[u],t);n.push.apply(n,c)}}return{element:e,finishers:r,extras:n}}(e,i);n.push(t.element),n.push.apply(n,t.extras),r.push.apply(r,t.finishers)}),!t)return{elements:n,finishers:r};var o=function(e,t){for(var n=[],r=t.length-1;r>=0;r--){var i=V(e),o=G((0,t[r])(i)||i);if(o.finisher!==x&&n.push(o.finisher),o.elements!==x){e=o.elements;for(var s=0;s<e.length-1;s++)for(var a=s+1;a<e.length;a++)if(e[s].key===e[a].key&&e[s].placement===e[a].placement)throw new m("Duplicated element ("+e[s].key+")")}}return{elements:e,finishers:n}}(n,t);return r.push.apply(r,o.finishers),o.finishers=r,o}(function(e){for(var t=[],n=function(e){return"method"===e.kind&&e.key===o.key&&e.placement===o.placement},r=0;r<e.length;r++){var i,o=e[r];if("method"===o.kind&&(i=t.find(n)))if($(o.descriptor)||$(i.descriptor)){if(L(o)||L(i))throw new ReferenceError("Duplicated methods ("+o.key+") can't be decorated.");i.descriptor=o.descriptor}else{if(L(o)){if(L(i))throw new ReferenceError("Decorators can't be placed on different accessors with for the same property ("+o.key+").");i.decorators=o.decorators}N(o,i)}else t.push(o)}return t}(r.d.map(F)),e);return function(e,t){var n=e.prototype;["method","field"].forEach(function(r){t.forEach(function(t){var i=t.placement;if(t.kind===r&&("static"===i||"prototype"===i)){var o="static"===i?e:n;U(o,t)}})})}(r.F,i.elements),function(e,t){for(var n=0;n<t.length;n++){var r=(0,t[n])(e);if(r!==x){if("function"!=typeof r)throw new m("Finishers must return a constructor.");e=r}}return e}(r.F,i.finishers)}function F(e){var t,n=Z(e.key);"method"===e.kind?(t={value:e.value,writable:!0,configurable:!0,enumerable:!1},c.defineProperty(e.value,"name",{value:"symbol"==typeof n?"":n,configurable:!0})):"get"===e.kind?t={get:e.value,configurable:!0,enumerable:!1}:"set"===e.kind?t={set:e.value,configurable:!0,enumerable:!1}:"field"===e.kind&&(t={configurable:!0,writable:!0,enumerable:!0});var r={kind:"field"===e.kind?"field":"method",key:n,placement:e.static?"static":"field"===e.kind?"own":"prototype",descriptor:t};return e.decorators&&(r.decorators=e.decorators),"field"===e.kind&&(r.initializer=e.value),r}function N(e,t){e.descriptor.get!==x?t.descriptor.get=e.descriptor.get:t.descriptor.set=e.descriptor.set}function L(e){return e.decorators&&e.decorators.length}function $(e){return e!==x&&!(e.value===x&&e.writable===x)}function U(e,t){var n=t.descriptor;if("field"===t.kind){var r=t.initializer;n={enumerable:n.enumerable,writable:n.writable,configurable:n.configurable,value:void 0===r?void 0:r.call(e)}}c.defineProperty(e,t.key,n)}function X(e,t,n){var r=t[e.placement];if(!n&&-1!==r.indexOf(e.key))throw new m("Duplicated element ("+e.key+")");r.push(e.key)}function Y(e){var t={kind:e.kind,key:e.key,placement:e.placement,descriptor:e.descriptor};return c.defineProperty(t,Symbol.toStringTag,{value:"Descriptor",configurable:!0}),"field"===e.kind&&(t.initializer=e.initializer),t}function q(e){if(e!==x)return d(e).map(function(e){var t=B(e);return K(e,"finisher","An element descriptor"),K(e,"extras","An element descriptor"),t})}function B(e){var t=s(e.kind);if("method"!==t&&"field"!==t)throw new m('An element descriptor\'s .kind property must be either "method" or "field", but a decorator created an element descriptor with .kind "'+t+'"');var n=Z(e.key),r=s(e.placement);if("static"!==r&&"prototype"!==r&&"own"!==r)throw new m('An element descriptor\'s .placement property must be one of "static", "prototype" or "own", but a decorator created an element descriptor with .placement "'+r+'"');var i=e.descriptor;K(e,"elements","An element descriptor");var o={kind:t,key:n,placement:r,descriptor:c.assign({},i)};return"field"!==t?K(e,"initializer","A method descriptor"):(K(i,"get","The property descriptor of a field descriptor"),K(i,"set","The property descriptor of a field descriptor"),K(i,"value","The property descriptor of a field descriptor"),o.initializer=e.initializer),o}function J(e){return{element:B(e),finisher:Q(e,"finisher"),extras:q(e.extras)}}function V(e){var t={kind:"class",elements:e.map(Y)};return c.defineProperty(t,Symbol.toStringTag,{value:"Descriptor",configurable:!0}),t}function G(e){var t=s(e.kind);if("class"!==t)throw new m('A class descriptor\'s .kind property must be "class", but a decorator created a class descriptor with .kind "'+t+'"');K(e,"key","A class descriptor"),K(e,"placement","A class descriptor"),K(e,"descriptor","A class descriptor"),K(e,"initializer","A class descriptor"),K(e,"extras","A class descriptor");var n=Q(e,"finisher");return{elements:q(e.elements),finisher:n}}function K(e,t,n){if(e[t]!==x)throw new m(n+" can't have a ."+t+" property.")}function Q(e,t){var n=e[t];if(n!==x&&"function"!=typeof n)throw new m("Expected '"+t+"' to be a function");return n}function Z(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(n!==x){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new m("@@toPrimitive must return a primitive value.")}return("string"===t?s:a)(e)}(e,"string");return"symbol"==typeof t?t:s(t)}const ee=S.create(),te=Symbol("resizable.handle"),ne=["nHandle","neHandle","eHandle","seHandle","sHandle","swHandle","wHandle","nwHandle"],re=/^[ns]H/,ie=/^[ew]H/,oe=/^touch/,se=()=>!1,ae=(e=>H.createElement(e))("template");ae.innerHTML="<div handle></div>";let le=I([function(e){return D.forEach(t=>{e.elements.push({key:t,kind:"method",placement:"prototype",descriptor:c.getOwnPropertyDescriptor(z.prototype,t)})}),e}],function(e,n){return{F:class extends n{constructor(n){super(),e(this);let r={opt:M({},this.constructor.defaults,n)};ee.set(this,r),r.opt.ele.ondragstart=se,this.emit=this.emit.bind(this),function(){let e,n=ee.get(this),r=n.opt;ne.forEach(i=>{e=null;const o=typeof r[i];if(r[i]){if("boolean"===o)(e=t.importNode(ae.content,!0).firstChild).setAttribute("class",`${r.handleClassPrefix} ${r.handleClassPrefix}-${i.substr(0,i.indexOf("H"))}`),e[te]=!0,r.ele.appendChild(e);else if("string"===o){if(!(e=r.ele.querySelector(r[i])))return}else{if(!r[i].addEventListener)return;e=r[i]}n[i]=new ce(this,e,ie.test(i)?"ew":re.test(i)?"ns":"all"),this.onDestroy(n[i].destroy)}})}.call(this)}},d:[]}},j);let ce=I(null,function(e,t){class n extends t{constructor(t,n,r){super(),e(this);const{opt:{ele:i,minWidth:o,minHeight:s}}=ee.get(t);this._emit=t.emit,this._ele=i,this._minWidth=o,this._minHeight=s,this._handle=n,this._ewMovement="all"===r||"ew"===r,this._nsMovement="all"===r||"ns"===r,this._mouseupEv=null,this._mousemoveEv=null,this._mousedownEv=null,this._boxWidth=0,this._boxHeight=0,this._handleX=0,this._handleY=0,this.connect()}}return{F:n,d:[{kind:"method",key:"connect",value(){this._mousedownEv||(this._mousedownEv=C(this._handle,"mousedown touchstart",this.handleMouseDown,!1))}},{kind:"method",key:"disconnect",value(){this.stopResizing(),this._mousedownEv&&(this._mousedownEv.remove(),this._mousedownEv=null)}},{kind:"method",decorators:[T],key:"handleMouseDown",value(e){this._boxWidth=this._ele.clientWidth,this._boxHeight=this._ele.clientHeight,this._handleX=ue(e,"x"),this._handleY=ue(e,"y"),this._mouseupEv=C(H,"mouseup touchend",this.stopResizing,!1),this._mousemoveEv=C(H,"mousemove touchmove",this.handleMouseMoves,!1),this._emit("resize-start")}},{kind:"method",decorators:[T],key:"handleMouseMoves",value(e){const t=this._boxWidth+(ue(e,"x")-this._handleX),n=this._boxHeight+(ue(e,"y")-this._handleY);let r=!1;return t>=this._minWidth&&this._ewMovement&&(this._ele.style.width=t+"px",r=!0),n>=this._minHeight&&this._nsMovement&&(this._ele.style.height=n+"px",r=!0),r&&this._emit("resize"),e.stopPropagation(),e.preventDefault(),!1}},{kind:"method",decorators:[T],key:"stopResizing",value(){let e=!1;this._mousemoveEv&&(this._mousemoveEv.remove(),this._mousemoveEv=null,e=!0),this._mouseupEv&&(this._mouseupEv.remove(),this._mouseupEv=null,e=!0),e&&this._emit("resize-end")}},{kind:"method",decorators:[T],key:"destroy",value(){o(i(n.prototype),"destroy",this).call(this),this.disconnect(),this._handle[te]&&this._handle.parentNode&&this._handle.parentNode.removeChild(this._handle)}}]}},j);function ue(e,t){return(oe.test(e.type)&&e.targetTouches?e.targetTouches.item(0):e)[`client${t.toUpperCase()}`]}le.defaults={ele:null,minWidth:50,minHeight:50,handleClassPrefix:"handle",nHandle:!1,neHandle:!1,eHandle:!1,seHandle:!1,sHandle:!1,swHandle:!1,wHandle:!1,nwHandle:!1};r.d(n,"VERSION",function(){return de}),r.d(n,"Resizable",function(){return le});const de="1.0.2"}])})}(window,document,location,setTimeout,setInterval,Date,String,Number,Array,Object,Function,atob,btoa,Math,JSON,TypeError,Error,encodeURIComponent,decodeURIComponent,requestAnimationFrame,CustomEvent,clearInterval,clearTimeout,console); //# sourceMappingURL=interactions.umd.min.js.map