UNPKG

@whitesev/pops

Version:

弹窗库

1,046 lines (1,027 loc) 617 kB
var pops = (function () { 'use strict'; const SymbolEvents = Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)); const PopsCoreDefaultEnv = { document: document, window: window, globalThis: globalThis, self: self, }; const PopsCoreEnv = { document: document, window: window, globalThis: globalThis, self: self, }; const PopsCore = { init(option) { if (!option) { option = Object.assign({}, PopsCoreDefaultEnv); } Object.assign(PopsCoreEnv, option); }, get document() { return PopsCoreEnv.document; }, get window() { return PopsCoreEnv.window; }, get globalThis() { return PopsCoreEnv.globalThis; }, get self() { return PopsCoreEnv.self; }, }; const OriginPrototype = { Object: { defineProperty: Object.defineProperty, }, }; let t$1 = class t{constructor(){this.__map={};}beforeEach(t){this.__interceptor=t;}on(t,i){const s=Array.isArray(t)?t:[t];for(const t of s){this.__map[t]=this.__map[t]||[];const s=this.__map[t];s&&s.push(i);}return this}emit(t,i,s){void 0!==this.__interceptor?this.__interceptor(t,(()=>{this.__emit(t,i),s&&s();})):(this.__emit(t,i),s&&s());}__emit(t,i){const s=this.__map[t];if(Array.isArray(s)&&(null==s?void 0:s.length))for(const _ of s)_(i,t);this.event=i;}off(t,i){const s=this.__map[t];if(void 0!==s)if(void 0===i)delete this.__map[t];else {const t=s.findIndex((t=>t===i));s.splice(t,1);}}destroy(){this.__map={};}}; const n$1="clientX",e$2="clientY",t=16,c$3="start",o$1="move",s$1="cancel",u$3="end",a$2="left",i$3="right",r$4="up",d$1="down",m$2={4:"start",5:"move",1:"end",3:"cancel"};function v$1(n){return m$2[n]}function b(n,e,t){const c={1:{0:{move:4},4:{move:5,end:1,cancel:3},5:{move:5,end:1,cancel:3}},0:{4:{move:2,end:1,cancel:3},5:{start:2,move:2,end:1,cancel:3}}}[Number(n)][e];return void 0!==c&&c[t]||0}function g$1(n){[1,3,2].includes(n.state)&&(n.state=0);}function h$3(n){return [5,1,3].includes(n)}function j(n){if(n.disabled)return n.state=0,!0}function O(n,e){return Object.assign(Object.assign(Object.assign({},n),e),{state:0,disabled:!1})}function p$3(n){return Math.round(100*n)/100} function r$3(){let t,o,i,r,a=0;return function(u){if(t=o,void 0!==u){a=Number.MAX_SAFE_INTEGER>a?++a:1;const h=function(t,o){const{phase:i,points:r,changedPoints:a,nativeEvent:u}=t,h=r.length,p=c$3===i,g=u$3===i&&0===h||s$1===i,l=Date.now(),{x:d,y:m}=c$2(r)||c$2(a),{currentTarget:v}=u;return Object.assign(t,{id:o,x:d,y:m,timestamp:l,isStart:p,isEnd:g,pointLength:h,currentTarget:v,getOffset(t=v){const e=t.getBoundingClientRect();return {x:d-Math.round(e.left),y:m-Math.round(e.top)}}})}(u,a);o=h;const{isStart:p,pointLength:g}=h;return p&&(i=h,t=void 0,r=1<g?h:void 0),Object.assign(Object.assign({},h),{prevInput:t,startMultiInput:r,startInput:i})}}}function c$2(t){const{length:e}=t;if(0<e){if(1===e){const{clientX:e,clientY:n}=t[0];return {x:Math.round(e),y:Math.round(n)}}const n=t.reduce(((t,e)=>(t.x+=e[n$1],t.y+=e[e$2],t)),{x:0,y:0});return {x:Math.round(n.x/e),y:Math.round(n.y/e)}}}function a$1(t,e,n,s){const o={};for(const t in n)["target","currentTarget","type"].includes(t)||(o[t]=n[t]);let i;return document.createEvent?(i=document.createEvent("HTMLEvents"),i.initEvent(t,null==s?void 0:s.bubbles,null==s?void 0:s.cancelable)):i=new Event(t,s),Object.assign(i,o,{match:()=>n.targets&&0<n.targets.length&&n.targets.every((t=>i.currentTarget.contains(t)))}),e.dispatchEvent(i)}function u$2(t,e){const{preventDefault:n}=e;return s=n,"[object Function]"===Object.prototype.toString.call(s)?n(t):!!n;var s;}const h$2=["touchstart","touchmove","touchend","touchcancel","mousedown"],p$2=["mousemove","mouseup"];const g={domEvents:{bubbles:!0,cancelable:!0},preventDefault:t=>{if(t.target&&"tagName"in t.target){const{tagName:e}=t.target;return !/^(?:INPUT|TEXTAREA|BUTTON|SELECT)$/.test(e)}return !1}};let l$1 = class l extends t$1{constructor(t,e){super(),this.v="2.1.3",this.__computeFunctionList=[],this.__computeFunctionCreatorList=[],this.__pluginContexts=[],this.__isIgnoreMouse=!1,this.el=t,this.c={},this.__options=Object.assign(Object.assign({},g),e);const n=function(t){const e=r$3();return function(n){const s=[],o=[];Array.from(n.touches).forEach((({clientX:e,clientY:n,target:i})=>{(null==t?void 0:t.contains(i))&&(s.push(i),o.push({clientX:e,clientY:n,target:i}));}));const i=Array.from(n.changedTouches).map((({clientX:t,clientY:e,target:n})=>({clientX:t,clientY:e,target:n})));return e({phase:n.type.replace("touch",""),changedPoints:i,points:o,nativeEvent:n,target:n.target,targets:s})}}(this.el),s=function(){let t,e=!1,n=null;const s=r$3();return function(o){const{clientX:i,clientY:r,type:c,button:a,target:u}=o;let h,p=[{clientX:i,clientY:r,target:u}];if("mousedown"===c&&0===a)n=u,e=!0,h="start";else {if(!e)return;"mousemove"===c?h="move":"mouseup"===c&&(p=[],h="end",e=!1);}const g=t||[{clientX:i,clientY:r,target:u}];if(t=[{clientX:i,clientY:r,target:u}],void 0!==h)return s({phase:h,changedPoints:g,points:p,target:n,targets:[n],nativeEvent:o})}}();if(this.__inputCreatorMap={touchstart:n,touchmove:n,touchend:n,touchcancel:n,mousedown:s,mousemove:s,mouseup:s},this.on("at:after",(t=>{const{target:e,__type:n}=t,{domEvents:s}=this.__options;s&&void 0!==this.el&&e&&(a$1(n,e,t,s),a$1("at:after",e,t,s));})),void 0!==t){t.style.webkitTapHighlightColor="rgba(0,0,0,0)";let e=!1;try{const t={};Object.defineProperty(t,"passive",{get(){e=!0;}}),window.addEventListener("_",(()=>{}),t);}catch(t){}this.on("u",function(t,e,n){return h$2.forEach((s=>{t.addEventListener(s,e,n);})),p$2.forEach((t=>{window.addEventListener(t,e,n);})),()=>{h$2.forEach((n=>{t.removeEventListener(n,e);})),p$2.forEach((t=>{window.removeEventListener(t,e);}));}}(t,this.catchEvent.bind(this),!1===this.__options.preventDefault&&e?{passive:!0}:{passive:!1}));}}use(t,e){this.__pluginContexts.push(t(this,e));}catchEvent(t){const e=this.__inputCreatorMap[t.type](t);if(void 0!==e){const n=()=>t.stopPropagation(),s=()=>t.stopImmediatePropagation(),o=()=>t.preventDefault();if(u$2(t,this.__options))o();else if("touchstart"===t.type?this.__isIgnoreMouse=!0:"touchmove"===t.type&&(this.__isIgnoreMouse=!1),this.__isIgnoreMouse&&t.type.startsWith("mouse"))return void("mouseup"===t.type&&(this.__isIgnoreMouse=!1));this.emit("input",e),this.emit2(`at:${e.phase}`,e,{});const i={};this.__computeFunctionList.forEach((t=>{const n=t(e,i);if(void 0!==n)for(const t in n)i[t]=n[t];})),this.emit("computed",Object.assign(Object.assign(Object.assign({},e),i),{stopPropagation:n,stopImmediatePropagation:s,preventDefault:o}));}}compute(t,e){for(const e of t)this.__computeFunctionCreatorList.includes(e)||(this.__computeFunctionCreatorList.push(e),this.__computeFunctionList.push(e()));this.on("computed",e);}beforeEach(t){super.beforeEach(((e,n)=>{var s;(null===(s=this.c)||void 0===s?void 0:s.name)?t(e,n):n();}));}get(t){return this.__pluginContexts.find((e=>t===e.name))}set(t){this.__options=Object.assign(Object.assign({},this.__options),t);}emit2(t,e,n){this.c=n,this.emit(t,Object.assign(Object.assign({},e),{type:t}),(()=>{this.emit("at:after",Object.assign(Object.assign({},e),{name:t,__type:t}));}));}destroy(){this.emit("u"),super.destroy();}}; var x=r=>Math.sqrt(r.x*r.x+r.y*r.y),y=(r,a)=>r.x*a.x+r.y*a.y,e$1=(r,a)=>{var t=x(r)*x(a);if(0===t)return 0;var h=y(r,a)/t;return h>1&&(h=1),Math.acos(h)},n=(r,a)=>r.x*a.y-a.x*r.y,o=r=>r/Math.PI*180,s=(r,a)=>{var t=e$1(r,a);return n(r,a)>0&&(t*=-1),o(t)},u$1=(x,y)=>{if(0!==x||0!==y)return Math.abs(x)>=Math.abs(y)?0<x?i$3:a$2:0<y?d$1:r$4}; function p$1(){let n=0,e=0;return function(o,r){const{prevVecotr:i,startVecotr:a,activeVecotr:c}=r;return c&&(e=Math.round(s(c,i)),n=Math.round(s(c,a))),{angle:n,deltaAngle:e}}}function d(){return function(t){const{prevInput:e}=t;let o$1=0,r=0,i=0;if(void 0!==e&&(o$1=t.x-e.x,r=t.y-e.y,0!==o$1||0!==r)){const t=Math.sqrt(Math.pow(o$1,2)+Math.pow(r,2));i=Math.round(o(Math.acos(Math.abs(o$1)/t)));}return {deltaX:o$1,deltaY:r,deltaXYAngle:i}}}function h$1(){let t,n=0,u=0,s=0,p=0,d=0;return function(h){const{phase:l,startInput:f}=h;return c$3===l?(n=0,u=0,s=0,p=0,d=0):o$1===l&&(n=Math.round(h.points[0][n$1]-f.points[0][n$1]),u=Math.round(h.points[0][e$2]-f.points[0][e$2]),s=Math.abs(n),p=Math.abs(u),d=Math.round(x({x:s,y:p})),t=u$1(n,u)),{displacementX:n,displacementY:u,distanceX:s,distanceY:p,distance:d,overallDirection:t}}}function l(){let t=1;return function(n,o){let r=1;const{prevVecotr:i,startVecotr:a,activeVecotr:c}=o;return c&&(r=p$3(x(c)/x(i)),t=p$3(x(c)/x(a))),{scale:t,deltaScale:r}}}function f(){let t$1,n,e=0,r=0,i=0,a=0;return function(c){if(void 0!==c){n=n||c.startInput;const u=c.timestamp-n.timestamp;if(t<u){const s=c.x-n.x,p=c.y-n.y;i=Math.round(s/u*100)/100,a=Math.round(p/u*100)/100,e=Math.abs(i),r=Math.abs(a),t$1=u$1(s,p),n=c;}}return {velocityX:e,velocityY:r,speedX:i,speedY:a,direction:t$1}}}function M(){let t=0;return function(n){const{phase:e}=n;return c$3===e&&(t=n.pointLength),{maxPointLength:t}}}function v(t){return {x:t.points[1][n$1]-t.points[0][n$1],y:t.points[1][e$2]-t.points[0][e$2]}}function m$1(){let t,n,e;return function(o){const{prevInput:r,startMultiInput:i}=o;return void 0!==i&&void 0!==r&&o.id!==i.id&&1<r.pointLength&&1<o.pointLength?(t=v(i),n=v(r),e=v(o)):e=void 0,{startVecotr:t,prevVecotr:n,activeVecotr:e}}} const m={name:"tap",pointLength:1,tapTimes:1,waitNextTapTime:300,maxDistance:2,maxDistanceFromPrevTap:9,maxPressTime:250};function r$2(r,s){const c=O(m,s);let p,u,x$1,T=0;function f(){T=0,p=void 0,u=void 0;}return r.compute([h$1,M],(t=>{if(j(c))return;const{phase:i,x:o,y:m}=t;u$3===i&&(c.state=0,!function(){const{startInput:e,pointLength:n,timestamp:a}=t,i=a-e.timestamp,{distance:o,maxPointLength:m}=t;return m===c.pointLength&&0===n&&c.maxDistance>=o&&c.maxPressTime>i}()?(f(),c.state=2):(clearTimeout(x$1),function(t,e){if(void 0!==p){const n=x({x:t.x-p.x,y:t.y-p.y});return p=t,e.maxDistanceFromPrevTap>=n}return p=t,!0}({x:o,y:m},c)&&function(t){const e=performance.now();if(void 0===u)return u=e,!0;{const n=e-u;return u=e,n<t}}(c.waitNextTapTime)?T++:T=1,0==T%c.tapTimes?(c.state=1,r.emit2(c.name,t,c),f()):x$1=setTimeout((()=>{c.state=2,f();}),c.waitNextTapTime)));})),c} const p={name:"pan",threshold:10,pointLength:1};function u(u,d$1){const f$1=O(p,d$1);return u.compute([f,h$1,d],(t=>{if(g$1(f$1),j(f$1))return;const c=function(){const{pointLength:e,distance:n}=t;return f$1.pointLength===e&&f$1.threshold<=n}();if(f$1.state=b(c,f$1.state,t.phase),c||h$3(f$1.state)){const{name:e}=f$1;u.emit2(e,t,f$1),u.emit2(e+v$1(f$1.state),t,f$1),![u$3,s$1].includes(t.phase)&&t.direction&&u.emit2(e+t.direction,t,f$1);}})),f$1} const c$1={name:"swipe",threshold:10,velocity:.3,pointLength:1};function a(a,r){const s=O(c$1,r);return a.compute([h$1,f,M],(t=>{if(s.state=0,!s.disabled&&function(){if(u$3!==t.phase)return !1;const{velocityX:o,velocityY:n,distance:i,maxPointLength:c}=t;return c===s.pointLength&&0===t.points.length&&s.threshold<i&&s.velocity<Math.max(o,n)}()){const{name:e}=s;s.state=1,a.emit2(e,t,s),a.emit2(e+t.direction,t,s);}})),s} const r$1={name:"press",pointLength:1,maxDistance:9,minPressTime:251};function c(c,u){const p=O(r$1,u);let f=0;return c.compute([h$1],(t=>{if(j(p))return;const{phase:o,startInput:r,pointLength:u}=t;if(c$3===o&&p.pointLength===u)g$1(p),clearTimeout(f),f=setTimeout((()=>{p.state=1,c.emit2(p.name,t,p);}),p.minPressTime);else if(u$3===o&&1===p.state)c.emit2(`${p.name}${r$4}`,t,p);else if(1!==p.state){const e=t.timestamp-r.timestamp;(!function(){const{distance:e}=t;return e&&p.maxDistance>e}()||p.minPressTime>e&&[u$3,s$1].includes(o))&&(clearTimeout(f),p.state=2);}})),p} const i$2={name:"pinch",threshold:0,pointLength:2};function r(r,m){const p=O(i$2,m);return r.compute([m$1,l],(t=>{if(g$1(p),j(p))return;const c=function(){const{pointLength:e,scale:n,deltaScale:o,phase:a}=t;return p.pointLength===e&&p.threshold<Math.abs(n-1)}();p.state=b(c,p.state,t.phase);const{name:h}=p;if(c||h$3(p.state)){r.emit2(h,t,p);const{deltaScale:e}=t;1!==e&&r.emit2(h+(1<e?"in":"out"),t,p);}const i=v$1(p.state);i&&r.emit2(h+i,t,p);})),p} const h={name:"rotate",threshold:0,pointLength:2};function i$1(i,m){const u=O(h,m);return i.compute([m$1,p$1],(t=>{if(j(u))return;g$1(u);const r=function(){const{pointLength:e,angle:n}=t;return u.pointLength===e&&u.threshold<Math.abs(n)}();u.state=b(r,u.state,t.phase);const{name:c}=u;(r||h$3(u.state))&&i.emit2(c,t,u);const h=v$1(u.state);h&&i.emit2(c+h,t,u);})),u} function e(e){e.use(r$2,{name:"doubletap",tapTimes:2});const a=e.get("doubletap");let o;return e.beforeEach(((t,e)=>{"tap"===t?(clearTimeout(o),o=setTimeout((()=>{[0,2].includes(a.state)&&e();}),300)):e();})),a} class i extends l$1{constructor(t,u$1){super(t,u$1),this.use(r$2),this.use(u),this.use(a),this.use(c),this.use(r),this.use(i$1);}}i.STATE_POSSIBLE=0,i.STATE_START=4,i.STATE_MOVE=5,i.STATE_END=1,i.STATE_CANCELLED=3,i.STATE_FAILED=2,i.STATE_RECOGNIZED=1,i.tap=r$2,i.pan=u,i.swipe=a,i.press=c,i.rotate=i$1,i.pinch=r,i.doubletap=e; class PopsUtils { /** * 判断是否是window,例如window、self、globalThis * @param target */ isWin(target) { if (typeof target !== "object") { return false; } if (target instanceof Node) { return false; } if (target === globalThis) { return true; } if (target === window) { return true; } if (target === self) { return true; } if (target === PopsCore.globalThis) { return true; } if (target === PopsCore.window) { return true; } if (target === PopsCore.self) { return true; } if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) { return true; } if (target?.Math?.toString() !== "[object Math]") { return false; } return true; } isDOM(target) { return target instanceof Node; } /** * 删除对象上的属性 * @param target * @param propName */ delete(target, propName) { if (typeof Reflect === "object" && Reflect.deleteProperty) { Reflect.deleteProperty(target, propName); } else { delete target[propName]; } } assign(target = {}, source = {}, isAdd = false) { let UtilsContext = this; if (source == null) { return target; } if (target == null) { target = {}; } if (Array.isArray(source)) { let canTraverse = source.filter((item) => { return typeof item === "object"; }); if (!canTraverse.length) { return source; } } if (isAdd) { for (const sourceKeyName in source) { const targetKeyName = sourceKeyName; let targetValue = target[targetKeyName]; let sourceValue = source[sourceKeyName]; if (typeof sourceValue === "object" && sourceValue != null && sourceKeyName in target && !UtilsContext.isDOM(sourceValue)) { /* 源端的值是object类型,且不是元素节点 */ target[sourceKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd); continue; } target[sourceKeyName] = sourceValue; } } else { for (const targetKeyName in target) { if (targetKeyName in source) { // @ts-ignore let targetValue = target[targetKeyName]; // @ts-ignore let sourceValue = source[targetKeyName]; if (typeof sourceValue === "object" && sourceValue != null && !UtilsContext.isDOM(sourceValue) && Object.keys(sourceValue).length) { /* 源端的值是object类型,且不是元素节点 */ // @ts-ignore target[targetKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd); continue; } /* 直接赋值 */ // @ts-ignore target[targetKeyName] = sourceValue; } } } return target; } /** * 生成uuid */ getRandomGUID() { if (typeof PopsCore.globalThis?.crypto?.randomUUID === "function") { return PopsCore.globalThis.crypto.randomUUID(); } else { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) { var randomValue = (Math.random() * 16) | 0, randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8; return randomCharValue.toString(16); }); } } /** * 字符串转HTMLElement * @param elementString * @returns */ parseTextToDOM(elementString) { /* 去除前后的换行和空格 */ elementString = elementString .replace(/^[\n|\s]*/g, "") .replace(/[\n|\s]*$/g, ""); let targetElement = popsDOMUtils.createElement("div", { innerHTML: elementString, }); return targetElement.firstChild; } contains(context, target) { if (arguments.length === 1) { // 只判断该页面是否存在该元素 return this.contains(PopsCore.document.body || PopsCore.document.documentElement, arguments[0]); } else { if (target == null) { return false; } if (typeof target[Symbol.iterator] === "function") { // 可遍历的数组 let flag = true; for (const targetNode of target) { if (!context.contains(targetNode)) { flag = false; break; } } return flag; } else { return context.contains(target); } } } formatTime(text = new Date(), formatType = "yyyy-MM-dd HH:mm:ss") { let time = text == null ? new Date() : new Date(text); /** * 校验时间补0 * @param timeNum * @returns */ function checkTime(timeNum) { if (timeNum < 10) return "0" + timeNum; return timeNum; } /** * 时间制修改 24小时制转12小时制 * @param hourNum 小时 * @returns */ function timeSystemChange(hourNum) { return hourNum > 12 ? hourNum - 12 : hourNum; } let timeRegexp = { yyyy: time.getFullYear(), /* 年 */ MM: checkTime(time.getMonth() + 1), /* 月 */ dd: checkTime(time.getDate()), /* 日 */ HH: checkTime(time.getHours()), /* 时 (24小时制) */ hh: checkTime(timeSystemChange(time.getHours())), /* 时 (12小时制) */ mm: checkTime(time.getMinutes()), /* 分 */ ss: checkTime(time.getSeconds()), /* 秒 */ }; Object.keys(timeRegexp).forEach(function (key) { let replaecRegexp = new RegExp(key, "g"); formatType = formatType.replace(replaecRegexp, timeRegexp[key]); }); return formatType; } formatByteToSize(byteSize, addType = true) { byteSize = parseInt(byteSize.toString()); if (isNaN(byteSize)) { throw new Error("Utils.formatByteToSize 参数 byteSize 格式不正确"); } let result = 0; let resultType = "KB"; let sizeData = {}; sizeData.B = 1; sizeData.KB = 1024; sizeData.MB = sizeData.KB * sizeData.KB; sizeData.GB = sizeData.MB * sizeData.KB; sizeData.TB = sizeData.GB * sizeData.KB; sizeData.PB = sizeData.TB * sizeData.KB; sizeData.EB = sizeData.PB * sizeData.KB; sizeData.ZB = sizeData.EB * sizeData.KB; sizeData.YB = sizeData.ZB * sizeData.KB; sizeData.BB = sizeData.YB * sizeData.KB; sizeData.NB = sizeData.BB * sizeData.KB; sizeData.DB = sizeData.NB * sizeData.KB; for (let key in sizeData) { result = byteSize / sizeData[key]; resultType = key; if (sizeData.KB >= result) { break; } } result = result.toFixed(2); result = addType ? result + resultType.toString() : parseFloat(result.toString()); return result; } AnyTouch = () => { return i; }; } const popsUtils = new PopsUtils(); const PopsSafeUtils = { /** * 获取安全的html */ getSafeHTML(text) { // @ts-ignore if (globalThis.trustedTypes) { // @ts-ignore const policy = globalThis.trustedTypes.createPolicy("safe-innerHTML", { createHTML: (html) => html, }); return policy.createHTML(text); } else { return text; } }, /** * 设置安全的html */ setSafeHTML($el, text) { // 创建 TrustedHTML 策略(需 CSP 允许) $el.innerHTML = this.getSafeHTML(text); }, }; class PopsDOMUtilsEvent { on(element, eventType, selector, callback, option) { /** * 获取option配置 * @param args * @param startIndex * @param option */ function getOption(args, startIndex, option) { if (typeof args[startIndex] === "boolean") { option.capture = args[startIndex]; if (typeof args[startIndex + 1] === "boolean") { option.once = args[startIndex + 1]; } if (typeof args[startIndex + 2] === "boolean") { option.passive = args[startIndex + 2]; } } else if (typeof args[startIndex] === "object" && ("capture" in args[startIndex] || "once" in args[startIndex] || "passive" in args[startIndex])) { option.capture = args[startIndex].capture; option.once = args[startIndex].once; option.passive = args[startIndex].passive; } return option; } let DOMUtilsContext = this; let args = arguments; if (typeof element === "string") { element = PopsCore.document.querySelectorAll(element); } if (element == null) { return; } let elementList = []; if (element instanceof NodeList || Array.isArray(element)) { element = element; elementList = [...element]; } else { elementList.push(element); } let eventTypeList = []; if (Array.isArray(eventType)) { eventTypeList = eventTypeList.concat(eventType); } else if (typeof eventType === "string") { eventTypeList = eventTypeList.concat(eventType.split(" ")); } let _selector_ = selector; let _callback_ = callback; let _option_ = { capture: false, once: false, passive: false, }; if (typeof selector === "function") { /* 这是为没有selector的情况 */ _selector_ = void 0; _callback_ = selector; _option_ = getOption(args, 3, _option_); } else { /* 这是存在selector的情况 */ _option_ = getOption(args, 4, _option_); } /** * 如果是once,那么删除该监听和元素上的事件和监听 */ function checkOptionOnceToRemoveEventListener() { if (_option_.once) { DOMUtilsContext.off(element, eventType, selector, callback, option); } } elementList.forEach((elementItem) => { function ownCallBack(event) { let target = event.target; if (_selector_) { /* 存在自定义子元素选择器 */ let totalParent = popsUtils.isWin(elementItem) ? PopsCore.document.documentElement : elementItem; if (target.matches(_selector_)) { /* 当前目标可以被selector所匹配到 */ _callback_.call(target, event); checkOptionOnceToRemoveEventListener(); } else if (target.closest(_selector_) && totalParent.contains(target.closest(_selector_))) { /* 在上层与主元素之间寻找可以被selector所匹配到的 */ let closestElement = target.closest(_selector_); /* event的target值不能直接修改 */ OriginPrototype.Object.defineProperty(event, "target", { get() { return closestElement; }, }); _callback_.call(closestElement, event); checkOptionOnceToRemoveEventListener(); } } else { _callback_.call(elementItem, event); checkOptionOnceToRemoveEventListener(); } } /* 遍历事件名设置元素事件 */ eventTypeList.forEach((eventName) => { elementItem.addEventListener(eventName, ownCallBack, _option_); if (_callback_ && _callback_.delegate) { elementItem.setAttribute("data-delegate", _selector_); } /* 获取对象上的事件 */ let elementEvents = elementItem[SymbolEvents] || {}; /* 初始化对象上的xx事件 */ elementEvents[eventName] = elementEvents[eventName] || []; elementEvents[eventName].push({ selector: _selector_, option: _option_, callback: ownCallBack, originCallBack: _callback_, }); /* 覆盖事件 */ elementItem[SymbolEvents] = elementEvents; }); }); } off(element, eventType, selector, callback, option, filter) { /** * 获取option配置 * @param args1 * @param startIndex * @param option */ function getOption(args1, startIndex, option) { if (typeof args1[startIndex] === "boolean") { option.capture = args1[startIndex]; } else if (typeof args1[startIndex] === "object" && "capture" in args1[startIndex]) { option.capture = args1[startIndex].capture; } return option; } let args = arguments; if (typeof element === "string") { element = PopsCore.document.querySelectorAll(element); } if (element == null) { return; } let elementList = []; if (element instanceof NodeList || Array.isArray(element)) { element = element; elementList = [...element]; } else { elementList.push(element); } let eventTypeList = []; if (Array.isArray(eventType)) { eventTypeList = eventTypeList.concat(eventType); } else if (typeof eventType === "string") { eventTypeList = eventTypeList.concat(eventType.split(" ")); } /** * 子元素选择器 */ let _selector_ = selector; /** * 事件的回调函数 */ let _callback_ = callback; /** * 事件的配置 */ let _option_ = { capture: false, }; if (typeof selector === "function") { /* 这是为没有selector的情况 */ _selector_ = void 0; _callback_ = selector; _option_ = getOption(args, 3, _option_); } else { _option_ = getOption(args, 4, _option_); } elementList.forEach((elementItem) => { /* 获取对象上的事件 */ let elementEvents = elementItem[SymbolEvents] || {}; eventTypeList.forEach((eventName) => { let handlers = elementEvents[eventName] || []; if (typeof filter === "function") { handlers = handlers.filter(filter); } for (let index = 0; index < handlers.length; index++) { let handler = handlers[index]; let flag = false; if (!_selector_ || handler.selector === _selector_) { /* selector不为空,进行selector判断 */ flag = true; } if (!_callback_ || handler.callback === _callback_ || handler.originCallBack === _callback_) { /* callback不为空,进行callback判断 */ flag = true; } if (flag) { elementItem.removeEventListener(eventName, handler.callback, _option_); handlers.splice(index--, 1); } } if (handlers.length === 0) { /* 如果没有任意的handler,那么删除该属性 */ popsUtils.delete(elementEvents, eventType); } }); elementItem[SymbolEvents] = elementEvents; }); } /** * 取消绑定所有的事件 * @param element 需要取消绑定的元素|元素数组 * @param eventType (可选)需要取消监听的事件 */ offAll(element, eventType) { if (typeof element === "string") { element = PopsCore.document.querySelectorAll(element); } if (element == null) { return; } let elementList = []; if (element instanceof NodeList || Array.isArray(element)) { elementList = [...element]; } else { elementList.push(element); } let eventTypeList = []; if (Array.isArray(eventType)) { eventTypeList = eventTypeList.concat(eventType); } else if (typeof eventType === "string") { eventTypeList = eventTypeList.concat(eventType.split(" ")); } elementList.forEach((elementItem) => { Object.getOwnPropertySymbols(elementItem).forEach((__symbolEvents) => { if (!__symbolEvents.toString().startsWith("Symbol(events_")) { return; } let elementEvents = elementItem[__symbolEvents] || {}; let iterEventNameList = eventTypeList.length ? eventTypeList : Object.keys(elementEvents); iterEventNameList.forEach((eventName) => { let handlers = elementEvents[eventName]; if (!handlers) { return; } for (const handler of handlers) { elementItem.removeEventListener(eventName, handler.callback, { capture: handler["option"]["capture"], }); } popsUtils.delete(elementItem[__symbolEvents], eventName); }); }); }); } /** * 等待文档加载完成后执行指定的函数 * @param callback 需要执行的函数 * @example * DOMUtils.ready(function(){ * console.log("文档加载完毕") * }) */ ready(callback) { if (typeof callback !== "function") { return; } /** * 检测文档是否加载完毕 */ function checkDOMReadyState() { try { if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) { return true; } else { return false; } } catch (error) { return false; } } /** * 成功加载完毕后触发的回调函数 */ function completed() { removeDomReadyListener(); callback(); } let targetList = [ { target: PopsCore.document, eventType: "DOMContentLoaded", callback: completed, }, { target: PopsCore.window, eventType: "load", callback: completed, }, ]; /** * 添加监听 */ function addDomReadyListener() { for (let index = 0; index < targetList.length; index++) { let item = targetList[index]; item.target.addEventListener(item.eventType, item.callback); } } /** * 移除监听 */ function removeDomReadyListener() { for (let index = 0; index < targetList.length; index++) { let item = targetList[index]; item.target.removeEventListener(item.eventType, item.callback); } } if (checkDOMReadyState()) { /* 检查document状态 */ setTimeout(callback); } else { /* 添加监听 */ addDomReadyListener(); } } /** * 主动触发事件 * @param element 需要触发的元素|元素数组|window * @param eventType 需要触发的事件 * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象 * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true * @example * // 触发元素a.xx的click事件 * DOMUtils.trigger(document.querySelector("a.xx"),"click") * DOMUtils.trigger("a.xx","click") * // 触发元素a.xx的click、tap、hover事件 * DOMUtils.trigger(document.querySelector("a.xx"),"click tap hover") * DOMUtils.trigger("a.xx",["click","tap","hover"]) */ trigger(element, eventType, details, useDispatchToTriggerEvent = true) { if (typeof element === "string") { element = PopsCore.document.querySelector(element); } if (element == null) { return; } let elementList = []; if (element instanceof NodeList || Array.isArray(element)) { element = element; elementList = [...element]; } else { elementList = [element]; } let eventTypeList = []; if (Array.isArray(eventType)) { eventTypeList = eventType; } else if (typeof eventType === "string") { eventTypeList = eventType.split(" "); } elementList.forEach((elementItem) => { /* 获取对象上的事件 */ let events = elementItem[SymbolEvents] || {}; eventTypeList.forEach((_eventType_) => { let event = null; if (details && details instanceof Event) { event = details; } else { event = new Event(_eventType_); if (details) { Object.keys(details).forEach((keyName) => { event[keyName] = details[keyName]; }); } } if (useDispatchToTriggerEvent == false && _eventType_ in events) { events[_eventType_].forEach((eventsItem) => { eventsItem.callback(event); }); } else { elementItem.dispatchEvent(event); } }); }); } /** * 绑定或触发元素的click事件 * @param element 目标元素 * @param handler (可选)事件处理函数 * @param details (可选)赋予触发的Event的额外属性 * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true * @example * // 触发元素a.xx的click事件 * DOMUtils.click(document.querySelector("a.xx")) * DOMUtils.click("a.xx") * DOMUtils.click("a.xx",function(){ * console.log("触发click事件成功") * }) * */ click(element, handler, details, useDispatchToTriggerEvent) { let DOMUtilsContext = this; if (typeof element === "string") { element = PopsCore.document.querySelector(element); } if (element == null) { return; } if (handler == null) { DOMUtilsContext.trigger(element, "click", details, useDispatchToTriggerEvent); } else { DOMUtilsContext.on(element, "click", null, handler); } } /** * 绑定或触发元素的blur事件 * @param element 目标元素 * @param handler (可选)事件处理函数 * @param details (可选)赋予触发的Event的额外属性 * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true * @example * // 触发元素a.xx的blur事件 * DOMUtils.blur(document.querySelector("a.xx")) * DOMUtils.blur("a.xx") * DOMUtils.blur("a.xx",function(){ * console.log("触发blur事件成功") * }) * */ blur(element, handler, details, useDispatchToTriggerEvent) { let DOMUtilsContext = this; if (typeof element === "string") { element = PopsCore.document.querySelector(element); } if (element == null) { return; } if (handler === null) { DOMUtilsContext.trigger(element, "blur", details, useDispatchToTriggerEvent); } else { DOMUtilsContext.on(element, "blur", null, handler); } } /** * 绑定或触发元素的focus事件 * @param element 目标元素 * @param handler (可选)事件处理函数 * @param details (可选)赋予触发的Event的额外属性 * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true * @example * // 触发元素a.xx的focus事件 * DOMUtils.focus(document.querySelector("a.xx")) * DOMUtils.focus("a.xx") * DOMUtils.focus("a.xx",function(){ * console.log("触发focus事件成功") * }) * */ focus(element, handler, details, useDispatchToTriggerEvent) { let DOMUtilsContext = this; if (typeof element === "string") { element = PopsCore.document.querySelector(element); } if (element == null) { return; } if (handler == null) { DOMUtilsContext.trigger(element, "focus", details, useDispatchToTriggerEvent); } else { DOMUtilsContext.on(element, "focus", null, handler); } } /** * 当鼠标移入或移出元素时触发事件 * @param element 当前元素 * @param handler 事件处理函数 * @param option 配置 * @example * // 监听a.xx元素的移入或移出 * DOMUtils.hover(document.querySelector("a.xx"),()=>{ * console.log("移入/移除"); * }) * DOMUtils.hover("a.xx",()=>{ * console.log("移入/移除"); * }) */ hover(element, handler, option) { let DOMUtilsContext = this; if (typeof element === "string") { element = PopsCore.document.querySelector(element); } if (element == null) { return; } DOMUtilsContext.on(element, "mouseenter", null, handler, option); DOMUtilsContext.on(element, "mouseleave", null, handler, option); } /** * 当按键松开时触发事件 * keydown - > keypress - > keyup * @param target 当前元素 * @param handler 事件处理函数 * @param option 配置 * @example * // 监听a.xx元素的按键松开 * DOMUtils.keyup(document.querySelector("a.xx"),()=>{ * console.log("按键松开"); * }) * DOMUtils.keyup("a.xx",()=>{ * console.log("按键松开"); * }) */ keyup(target, handler, option) { let DOMUtilsContext = this; if (target == null) { return; } if (typeof target === "string") { target = PopsCore.document.querySelector(target); } DOMUtilsContext.on(target, "keyup", null, handler, option); } /** * 当按键按下时触发事件 * keydown - > keypress - > keyup * @param target 目标 * @param handler 事件处理函数 * @param option 配置 * @example * // 监听a.xx元素的按键按下 * DOMUtils.keydown(document.querySelector("a.xx"),()=>{ * console.log("按键按下"); * }) * DOMUtils.keydown("a.xx",()=>{ * console.log("按键按下"); * }) */ keydown(target, handler, option) { let DOMUtilsContext = this; if (target == null) { return; } if (typeof target === "string") { target = PopsCore.document.querySelector(target); } DOMUtilsContext.on(target, "keydown", null, handler, option); } /** * 当按键按下时触发事件 * keydown - > keypress - > keyup * @param target 目标 * @param handler 事件处理函数 * @param option 配置 * @example * // 监听a.xx元素的按键按下 * DOMUtils.keypress(document.querySelector("a.xx"),()=>{ * console.log("按键按下"); * }) * DOMUtils.keypress("a.xx",()=>{ * console.log("按键按下"); * }) */ keypress(target, handler, option) { let DOMUtilsContext = this; if (target == null) { return; } if (typeof target === "string") { target = PopsCore.document.querySelector(target); } DOMUtilsContext.on(target, "keypress", null, handler, option); } preventEvent(element, eventNameList = [], capture) { function stopEvent(event) { /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */ event?.preventDefault(); /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */ event?.stopPropagation(); /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */ event?.stopImmediatePropagation(); return false; } if (arguments.length === 1) { /* 直接阻止事件 */ return stopEvent(arguments[0]); } else { /* 添加对应的事件来阻止触发 */ if (typeof eventNameList === "string") { eventNameList = [eventNameList]; } eventNameList.forEach((eventName) => { element.addEventListener(eventName, stopEvent, { capture: Boolean(capture), }); }); } } } class PopsDOMUtils extends PopsDOMUtilsEvent { /** 获取 animationend 在各个浏览器的兼容名 */ getAnimationEndNameList() { return [ "webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend", ]; } /** 获取 transitionend 在各个浏览器的兼容名 */ getTransitionEndNameList() { return [ "webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend", ]; } /** * 实现jQuery中的$().offset(); * @param element * @param calcScroll 计算滚动距离 */ offset(element, calcScroll = true) { let rect = element.getBoundingClientRect(); let win = element.ownerDocument.defaultView; let resultRect = new DOMRect(calcScroll ? parseFloat((rect.left + (win?.pageXOffset || 0)).toString()) : rect.left, calcScroll ? parseFloat((rect.top + (win?.pageYOffset || 0)).toString()) : rect.top, rect.width, rect.height); return resultRect; } width(element, isShow = false, parent) { let DOMUtilsContext = this; if (typeof element === "string") { element = PopsCore.document.querySelector(element); } if (element == null) { return; } if (popsUtils.isWin(element)) { return PopsCore.window.document.documentElement.clientWidth; } if (element.nodeType === 9) { /* Document文档节点 */ element = element; return Math.max(element.body.scrollWidth, element.documentElement.scrollWidth, element.body.offsetWidth, element.documentElement.offsetWidth, element.documentElement.clientWidth); } if (isShow || (!isShow && popsDOMUtils.isShow(element))) { /* 已显示 */ /* 不从style中获取对应的宽度,因为可能使用了class定义了width !important */ element = element; /* 如果element.style.width为空 则从css里面获取是否定义了width信息如果定义了 则读取css里面定义的宽度width */ if (parseFloat(popsDOMUtils.getStyleValue(element, "width").toString()) > 0) { return parseFloat(popsDOMUtils.getStyleValue(element, "width").toString()); } /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetWidth来进行计算 */ if (element.offsetWidth > 0) { let borderLeftWidth = popsDOMUtils.getStyleValue(element, "borderLeftWidth"); let borderRightWidth = popsDOMUtils.getStyleValue(element, "borderRightWidth"); let paddingLeft = popsDOMUtils.getStyleValue(element, "paddingLeft"); let paddingRight = popsDOMUtils.getStyleValue(element, "paddingRight"); let backHeight = parseFloat(element.offsetWidth.toString()) - parseFloat(borderLeftWidth.toString()) - parseFloat(borderRightWidth.toString()) - parseFloat(paddingLeft.toString()) - parseFloat(paddingRight.toString()); return parseFloat(backHeight.toString()); } return 0; } else {