@whitesev/pops 3.3.2 → 3.3.3

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
@@ -297,6 +297,13 @@
297
297
  isDOM(target) {
298
298
  return target instanceof Node;
299
299
  }
300
+ /**
301
+ * 判断是否是元素列表
302
+ * @param $ele
303
+ */
304
+ isNodeList($ele) {
305
+ return Array.isArray($ele) || $ele instanceof NodeList;
306
+ }
300
307
  /**
301
308
  * 删除对象上的属性
302
309
  * @param target
@@ -1029,7 +1036,7 @@
1029
1036
  */
1030
1037
  emit(element, eventType, details, useDispatchToEmitEvent = true) {
1031
1038
  if (typeof element === "string") {
1032
- element = PopsCore.document.querySelector(element);
1039
+ element = this.selector(element);
1033
1040
  }
1034
1041
  if (element == null) {
1035
1042
  return;
@@ -1091,18 +1098,18 @@
1091
1098
  * })
1092
1099
  * */
1093
1100
  click(element, handler, details, useDispatchToEmitEvent) {
1094
- const DOMUtilsContext = this;
1095
1101
  if (typeof element === "string") {
1096
- element = PopsCore.document.querySelector(element);
1102
+ element = this.selector(element);
1097
1103
  }
1098
1104
  if (element == null) {
1099
1105
  return;
1100
1106
  }
1101
1107
  if (handler == null) {
1102
- DOMUtilsContext.emit(element, "click", details, useDispatchToEmitEvent);
1108
+ this.emit(element, "click", details, useDispatchToEmitEvent);
1103
1109
  }
1104
1110
  else {
1105
- DOMUtilsContext.on(element, "click", null, handler);
1111
+ const listener = this.on(element, "click", handler);
1112
+ return listener;
1106
1113
  }
1107
1114
  }
1108
1115
  /**
@@ -1120,18 +1127,18 @@
1120
1127
  * })
1121
1128
  * */
1122
1129
  blur(element, handler, details, useDispatchToEmitEvent) {
1123
- const DOMUtilsContext = this;
1124
1130
  if (typeof element === "string") {
1125
- element = PopsCore.document.querySelector(element);
1131
+ element = this.selector(element);
1126
1132
  }
1127
1133
  if (element == null) {
1128
1134
  return;
1129
1135
  }
1130
1136
  if (handler === null) {
1131
- DOMUtilsContext.emit(element, "blur", details, useDispatchToEmitEvent);
1137
+ this.emit(element, "blur", details, useDispatchToEmitEvent);
1132
1138
  }
1133
1139
  else {
1134
- DOMUtilsContext.on(element, "blur", null, handler);
1140
+ const listener = this.on(element, "blur", handler);
1141
+ return listener;
1135
1142
  }
1136
1143
  }
1137
1144
  /**
@@ -1149,18 +1156,18 @@
1149
1156
  * })
1150
1157
  * */
1151
1158
  focus(element, handler, details, useDispatchToEmitEvent) {
1152
- const DOMUtilsContext = this;
1153
1159
  if (typeof element === "string") {
1154
- element = PopsCore.document.querySelector(element);
1160
+ element = this.selector(element);
1155
1161
  }
1156
1162
  if (element == null) {
1157
1163
  return;
1158
1164
  }
1159
1165
  if (handler == null) {
1160
- DOMUtilsContext.emit(element, "focus", details, useDispatchToEmitEvent);
1166
+ this.emit(element, "focus", details, useDispatchToEmitEvent);
1161
1167
  }
1162
1168
  else {
1163
- DOMUtilsContext.on(element, "focus", null, handler);
1169
+ const listener = this.on(element, "focus", handler);
1170
+ return listener;
1164
1171
  }
1165
1172
  }
1166
1173
  /**
@@ -1178,15 +1185,39 @@
1178
1185
  * })
1179
1186
  */
1180
1187
  onHover(element, handler, option) {
1181
- const DOMUtilsContext = this;
1188
+ const that = this;
1182
1189
  if (typeof element === "string") {
1183
- element = PopsCore.document.querySelector(element);
1190
+ element = that.selectorAll(element);
1184
1191
  }
1185
1192
  if (element == null) {
1186
1193
  return;
1187
1194
  }
1188
- DOMUtilsContext.on(element, "mouseenter", null, handler, option);
1189
- DOMUtilsContext.on(element, "mouseleave", null, handler, option);
1195
+ if (popsUtils.isNodeList(element)) {
1196
+ // 设置
1197
+ const listenerList = [];
1198
+ element.forEach(($ele) => {
1199
+ const listener = that.onHover($ele, handler, option);
1200
+ listenerList.push(listener);
1201
+ });
1202
+ return {
1203
+ off() {
1204
+ listenerList.forEach((listener) => {
1205
+ if (!listener) {
1206
+ return;
1207
+ }
1208
+ listener.off();
1209
+ });
1210
+ },
1211
+ };
1212
+ }
1213
+ const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
1214
+ const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
1215
+ return {
1216
+ off() {
1217
+ mouseenter_listener.off();
1218
+ mouseleave_listener.off();
1219
+ },
1220
+ };
1190
1221
  }
