@whitesev/pops 4.2.0 → 4.2.1

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.umd.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.pops = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const version = "4.2.0";
7
+ const version = "4.2.1";
8
8
 
9
9
  const GlobalConfig = {
10
10
  config: {},
@@ -820,7 +820,7 @@
820
820
  * @param startIndex
821
821
  * @param option
822
822
  */
823
- function getOption(args, startIndex, option) {
823
+ const getOption = function (args, startIndex, option) {
824
824
  const currentParam = args[startIndex];
825
825
  if (typeof currentParam === "boolean") {
826
826
  option.capture = currentParam;
@@ -842,7 +842,7 @@
842
842
  option.isComposedPath = currentParam.isComposedPath;
843
843
  }
844
844
  return option;
845
- }
845
+ };
846
846
  const that = this;
847
847
  // eslint-disable-next-line prefer-rest-params
848
848
  const args = arguments;
@@ -857,8 +857,7 @@
857
857
  }
858
858
  let $elList = [];
859
859
  if (element instanceof NodeList || Array.isArray(element)) {
860
- element = element;
861
- $elList = [...element];
860
+ $elList = $elList.concat(Array.from(element));
862
861
  }
863
862
  else {
864
863
  $elList.push(element);
@@ -866,15 +865,15 @@
866
865
  // 事件名
867
866
  let eventTypeList = [];
868
867
  if (Array.isArray(eventType)) {
869
- eventTypeList = eventTypeList.concat(eventType.filter((eventTypeItem) => typeof eventTypeItem === "string" && eventTypeItem.toString() !== ""));
868
+ eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
870
869
  }
871
870
  else if (typeof eventType === "string") {
872
- eventTypeList = eventTypeList.concat(eventType.split(" ").filter((eventTypeItem) => eventTypeItem !== ""));
871
+ eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
873
872
  }
874
873
  // 子元素选择器
875
874
  let selectorList = [];
876
875
  if (Array.isArray(selector)) {
877
- selectorList = selectorList.concat(selector.filter((selectorItem) => typeof selectorItem === "string" && selectorItem.toString() !== ""));
876
+ selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
878
877
  }
879
878
  else if (typeof selector === "string") {
880
879
  selectorList.push(selector);
@@ -901,39 +900,52 @@
901
900
  /**
902
901
  * 如果是once,那么删除该监听和元素上的事件和监听
903
902
  */
904
- function checkOptionOnceToRemoveEventListener() {
903
+ const checkOptionOnceToRemoveEventListener = ($el) => {
905
904
  if (listenerOption.once) {
906
- that.off(element, eventType, selector, callback, option);
905
+ this.off($el, eventTypeList, selector, callback, option);
907
906
  }
908
- }
909
- $elList.forEach((elementItem) => {
907
+ };
908
+ $elList.forEach(($elItem) => {
910
909
  /**
911
910
  * 事件回调
912
911
  * @param event
913
912
  */
914
- function domUtilsEventCallBack(event) {
913
+ const handlerCallBack = function (event) {
914
+ let call_this = void 0;
915
+ let call_event = void 0;
916
+ let call_$selector = void 0;
917
+ let execCallback = false;
915
918
  if (selectorList.length) {
916
919
  // 存在子元素选择器
917
920
  // 这时候的this和target都是子元素选择器的元素
918
- let eventTarget = listenerOption.isComposedPath
919
- ? event.composedPath()[0]
920
- : event.target;
921
- let totalParent = elementItem;
922
- if (popsUtils.isWin(totalParent)) {
923
- if (totalParent === PopsCore.document) {
924
- totalParent = PopsCore.document.documentElement;
921
+ let $target;
922
+ if (listenerOption.isComposedPath) {
923
+ // 可能为空
924
+ const composedPath = event.composedPath();
925
+ if (!composedPath.length && event.target) {
926
+ composedPath.push(event.target);
925
927
  }
928
+ $target = composedPath[0];
929
+ }
930
+ else {
931
+ $target = event.target;
926
932
  }
927
- const findValue = selectorList.find((selectorItem) => {
933
+ let $parent = $elItem;
934
+ if (popsUtils.isWin($parent)) {
935
+ // window和document共用一个对象
936
+ // 这样就能处理子元素选择器无法匹配的问题
937
+ $parent = PopsCore.document.documentElement;
938
+ }
939
+ const findValue = selectorList.find((selectors) => {
928
940
  // 判断目标元素是否匹配选择器
929
- if (that.matches(eventTarget, selectorItem)) {
941
+ if (that.matches($target, selectors)) {
930
942
  // 当前目标可以被selector所匹配到
931
943
  return true;
932
944
  }
933
945
  // 在上层与主元素之间寻找可以被selector所匹配到的
934
- const $closestMatches = that.closest(eventTarget, selectorItem);
935
- if ($closestMatches && totalParent?.contains($closestMatches)) {
936
- eventTarget = $closestMatches;
946
+ const $closestMatches = that.closest($target, selectors);
947
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
948
+ $target = $closestMatches;
937
949
  return true;
938
950
  }
939
951
  return false;
@@ -943,38 +955,47 @@
943
955
  try {
944
956
  OriginPrototype.Object.defineProperty(event, "target", {
945
957
  get() {
946
- return eventTarget;
958
+ return $target;
947
959
  },
948
960
  });
961
+ // oxlint-disable-next-line no-empty
949
962
  }
950
- catch {
951
- // 忽略
952
- }
953
- listenerCallBack.call(eventTarget, event, eventTarget);
954
- checkOptionOnceToRemoveEventListener();
963
+ catch { }
964
+ execCallback = true;
965
+ call_this = $target;
966
+ call_event = event;
967
+ call_$selector = $target;
955
968
  }
956
969
  }
957
970
  else {
958
- // 这时候的this指向监听的元素
959
- listenerCallBack.call(elementItem, event);
960
- checkOptionOnceToRemoveEventListener();
971
+ execCallback = true;
972
+ call_this = $elItem;
973
+ call_event = event;
974
+ }
975
+ if (execCallback) {
976
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
977
+ checkOptionOnceToRemoveEventListener($elItem);
978
+ if (typeof result === "boolean" && !result) {
979
+ return false;
980
+ }
961
981
  }
962
- }
982
+ };
963
983
  // 遍历事件名设置元素事件
964
984
  eventTypeList.forEach((eventName) => {
965
- elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
985
+ // add listener
986
+ $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
966
987
  // 获取对象上的事件
967
- const elementEvents = Reflect.get(elementItem, SymbolEvents) || {};
988
+ const elementEvents = Reflect.get($elItem, SymbolEvents) || {};
968
989
  // 初始化对象上的xx事件
969
990
  elementEvents[eventName] = elementEvents[eventName] || [];
970
991
  elementEvents[eventName].push({
971
992
  selector: selectorList,
972
993
  option: listenerOption,
973
- callback: domUtilsEventCallBack,
974
- originCallBack: listenerCallBack,
994
+ handlerCallBack: handlerCallBack,
995
+ callback: listenerCallBack,
975
996
  });
976
997
  // 覆盖事件
977
- Reflect.set(elementItem, SymbolEvents, elementEvents);
998
+ Reflect.set($elItem, SymbolEvents, elementEvents);
978
999
  });
979
1000
  });
980
1001
  return {
@@ -987,11 +1008,11 @@
987
1008
  },
988
1009
  /**
989
1010
  * 主动触发事件
990
- * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
991
- * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
1011
+ * @param extraDetails 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1012
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了`$selector`的没有值
992
1013
  */
993
- emit: (details, useDispatchToEmit) => {
994
- that.emit($elList, eventTypeList, details, useDispatchToEmit);
1014
+ emit: (extraDetails, useDispatchToTriggerEvent) => {
1015
+ that.emit($elList, eventTypeList, extraDetails, useDispatchToTriggerEvent);
995
1016
  },
996
1017
  };
997
1018
  }
@@ -1002,7 +1023,7 @@
1002
1023
  * @param startIndex
1003
1024
  * @param option
1004
1025
  */
1005
- function getOption(args1, startIndex, option) {
1026
+ const getOption = function (args1, startIndex, option) {
1006
1027
  const currentParam = args1[startIndex];
1007
1028
  if (typeof currentParam === "boolean") {
1008
1029
  option.capture = currentParam;
@@ -1011,12 +1032,12 @@
1011
1032
  option.capture = currentParam.capture;
1012
1033
  }
1013
1034
  return option;
1014
- }
1015
- const DOMUtilsContext = this;
1035
+ };
1036
+ const that = this;
1016
1037
  // eslint-disable-next-line prefer-rest-params
1017
1038
  const args = arguments;
1018
1039
  if (typeof element === "string") {
1019
- element = DOMUtilsContext.selectorAll(element);
1040
+ element = that.selectorAll(element);
1020
1041
  }
1021
1042
  if (element == null) {
1022
1043
  return;
@@ -1024,22 +1045,22 @@
1024
1045
  let $elList = [];
1025
1046
  if (element instanceof NodeList || Array.isArray(element)) {
1026
1047
  element = element;
1027
- $elList = $elList.concat(element);
1048
+ $elList = $elList.concat(Array.from(element));
1028
1049
  }
1029
1050
  else {
1030
1051
  $elList.push(element);
1031
1052
  }
1032
1053
  let eventTypeList = [];
1033
1054
  if (Array.isArray(eventType)) {
1034
- eventTypeList = eventTypeList.concat(eventType.filter((eventTypeItem) => typeof eventTypeItem === "string" && eventTypeItem.toString() !== ""));
1055
+ eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
1035
1056
  }
1036
1057
  else if (typeof eventType === "string") {
1037
- eventTypeList = eventTypeList.concat(eventType.split(" ").filter((eventTypeItem) => eventTypeItem !== ""));
1058
+ eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
1038
1059
  }
1039
1060
  // 子元素选择器
1040
1061
  let selectorList = [];
1041
1062
  if (Array.isArray(selector)) {
1042
- selectorList = selectorList.concat(selector.filter((selectorItem) => typeof selectorItem === "string" && selectorItem.toString() !== ""));
1063
+ selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
1043
1064
  }
1044
1065
  else if (typeof selector === "string") {
1045
1066
  selectorList.push(selector);
@@ -1068,16 +1089,16 @@
1068
1089
  // 目标函数、事件名、回调函数、事件配置、过滤函数
1069
1090
  filter = option;
1070
1091
  }
1071
- $elList.forEach((elementItem) => {
1092
+ $elList.forEach(($elItem) => {
1072
1093
  // 获取对象上的事件
1073
- const elementEvents = Reflect.get(elementItem, SymbolEvents) || {};
1094
+ const elementEvents = Reflect.get($elItem, SymbolEvents) || {};
1074
1095
  eventTypeList.forEach((eventName) => {
1075
1096
  const handlers = elementEvents[eventName] || [];
1076
1097
  const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
1077
1098
  for (let index = 0; index < filterHandler.length; index++) {
1078
1099
  const handler = filterHandler[index];
1079
1100
  let flag = true;
1080
- if (flag && listenerCallBack && handler.originCallBack !== listenerCallBack) {
1101
+ if (flag && listenerCallBack && handler.callback !== listenerCallBack) {
1081
1102
  // callback不同
1082
1103
  flag = false;
1083
1104
  }
@@ -1094,7 +1115,7 @@
1094
1115
  flag = false;
1095
1116
  }
1096
1117
  if (flag) {
1097
- elementItem.removeEventListener(eventName, handler.callback, handler.option);
1118
+ $elItem.removeEventListener(eventName, handler.handlerCallBack, handler.option);
1098
1119
  const findIndex = handlers.findIndex((item) => item === handler);
1099
1120
  if (findIndex !== -1) {
1100
1121
  handlers.splice(findIndex, 1);
@@ -1106,7 +1127,7 @@
1106
1127
  popsUtils.delete(elementEvents, eventType);
1107
1128
  }
1108
1129
  });
1109
- Reflect.set(elementItem, SymbolEvents, elementEvents);
1130
+ Reflect.set($elItem, SymbolEvents, elementEvents);
1110
1131
  });
1111
1132
  }
1112
1133
  /**
@@ -1490,11 +1511,11 @@
1490
1511
  * 阻止事件的默认行为发生,并阻止事件传播
1491
1512
  */
1492
1513
  const stopEvent = (event, onlyStopPropagation) => {
1514
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1515
+ event?.stopPropagation();
1516
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1517
+ event?.stopImmediatePropagation();
1493
1518
  if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
1494
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1495
- event?.stopPropagation();
1496
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1497
- event?.stopImmediatePropagation();
1498
1519
  return;
1499
1520
  }
1500
1521
  // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字