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