1191
1222
  /**
1192
1223
  * 当按键松开时触发事件
@@ -1204,14 +1235,14 @@
1204
1235
  * })
1205
1236
  */
1206
1237
  onKeyup(target, handler, option) {
1207
- const DOMUtilsContext = this;
1208
1238
  if (target == null) {
1209
1239
  return;
1210
1240
  }
1211
1241
  if (typeof target === "string") {
1212
- target = PopsCore.document.querySelector(target);
1242
+ target = this.selector(target);
1213
1243
  }
1214
- DOMUtilsContext.on(target, "keyup", null, handler, option);
1244
+ const listener = this.on(target, "keyup", handler, option);
1245
+ return listener;
1215
1246
  }
1216
1247
  /**
1217
1248
  * 当按键按下时触发事件
@@ -1229,14 +1260,14 @@
1229
1260
  * })
1230
1261
  */
1231
1262
  onKeydown(target, handler, option) {
1232
- const DOMUtilsContext = this;
1233
1263
  if (target == null) {
1234
1264
  return;
1235
1265
  }
1236
1266
  if (typeof target === "string") {
1237
- target = PopsCore.document.querySelector(target);
1267
+ target = this.selector(target);
1238
1268
  }
1239
- DOMUtilsContext.on(target, "keydown", null, handler, option);
1269
+ const listener = this.on(target, "keydown", handler, option);
1270
+ return listener;
1240
1271
  }
1241
1272
  /**
1242
1273
  * 当按键按下时触发事件
@@ -1254,38 +1285,69 @@
1254
1285
  * })
1255
1286
  */
1256
1287
  onKeypress(target, handler, option) {
1257
- const DOMUtilsContext = this;
1258
1288
  if (target == null) {
1259
1289
  return;
1260
1290
  }
1261
1291
  if (typeof target === "string") {
1262
- target = PopsCore.document.querySelector(target);
1292
+ target = this.selector(target);
1263
1293
  }
1264
- DOMUtilsContext.on(target, "keypress", null, handler, option);
1294
+ const listener = this.on(target, "keypress", handler, option);
1295
+ return listener;
1265
1296
  }
1266
- preventEvent(element, eventNameList = [], capture) {
1267
- function stopEvent(event) {
1268
- // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL
1297
+ preventEvent(...args) {
1298
+ /**
1299
+ * 阻止事件的默认行为发生,并阻止事件传播
1300
+ */
1301
+ const stopEvent = (event, onlyStopPropagation) => {
1302
+ if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
1303
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1304
+ event?.stopPropagation();
1305
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1306
+ event?.stopImmediatePropagation();
1307
+ return;
1308
+ }
1309
+ // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
1269
1310
  event?.preventDefault();
1270
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1271
- event?.stopPropagation();
1272
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1273
- event?.stopImmediatePropagation();
1274
1311
  return false;
1275
- }
1276
- if (arguments.length === 1) {
1312
+ };
1313
+ if (args[0] instanceof Event) {
1277
1314
  // 直接阻止事件
1278
- // eslint-disable-next-line prefer-rest-params
1279
- return stopEvent(arguments[0]);
1315
+ const onlyStopPropagation = args[1];
1316
+ return stopEvent(args[0], onlyStopPropagation);
1280
1317
  }
1281
1318
  else {
1319
+ const $el = args[0];
1320
+ let eventNameList = args[1];
1321
+ let selector = void 0;
1322
+ let capture = false;
1323
+ let onlyStopPropagation = false;
1282
1324
  // 添加对应的事件来阻止触发
1283
1325
  if (typeof eventNameList === "string") {
1284
1326
  eventNameList = [eventNameList];
1285
1327
  }
1286
- eventNameList.forEach((eventName) => {
1287
- this.on(element, eventName, stopEvent, { capture: Boolean(capture) });
1288
- });
1328
+ let option = void 0;
1329
+ if (typeof args[2] === "string" || Array.isArray(args[2])) {
1330
+ // selector
1331
+ selector = args[2];
1332
+ if (typeof args[3] === "object" && args[3] != null) {
1333
+ option = args[3];
1334
+ }
1335
+ }
1336
+ else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
1337
+ // option
1338
+ option = args[2];
1339
+ }
1340
+ else {
1341
+ throw new TypeError("Invalid argument");
1342
+ }
1343
+ if (option) {
1344
+ capture = Boolean(option.capture);
1345
+ onlyStopPropagation = Boolean(option.onlyStopPropagation);
1346
+ }
1347
+ const listener = this.on($el, eventNameList, selector, (evt) => {
1348
+ return stopEvent(evt, onlyStopPropagation);
1349
+ }, { capture: capture });
1350
+ return listener;
1289
1351
  }
1290
1352
  }
