@whitesev/pops 4.2.0 → 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.
package/dist/index.cjs.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const version = "4.2.0";
3
+ const version = "4.2.2";
4
4
 
5
5
  const GlobalConfig = {
6
6
  config: {},
@@ -244,6 +244,24 @@ const PopsIcon = {
244
244
  },
245
245
  };
246
246
 
247
+ /**
248
+ * 通用的CSS类名
249
+ */
250
+ const PopsCommonCSSClassName = {
251
+ flexCenter: "pops-flex-items-center",
252
+ flexYCenter: "pops-flex-y-center",
253
+ flexXCenter: "pops-flex-x-center",
254
+ hide: "pops-hide",
255
+ hideImportant: "pops-hide-important",
256
+ noBorder: "pops-no-border",
257
+ noBorderImportant: "pops-no-border-important",
258
+ userSelectNone: "pops-user-select-none",
259
+ lineHeightCenter: "pops-line-height-center",
260
+ widthFill: "pops-width-fill",
261
+ textIsDisabled: "pops-text-is-disabled",
262
+ textIsDisabledImportant: "pops-text-is-disabled-important",
263
+ };
264
+
247
265
  const OriginPrototype = {
248
266
  Object: {
249
267
  defineProperty: Object.defineProperty,
@@ -293,6 +311,30 @@ const PopsCore = {
293
311
  },
294
312
  };
295
313
 
314
+ const PopsSafeUtils = {
315
+ /**
316
+ * 获取安全的html
317
+ */
318
+ getSafeHTML(text) {
319
+ if (window.trustedTypes) {
320
+ const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
321
+ createHTML: (html) => html,
322
+ });
323
+ return policy.createHTML(text);
324
+ }
325
+ else {
326
+ return text;
327
+ }
328
+ },
329
+ /**
330
+ * 设置安全的html
331
+ */
332
+ setSafeHTML($el, text) {
333
+ // 创建 TrustedHTML 策略(需 CSP 允许)
334
+ $el.innerHTML = this.getSafeHTML(text);
335
+ },
336
+ };
337
+
296
338
  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}
297
339
 
298
340
  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};
@@ -762,48 +804,6 @@ class PopsUtils {
762
804
  }
763
805
  const popsUtils = new PopsUtils();
764
806
 
765
- const PopsSafeUtils = {
766
- /**
767
- * 获取安全的html
768
- */
769
- getSafeHTML(text) {
770
- if (window.trustedTypes) {
771
- const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
772
- createHTML: (html) => html,
773
- });
774
- return policy.createHTML(text);
775
- }
776
- else {
777
- return text;
778
- }
779
- },
780
- /**
781
- * 设置安全的html
782
- */
783
- setSafeHTML($el, text) {
784
- // 创建 TrustedHTML 策略(需 CSP 允许)
785
- $el.innerHTML = this.getSafeHTML(text);
786
- },
787
- };
788
-
789
- /**
790
- * 通用的CSS类名
791
- */
792
- const PopsCommonCSSClassName = {
793
- flexCenter: "pops-flex-items-center",
794
- flexYCenter: "pops-flex-y-center",
795
- flexXCenter: "pops-flex-x-center",
796
- hide: "pops-hide",
797
- hideImportant: "pops-hide-important",
798
- noBorder: "pops-no-border",
799
- noBorderImportant: "pops-no-border-important",
800
- userSelectNone: "pops-user-select-none",
801
- lineHeightCenter: "pops-line-height-center",
802
- widthFill: "pops-width-fill",
803
- textIsDisabled: "pops-text-is-disabled",
804
- textIsDisabledImportant: "pops-text-is-disabled-important",
805
- };
806
-
807
807
  /**
808
808
  * 存储在元素属性上的事件名
809
809
  */
