@whitesev/domutils 1.4.3 → 1.4.4

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
@@ -182,8 +182,9 @@
182
182
  * @param option
183
183
  */
184
184
  function getOption(args, startIndex, option) {
185
- if (typeof args[startIndex] === "boolean") {
186
- option.capture = args[startIndex];
185
+ let currentParam = args[startIndex];
186
+ if (typeof currentParam === "boolean") {
187
+ option.capture = currentParam;
187
188
  if (typeof args[startIndex + 1] === "boolean") {
188
189
  option.once = args[startIndex + 1];
189
190
  }
@@ -191,13 +192,15 @@
191
192
  option.passive = args[startIndex + 2];
192
193
  }
193
194
  }
194
- else if (typeof args[startIndex] === "object" &&
195
- ("capture" in args[startIndex] ||
196
- "once" in args[startIndex] ||
197
- "passive" in args[startIndex])) {
198
- option.capture = args[startIndex].capture;
199
- option.once = args[startIndex].once;
200
- option.passive = args[startIndex].passive;
195
+ else if (typeof currentParam === "object" &&
196
+ ("capture" in currentParam ||
197
+ "once" in currentParam ||
198
+ "passive" in currentParam ||
199
+ "isComposedPath" in currentParam)) {
200
+ option.capture = currentParam.capture;
201
+ option.once = currentParam.once;
202
+ option.passive = currentParam.passive;
203
+ option.isComposedPath = currentParam.isComposedPath;
201
204
  }
202
205
  return option;
203
206
  }
@@ -234,27 +237,28 @@
234
237
  selectorList.push(selector);
235
238
  }
236
239
  // 事件回调
237
- let _callback_ = callback;
240
+ let listenerCallBack = callback;
238
241
  // 事件配置
