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