@studiometa/js-toolkit 2.0.0-rc.2 → 2.1.0
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/Base/index.cjs +54 -18
- package/Base/index.d.ts +47 -26
- package/Base/index.js +54 -18
- package/Base/managers/AbstractManager.cjs +56 -0
- package/Base/managers/AbstractManager.d.ts +44 -0
- package/Base/managers/AbstractManager.js +35 -0
- package/Base/managers/ChildrenManager.cjs +79 -100
- package/Base/managers/ChildrenManager.d.ts +8 -83
- package/Base/managers/ChildrenManager.js +76 -100
- package/Base/managers/EventsManager.cjs +75 -99
- package/Base/managers/EventsManager.d.ts +37 -113
- package/Base/managers/EventsManager.js +75 -99
- package/Base/managers/OptionsManager.cjs +65 -79
- package/Base/managers/OptionsManager.d.ts +41 -80
- package/Base/managers/OptionsManager.js +66 -80
- package/Base/managers/RefsManager.cjs +44 -68
- package/Base/managers/RefsManager.d.ts +12 -66
- package/Base/managers/RefsManager.js +41 -69
- package/Base/managers/ResponsiveOptionsManager.cjs +62 -0
- package/Base/managers/ResponsiveOptionsManager.d.ts +26 -0
- package/Base/managers/ResponsiveOptionsManager.js +37 -0
- package/Base/managers/ServicesManager.cjs +7 -10
- package/Base/managers/ServicesManager.d.ts +16 -19
- package/Base/managers/ServicesManager.js +7 -10
- package/Base/utils.d.ts +4 -4
- package/decorators/index.cjs +2 -0
- package/decorators/index.d.ts +1 -0
- package/decorators/index.js +6 -4
- package/decorators/withBreakpointManager.cjs +0 -1
- package/decorators/withBreakpointManager.d.ts +2 -2
- package/decorators/withBreakpointManager.js +0 -1
- package/decorators/withBreakpointObserver.cjs +1 -2
- package/decorators/withBreakpointObserver.d.ts +1 -1
- package/decorators/withBreakpointObserver.js +1 -2
- package/decorators/withDrag.d.ts +1 -1
- package/decorators/withExtraConfig.d.ts +1 -1
- package/decorators/withFreezedOptions.cjs +12 -3
- package/decorators/withFreezedOptions.d.ts +1 -1
- package/decorators/withFreezedOptions.js +13 -3
- package/decorators/withIntersectionObserver.cjs +0 -1
- package/decorators/withIntersectionObserver.d.ts +1 -1
- package/decorators/withIntersectionObserver.js +0 -1
- package/decorators/withMountWhenInView.cjs +0 -1
- package/decorators/withMountWhenInView.d.ts +1 -1
- package/decorators/withMountWhenInView.js +0 -1
- package/decorators/withResponsiveOptions.cjs +39 -0
- package/decorators/withResponsiveOptions.d.ts +8 -0
- package/decorators/withResponsiveOptions.js +14 -0
- package/decorators/withScrolledInView.cjs +58 -23
- package/decorators/withScrolledInView.d.ts +5 -3
- package/decorators/withScrolledInView.js +59 -24
- package/helpers/createApp.d.ts +3 -26
- package/helpers/getInstanceFromElement.cjs +32 -0
- package/helpers/getInstanceFromElement.d.ts +14 -0
- package/helpers/getInstanceFromElement.js +10 -0
- package/helpers/importOnInteraction.d.ts +3 -5
- package/helpers/importWhenIdle.d.ts +3 -5
- package/helpers/importWhenVisible.d.ts +4 -6
- package/helpers/index.cjs +2 -0
- package/helpers/index.d.ts +1 -0
- package/helpers/index.js +8 -6
- package/helpers/utils.d.ts +3 -4
- package/package.json +2 -1
- package/services/Service.d.ts +9 -9
- package/services/drag.cjs +0 -1
- package/services/drag.d.ts +3 -3
- package/services/drag.js +0 -1
- package/services/key.d.ts +1 -1
- package/services/load.d.ts +1 -1
- package/services/pointer.d.ts +1 -1
- package/services/raf.d.ts +1 -1
- package/services/resize.cjs +2 -2
- package/services/resize.d.ts +1 -1
- package/services/resize.js +2 -2
- package/services/scroll.cjs +3 -1
- package/services/scroll.d.ts +1 -1
- package/services/scroll.js +3 -1
- package/utils/collide/boundingRectToCircle.d.ts +5 -5
- package/utils/collide/collideCircleCircle.d.ts +4 -4
- package/utils/collide/collideCircleRect.d.ts +8 -8
- package/utils/collide/collidePointCircle.d.ts +6 -6
- package/utils/collide/collidePointRect.d.ts +7 -7
- package/utils/collide/collideRectRect.d.ts +5 -5
- package/utils/css/animate.cjs +190 -0
- package/utils/css/animate.d.ts +57 -0
- package/utils/css/animate.js +165 -0
- package/utils/css/classes.cjs +3 -3
- package/utils/css/classes.d.ts +10 -10
- package/utils/css/classes.js +3 -3
- package/utils/css/getOffsetSizes.cjs +47 -0
- package/utils/css/getOffsetSizes.d.ts +15 -0
- package/utils/css/getOffsetSizes.js +25 -0
- package/utils/css/index.cjs +6 -0
- package/utils/css/index.d.ts +3 -0
- package/utils/css/index.js +9 -4
- package/utils/css/matrix.d.ts +14 -15
- package/utils/css/styles.d.ts +2 -2
- package/utils/css/transform.cjs +91 -0
- package/utils/css/transform.d.ts +43 -0
- package/utils/css/transform.js +66 -0
- package/utils/css/transition.cjs +2 -0
- package/utils/css/transition.d.ts +2 -2
- package/utils/css/transition.js +2 -0
- package/utils/debounce.d.ts +4 -4
- package/utils/focusTrap.cjs +1 -1
- package/utils/focusTrap.d.ts +1 -0
- package/utils/focusTrap.js +1 -1
- package/utils/history.d.ts +13 -11
- package/utils/index.cjs +4 -0
- package/utils/index.d.ts +2 -0
- package/utils/index.js +4 -0
- package/utils/isDefined.cjs +28 -0
- package/utils/isDefined.d.ts +7 -0
- package/utils/isDefined.js +6 -0
- package/utils/isFunction.cjs +28 -0
- package/utils/isFunction.d.ts +7 -0
- package/utils/isFunction.js +6 -0
- package/utils/math/clamp.d.ts +1 -1
- package/utils/math/clamp01.d.ts +1 -1
- package/utils/math/damp.d.ts +6 -6
- package/utils/math/ease.d.ts +7 -7
- package/utils/math/index.cjs +2 -0
- package/utils/math/index.d.ts +1 -0
- package/utils/math/index.js +2 -0
- package/utils/math/inertiaFinalValue.d.ts +5 -5
- package/utils/math/lerp.d.ts +4 -4
- package/utils/math/map.d.ts +6 -6
- package/utils/math/round.d.ts +4 -4
- package/utils/memoize.d.ts +1 -1
- package/utils/nextFrame.d.ts +3 -4
- package/utils/nextMicrotask.d.ts +1 -1
- package/utils/nextTick.d.ts +1 -1
- package/utils/object/autoBind.d.ts +3 -3
- package/utils/object/getAllProperties.d.ts +3 -3
- package/utils/object/isObject.d.ts +1 -1
- package/utils/scrollTo.cjs +1 -1
- package/utils/scrollTo.d.ts +1 -1
- package/utils/scrollTo.js +1 -1
- package/utils/string/withLeadingCharacters.d.ts +3 -2
- package/utils/string/withLeadingSlash.d.ts +1 -1
- package/utils/string/withTrailingCharacters.d.ts +3 -2
- package/utils/string/withTrailingSlash.d.ts +1 -1
- package/utils/string/withoutLeadingCharacters.d.ts +3 -2
- package/utils/string/withoutLeadingCharactersRecursive.d.ts +3 -2
- package/utils/string/withoutLeadingSlash.d.ts +1 -1
- package/utils/string/withoutTrailingCharacters.d.ts +3 -2
- package/utils/string/withoutTrailingCharactersRecursive.d.ts +3 -2
- package/utils/string/withoutTrailingSlash.d.ts +1 -1
- package/utils/throttle.cjs +1 -1
- package/utils/throttle.d.ts +3 -3
- package/utils/throttle.js +1 -1
|
@@ -27,19 +27,70 @@ var __publicField = (obj, key, value) => {
|
|
|
27
27
|
// packages/js-toolkit/Base/managers/EventsManager.js
|
|
28
28
|
var EventsManager_exports = {};
|
|
29
29
|
__export(EventsManager_exports, {
|
|
30
|
-
default: () => EventsManager
|
|
30
|
+
default: () => EventsManager,
|
|
31
|
+
normalizeEventName: () => normalizeEventName,
|
|
32
|
+
normalizeName: () => normalizeName
|
|
31
33
|
});
|
|
32
34
|
module.exports = __toCommonJS(EventsManager_exports);
|
|
33
35
|
var import_getAllProperties = __toESM(require("../../utils/object/getAllProperties.cjs"), 1);
|
|
34
|
-
var
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
var import_AbstractManager = __toESM(require("./AbstractManager.cjs"), 1);
|
|
37
|
+
var import_RefsManager = require("./RefsManager.cjs");
|
|
38
|
+
function normalizeName(name) {
|
|
39
|
+
return name.replace(/[A-Z]([A-Z].*)/g, (c) => c.toLowerCase()).replace(/[^a-zA-Z\d\s:]/g, " ").replace(/(^\w|\s+\w)/g, (c) => c.trim().toUpperCase()).trim();
|
|
40
|
+
}
|
|
41
|
+
function normalizeEventName(name) {
|
|
42
|
+
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`).replace(/^-/, "");
|
|
43
|
+
}
|
|
44
|
+
function __getEventNameByMethod(that, method, name = "") {
|
|
45
|
+
const normalizedName = normalizeName(name);
|
|
46
|
+
const regex = new RegExp(`^on${normalizedName}([A-Z].*)$`);
|
|
47
|
+
const [, event] = method.match(regex);
|
|
48
|
+
return normalizeEventName(event);
|
|
49
|
+
}
|
|
50
|
+
function __getEventMethodsByName(that, name = "") {
|
|
51
|
+
const normalizedName = normalizeName(name);
|
|
52
|
+
const regex = new RegExp(`^on${normalizedName}[A-Z].*$`);
|
|
53
|
+
const key = regex.toString();
|
|
54
|
+
let methods = that.__methodsCache.get(key);
|
|
55
|
+
if (!methods) {
|
|
56
|
+
methods = Array.from((0, import_getAllProperties.default)(that.__base, [], (method) => regex.test(method)).reduce((set, [method]) => set.add(method), /* @__PURE__ */ new Set()));
|
|
57
|
+
that.__methodsCache.set(key, methods);
|
|
58
|
+
}
|
|
59
|
+
return methods;
|
|
60
|
+
}
|
|
61
|
+
function __manageRef(that, name, elements, mode = "add") {
|
|
62
|
+
const action = `${mode}EventListener`;
|
|
63
|
+
const methods = __getEventMethodsByName(that, name);
|
|
64
|
+
methods.forEach((method) => {
|
|
65
|
+
const event = __getEventNameByMethod(that, method, name);
|
|
66
|
+
elements.forEach((element) => element[action](event, that.__refsHandler));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function __manageChild(that, name, instance, mode = "add") {
|
|
70
|
+
const action = mode === "add" ? "$on" : "$off";
|
|
71
|
+
const methods = __getEventMethodsByName(that, name);
|
|
72
|
+
methods.forEach((method) => {
|
|
73
|
+
const event = __getEventNameByMethod(that, method, name);
|
|
74
|
+
instance[action](event, that.__childrenHandler);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function __manageRootElement(that, mode = "add") {
|
|
78
|
+
const modeMethod = `${mode}EventListener`;
|
|
79
|
+
const methods = __getEventMethodsByName(that);
|
|
80
|
+
const { emits = [] } = that.__base.__config;
|
|
81
|
+
methods.forEach((method) => {
|
|
82
|
+
const event = __getEventNameByMethod(that, method);
|
|
83
|
+
const target = emits.includes(event) ? that.__base : that.__element;
|
|
84
|
+
target[modeMethod](event, that.__rootElementHandler);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
var EventsManager = class extends import_AbstractManager.default {
|
|
88
|
+
constructor(base) {
|
|
89
|
+
super(base);
|
|
39
90
|
__publicField(this, "__methodsCache", /* @__PURE__ */ new Map());
|
|
40
91
|
__publicField(this, "__rootElementHandler", {
|
|
41
92
|
handleEvent: (event) => {
|
|
42
|
-
const normalizedEventName =
|
|
93
|
+
const normalizedEventName = normalizeName(event.type);
|
|
43
94
|
const method = `on${normalizedEventName}`;
|
|
44
95
|
this.__base[method](event);
|
|
45
96
|
}
|
|
@@ -47,9 +98,9 @@ var EventsManager = class {
|
|
|
47
98
|
__publicField(this, "__refsHandler", {
|
|
48
99
|
handleEvent: (event) => {
|
|
49
100
|
const ref = event.currentTarget;
|
|
50
|
-
const refName = import_RefsManager.
|
|
51
|
-
const normalizedRefName =
|
|
52
|
-
const normalizedEventName =
|
|
101
|
+
const refName = (0, import_RefsManager.normalizeRefName)(ref.dataset.ref);
|
|
102
|
+
const normalizedRefName = normalizeName(refName);
|
|
103
|
+
const normalizedEventName = normalizeName(event.type);
|
|
53
104
|
const method = `on${normalizedRefName}${normalizedEventName}`;
|
|
54
105
|
let index = 0;
|
|
55
106
|
if (Array.isArray(this.__base.$refs[refName])) {
|
|
@@ -72,112 +123,37 @@ var EventsManager = class {
|
|
|
72
123
|
}
|
|
73
124
|
return false;
|
|
74
125
|
});
|
|
75
|
-
const normalizedRefName =
|
|
76
|
-
const normalizedEventName =
|
|
126
|
+
const normalizedRefName = normalizeName(childName);
|
|
127
|
+
const normalizedEventName = normalizeName(event.type);
|
|
77
128
|
const method = `on${normalizedRefName}${normalizedEventName}`;
|
|
78
129
|
const args = Array.isArray(event.detail) ? event.detail : [];
|
|
79
130
|
this.__base[method](...args, index, event);
|
|
80
131
|
}
|
|
81
132
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
__base: {
|
|
89
|
-
enumerable: false,
|
|
90
|
-
writable: false,
|
|
91
|
-
value: base
|
|
92
|
-
},
|
|
93
|
-
__methodsCache: {
|
|
94
|
-
enumerable: false,
|
|
95
|
-
writable: false,
|
|
96
|
-
value: this.__methodsCache
|
|
97
|
-
},
|
|
98
|
-
__rootElementHandler: {
|
|
99
|
-
enumerable: false,
|
|
100
|
-
writable: false,
|
|
101
|
-
value: this.__rootElementHandler
|
|
102
|
-
},
|
|
103
|
-
__refsHandler: {
|
|
104
|
-
enumerable: false,
|
|
105
|
-
writable: false,
|
|
106
|
-
value: this.__refsHandler
|
|
107
|
-
},
|
|
108
|
-
__childrenHandler: {
|
|
109
|
-
enumerable: false,
|
|
110
|
-
writable: false,
|
|
111
|
-
value: this.__childrenHandler
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
__manageRef(name, elements, mode = "add") {
|
|
116
|
-
const action = `${mode}EventListener`;
|
|
117
|
-
const methods = this.__getEventMethodsByName(name);
|
|
118
|
-
methods.forEach((method) => {
|
|
119
|
-
const event = this.__getEventNameByMethod(method, name);
|
|
120
|
-
elements.forEach((element) => element[action](event, this.__refsHandler));
|
|
121
|
-
});
|
|
133
|
+
this.__hideProperties([
|
|
134
|
+
"__methodsCache",
|
|
135
|
+
"__rootElementHandler",
|
|
136
|
+
"__refsHandler",
|
|
137
|
+
"__childrenHandler"
|
|
138
|
+
]);
|
|
122
139
|
}
|
|
123
140
|
bindRef(name, elements) {
|
|
124
|
-
|
|
141
|
+
__manageRef(this, name, elements);
|
|
125
142
|
}
|
|
126
143
|
unbindRef(name, elements) {
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
__manageChild(name, instance, mode = "add") {
|
|
130
|
-
const action = mode === "add" ? "$on" : "$off";
|
|
131
|
-
const methods = this.__getEventMethodsByName(name);
|
|
132
|
-
methods.forEach((method) => {
|
|
133
|
-
const event = this.__getEventNameByMethod(method, name);
|
|
134
|
-
instance[action](event, this.__childrenHandler);
|
|
135
|
-
});
|
|
144
|
+
__manageRef(this, name, elements, "remove");
|
|
136
145
|
}
|
|
137
146
|
bindChild(name, instance) {
|
|
138
|
-
|
|
147
|
+
__manageChild(this, name, instance);
|
|
139
148
|
}
|
|
140
149
|
unbindChild(name, instance) {
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
__manageRootElement(mode = "add") {
|
|
144
|
-
const modeMethod = `${mode}EventListener`;
|
|
145
|
-
const methods = this.__getEventMethodsByName();
|
|
146
|
-
const { emits = [] } = this.__base.__config;
|
|
147
|
-
methods.forEach((method) => {
|
|
148
|
-
const event = this.__getEventNameByMethod(method);
|
|
149
|
-
const target = emits.includes(event) ? this.__base : this.__rootElement;
|
|
150
|
-
target[modeMethod](event, this.__rootElementHandler);
|
|
151
|
-
});
|
|
150
|
+
__manageChild(this, name, instance, "remove");
|
|
152
151
|
}
|
|
153
152
|
bindRootElement() {
|
|
154
|
-
|
|
153
|
+
__manageRootElement(this);
|
|
155
154
|
}
|
|
156
155
|
unbindRootElement() {
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
__getEventNameByMethod(method, name = "") {
|
|
160
|
-
const normalizedName = EventsManager.normalizeName(name);
|
|
161
|
-
const regex = new RegExp(`^on${normalizedName}([A-Z].*)$`);
|
|
162
|
-
const [, event] = method.match(regex);
|
|
163
|
-
return EventsManager.normalizeEventName(event);
|
|
164
|
-
}
|
|
165
|
-
__getEventMethodsByName(name = "") {
|
|
166
|
-
const normalizedName = EventsManager.normalizeName(name);
|
|
167
|
-
const regex = new RegExp(`^on${normalizedName}[A-Z].*$`);
|
|
168
|
-
const key = regex.toString();
|
|
169
|
-
let methods = this.__methodsCache.get(key);
|
|
170
|
-
if (!methods) {
|
|
171
|
-
methods = Array.from((0, import_getAllProperties.default)(this.__base, [], (method) => regex.test(method)).reduce((set, [method]) => set.add(method), /* @__PURE__ */ new Set()));
|
|
172
|
-
this.__methodsCache.set(key, methods);
|
|
173
|
-
}
|
|
174
|
-
return methods;
|
|
175
|
-
}
|
|
176
|
-
static normalizeName(name) {
|
|
177
|
-
return name.replace(/[A-Z]([A-Z].*)/g, (c) => c.toLowerCase()).replace(/[^a-zA-Z\d\s:]/g, " ").replace(/(^\w|\s+\w)/g, (c) => c.trim().toUpperCase()).trim();
|
|
178
|
-
}
|
|
179
|
-
static normalizeEventName(name) {
|
|
180
|
-
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`).replace(/^-/, "");
|
|
156
|
+
__manageRootElement(this, "remove");
|
|
181
157
|
}
|
|
182
158
|
};
|
|
183
159
|
if (module.exports.default) module.exports = module.exports.default;
|
|
@@ -2,62 +2,44 @@
|
|
|
2
2
|
* @typedef {import('../index.js').default} Base
|
|
3
3
|
* @typedef {import('./ChildrenManager.js').default} ChildrenManager
|
|
4
4
|
*/
|
|
5
|
+
/**
|
|
6
|
+
* Normalize the given name to PascalCase, such as:
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* sentence case => SentenceCase
|
|
10
|
+
* lowercase => Lowercase
|
|
11
|
+
* UPPERCASE => Uppercase
|
|
12
|
+
* kebab-case => KebabCase
|
|
13
|
+
* snake_case => SnakeCase
|
|
14
|
+
* camelCase => CamelCase
|
|
15
|
+
* PascalCase => PascalCase
|
|
16
|
+
* .class-selector => ClassSelector
|
|
17
|
+
* .bem__selector => BemSelector
|
|
18
|
+
* __id-selector => IdSelector
|
|
19
|
+
* .complex[class^ ="__"] => ComplexClass
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param {string} name
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
export function normalizeName(name: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Normalize the event names from PascalCase to kebab-case.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} name
|
|
30
|
+
* @returns {string}
|
|
31
|
+
*/
|
|
32
|
+
export function normalizeEventName(name: string): string;
|
|
5
33
|
/**
|
|
6
34
|
* Event management class.
|
|
7
35
|
* @todo Prevent binding of `onChildOrRefEvent` to the root element
|
|
8
36
|
* @todo Use event delegation?
|
|
9
37
|
*/
|
|
10
|
-
export default class EventsManager {
|
|
11
|
-
/**
|
|
12
|
-
* Normalize the given name to PascalCase, such as:
|
|
13
|
-
*
|
|
14
|
-
* ```
|
|
15
|
-
* sentence case => SentenceCase
|
|
16
|
-
* lowercase => Lowercase
|
|
17
|
-
* UPPERCASE => Uppercase
|
|
18
|
-
* kebab-case => KebabCase
|
|
19
|
-
* snake_case => SnakeCase
|
|
20
|
-
* camelCase => CamelCase
|
|
21
|
-
* PascalCase => PascalCase
|
|
22
|
-
* .class-selector => ClassSelector
|
|
23
|
-
* .bem__selector => BemSelector
|
|
24
|
-
* __id-selector => IdSelector
|
|
25
|
-
* .complex[class^ ="__"] => ComplexClass
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @param {string} name
|
|
29
|
-
* @return {string}
|
|
30
|
-
*/
|
|
31
|
-
static normalizeName(name: string): string;
|
|
32
|
-
/**
|
|
33
|
-
* Normalize the event names from PascalCase to kebab-case.
|
|
34
|
-
*
|
|
35
|
-
* @param {string} name
|
|
36
|
-
* @return {string}
|
|
37
|
-
*/
|
|
38
|
-
static normalizeEventName(name: string): string;
|
|
39
|
-
/**
|
|
40
|
-
* Class constructor.
|
|
41
|
-
*
|
|
42
|
-
* @param {HTMLElement} rootElement
|
|
43
|
-
* @param {Base} base
|
|
44
|
-
*/
|
|
45
|
-
constructor(rootElement: HTMLElement, base: Base);
|
|
46
|
-
/**
|
|
47
|
-
* @type {HTMLElement}
|
|
48
|
-
* @private
|
|
49
|
-
*/
|
|
50
|
-
private __rootElement;
|
|
51
|
-
/**
|
|
52
|
-
* @type {Base}
|
|
53
|
-
* @private
|
|
54
|
-
*/
|
|
55
|
-
private __base;
|
|
38
|
+
export default class EventsManager extends AbstractManager {
|
|
56
39
|
/**
|
|
57
40
|
* @type {WeakMap}
|
|
58
|
-
* @private
|
|
59
41
|
*/
|
|
60
|
-
|
|
42
|
+
__methodsCache: WeakMap<any, any>;
|
|
61
43
|
/**
|
|
62
44
|
* Event listener object for the root element.
|
|
63
45
|
*
|
|
@@ -76,20 +58,6 @@ export default class EventsManager {
|
|
|
76
58
|
* @type {EventListenerObject}
|
|
77
59
|
*/
|
|
78
60
|
__childrenHandler: EventListenerObject;
|
|
79
|
-
/**
|
|
80
|
-
* Manage a ref event methods.
|
|
81
|
-
*
|
|
82
|
-
* @param {string} name
|
|
83
|
-
* The name of the ref.
|
|
84
|
-
* @param {HTMLElement[]} elements
|
|
85
|
-
* The elements of the ref.
|
|
86
|
-
* @param {'add'|'remove'} [mode]
|
|
87
|
-
* The action to perform: add or remove the event listeners.
|
|
88
|
-
*
|
|
89
|
-
* @return {void}
|
|
90
|
-
* @private
|
|
91
|
-
*/
|
|
92
|
-
private __manageRef;
|
|
93
61
|
/**
|
|
94
62
|
* Bind event methods to the given ref elements.
|
|
95
63
|
*
|
|
@@ -97,8 +65,7 @@ export default class EventsManager {
|
|
|
97
65
|
* The name of the ref.
|
|
98
66
|
* @param {HTMLElement[]} elements
|
|
99
67
|
* The elements of the ref.
|
|
100
|
-
*
|
|
101
|
-
* @return {void}
|
|
68
|
+
* @returns {void}
|
|
102
69
|
*/
|
|
103
70
|
bindRef(name: string, elements: HTMLElement[]): void;
|
|
104
71
|
/**
|
|
@@ -108,24 +75,9 @@ export default class EventsManager {
|
|
|
108
75
|
* The name of the ref.
|
|
109
76
|
* @param {HTMLElement[]} elements
|
|
110
77
|
* The elements of the ref.
|
|
111
|
-
*
|
|
112
|
-
* @return {void}
|
|
78
|
+
* @returns {void}
|
|
113
79
|
*/
|
|
114
80
|
unbindRef(name: string, elements: HTMLElement[]): void;
|
|
115
|
-
/**
|
|
116
|
-
* Manage event methods to the given child instance.
|
|
117
|
-
*
|
|
118
|
-
* @param {string} name
|
|
119
|
-
* The name of the ref.
|
|
120
|
-
* @param {Base} instance
|
|
121
|
-
* A base instance.
|
|
122
|
-
* @param {'add'|'remove'} [mode]
|
|
123
|
-
* The action to perform: add or remove the event listeners.
|
|
124
|
-
*
|
|
125
|
-
* @return {void}
|
|
126
|
-
* @private
|
|
127
|
-
*/
|
|
128
|
-
private __manageChild;
|
|
129
81
|
/**
|
|
130
82
|
* Bind event methods to the given child instance.
|
|
131
83
|
*
|
|
@@ -133,8 +85,7 @@ export default class EventsManager {
|
|
|
133
85
|
* The name of the ref.
|
|
134
86
|
* @param {Base} instance
|
|
135
87
|
* A base instance.
|
|
136
|
-
*
|
|
137
|
-
* @return {void}
|
|
88
|
+
* @returns {void}
|
|
138
89
|
*/
|
|
139
90
|
bindChild(name: string, instance: Base): void;
|
|
140
91
|
/**
|
|
@@ -144,49 +95,22 @@ export default class EventsManager {
|
|
|
144
95
|
* The name of the ref.
|
|
145
96
|
* @param {Base} instance
|
|
146
97
|
* A base instance.
|
|
147
|
-
*
|
|
148
|
-
* @return {void}
|
|
98
|
+
* @returns {void}
|
|
149
99
|
*/
|
|
150
100
|
unbindChild(name: string, instance: Base): void;
|
|
151
|
-
/**
|
|
152
|
-
* Manage event methods on the root element.
|
|
153
|
-
*
|
|
154
|
-
* @param {'add'|'remove'} [mode]
|
|
155
|
-
* The action to perform: add or remove the event listeners, defaults to 'add'.
|
|
156
|
-
*
|
|
157
|
-
* @return {void}
|
|
158
|
-
* @private
|
|
159
|
-
*/
|
|
160
|
-
private __manageRootElement;
|
|
161
101
|
/**
|
|
162
102
|
* Bind event methods on the root element.
|
|
163
103
|
*
|
|
164
|
-
* @
|
|
104
|
+
* @returns {void}
|
|
165
105
|
*/
|
|
166
106
|
bindRootElement(): void;
|
|
167
107
|
/**
|
|
168
108
|
* Unbind event method from the root element.
|
|
169
109
|
*
|
|
170
|
-
* @
|
|
110
|
+
* @returns {void}
|
|
171
111
|
*/
|
|
172
112
|
unbindRootElement(): void;
|
|
173
|
-
/**
|
|
174
|
-
* Get the event name from the given method and its tied ref or children name.
|
|
175
|
-
*
|
|
176
|
-
* @param {string} method
|
|
177
|
-
* @param {string} name
|
|
178
|
-
* @return {string}
|
|
179
|
-
* @private
|
|
180
|
-
*/
|
|
181
|
-
private __getEventNameByMethod;
|
|
182
|
-
/**
|
|
183
|
-
* Get the base instance methods matching the `onSomething` pattern.
|
|
184
|
-
*
|
|
185
|
-
* @param {string} [name]
|
|
186
|
-
* @return {string[]}
|
|
187
|
-
* @private
|
|
188
|
-
*/
|
|
189
|
-
private __getEventMethodsByName;
|
|
190
113
|
}
|
|
191
114
|
export type Base = import('../index.js').default;
|
|
192
115
|
export type ChildrenManager = import('./ChildrenManager.js').default;
|
|
116
|
+
import AbstractManager from "./AbstractManager.js";
|
|
@@ -5,15 +5,64 @@ var __publicField = (obj, key, value) => {
|
|
|
5
5
|
return value;
|
|
6
6
|
};
|
|
7
7
|
import getAllProperties from "../../utils/object/getAllProperties.js";
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
import AbstractManager from "./AbstractManager.js";
|
|
9
|
+
import { normalizeRefName } from "./RefsManager.js";
|
|
10
|
+
function normalizeName(name) {
|
|
11
|
+
return name.replace(/[A-Z]([A-Z].*)/g, (c) => c.toLowerCase()).replace(/[^a-zA-Z\d\s:]/g, " ").replace(/(^\w|\s+\w)/g, (c) => c.trim().toUpperCase()).trim();
|
|
12
|
+
}
|
|
13
|
+
function normalizeEventName(name) {
|
|
14
|
+
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`).replace(/^-/, "");
|
|
15
|
+
}
|
|
16
|
+
function __getEventNameByMethod(that, method, name = "") {
|
|
17
|
+
const normalizedName = normalizeName(name);
|
|
18
|
+
const regex = new RegExp(`^on${normalizedName}([A-Z].*)$`);
|
|
19
|
+
const [, event] = method.match(regex);
|
|
20
|
+
return normalizeEventName(event);
|
|
21
|
+
}
|
|
22
|
+
function __getEventMethodsByName(that, name = "") {
|
|
23
|
+
const normalizedName = normalizeName(name);
|
|
24
|
+
const regex = new RegExp(`^on${normalizedName}[A-Z].*$`);
|
|
25
|
+
const key = regex.toString();
|
|
26
|
+
let methods = that.__methodsCache.get(key);
|
|
27
|
+
if (!methods) {
|
|
28
|
+
methods = Array.from(getAllProperties(that.__base, [], (method) => regex.test(method)).reduce((set, [method]) => set.add(method), /* @__PURE__ */ new Set()));
|
|
29
|
+
that.__methodsCache.set(key, methods);
|
|
30
|
+
}
|
|
31
|
+
return methods;
|
|
32
|
+
}
|
|
33
|
+
function __manageRef(that, name, elements, mode = "add") {
|
|
34
|
+
const action = `${mode}EventListener`;
|
|
35
|
+
const methods = __getEventMethodsByName(that, name);
|
|
36
|
+
methods.forEach((method) => {
|
|
37
|
+
const event = __getEventNameByMethod(that, method, name);
|
|
38
|
+
elements.forEach((element) => element[action](event, that.__refsHandler));
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function __manageChild(that, name, instance, mode = "add") {
|
|
42
|
+
const action = mode === "add" ? "$on" : "$off";
|
|
43
|
+
const methods = __getEventMethodsByName(that, name);
|
|
44
|
+
methods.forEach((method) => {
|
|
45
|
+
const event = __getEventNameByMethod(that, method, name);
|
|
46
|
+
instance[action](event, that.__childrenHandler);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function __manageRootElement(that, mode = "add") {
|
|
50
|
+
const modeMethod = `${mode}EventListener`;
|
|
51
|
+
const methods = __getEventMethodsByName(that);
|
|
52
|
+
const { emits = [] } = that.__base.__config;
|
|
53
|
+
methods.forEach((method) => {
|
|
54
|
+
const event = __getEventNameByMethod(that, method);
|
|
55
|
+
const target = emits.includes(event) ? that.__base : that.__element;
|
|
56
|
+
target[modeMethod](event, that.__rootElementHandler);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
class EventsManager extends AbstractManager {
|
|
60
|
+
constructor(base) {
|
|
61
|
+
super(base);
|
|
13
62
|
__publicField(this, "__methodsCache", /* @__PURE__ */ new Map());
|
|
14
63
|
__publicField(this, "__rootElementHandler", {
|
|
15
64
|
handleEvent: (event) => {
|
|
16
|
-
const normalizedEventName =
|
|
65
|
+
const normalizedEventName = normalizeName(event.type);
|
|
17
66
|
const method = `on${normalizedEventName}`;
|
|
18
67
|
this.__base[method](event);
|
|
19
68
|
}
|
|
@@ -21,9 +70,9 @@ class EventsManager {
|
|
|
21
70
|
__publicField(this, "__refsHandler", {
|
|
22
71
|
handleEvent: (event) => {
|
|
23
72
|
const ref = event.currentTarget;
|
|
24
|
-
const refName =
|
|
25
|
-
const normalizedRefName =
|
|
26
|
-
const normalizedEventName =
|
|
73
|
+
const refName = normalizeRefName(ref.dataset.ref);
|
|
74
|
+
const normalizedRefName = normalizeName(refName);
|
|
75
|
+
const normalizedEventName = normalizeName(event.type);
|
|
27
76
|
const method = `on${normalizedRefName}${normalizedEventName}`;
|
|
28
77
|
let index = 0;
|
|
29
78
|
if (Array.isArray(this.__base.$refs[refName])) {
|
|
@@ -46,114 +95,41 @@ class EventsManager {
|
|
|
46
95
|
}
|
|
47
96
|
return false;
|
|
48
97
|
});
|
|
49
|
-
const normalizedRefName =
|
|
50
|
-
const normalizedEventName =
|
|
98
|
+
const normalizedRefName = normalizeName(childName);
|
|
99
|
+
const normalizedEventName = normalizeName(event.type);
|
|
51
100
|
const method = `on${normalizedRefName}${normalizedEventName}`;
|
|
52
101
|
const args = Array.isArray(event.detail) ? event.detail : [];
|
|
53
102
|
this.__base[method](...args, index, event);
|
|
54
103
|
}
|
|
55
104
|
});
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
__base: {
|
|
63
|
-
enumerable: false,
|
|
64
|
-
writable: false,
|
|
65
|
-
value: base
|
|
66
|
-
},
|
|
67
|
-
__methodsCache: {
|
|
68
|
-
enumerable: false,
|
|
69
|
-
writable: false,
|
|
70
|
-
value: this.__methodsCache
|
|
71
|
-
},
|
|
72
|
-
__rootElementHandler: {
|
|
73
|
-
enumerable: false,
|
|
74
|
-
writable: false,
|
|
75
|
-
value: this.__rootElementHandler
|
|
76
|
-
},
|
|
77
|
-
__refsHandler: {
|
|
78
|
-
enumerable: false,
|
|
79
|
-
writable: false,
|
|
80
|
-
value: this.__refsHandler
|
|
81
|
-
},
|
|
82
|
-
__childrenHandler: {
|
|
83
|
-
enumerable: false,
|
|
84
|
-
writable: false,
|
|
85
|
-
value: this.__childrenHandler
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
__manageRef(name, elements, mode = "add") {
|
|
90
|
-
const action = `${mode}EventListener`;
|
|
91
|
-
const methods = this.__getEventMethodsByName(name);
|
|
92
|
-
methods.forEach((method) => {
|
|
93
|
-
const event = this.__getEventNameByMethod(method, name);
|
|
94
|
-
elements.forEach((element) => element[action](event, this.__refsHandler));
|
|
95
|
-
});
|
|
105
|
+
this.__hideProperties([
|
|
106
|
+
"__methodsCache",
|
|
107
|
+
"__rootElementHandler",
|
|
108
|
+
"__refsHandler",
|
|
109
|
+
"__childrenHandler"
|
|
110
|
+
]);
|
|
96
111
|
}
|
|
97
112
|
bindRef(name, elements) {
|
|
98
|
-
|
|
113
|
+
__manageRef(this, name, elements);
|
|
99
114
|
}
|
|
100
115
|
unbindRef(name, elements) {
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
__manageChild(name, instance, mode = "add") {
|
|
104
|
-
const action = mode === "add" ? "$on" : "$off";
|
|
105
|
-
const methods = this.__getEventMethodsByName(name);
|
|
106
|
-
methods.forEach((method) => {
|
|
107
|
-
const event = this.__getEventNameByMethod(method, name);
|
|
108
|
-
instance[action](event, this.__childrenHandler);
|
|
109
|
-
});
|
|
116
|
+
__manageRef(this, name, elements, "remove");
|
|
110
117
|
}
|
|
111
118
|
bindChild(name, instance) {
|
|
112
|
-
|
|
119
|
+
__manageChild(this, name, instance);
|
|
113
120
|
}
|
|
114
121
|
unbindChild(name, instance) {
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
__manageRootElement(mode = "add") {
|
|
118
|
-
const modeMethod = `${mode}EventListener`;
|
|
119
|
-
const methods = this.__getEventMethodsByName();
|
|
120
|
-
const { emits = [] } = this.__base.__config;
|
|
121
|
-
methods.forEach((method) => {
|
|
122
|
-
const event = this.__getEventNameByMethod(method);
|
|
123
|
-
const target = emits.includes(event) ? this.__base : this.__rootElement;
|
|
124
|
-
target[modeMethod](event, this.__rootElementHandler);
|
|
125
|
-
});
|
|
122
|
+
__manageChild(this, name, instance, "remove");
|
|
126
123
|
}
|
|
127
124
|
bindRootElement() {
|
|
128
|
-
|
|
125
|
+
__manageRootElement(this);
|
|
129
126
|
}
|
|
130
127
|
unbindRootElement() {
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
__getEventNameByMethod(method, name = "") {
|
|
134
|
-
const normalizedName = EventsManager.normalizeName(name);
|
|
135
|
-
const regex = new RegExp(`^on${normalizedName}([A-Z].*)$`);
|
|
136
|
-
const [, event] = method.match(regex);
|
|
137
|
-
return EventsManager.normalizeEventName(event);
|
|
138
|
-
}
|
|
139
|
-
__getEventMethodsByName(name = "") {
|
|
140
|
-
const normalizedName = EventsManager.normalizeName(name);
|
|
141
|
-
const regex = new RegExp(`^on${normalizedName}[A-Z].*$`);
|
|
142
|
-
const key = regex.toString();
|
|
143
|
-
let methods = this.__methodsCache.get(key);
|
|
144
|
-
if (!methods) {
|
|
145
|
-
methods = Array.from(getAllProperties(this.__base, [], (method) => regex.test(method)).reduce((set, [method]) => set.add(method), /* @__PURE__ */ new Set()));
|
|
146
|
-
this.__methodsCache.set(key, methods);
|
|
147
|
-
}
|
|
148
|
-
return methods;
|
|
149
|
-
}
|
|
150
|
-
static normalizeName(name) {
|
|
151
|
-
return name.replace(/[A-Z]([A-Z].*)/g, (c) => c.toLowerCase()).replace(/[^a-zA-Z\d\s:]/g, " ").replace(/(^\w|\s+\w)/g, (c) => c.trim().toUpperCase()).trim();
|
|
152
|
-
}
|
|
153
|
-
static normalizeEventName(name) {
|
|
154
|
-
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`).replace(/^-/, "");
|
|
128
|
+
__manageRootElement(this, "remove");
|
|
155
129
|
}
|
|
156
130
|
}
|
|
157
131
|
export {
|
|
158
|
-
EventsManager as default
|
|
132
|
+
EventsManager as default,
|
|
133
|
+
normalizeEventName,
|
|
134
|
+
normalizeName
|
|
159
135
|
};
|