@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.
@@ -294,6 +294,13 @@ var pops = (function () {
294
294
  isDOM(target) {
295
295
  return target instanceof Node;
296
296
  }
297
+ /**
298
+ * 判断是否是元素列表
299
+ * @param $ele
300
+ */
301
+ isNodeList($ele) {
302
+ return Array.isArray($ele) || $ele instanceof NodeList;
303
+ }
297
304
  /**
298
305
  * 删除对象上的属性
299
306
  * @param target
@@ -1026,7 +1033,7 @@ var pops = (function () {
1026
1033
  */
1027
1034
  emit(element, eventType, details, useDispatchToEmitEvent = true) {
1028
1035
  if (typeof element === "string") {
1029
- element = PopsCore.document.querySelector(element);
1036
+ element = this.selector(element);
1030
1037
  }
1031
1038
  if (element == null) {
1032
1039
  return;
@@ -1088,18 +1095,18 @@ var pops = (function () {
1088
1095
  * })
1089
1096
  * */
1090
1097
  click(element, handler, details, useDispatchToEmitEvent) {
1091
- const DOMUtilsContext = this;
1092
1098
  if (typeof element === "string") {
1093
- element = PopsCore.document.querySelector(element);
1099
+ element = this.selector(element);
1094
1100
  }
1095
1101
  if (element == null) {
1096
1102
  return;
1097
1103
  }
1098
1104
  if (handler == null) {
1099
- DOMUtilsContext.emit(element, "click", details, useDispatchToEmitEvent);
1105
+ this.emit(element, "click", details, useDispatchToEmitEvent);
1100
1106
  }
1101
1107
  else {
1102
- DOMUtilsContext.on(element, "click", null, handler);
1108
+ const listener = this.on(element, "click", handler);
1109
+ return listener;
1103
1110
  }
1104
1111
  }
1105
1112
  /**
@@ -1117,18 +1124,18 @@ var pops = (function () {
1117
1124
  * })
1118
1125
  * */
1119
1126
  blur(element, handler, details, useDispatchToEmitEvent) {
1120
- const DOMUtilsContext = this;
1121
1127
  if (typeof element === "string") {
1122
- element = PopsCore.document.querySelector(element);
1128
+ element = this.selector(element);
1123
1129
  }
1124
1130
  if (element == null) {
1125
1131
  return;
1126
1132
  }
1127
1133
  if (handler === null) {
1128
- DOMUtilsContext.emit(element, "blur", details, useDispatchToEmitEvent);
1134
+ this.emit(element, "blur", details, useDispatchToEmitEvent);
1129
1135
  }
1130
1136
  else {
1131
- DOMUtilsContext.on(element, "blur", null, handler);
1137
+ const listener = this.on(element, "blur", handler);
1138
+ return listener;
1132
1139
  }
1133
1140
  }
1134
1141
  /**
@@ -1146,18 +1153,18 @@ var pops = (function () {
1146
1153
  * })
1147
1154
  * */
1148
1155
  focus(element, handler, details, useDispatchToEmitEvent) {
1149
- const DOMUtilsContext = this;
1150
1156
  if (typeof element === "string") {
1151
- element = PopsCore.document.querySelector(element);
1157
+ element = this.selector(element);
1152
1158
  }
1153
1159
  if (element == null) {
1154
1160
  return;
1155
1161
  }
1156
1162
  if (handler == null) {
1157
- DOMUtilsContext.emit(element, "focus", details, useDispatchToEmitEvent);
1163
+ this.emit(element, "focus", details, useDispatchToEmitEvent);
1158
1164
  }
1159
1165
  else {
1160
- DOMUtilsContext.on(element, "focus", null, handler);
1166
+ const listener = this.on(element, "focus", handler);
1167
+ return listener;
1161
1168
  }
1162
1169
  }
1163
1170
  /**
@@ -1175,15 +1182,39 @@ var pops = (function () {
1175
1182
  * })
1176
1183
  */
1177
1184
  onHover(element, handler, option) {
1178
- const DOMUtilsContext = this;
1185
+ const that = this;
1179
1186
  if (typeof element === "string") {
1180
- element = PopsCore.document.querySelector(element);
1187
+ element = that.selectorAll(element);
1181
1188
  }
1182
1189
  if (element == null) {
1183
1190
  return;
1184
1191
  }
1185
- DOMUtilsContext.on(element, "mouseenter", null, handler, option);
1186
- DOMUtilsContext.on(element, "mouseleave", null, handler, option);
1192
+ if (popsUtils.isNodeList(element)) {
1193
+ // 设置
1194
+ const listenerList = [];
1195
+ element.forEach(($ele) => {
1196
+ const listener = that.onHover($ele, handler, option);
1197
+ listenerList.push(listener);
1198
+ });
1199
+ return {
1200
+ off() {
1201
+ listenerList.forEach((listener) => {
1202
+ if (!listener) {
1203
+ return;
1204
+ }
1205
+ listener.off();
1206
+ });
1207
+ },
1208
+ };
1209
+ }
1210
+ const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
1211
+ const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
1212
+ return {
1213
+ off() {
1214
+ mouseenter_listener.off();
1215
+ mouseleave_listener.off();
1216
+ },
1217
+ };
1187
1218
  }
1188
1219
  /**
1189
1220
  * 当按键松开时触发事件
@@ -1201,14 +1232,14 @@ var pops = (function () {
1201
1232
  * })
1202
1233
  */
1203
1234
  onKeyup(target, handler, option) {
1204
- const DOMUtilsContext = this;
1205
1235
  if (target == null) {
1206
1236
  return;
1207
1237
  }
1208
1238
  if (typeof target === "string") {
1209
- target = PopsCore.document.querySelector(target);
1239
+ target = this.selector(target);
1210
1240
  }
1211
- DOMUtilsContext.on(target, "keyup", null, handler, option);
1241
+ const listener = this.on(target, "keyup", handler, option);
1242
+ return listener;
1212
1243
  }
1213
1244
  /**
1214
1245
  * 当按键按下时触发事件
@@ -1226,14 +1257,14 @@ var pops = (function () {
1226
1257
  * })
1227
1258
  */
1228
1259
  onKeydown(target, handler, option) {
1229
- const DOMUtilsContext = this;
1230
1260
  if (target == null) {
1231
1261
  return;
1232
1262
  }
1233
1263
  if (typeof target === "string") {
1234
- target = PopsCore.document.querySelector(target);
1264
+ target = this.selector(target);
1235
1265
  }
1236
- DOMUtilsContext.on(target, "keydown", null, handler, option);
1266
+ const listener = this.on(target, "keydown", handler, option);
1267
+ return listener;
1237
1268
  }
1238
1269
  /**
1239
1270
  * 当按键按下时触发事件
@@ -1251,38 +1282,69 @@ var pops = (function () {
1251
1282
  * })
1252
1283
  */
1253
1284
  onKeypress(target, handler, option) {
1254
- const DOMUtilsContext = this;
1255
1285
  if (target == null) {
1256
1286
  return;
1257
1287
  }
1258
1288
  if (typeof target === "string") {
1259
- target = PopsCore.document.querySelector(target);
1289
+ target = this.selector(target);
1260
1290
  }
1261
- DOMUtilsContext.on(target, "keypress", null, handler, option);
1291
+ const listener = this.on(target, "keypress", handler, option);
1292
+ return listener;
1262
1293
  }
1263
- preventEvent(element, eventNameList = [], capture) {
1264
- function stopEvent(event) {
1265
- // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL
1294
+ preventEvent(...args) {
1295
+ /**
1296
+ * 阻止事件的默认行为发生,并阻止事件传播
1297
+ */
1298
+ const stopEvent = (event, onlyStopPropagation) => {
1299
+ if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
1300
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1301
+ event?.stopPropagation();
1302
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1303
+ event?.stopImmediatePropagation();
1304
+ return;
1305
+ }
1306
+ // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
1266
1307
  event?.preventDefault();
1267
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
1268
- event?.stopPropagation();
1269
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
1270
- event?.stopImmediatePropagation();
1271
1308
  return false;
1272
- }
1273
- if (arguments.length === 1) {
1309
+ };
1310
+ if (args[0] instanceof Event) {
1274
1311
  // 直接阻止事件
1275
- // eslint-disable-next-line prefer-rest-params
1276
- return stopEvent(arguments[0]);
1312
+ const onlyStopPropagation = args[1];
1313
+ return stopEvent(args[0], onlyStopPropagation);
1277
1314
  }
1278
1315
  else {
1316
+ const $el = args[0];
1317
+ let eventNameList = args[1];
1318
+ let selector = void 0;
1319
+ let capture = false;
1320
+ let onlyStopPropagation = false;
1279
1321
  // 添加对应的事件来阻止触发
1280
1322
  if (typeof eventNameList === "string") {
1281
1323
  eventNameList = [eventNameList];
1282
1324
  }
1283
- eventNameList.forEach((eventName) => {
1284
- this.on(element, eventName, stopEvent, { capture: Boolean(capture) });
1285
- });
1325
+ let option = void 0;
1326
+ if (typeof args[2] === "string" || Array.isArray(args[2])) {
1327
+ // selector
1328
+ selector = args[2];
1329
+ if (typeof args[3] === "object" && args[3] != null) {
1330
+ option = args[3];
1331
+ }
1332
+ }
1333
+ else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
1334
+ // option
1335
+ option = args[2];
1336
+ }
1337
+ else {
1338
+ throw new TypeError("Invalid argument");
1339
+ }
1340
+ if (option) {
1341
+ capture = Boolean(option.capture);
1342
+ onlyStopPropagation = Boolean(option.onlyStopPropagation);
1343
+ }
1344
+ const listener = this.on($el, eventNameList, selector, (evt) => {
1345
+ return stopEvent(evt, onlyStopPropagation);
1346
+ }, { capture: capture });
1347
+ return listener;
1286
1348
  }
1287
1349
  }
1288
1350
  selector(selector) {
@@ -1468,7 +1530,7 @@ var pops = (function () {
1468
1530
  width(element, isShow = false, parent) {
1469
1531
  const DOMUtilsContext = this;
1470
1532
  if (typeof element === "string") {
1471
- element = PopsCore.document.querySelector(element);
1533
+ element = this.selector(element);
1472
1534
  }
1473
1535
  if (element == null) {
1474
1536
  return;
@@ -1519,7 +1581,7 @@ var pops = (function () {
1519
1581
  return PopsCore.window.document.documentElement.clientHeight;
1520
1582
  }
1521
1583
  if (typeof element === "string") {
1522
- element = PopsCore.document.querySelector(element);
1584
+ element = this.selector(element);
1523
1585
  }
1524
1586
  if (element == null) {
1525
1587
  return;
@@ -1567,7 +1629,7 @@ var pops = (function () {
1567
1629
  return PopsCore.window.innerWidth;
1568
1630
  }
1569
1631
  if (typeof element === "string") {
1570
- element = PopsCore.document.querySelector(element);
1632
+ element = this.selector(element);
1571
1633
  }
1572
1634
  if (element == null) {
1573
1635
  return;
@@ -1592,7 +1654,7 @@ var pops = (function () {
1592
1654
  return PopsCore.window.innerHeight;
1593
1655
  }
1594
1656
  if (typeof element === "string") {
1595
- element = PopsCore.document.querySelector(element);
1657
+ element = this.selector(element);
1596
1658
  }
1597
1659
  element = element;
1598
1660
  if (isShow || (!isShow && popsDOMUtils.isShow(element))) {
@@ -2014,12 +2076,12 @@ var pops = (function () {
2014
2076
  * @param content 子元素或HTML字符串
2015
2077
  * @example
2016
2078
  * // 元素a.xx的内部末尾添加一个元素
2017
- * DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
2079
+ * DOMUtils.append(document.querySelector("a.xx"), document.querySelector("b.xx"))
2018
2080
  * DOMUtils.append("a.xx","'<b class="xx"></b>")
2019
2081
  * */
2020
2082
  append(element, content) {
2021
2083
  if (typeof element === "string") {
2022
- element = PopsCore.document.querySelector(element);
2084
+ element = this.selector(element);
2023
2085
  }
2024
2086
  if (element == null) {
2025
2087
  return;
@@ -2155,7 +2217,7 @@ var pops = (function () {
2155
2217
  * */
2156
2218
  before(element, content) {
2157
2219
  if (typeof element === "string") {
2158
- element = PopsCore.document.querySelector(element);
2220
+ element = this.selector(element);
2159
2221
  }
2160
2222
  if (element == null) {
2161
2223
  return;
@@ -2178,7 +2240,7 @@ var pops = (function () {
2178
2240
  * */
2179
2241
  after(element, content) {
2180
2242
  if (typeof element === "string") {
2181
- element = PopsCore.document.querySelector(element);
2243
+ element = this.selector(element);
2182
2244
  }
2183
2245
  if (element == null) {
2184
2246
  return;
@@ -5829,7 +5891,7 @@ var pops = (function () {
5829
5891
  */
5830
5892
  const $anim = PopsElementHandler.parseElement(animHTML);
5831
5893
  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);
5832
- let $iframeContainer = PopsCore.document.querySelector(".pops-iframe-container");
5894
+ let $iframeContainer = popsDOMUtils.selector(".pops-iframe-container");
5833
5895
  if (!$iframeContainer) {
5834
5896
  $iframeContainer = popsDOMUtils.createElement("div", {
5835
5897
  className: "pops-iframe-container",
@@ -13307,7 +13369,7 @@ var pops = (function () {
13307
13369
  },
13308
13370
  };
13309
13371
 
13310
- const version = "3.3.2";
13372
+ const version = "3.3.3";
13311
13373
 
13312
13374
  class Pops {
13313
13375
  /** 配置 */