@whitesev/pops 4.2.1 → 4.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  var pops = (function () {
2
2
  'use strict';
3
3
 
4
- const version = "4.2.1";
4
+ const version = "4.2.2";
5
5
 
6
6
  const GlobalConfig = {
7
7
  config: {},
@@ -245,6 +245,24 @@ var pops = (function () {
245
245
  },
246
246
  };
247
247
 
248
+ /**
249
+ * 通用的CSS类名
250
+ */
251
+ const PopsCommonCSSClassName = {
252
+ flexCenter: "pops-flex-items-center",
253
+ flexYCenter: "pops-flex-y-center",
254
+ flexXCenter: "pops-flex-x-center",
255
+ hide: "pops-hide",
256
+ hideImportant: "pops-hide-important",
257
+ noBorder: "pops-no-border",
258
+ noBorderImportant: "pops-no-border-important",
259
+ userSelectNone: "pops-user-select-none",
260
+ lineHeightCenter: "pops-line-height-center",
261
+ widthFill: "pops-width-fill",
262
+ textIsDisabled: "pops-text-is-disabled",
263
+ textIsDisabledImportant: "pops-text-is-disabled-important",
264
+ };
265
+
248
266
  const OriginPrototype = {
249
267
  Object: {
250
268
  defineProperty: Object.defineProperty,
@@ -294,6 +312,30 @@ var pops = (function () {
294
312
  },
295
313
  };
296
314
 
315
+ const PopsSafeUtils = {
316
+ /**
317
+ * 获取安全的html
318
+ */
319
+ getSafeHTML(text) {
320
+ if (window.trustedTypes) {
321
+ const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
322
+ createHTML: (html) => html,
323
+ });
324
+ return policy.createHTML(text);
325
+ }
326
+ else {
327
+ return text;
328
+ }
329
+ },
330
+ /**
331
+ * 设置安全的html
332
+ */
333
+ setSafeHTML($el, text) {
334
+ // 创建 TrustedHTML 策略(需 CSP 允许)
335
+ $el.innerHTML = this.getSafeHTML(text);
336
+ },
337
+ };
338
+
297
339
  const n$1="clientX",e$2="clientY",t$1=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,true}function O(n,e){return Object.assign(Object.assign(Object.assign({},n),e),{state:0,disabled:false})}function p$3(n){return Math.round(100*n)/100}
298
340
 
299
341
  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$2=(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};
@@ -763,48 +805,6 @@ var pops = (function () {
763
805
  }
764
806
  const popsUtils = new PopsUtils();
765
807
 
766
- const PopsSafeUtils = {
767
- /**
768
- * 获取安全的html
769
- */
770
- getSafeHTML(text) {
771
- if (window.trustedTypes) {
772
- const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
773
- createHTML: (html) => html,
774
- });
775
- return policy.createHTML(text);
776
- }
777
- else {
778
- return text;
779
- }
780
- },
781
- /**
782
- * 设置安全的html
783
- */
784
- setSafeHTML($el, text) {
785
- // 创建 TrustedHTML 策略(需 CSP 允许)
786
- $el.innerHTML = this.getSafeHTML(text);
787
- },
788
- };
789
-
790
- /**
791
- * 通用的CSS类名
792
- */
793
- const PopsCommonCSSClassName = {
794
- flexCenter: "pops-flex-items-center",
795
- flexYCenter: "pops-flex-y-center",
796
- flexXCenter: "pops-flex-x-center",
797
- hide: "pops-hide",
798
- hideImportant: "pops-hide-important",
799
- noBorder: "pops-no-border",
800
- noBorderImportant: "pops-no-border-important",
801
- userSelectNone: "pops-user-select-none",
802
- lineHeightCenter: "pops-line-height-center",
803
- widthFill: "pops-width-fill",
804
- textIsDisabled: "pops-text-is-disabled",
805
- textIsDisabledImportant: "pops-text-is-disabled-important",
806
- };
807
-
808
808
  /**
809
809
  * 存储在元素属性上的事件名
810
810
  */
@@ -894,91 +894,91 @@ var pops = (function () {
894
894
  // 这是存在selector的情况
895
895
  listenerOption = getOption(args, 4, listenerOption);
896
896
  }
897
- /**
898
- * 如果是once,那么删除该监听和元素上的事件和监听
899
- */
900
- const checkOptionOnceToRemoveEventListener = ($el) => {
901
- if (listenerOption.once) {
902
- this.off($el, eventTypeList, selector, callback, option);
903
- }
904
- };
905
897
  $elList.forEach(($elItem) => {
906
- /**
907
- * 事件回调
908
- * @param event
909
- */
910
- const handlerCallBack = function (event) {
911
- let call_this = void 0;
912
- let call_event = void 0;
913
- let call_$selector = void 0;
914
- let execCallback = false;
915
- if (selectorList.length) {
916
- // 存在子元素选择器
917
- // 这时候的this和target都是子元素选择器的元素
918
- let $target;
919
- if (listenerOption.isComposedPath) {
920
- // 可能为空
921
- const composedPath = event.composedPath();
922
- if (!composedPath.length && event.target) {
923
- composedPath.push(event.target);
924
- }
925
- $target = composedPath[0];
898
+ // 遍历事件名设置元素事件
899
+ eventTypeList.forEach((eventName) => {
900
+ /**
901
+ * 如果是option.once,那么删除该监听和元素上的事件和监听
902
+ */
903
+ const checkOptionOnceToRemoveEventListener = () => {
904
+ if (listenerOption.once) {
905
+ this.off($elItem, eventName, selector, callback, option);
926
906
  }
927
- else {
928
- $target = event.target;
929
- }
930
- let $parent = $elItem;
931
- if (popsUtils.isWin($parent)) {
932
- // window和document共用一个对象
933
- // 这样就能处理子元素选择器无法匹配的问题
934
- $parent = PopsCore.document.documentElement;
935
- }
936
- const findValue = selectorList.find((selectors) => {
937
- // 判断目标元素是否匹配选择器
938
- if (that.matches($target, selectors)) {
939
- // 当前目标可以被selector所匹配到
940
- return true;
907
+ };
908
+ /**
909
+ * 事件回调
910
+ * @param event
911
+ */
912
+ const handlerCallBack = function (event) {
913
+ let call_this = void 0;
914
+ let call_event = void 0;
915
+ let call_$selector = void 0;
916
+ let execCallback = false;
917
+ if (selectorList.length) {
918
+ // 存在子元素选择器
919
+ // 这时候的this和target都是子元素选择器的元素
920
+ let $target;
921
+ if (listenerOption.isComposedPath) {
922
+ // 可能为空
923
+ const composedPath = event.composedPath();
924
+ if (!composedPath.length && event.target) {
925
+ composedPath.push(event.target);
926
+ }
927
+ $target = composedPath[0];
941
928
  }
942
- // 在上层与主元素之间寻找可以被selector所匹配到的
943
- const $closestMatches = that.closest($target, selectors);
944
- if ($closestMatches && $parent?.contains?.($closestMatches)) {
945
- $target = $closestMatches;
946
- return true;
929
+ else {
930
+ $target = event.target;
947
931
  }
948
- return false;
949
- });
950
- if (findValue) {
951
- // 这里尝试使用defineProperty修改event的target值
952
- try {
953
- OriginPrototype.Object.defineProperty(event, "target", {
954
- get() {
955
- return $target;
956
- },
957
- });
958
- // oxlint-disable-next-line no-empty
932
+ let $parent = $elItem;
933
+ if (popsUtils.isWin($parent)) {
934
+ // window和document共用一个对象
935
+ // 这样就能处理子元素选择器无法匹配的问题
936
+ $parent = PopsCore.document.documentElement;
959
937
  }
960
- catch { }
938
+ const findValue = selectorList.find((selectors) => {
939
+ // 判断目标元素是否匹配选择器
940
+ if (that.matches($target, selectors)) {
941
+ // 当前目标可以被selector所匹配到
942
+ return true;
943
+ }
944
+ // 在上层与主元素之间寻找可以被selector所匹配到的
945
+ const $closestMatches = that.closest($target, selectors);
946
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
947
+ $target = $closestMatches;
948
+ return true;
949
+ }
950
+ return false;
951
+ });
952
+ if (findValue) {
953
+ // 这里尝试使用defineProperty修改event的target值
954
+ try {
955
+ OriginPrototype.Object.defineProperty(event, "target", {
956
+ get() {
957
+ return $target;
958
+ },
959
+ });
960
+ // oxlint-disable-next-line no-empty
961
+ }
962
+ catch { }
963
+ execCallback = true;
964
+ call_this = $target;
965
+ call_event = event;
966
+ call_$selector = $target;
967
+ }
968
+ }
969
+ else {
961
970
  execCallback = true;
962
- call_this = $target;
971
+ call_this = $elItem;
963
972
  call_event = event;
964
- call_$selector = $target;
965
973
  }
966
- }
967
- else {
968
- execCallback = true;
969
- call_this = $elItem;
970
- call_event = event;
971
- }
972
- if (execCallback) {
973
- const result = listenerCallBack.call(call_this, call_event, call_$selector);
974
- checkOptionOnceToRemoveEventListener($elItem);
975
- if (typeof result === "boolean" && !result) {
976
- return false;
974
+ if (execCallback) {
975
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
976
+ checkOptionOnceToRemoveEventListener();
977
+ if (typeof result === "boolean" && !result) {
978
+ return false;
979
+ }
977
980
  }
978
- }
979
- };
980
- // 遍历事件名设置元素事件
981
- eventTypeList.forEach((eventName) => {
981
+ };
982
982
  // add listener
983
983
  $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
984
984
  // 获取对象上的事件
@@ -1091,31 +1091,34 @@ var pops = (function () {
1091
1091
  const elementEvents = Reflect.get($elItem, SymbolEvents) || {};
1092
1092
  eventTypeList.forEach((eventName) => {
1093
1093
  const handlers = elementEvents[eventName] || [];
1094
- const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
1095
- for (let index = 0; index < filterHandler.length; index++) {
1096
- const handler = filterHandler[index];
1094
+ // 过滤出需要删除的事件
1095
+ const handlersFiltered = typeof filter === "function" ? handlers.filter(filter) : handlers;
1096
+ for (let index = 0; index < handlersFiltered.length; index++) {
1097
+ const handler = handlersFiltered[index];
1098
+ // 过滤出的事件再根据下面的条件进行判断处理移除
1099
+ // 1. callback内存地址必须相同
1100
+ // 2. selector必须相同
1101
+ // 3. option.capture必须相同
1097
1102
  let flag = true;
1098
1103
  if (flag && listenerCallBack && handler.callback !== listenerCallBack) {
1099
- // callback不同
1100
1104
  flag = false;
1101
1105
  }
1102
1106
  if (flag && selectorList.length && Array.isArray(handler.selector)) {
1103
1107
  if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
1104
- // 子元素选择器不同
1105
1108
  flag = false;
1106
1109
  }
1107
1110
  }
1108
1111
  if (flag &&
1109
1112
  typeof handler.option.capture === "boolean" &&
1110
1113
  listenerOption.capture !== handler.option.capture) {
1111
- // 事件的配置项不同
1112
1114
  flag = false;
1113
1115
  }
1114
1116
  if (flag) {
1115
1117
  $elItem.removeEventListener(eventName, handler.handlerCallBack, handler.option);
1116
- const findIndex = handlers.findIndex((item) => item === handler);
1117
- if (findIndex !== -1) {
1118
- handlers.splice(findIndex, 1);
1118
+ for (let i = handlers.length - 1; i >= 0; i--) {
1119
+ if (handlers[i] === handler) {
1120
+ handlers.splice(i, 1);
1121
+ }
1119
1122
  }
1120
1123
  }
1121
1124
  }