@@ -816,7 +816,7 @@ class PopsDOMUtilsEvent {
816
816
  * @param startIndex
817
817
  * @param option
818
818
  */
819
- function getOption(args, startIndex, option) {
819
+ const getOption = function (args, startIndex, option) {
820
820
  const currentParam = args[startIndex];
821
821
  if (typeof currentParam === "boolean") {
822
822
  option.capture = currentParam;
@@ -838,7 +838,7 @@ class PopsDOMUtilsEvent {
838
838
  option.isComposedPath = currentParam.isComposedPath;
839
839
  }
840
840
  return option;
841
- }
841
+ };
842
842
  const that = this;
843
843
  // eslint-disable-next-line prefer-rest-params
844
844
  const args = arguments;
@@ -853,8 +853,7 @@ class PopsDOMUtilsEvent {
853
853
  }
854
854
  let $elList = [];
855
855
  if (element instanceof NodeList || Array.isArray(element)) {
856
- element = element;
857
- $elList = [...element];
856
+ $elList = $elList.concat(Array.from(element));
858
857
  }
859
858
  else {
860
859
  $elList.push(element);
@@ -862,15 +861,15 @@ class PopsDOMUtilsEvent {
862
861
  // 事件名
863
862
  let eventTypeList = [];
864
863
  if (Array.isArray(eventType)) {
865
- eventTypeList = eventTypeList.concat(eventType.filter((eventTypeItem) => typeof eventTypeItem === "string" && eventTypeItem.toString() !== ""));
864
+ eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
866
865
  }
867
866
  else if (typeof eventType === "string") {
868
- eventTypeList = eventTypeList.concat(eventType.split(" ").filter((eventTypeItem) => eventTypeItem !== ""));
867
+ eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
869
868
  }
870
869
  // 子元素选择器
871
870
  let selectorList = [];
872
871
  if (Array.isArray(selector)) {
873
- selectorList = selectorList.concat(selector.filter((selectorItem) => typeof selectorItem === "string" && selectorItem.toString() !== ""));
872
+ selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
874
873
  }
875
874
  else if (typeof selector === "string") {
876
875
  selectorList.push(selector);
@@ -894,83 +893,105 @@ class PopsDOMUtilsEvent {
894
893
  // 这是存在selector的情况
895
894
  listenerOption = getOption(args, 4, listenerOption);
896
895
  }
897
- /**
898
- * 如果是once,那么删除该监听和元素上的事件和监听
899
- */
900
- function checkOptionOnceToRemoveEventListener() {
901
- if (listenerOption.once) {
902
- that.off(element, eventType, selector, callback, option);
903
- }
904
- }
905
- $elList.forEach((elementItem) => {
906
- /**
907
- * 事件回调
908
- * @param event
909
- */
910
- function domUtilsEventCallBack(event) {
911
- if (selectorList.length) {
912
- // 存在子元素选择器
913
- // 这时候的this和target都是子元素选择器的元素
914
- let eventTarget = listenerOption.isComposedPath
915
- ? event.composedPath()[0]
916
- : event.target;
917
- let totalParent = elementItem;
918
- if (popsUtils.isWin(totalParent)) {
919
- if (totalParent === PopsCore.document) {
920
- totalParent = PopsCore.document.documentElement;
921
- }
922
- }
923
- const findValue = selectorList.find((selectorItem) => {
924
- // 判断目标元素是否匹配选择器
925
- if (that.matches(eventTarget, selectorItem)) {
926
- // 当前目标可以被selector所匹配到
927
- return true;
928
- }
929
- // 在上层与主元素之间寻找可以被selector所匹配到的
930
- const $closestMatches = that.closest(eventTarget, selectorItem);
931
- if ($closestMatches && totalParent?.contains($closestMatches)) {
932
- eventTarget = $closestMatches;
933
- return true;
934
- }
935
- return false;
936
- });
937
- if (findValue) {
938
- // 这里尝试使用defineProperty修改event的target值
939
- try {
940
- OriginPrototype.Object.defineProperty(event, "target", {
941
- get() {
942
- return eventTarget;
943
- },
944
- });
896
+ $elList.forEach(($elItem) => {
897
+ // 遍历事件名设置元素事件
898
+ eventTypeList.forEach((eventName) => {
899
+ /**
900
+ * 如果是option.once,那么删除该监听和元素上的事件和监听
901
+ */
902
+ const checkOptionOnceToRemoveEventListener = () => {
903
+ if (listenerOption.once) {
904
+ this.off($elItem, eventName, selector, callback, option);
905
+ }
906
+ };
907
+ /**
908
+ * 事件回调
909
+ * @param event
910
+ */
911
+ const handlerCallBack = function (event) {
912
+ let call_this = void 0;
913
+ let call_event = void 0;
914
+ let call_$selector = void 0;
915
+ let execCallback = false;
916
+ if (selectorList.length) {
917
+ // 存在子元素选择器
918
+ // 这时候的this和target都是子元素选择器的元素
919
+ let $target;
920
+ if (listenerOption.isComposedPath) {
921
+ // 可能为空
922
+ const composedPath = event.composedPath();
923
+ if (!composedPath.length && event.target) {
924
+ composedPath.push(event.target);
925
+ }
926
+ $target = composedPath[0];
945
927
  }
946
- catch {
947
- // 忽略
928
+ else {
929
+ $target = event.target;
930
+ }
931
+ let $parent = $elItem;
932
+ if (popsUtils.isWin($parent)) {
933
+ // window和document共用一个对象
934
+ // 这样就能处理子元素选择器无法匹配的问题
935
+ $parent = PopsCore.document.documentElement;
936
+ }
937
+ const findValue = selectorList.find((selectors) => {
938
+ // 判断目标元素是否匹配选择器
939
+ if (that.matches($target, selectors)) {
940
+ // 当前目标可以被selector所匹配到
941
+ return true;
942
+ }
943
+ // 在上层与主元素之间寻找可以被selector所匹配到的
944
+ const $closestMatches = that.closest($target, selectors);
945
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
946
+ $target = $closestMatches;
947
+ return true;
948
+ }
949
+ return false;
950
+ });
951
+ if (findValue) {
952
+ // 这里尝试使用defineProperty修改event的target值
953
+ try {
954
+ OriginPrototype.Object.defineProperty(event, "target", {
955
+ get() {
956
+ return $target;
957
+ },
958
+ });
959
+ // oxlint-disable-next-line no-empty
960
+ }
961
+ catch { }
962
+ execCallback = true;
963
+ call_this = $target;
964
+ call_event = event;
965
+ call_$selector = $target;
948
966
  }
949
- listenerCallBack.call(eventTarget, event, eventTarget);
967
+ }
968
+ else {
969
+ execCallback = true;
970
+ call_this = $elItem;
971
+ call_event = event;
972
+ }
973
+ if (execCallback) {
974
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
950
975
  checkOptionOnceToRemoveEventListener();
976
+ if (typeof result === "boolean" && !result) {
977
+ return false;
978
+ }
951
979
  }
952
- }
953
- else {
954
- // 这时候的this指向监听的元素
955
- listenerCallBack.call(elementItem, event);
956
- checkOptionOnceToRemoveEventListener();
957
- }
958
- }
959
- // 遍历事件名设置元素事件
960
- eventTypeList.forEach((eventName) => {
961
- elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
980
+ };
981
+ // add listener
982
+ $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
962
983
  // 获取对象上的事件
963
- const elementEvents = Reflect.get(elementItem, SymbolEvents) || {};
984
+ const elementEvents = Reflect.get($elItem, SymbolEvents) || {};
964
985
  // 初始化对象上的xx事件
965
986
  elementEvents[eventName] = elementEvents[eventName] || [];
966
987
  elementEvents[eventName].push({
967
988
  selector: selectorList,
968
989
  option: listenerOption,
969
- callback: domUtilsEventCallBack,
970
- originCallBack: listenerCallBack,
990
+ handlerCallBack: handlerCallBack,
991
+ callback: listenerCallBack,
971
992
  });
972
993
  // 覆盖事件
973
- Reflect.set(elementItem, SymbolEvents, elementEvents);
994
+ Reflect.set($elItem, SymbolEvents, elementEvents);
974
995
  });
975
996
  });
976
997
  return {
@@ -983,11 +1004,11 @@ class PopsDOMUtilsEvent {
983
1004
  },
984
1005
  /**
985
1006
  * 主动触发事件
986
- * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
987
- * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
1007
+ * @param extraDetails 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1008
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了`$selector`的没有值
988
1009
  */
989
- emit: (details, useDispatchToEmit) => {
990
- that.emit($elList, eventTypeList, details, useDispatchToEmit);
1010
+ emit: (extraDetails, useDispatchToTriggerEvent) => {
1011
+ that.emit($elList, eventTypeList, extraDetails, useDispatchToTriggerEvent);
991
1012
  },
992
1013
  };
993
1014
  }
@@ -998,7 +1019,7 @@ class PopsDOMUtilsEvent {
998
1019
  * @param startIndex
999
1020
  * @param option
1000
1021
  */
1001
- function getOption(args1, startIndex, option) {
1022
+ const getOption = function (args1, startIndex, option) {
1002
1023
  const currentParam = args1[startIndex];
1003
1024
  if (typeof currentParam === "boolean") {
1004
1025
  option.capture = currentParam;
@@ -1007,12 +1028,12 @@ class PopsDOMUtilsEvent {
1007
1028
  option.capture = currentParam.capture;
1008
1029
  }
1009
1030
  return option;
1010
- }
1011
- const DOMUtilsContext = this;
1031
+ };
1032
+ const that = this;
1012
1033
  // eslint-disable-next-line prefer-rest-params
1013
1034
  const args = arguments;
1014
1035
  if (typeof element === "string") {
1015
- element = DOMUtilsContext.selectorAll(element);
1036
+ element = that.selectorAll(element);
1016
1037
  }
1017
1038
  if (element == null) {
1018
1039
  return;
@@ -1020,22 +1041,22 @@ class PopsDOMUtilsEvent {
1020
1041
  let $elList = [];
1021
1042
  if (element instanceof NodeList || Array.isArray(element)) {
1022
1043
  element = element;
1023
- $elList = $elList.concat(element);
1044
+ $elList = $elList.concat(Array.from(element));
1024
1045
  }
1025
1046
  else {
1026
1047
  $elList.push(element);
1027
1048
  }
1028
1049
  let eventTypeList = [];
1029
1050
  if (Array.isArray(eventType)) {
1030
- eventTypeList = eventTypeList.concat(eventType.filter((eventTypeItem) => typeof eventTypeItem === "string" && eventTypeItem.toString() !== ""));
1051
+ eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
1031
1052
  }
1032
1053
  else if (typeof eventType === "string") {
1033
- eventTypeList = eventTypeList.concat(eventType.split(" ").filter((eventTypeItem) => eventTypeItem !== ""));
1054
+ eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
1034
1055
  }
1035
1056
  // 子元素选择器
1036
1057
  let selectorList = [];
1037
1058
  if (Array.isArray(selector)) {
1038
- selectorList = selectorList.concat(selector.filter((selectorItem) => typeof selectorItem === "string" && selectorItem.toString() !== ""));
1059
+ selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
1039
1060
  }
1040
1061
  else if (typeof selector === "string") {
1041
1062
  selectorList.push(selector);
@@ -1064,36 +1085,39 @@ class PopsDOMUtilsEvent {
1064
1085
  // 目标函数、事件名、回调函数、事件配置、过滤函数
1065
1086
  filter = option;
1066
1087
  }
1067
- $elList.forEach((elementItem) => {
1088
+ $elList.forEach(($elItem) => {
1068
1089
  // 获取对象上的事件
1069
- const elementEvents = Reflect.get(elementItem, SymbolEvents) || {};
1090
+ const elementEvents = Reflect.get($elItem, SymbolEvents) || {};
1070
1091
  eventTypeList.forEach((eventName) => {
1071
1092
  const handlers = elementEvents[eventName] || [];
1072
- const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
1073
- for (let index = 0; index < filterHandler.length; index++) {
1074
- const handler = filterHandler[index];
1093
+ // 过滤出需要删除的事件
1094
+ const handlersFiltered = typeof filter === "function" ? handlers.filter(filter) : handlers;
1095
+ for (let index = 0; index < handlersFiltered.length; index++) {
1096
+ const handler = handlersFiltered[index];
1097
+ // 过滤出的事件再根据下面的条件进行判断处理移除
1098
+ // 1. callback内存地址必须相同
1099
+ // 2. selector必须相同
1100
+ // 3. option.capture必须相同
1075
1101
  let flag = true;
1076
- if (flag && listenerCallBack && handler.originCallBack !== listenerCallBack) {
1077
- // callback不同
1102
+ if (flag && listenerCallBack && handler.callback !== listenerCallBack) {
1078
1103
  flag = false;
1079
1104
  }
1080
1105
  if (flag && selectorList.length && Array.isArray(handler.selector)) {
1081
1106
  if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
1082
- // 子元素选择器不同
1083
1107
  flag = false;
1084
1108
  }
1085
1109
  }
1086
1110
  if (flag &&
1087
1111
  typeof handler.option.capture === "boolean" &&
1088
1112
  listenerOption.capture !== handler.option.capture) {
1089
- // 事件的配置项不同
1090
1113
  flag = false;
1091
1114
  }
1092
1115
  if (flag) {
1093
- elementItem.removeEventListener(eventName, handler.callback, handler.option);
1094
- const findIndex = handlers.findIndex((item) => item === handler);
1095
- if (findIndex !== -1) {
1096
- handlers.splice(findIndex, 1);
1116
+ $elItem.removeEventListener(eventName, handler.handlerCallBack, handler.option);
1117
+ for (let i = handlers.length - 1; i >= 0; i--) {
1118
+ if (handlers[i] === handler) {
1119
+ handlers.splice(i, 1);
1120
+ }
1097
1121
  }
1098
1122
  }
1099
1123
  }
@@ -1102,7 +1126,7 @@ class PopsDOMUtilsEvent {
1102
1126
  popsUtils.delete(elementEvents, eventType);
1103
1127
  }
1104
1128
  });
1105
- Reflect.set(elementItem, SymbolEvents, elementEvents);
1129
+ Reflect.set($elItem, SymbolEvents, elementEvents);
1106
1130
  });
1107
1131
  }
1108
1132
  /**
@@ -1486,11 +1510,11 @@ class PopsDOMUtilsEvent {
1486
1510
  * 阻止事件的默认行为发生,并阻止事件传播
1487
1511
  */
1488
1512
  const stopEvent = (event, onlyStopPropagation) => {
1513
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1514
+ event?.stopPropagation();
1515
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1516
+ event?.stopImmediatePropagation();
1489
1517
  if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
1490
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1491
- event?.stopPropagation();
1492
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1493
- event?.stopImmediatePropagation();
1494
1518
  return;
1495
1519
  }
1496
1520
  // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字