2 lines
3.9 KiB
JavaScript
2 lines
3.9 KiB
JavaScript
import*as t from"../utils/arr.prod.js";import e from"../utils/browser.prod.js";import s from"../utils/clone.prod.js";import{getElementRect as o,matchesUpTo as i,nodeContains as n,trySelector as r}from"../utils/domUtils.prod.js";import h from"../utils/extend.prod.js";import c from"../utils/is.prod.js";import a from"../utils/normalizeListeners.prod.js";import{getWindow as l}from"../utils/window.prod.js";import{Eventable as p}from"./Eventable.prod.js";import d from"./isNonNativeEvent.prod.js";export class Interactable{get _defaults(){return{base:{},perAction:{},actions:{}}}constructor(t,e,s,o){this.options=void 0,this._actions=void 0,this.target=void 0,this.events=new p,this._context=void 0,this._win=void 0,this._doc=void 0,this._scopeEvents=void 0,this._rectChecker=void 0,this._actions=e.actions,this.target=t,this._context=e.context||s,this._win=l(r(t)?this._context:t),this._doc=this._win.document,this._scopeEvents=o,this.set(e)}setOnEvents(t,e){return c.func(e.onstart)&&this.on(t+"start",e.onstart),c.func(e.onmove)&&this.on(t+"move",e.onmove),c.func(e.onend)&&this.on(t+"end",e.onend),c.func(e.oninertiastart)&&this.on(t+"inertiastart",e.oninertiastart),this}updatePerActionListeners(t,e,s){(c.array(e)||c.object(e))&&this.off(t,e),(c.array(s)||c.object(s))&&this.on(t,s)}setPerAction(e,o){const i=this._defaults;for(const n in o){const r=n,a=this.options[e],l=o[r];"listeners"===r&&this.updatePerActionListeners(e,a.listeners,l),c.array(l)?a[r]=t.from(l):c.plainObject(l)?(a[r]=h(a[r]||{},s(l)),c.object(i.perAction[r])&&"enabled"in i.perAction[r]&&(a[r].enabled=!1!==l.enabled)):c.bool(l)&&c.object(i.perAction[r])?a[r].enabled=l:a[r]=l}}getRect(t){return t=t||(c.element(this.target)?this.target:null),c.string(this.target)&&(t=t||this._context.querySelector(this.target)),o(t)}rectChecker(t){return c.func(t)?(this._rectChecker=t,this.getRect=t=>{const e=h({},this._rectChecker(t));return"width"in e||(e.width=e.right-e.left,e.height=e.bottom-e.top),e},this):null===t?(delete this.getRect,delete this._rectChecker,this):this.getRect}_backCompatOption(t,e){if(r(e)||c.object(e)){this.options[t]=e;for(const s in this._actions.map)this.options[s][t]=e;return this}return this.options[t]}origin(t){return this._backCompatOption("origin",t)}deltaSource(t){return"page"===t||"client"===t?(this.options.deltaSource=t,this):this.options.deltaSource}context(){return this._context}inContext(t){return this._context===t.ownerDocument||n(this._context,t)}testIgnoreAllow(t,e,s){return!this.testIgnore(t.ignoreFrom,e,s)&&this.testAllow(t.allowFrom,e,s)}testAllow(t,e,s){return!t||!!c.element(s)&&(c.string(t)?i(s,t,e):!!c.element(t)&&n(t,s))}testIgnore(t,e,s){return!(!t||!c.element(s))&&(c.string(t)?i(s,t,e):!!c.element(t)&&n(t,s))}fire(t){return this.events.fire(t),this}_onOff(t,s,o,i){c.object(s)&&!c.array(s)&&(i=o,o=null);const n="on"===t?"add":"remove",r=a(s,o);for(let s in r){"wheel"===s&&(s=e.wheelEvent);for(const e of r[s])d(s,this._actions)?this.events[t](s,e):c.string(this.target)?this._scopeEvents[n+"Delegate"](this.target,this._context,s,e,i):this._scopeEvents[n](this.target,s,e,i)}return this}on(t,e,s){return this._onOff("on",t,e,s)}off(t,e,s){return this._onOff("off",t,e,s)}set(t){const e=this._defaults;c.object(t)||(t={}),this.options=s(e.base);for(const s in this._actions.methodDict){const o=s,i=this._actions.methodDict[o];this.options[o]={},this.setPerAction(o,h(h({},e.perAction),e.actions[o])),this[i](t[o])}for(const e in t)c.func(this[e])&&this[e](t[e]);return this}unset(){if(c.string(this.target))for(const t in this._scopeEvents.delegatedEvents){const e=this._scopeEvents.delegatedEvents[t];for(let s=e.length-1;s>=0;s--){const{selector:o,context:i,listeners:n}=e[s];o===this.target&&i===this._context&&e.splice(s,1);for(let e=n.length-1;e>=0;e--)this._scopeEvents.removeDelegate(this.target,this._context,t,n[e][0],n[e][1])}}else this._scopeEvents.remove(this.target,"all")}}
|
|
//# sourceMappingURL=Interactable.prod.js.map
|