ph-utils 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
package/lib/dom.d.ts CHANGED
@@ -32,9 +32,12 @@ export declare function transform(element: HTMLElement, value: string): void;
32
32
  * @param {HTMLElement} element 添加事件的节点
33
33
  * @param {string} listener 事件名称
34
34
  * @param {function} event 事件处理函数
35
- * @param {boolean} once 是否是只运行一次的处理函数
35
+ * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
36
36
  */
37
- export declare function on(element: HTMLElement, listener: string, event: (e?: Event) => void, once?: boolean): void;
37
+ export declare function on(element: HTMLElement, listener: string, fn: (e: Event, target?: HTMLElement, flag?: string) => void, once?: boolean | {
38
+ once?: boolean;
39
+ eventFlag?: string;
40
+ }): void;
38
41
  /**
39
42
  * 设置或获取节点的 innerHTML 属性
40
43
  * @param element
package/lib/dom.js CHANGED
@@ -54,10 +54,34 @@ export function transform(element, value) {
54
54
  * @param {HTMLElement} element 添加事件的节点
55
55
  * @param {string} listener 事件名称
56
56
  * @param {function} event 事件处理函数
57
- * @param {boolean} once 是否是只运行一次的处理函数
57
+ * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
58
58
  */
59
- export function on(element, listener, event, once = false) {
60
- element.addEventListener(listener, event, { once });
59
+ export function on(element, listener, fn, once = false) {
60
+ let eventExtra = {};
61
+ if (typeof once === 'boolean') {
62
+ eventExtra.once = true;
63
+ }
64
+ else {
65
+ eventExtra = once;
66
+ }
67
+ if (eventExtra.eventFlag !== null) {
68
+ element.addEventListener(listener, (e) => {
69
+ let target = e.target;
70
+ let flag = '';
71
+ do {
72
+ flag = target.getAttribute(eventExtra.eventFlag) || '';
73
+ if (flag === '') {
74
+ target = target.parentNode;
75
+ }
76
+ } while (flag === '');
77
+ if (flag !== '__stop__') {
78
+ fn(e, target, flag);
79
+ }
80
+ }, eventExtra);
81
+ }
82
+ else {
83
+ element.addEventListener(listener, fn, eventExtra);
84
+ }
61
85
  }
62
86
  /**
63
87
  * 设置或获取节点的 innerHTML 属性
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "module": "lib/index_m.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "browser": "lib/index_m.js",
8
- "version": "0.1.1",
8
+ "version": "0.1.4",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",