1291
1353
  selector(selector) {
@@ -1471,7 +1533,7 @@
1471
1533
  width(element, isShow = false, parent) {
1472
1534
  const DOMUtilsContext = this;
1473
1535
  if (typeof element === "string") {
1474
- element = PopsCore.document.querySelector(element);
1536
+ element = this.selector(element);
1475
1537
  }
1476
1538
  if (element == null) {
1477
1539
  return;
@@ -1522,7 +1584,7 @@
1522
1584
  return PopsCore.window.document.documentElement.clientHeight;
1523
1585
  }
1524
1586
  if (typeof element === "string") {
1525
- element = PopsCore.document.querySelector(element);
1587
+ element = this.selector(element);
1526
1588
  }
1527
1589
  if (element == null) {
1528
1590
  return;
@@ -1570,7 +1632,7 @@
1570
1632
  return PopsCore.window.innerWidth;
1571
1633
  }
1572
1634
  if (typeof element === "string") {
1573
- element = PopsCore.document.querySelector(element);
1635
+ element = this.selector(element);
1574
1636
  }
1575
1637
  if (element == null) {
1576
1638
  return;
@@ -1595,7 +1657,7 @@
1595
1657
  return PopsCore.window.innerHeight;
1596
1658
  }
1597
1659
  if (typeof element === "string") {
1598
- element = PopsCore.document.querySelector(element);
1660
+ element = this.selector(element);
1599
1661
  }
1600
1662
  element = element;
1601
1663
  if (isShow || (!isShow && popsDOMUtils.isShow(element))) {
@@ -2017,12 +2079,12 @@
2017
2079
  * @param content 子元素或HTML字符串
2018
2080
  * @example
2019
2081
  * // 元素a.xx的内部末尾添加一个元素
2020
- * DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
2082
+ * DOMUtils.append(document.querySelector("a.xx"), document.querySelector("b.xx"))
2021
2083
  * DOMUtils.append("a.xx","'<b class="xx"></b>")
2022
2084
  * */
2023
2085
  append(element, content) {
2024
2086
  if (typeof element === "string") {
2025
- element = PopsCore.document.querySelector(element);
2087
+ element = this.selector(element);
2026
2088
  }
2027
2089
  if (element == null) {
2028
2090
  return;
@@ -2158,7 +2220,7 @@
2158
2220
  * */
2159
2221
  before(element, content) {
2160
2222
  if (typeof element === "string") {
2161
- element = PopsCore.document.querySelector(element);
2223
+ element = this.selector(element);
2162
2224
  }
2163
2225
  if (element == null) {
2164
2226
  return;
@@ -2181,7 +2243,7 @@
2181
2243
  * */
2182
2244
  after(element, content) {
2183
2245
  if (typeof element === "string") {
2184
- element = PopsCore.document.querySelector(element);
2246
+ element = this.selector(element);
2185
2247
  }
2186
2248
  if (element == null) {
2187
2249
  return;
@@ -5832,7 +5894,7 @@
5832
5894
  */
5833
5895
  const $anim = PopsElementHandler.parseElement(animHTML);
5834
5896
  const { $pops: $pops, $headerBtnClose: headerCloseBtnElement, $headerControls: headerControlsElement, $title: $title, $iframe: $iframe, $loading: loadingElement, $contentLoading: $contentLoading, $headerBtnMin: headerMinBtnElement, $headerBtnMax: headerMaxBtnElement, $headerBtnMise: headerMiseBtnElement, } = PopsHandler.handleQueryElement($anim, popsType);
5835
- let $iframeContainer = PopsCore.document.querySelector(".pops-iframe-container");
5897
+ let $iframeContainer = popsDOMUtils.selector(".pops-iframe-container");
5836
5898
  if (!$iframeContainer) {
5837
5899
  $iframeContainer = popsDOMUtils.createElement("div", {
5838
5900
  className: "pops-iframe-container",
@@ -13310,7 +13372,7 @@
13310
13372
  },
13311
13373
  };
13312
13374
 
13313
- const version = "3.3.2";
13375
+ const version = "3.3.3";
13314
13376
 
13315
13377
  class Pops {
13316
13378
  /** 配置 */