239
- let _option_ = {
242
+ let listenerOption = {
240
243
  capture: false,
241
244
  once: false,
242
245
  passive: false,
246
+ isComposedPath: false,
243
247
  };
244
248
  if (typeof selector === "function") {
245
249
  /* 这是为没有selector的情况 */
246
- _callback_ = selector;
247
- _option_ = getOption(args, 3, _option_);
250
+ listenerCallBack = selector;
251
+ listenerOption = getOption(args, 3, listenerOption);
248
252
  }
249
253
  else {
250
254
  /* 这是存在selector的情况 */
251
- _option_ = getOption(args, 4, _option_);
255
+ listenerOption = getOption(args, 4, listenerOption);
252
256
  }
253
257
  /**
254
258
  * 如果是once,那么删除该监听和元素上的事件和监听
255
259
  */
256
260
  function checkOptionOnceToRemoveEventListener() {
257
- if (_option_.once) {
261
+ if (listenerOption.once) {
258
262
  DOMUtilsContext.off(element, eventType, selector, callback, option);
259
263
  }
260
264
  }
@@ -264,53 +268,56 @@
264
268
  * @param event
265
269
  */
266
270
  function domUtilsEventCallBack(event) {
267
- let target = event.target;
271
+ let eventTarget = listenerOption.isComposedPath
272
+ ? event.composedPath()[0]
273
+ : event.target;
268
274
  if (selectorList.length) {
269
- /* 存在自定义子元素选择器 */
275
+ /* 存在子元素选择器 */
270
276
  let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
271
277
  ? DOMUtilsContext.windowApi.document.documentElement
272
278
  : elementItem;
273
279
  for (let index = 0; index < selectorList.length; index++) {
274
280
  const selectorItem = selectorList[index];
275
- if (target.matches(selectorItem)) {
281
+ if (eventTarget.matches(selectorItem)) {
276
282
  /* 当前目标可以被selector所匹配到 */
277
- _callback_.call(target, event);
283
+ listenerCallBack.call(eventTarget, event);
278
284
  checkOptionOnceToRemoveEventListener();
279
285
  break;
280
286
  }
281
- else if (target.closest(selectorItem) &&
282
- totalParent.contains(target.closest(selectorItem))) {
287
+ else {
283
288
  /* 在上层与主元素之间寻找可以被selector所匹配到的 */
284
- let closestElement = target.closest(selectorItem);
285
- /* event的target值不能直接修改 */
286
- OriginPrototype.Object.defineProperty(event, "target", {
287
- get() {
288
- return closestElement;
289
- },
290
- });
291
- _callback_.call(closestElement, event);
292
- checkOptionOnceToRemoveEventListener();
293
- break;
289
+ let $closestMatches = eventTarget.closest(selectorItem);
290
+ if ($closestMatches && totalParent.contains($closestMatches)) {
291
+ /* eventtarget值不能直接修改 */
292
+ OriginPrototype.Object.defineProperty(event, "target", {
293
+ get() {
294
+ return $closestMatches;
295
+ },
296
+ });
297
+ listenerCallBack.call($closestMatches, event);
298
+ checkOptionOnceToRemoveEventListener();
299
+ break;
300
+ }
294
301
  }
295
302
  }
296
303
  }
297
304
  else {
298
- _callback_.call(elementItem, event);
305
+ listenerCallBack.call(elementItem, event);
299
306
  checkOptionOnceToRemoveEventListener();
300
307
  }
301
308
  }
302
309
  /* 遍历事件名设置元素事件 */
303
310
  eventTypeList.forEach((eventName) => {
304
- elementItem.addEventListener(eventName, domUtilsEventCallBack, _option_);
311
+ elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
305
312
  /* 获取对象上的事件 */
306
313
  let elementEvents = elementItem[DOMUtilsData.SymbolEvents] || {};
307
314
  /* 初始化对象上的xx事件 */
308
315
  elementEvents[eventName] = elementEvents[eventName] || [];
309
316
  elementEvents[eventName].push({
310
317
  selector: selectorList,
311
- option: _option_,
318
+ option: listenerOption,
312
319
  callback: domUtilsEventCallBack,
313
- originCallBack: _callback_,
320
+ originCallBack: listenerCallBack,
314
321
  });
315
322
  /* 覆盖事件 */
316
323
  // @ts-ignore
@@ -326,12 +333,13 @@
326
333
  * @param option
327
334
  */
328
335
  function getOption(args1, startIndex, option) {
329
- if (typeof args1[startIndex] === "boolean") {
330
- option.capture = args1[startIndex];
336
+ let currentParam = args1[startIndex];
337
+ if (typeof currentParam === "boolean") {
338
+ option.capture = currentParam;
331
339
  }
332
- else if (typeof args1[startIndex] === "object" &&
333
- "capture" in args1[startIndex]) {
334
- option.capture = args1[startIndex].capture;
340
+ else if (typeof currentParam === "object" &&
341
+ "capture" in currentParam) {
342
+ option.capture = currentParam.capture;
335
343
  }
336
344
  return option;
337
345
  }
@@ -369,20 +377,31 @@
369
377
  /**
370
378
  * 事件的回调函数
371
379
  */
372
- let _callback_ = callback;
380
+ let listenerCallBack = callback;
373
381
  /**
374
382
  * 事件的配置
375
383
  */
376
- let _option_ = {
384
+ let listenerOption = {
377
385
  capture: false,
378
386
  };
379
387
  if (typeof selector === "function") {
380
388
  /* 这是为没有selector的情况 */
381
- _callback_ = selector;
382
- _option_ = getOption(args, 3, _option_);
389
+ listenerCallBack = selector;
390
+ listenerOption = getOption(args, 3, listenerOption);
383
391
  }
384
392
  else {
385
- _option_ = getOption(args, 4, _option_);
393
+ listenerOption = getOption(args, 4, listenerOption);
394
+ }
395
+ // 是否移除所有事件
396
+ let isRemoveAll = false;
397
+ if (args.length === 2) {
398
+ // 目标函数、事件名
399
+ isRemoveAll = true;
400
+ }
401
+ else if ((args.length === 3 && typeof args[2] === "string") ||
402
+ Array.isArray(args[2])) {
403
+ // 目标函数、事件名、子元素选择器
404
+ isRemoveAll = true;
386
405
  }
387
406
  elementList.forEach((elementItem) => {
388
407
  /* 获取对象上的事件 */
@@ -395,26 +414,25 @@
395
414
  }
396
415
  for (let index = 0; index < handlers.length; index++) {
397
416
  let handler = handlers[index];
398
- let flag = false;
399
- if (!selectorList.length) {
400
- // selectorList是空的,默认移除
401
- flag = true;
417
+ let flag = true;
418
+ if (flag &&
419
+ listenerCallBack &&
420
+ handler.originCallBack !== listenerCallBack) {
421
+ // callback不同
422
+ flag = false;
402
423
  }
403
- else {
404
- if (Array.isArray(handler.selector) &&
405
- JSON.stringify(handler.selector) === JSON.stringify(selectorList)) {
406
- // 元素上的selectorList不为空且和传入的相同
407
- flag = true;
424
+ if (flag && selectorList.length && Array.isArray(handler.selector)) {
425
+ if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
426
+ // 子元素选择器不同
427
+ flag = false;
408
428
  }
409
429
  }
410
- if (!_callback_ ||
411
- handler.callback === _callback_ ||
412
- handler.originCallBack === _callback_) {
413
- /* callback不为空,且callback相同 */
414
- flag = true;
430
+ if (flag && listenerOption.capture !== handler.option.capture) {
431
+ // 事件的配置项不同
432
+ flag = false;
415
433
  }
416
- if (flag) {
417
- elementItem.removeEventListener(eventName, handler.callback, _option_);
434
+ if (flag || isRemoveAll) {
435
+ elementItem.removeEventListener(eventName, handler.callback, handler.option);
418
436
  handlers.splice(index--, 1);
419
437
  }
420
438
  }