@studiometa/js-toolkit 1.7.2 → 2.0.0-alpha.10
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.d.ts +217 -0
- package/Base/index.js +237 -0
- package/Base/managers/ChildrenManager.d.ts +108 -0
- package/Base/managers/ChildrenManager.js +124 -0
- package/Base/managers/EventsManager.d.ts +190 -0
- package/Base/managers/EventsManager.js +195 -0
- package/Base/managers/OptionsManager.d.ts +120 -0
- package/Base/managers/OptionsManager.js +180 -0
- package/Base/managers/RefsManager.d.ts +84 -0
- package/Base/managers/RefsManager.js +83 -0
- package/{abstracts/Base/classes/Services.d.ts → Base/managers/ServicesManager.d.ts} +22 -7
- package/Base/managers/ServicesManager.js +115 -0
- package/{abstracts/Base → Base}/utils.d.ts +0 -30
- package/Base/utils.js +43 -0
- package/README.md +12 -70
- package/decorators/index.js +1 -2
- package/decorators/withBreakpointManager.d.ts +5 -5
- package/decorators/withBreakpointManager.js +41 -79
- package/decorators/withBreakpointObserver.d.ts +4 -4
- package/decorators/withBreakpointObserver.js +54 -77
- package/decorators/withDrag.d.ts +6 -6
- package/decorators/withDrag.js +17 -41
- package/decorators/withExtraConfig.d.ts +6 -6
- package/decorators/withExtraConfig.js +2 -24
- package/decorators/withIntersectionObserver.d.ts +4 -4
- package/decorators/withIntersectionObserver.js +17 -60
- package/decorators/withMountWhenInView.d.ts +8 -8
- package/decorators/withMountWhenInView.js +23 -51
- package/helpers/createApp.d.ts +4 -4
- package/helpers/createApp.js +10 -12
- package/helpers/importOnInteraction.d.ts +6 -6
- package/helpers/importOnInteraction.js +7 -8
- package/helpers/importWhenIdle.d.ts +4 -4
- package/helpers/importWhenIdle.js +7 -8
- package/helpers/importWhenVisible.d.ts +6 -6
- package/helpers/importWhenVisible.js +9 -16
- package/helpers/index.d.ts +1 -3
- package/helpers/index.js +1 -4
- package/helpers/utils.js +2 -3
- package/index.d.ts +19 -5
- package/index.js +5 -6
- package/index.modern.js +1 -3230
- package/index.umd.js +1 -3586
- package/package.json +3 -6
- package/{abstracts → services}/Service.d.ts +0 -0
- package/services/Service.js +63 -0
- package/services/drag.js +144 -180
- package/services/index.js +1 -2
- package/services/key.js +56 -98
- package/services/load.d.ts +1 -1
- package/services/load.js +22 -51
- package/services/pointer.js +118 -157
- package/services/raf.js +37 -65
- package/services/resize.js +46 -98
- package/services/scroll.js +81 -120
- package/utils/collide/boundingRectToCircle.js +7 -6
- package/utils/collide/collideCircleCircle.js +1 -2
- package/utils/collide/collideCircleRect.js +1 -2
- package/utils/collide/collidePointCircle.js +1 -2
- package/utils/collide/collidePointRect.js +1 -2
- package/utils/collide/collideRectRect.js +1 -2
- package/utils/collide/index.js +1 -2
- package/utils/css/classes.js +2 -3
- package/utils/css/index.js +1 -2
- package/utils/css/matrix.js +1 -2
- package/utils/css/styles.js +3 -8
- package/utils/css/transition.js +67 -140
- package/utils/debounce.js +3 -4
- package/utils/focusTrap.js +4 -5
- package/utils/history.js +12 -19
- package/utils/index.d.ts +2 -1
- package/utils/index.js +2 -2
- package/utils/keyCodes.js +1 -2
- package/utils/math/clamp.js +1 -2
- package/utils/math/clamp01.js +1 -2
- package/utils/math/damp.js +1 -2
- package/utils/math/index.js +1 -2
- package/utils/math/inertiaFinalValue.js +1 -2
- package/utils/math/lerp.js +1 -2
- package/utils/math/map.js +1 -2
- package/utils/math/round.js +1 -2
- package/utils/memoize.d.ts +30 -0
- package/utils/memoize.js +30 -0
- package/utils/nextFrame.js +4 -9
- package/utils/object/autoBind.js +11 -19
- package/utils/object/getAllProperties.d.ts +6 -3
- package/utils/object/getAllProperties.js +10 -8
- package/utils/object/index.js +1 -2
- package/utils/object/isObject.js +2 -4
- package/utils/string/index.js +1 -2
- package/utils/string/withLeadingCharacters.js +1 -2
- package/utils/string/withLeadingSlash.js +1 -2
- package/utils/string/withTrailingCharacters.js +1 -2
- package/utils/string/withTrailingSlash.js +1 -2
- package/utils/string/withoutLeadingCharacters.js +1 -2
- package/utils/string/withoutLeadingCharactersRecursive.js +1 -2
- package/utils/string/withoutLeadingSlash.js +1 -2
- package/utils/string/withoutTrailingCharacters.js +1 -2
- package/utils/string/withoutTrailingCharactersRecursive.js +1 -2
- package/utils/string/withoutTrailingSlash.js +1 -2
- package/utils/throttle.js +2 -3
- package/abstracts/Base/children.d.ts +0 -16
- package/abstracts/Base/children.js +0 -66
- package/abstracts/Base/children.js.map +0 -1
- package/abstracts/Base/classes/Options.d.ts +0 -37
- package/abstracts/Base/classes/Options.js +0 -182
- package/abstracts/Base/classes/Options.js.map +0 -1
- package/abstracts/Base/classes/Services.js +0 -120
- package/abstracts/Base/classes/Services.js.map +0 -1
- package/abstracts/Base/components.d.ts +0 -22
- package/abstracts/Base/components.js +0 -67
- package/abstracts/Base/components.js.map +0 -1
- package/abstracts/Base/events.d.ts +0 -8
- package/abstracts/Base/events.js +0 -185
- package/abstracts/Base/events.js.map +0 -1
- package/abstracts/Base/index.d.ts +0 -176
- package/abstracts/Base/index.js +0 -215
- package/abstracts/Base/index.js.map +0 -1
- package/abstracts/Base/options.d.ts +0 -17
- package/abstracts/Base/options.js +0 -112
- package/abstracts/Base/options.js.map +0 -1
- package/abstracts/Base/refs.d.ts +0 -30
- package/abstracts/Base/refs.js +0 -60
- package/abstracts/Base/refs.js.map +0 -1
- package/abstracts/Base/utils.js +0 -102
- package/abstracts/Base/utils.js.map +0 -1
- package/abstracts/EventManager.d.ts +0 -41
- package/abstracts/EventManager.js +0 -88
- package/abstracts/EventManager.js.map +0 -1
- package/abstracts/Service.js +0 -82
- package/abstracts/Service.js.map +0 -1
- package/abstracts/index.d.ts +0 -4
- package/abstracts/index.js +0 -5
- package/abstracts/index.js.map +0 -1
- package/components/Accordion/Accordion.d.ts +0 -83
- package/components/Accordion/Accordion.js +0 -121
- package/components/Accordion/Accordion.js.map +0 -1
- package/components/Accordion/AccordionItem.d.ts +0 -122
- package/components/Accordion/AccordionItem.js +0 -327
- package/components/Accordion/AccordionItem.js.map +0 -1
- package/components/Accordion/index.d.ts +0 -27
- package/components/Accordion/index.js +0 -39
- package/components/Accordion/index.js.map +0 -1
- package/components/Cursor.d.ts +0 -138
- package/components/Cursor.js +0 -167
- package/components/Cursor.js.map +0 -1
- package/components/Draggable.d.ts +0 -49
- package/components/Draggable.js +0 -77
- package/components/Draggable.js.map +0 -1
- package/components/Modal.d.ts +0 -158
- package/components/Modal.js +0 -270
- package/components/Modal.js.map +0 -1
- package/components/Tabs.d.ts +0 -108
- package/components/Tabs.js +0 -195
- package/components/Tabs.js.map +0 -1
- package/components/index.d.ts +0 -6
- package/components/index.js +0 -7
- package/components/index.js.map +0 -1
- package/decorators/index.js.map +0 -1
- package/decorators/withBreakpointManager.js.map +0 -1
- package/decorators/withBreakpointObserver.js.map +0 -1
- package/decorators/withDrag.js.map +0 -1
- package/decorators/withExtraConfig.js.map +0 -1
- package/decorators/withIntersectionObserver.js.map +0 -1
- package/decorators/withMountWhenInView.js.map +0 -1
- package/full.d.ts +0 -10
- package/full.js +0 -12
- package/full.js.map +0 -1
- package/full.modern.js +0 -7523
- package/full.modern.js.map +0 -1
- package/full.umd.js +0 -8277
- package/full.umd.js.map +0 -1
- package/helpers/createApp.js.map +0 -1
- package/helpers/createBase.d.ts +0 -7
- package/helpers/createBase.js +0 -7
- package/helpers/createBase.js.map +0 -1
- package/helpers/defineComponent.d.ts +0 -11
- package/helpers/defineComponent.js +0 -68
- package/helpers/defineComponent.js.map +0 -1
- package/helpers/importOnInteraction.js.map +0 -1
- package/helpers/importWhenIdle.js.map +0 -1
- package/helpers/importWhenVisible.js.map +0 -1
- package/helpers/index.js.map +0 -1
- package/helpers/utils.js.map +0 -1
- package/index.js.map +0 -1
- package/index.modern.js.map +0 -1
- package/index.umd.js.map +0 -1
- package/services/drag.js.map +0 -1
- package/services/index.js.map +0 -1
- package/services/key.js.map +0 -1
- package/services/load.js.map +0 -1
- package/services/pointer.js.map +0 -1
- package/services/raf.js.map +0 -1
- package/services/resize.js.map +0 -1
- package/services/scroll.js.map +0 -1
- package/utils/collide/boundingRectToCircle.js.map +0 -1
- package/utils/collide/collideCircleCircle.js.map +0 -1
- package/utils/collide/collideCircleRect.js.map +0 -1
- package/utils/collide/collidePointCircle.js.map +0 -1
- package/utils/collide/collidePointRect.js.map +0 -1
- package/utils/collide/collideRectRect.js.map +0 -1
- package/utils/collide/index.js.map +0 -1
- package/utils/css/classes.js.map +0 -1
- package/utils/css/index.js.map +0 -1
- package/utils/css/matrix.js.map +0 -1
- package/utils/css/styles.js.map +0 -1
- package/utils/css/transition.js.map +0 -1
- package/utils/debounce.js.map +0 -1
- package/utils/focusTrap.js.map +0 -1
- package/utils/history.js.map +0 -1
- package/utils/index.js.map +0 -1
- package/utils/keyCodes.js.map +0 -1
- package/utils/math/clamp.js.map +0 -1
- package/utils/math/clamp01.js.map +0 -1
- package/utils/math/damp.js.map +0 -1
- package/utils/math/index.js.map +0 -1
- package/utils/math/inertiaFinalValue.js.map +0 -1
- package/utils/math/lerp.js.map +0 -1
- package/utils/math/map.js.map +0 -1
- package/utils/math/round.js.map +0 -1
- package/utils/nextFrame.js.map +0 -1
- package/utils/object/autoBind.js.map +0 -1
- package/utils/object/getAllProperties.js.map +0 -1
- package/utils/object/index.js.map +0 -1
- package/utils/object/isObject.js.map +0 -1
- package/utils/string/index.js.map +0 -1
- package/utils/string/withLeadingCharacters.js.map +0 -1
- package/utils/string/withLeadingSlash.js.map +0 -1
- package/utils/string/withTrailingCharacters.js.map +0 -1
- package/utils/string/withTrailingSlash.js.map +0 -1
- package/utils/string/withoutLeadingCharacters.js.map +0 -1
- package/utils/string/withoutLeadingCharactersRecursive.js.map +0 -1
- package/utils/string/withoutLeadingSlash.js.map +0 -1
- package/utils/string/withoutTrailingCharacters.js.map +0 -1
- package/utils/string/withoutTrailingCharactersRecursive.js.map +0 -1
- package/utils/string/withoutTrailingSlash.js.map +0 -1
- package/utils/throttle.js.map +0 -1
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../index.js').default} Base
|
|
3
|
+
* @typedef {import('./ChildrenManager.js').default} ChildrenManager
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Event management class.
|
|
7
|
+
*/
|
|
8
|
+
export default class EventsManager {
|
|
9
|
+
/**
|
|
10
|
+
* Normalize the given name to PascalCase, such as:
|
|
11
|
+
*
|
|
12
|
+
* ```
|
|
13
|
+
* sentence case => SentenceCase
|
|
14
|
+
* lowercase => Lowercase
|
|
15
|
+
* UPPERCASE => Uppercase
|
|
16
|
+
* kebab-case => KebabCase
|
|
17
|
+
* snake_case => SnakeCase
|
|
18
|
+
* camelCase => CamelCase
|
|
19
|
+
* PascalCase => PascalCase
|
|
20
|
+
* .class-selector => ClassSelector
|
|
21
|
+
* .bem__selector => BemSelector
|
|
22
|
+
* __id-selector => IdSelector
|
|
23
|
+
* .complex[class^ ="__"] => ComplexClass
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @param {string} name
|
|
27
|
+
* @return {string}
|
|
28
|
+
*/
|
|
29
|
+
static normalizeName(name: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Normalize the event names from PascalCase to kebab-case.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} name
|
|
34
|
+
* @return {string}
|
|
35
|
+
*/
|
|
36
|
+
static normalizeEventName(name: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* Class constructor.
|
|
39
|
+
*
|
|
40
|
+
* @param {HTMLElement} rootElement
|
|
41
|
+
* @param {Base} base
|
|
42
|
+
*/
|
|
43
|
+
constructor(rootElement: HTMLElement, base: Base);
|
|
44
|
+
/**
|
|
45
|
+
* @type {HTMLElement}
|
|
46
|
+
* @private
|
|
47
|
+
*/
|
|
48
|
+
private __rootElement;
|
|
49
|
+
/**
|
|
50
|
+
* @type {Base}
|
|
51
|
+
* @private
|
|
52
|
+
*/
|
|
53
|
+
private __base;
|
|
54
|
+
/**
|
|
55
|
+
* @type {WeakMap}
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
private __methodsCache;
|
|
59
|
+
/**
|
|
60
|
+
* Event listener object for the root element.
|
|
61
|
+
*
|
|
62
|
+
* @type {EventListenerObject}
|
|
63
|
+
*/
|
|
64
|
+
__rootElementHandler: EventListenerObject;
|
|
65
|
+
/**
|
|
66
|
+
* Event listener object for the refs.
|
|
67
|
+
*
|
|
68
|
+
* @type {EventListenerObject}
|
|
69
|
+
*/
|
|
70
|
+
__refsHandler: EventListenerObject;
|
|
71
|
+
/**
|
|
72
|
+
* Event listener object for the children.
|
|
73
|
+
*
|
|
74
|
+
* @type {EventListenerObject}
|
|
75
|
+
*/
|
|
76
|
+
__childrenHandler: EventListenerObject;
|
|
77
|
+
/**
|
|
78
|
+
* Manage a ref event methods.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} name
|
|
81
|
+
* The name of the ref.
|
|
82
|
+
* @param {HTMLElement[]} elements
|
|
83
|
+
* The elements of the ref.
|
|
84
|
+
* @param {'add'|'remove'} [mode]
|
|
85
|
+
* The action to perform: add or remove the event listeners.
|
|
86
|
+
*
|
|
87
|
+
* @return {void}
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
private __manageRef;
|
|
91
|
+
/**
|
|
92
|
+
* Bind event methods to the given ref elements.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} name
|
|
95
|
+
* The name of the ref.
|
|
96
|
+
* @param {HTMLElement[]} elements
|
|
97
|
+
* The elements of the ref.
|
|
98
|
+
*
|
|
99
|
+
* @return {void}
|
|
100
|
+
*/
|
|
101
|
+
bindRef(name: string, elements: HTMLElement[]): void;
|
|
102
|
+
/**
|
|
103
|
+
* Bind event methods to the given ref elements.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} name
|
|
106
|
+
* The name of the ref.
|
|
107
|
+
* @param {HTMLElement[]} elements
|
|
108
|
+
* The elements of the ref.
|
|
109
|
+
*
|
|
110
|
+
* @return {void}
|
|
111
|
+
*/
|
|
112
|
+
unbindRef(name: string, elements: HTMLElement[]): void;
|
|
113
|
+
/**
|
|
114
|
+
* Manage event methods to the given child instance.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} name
|
|
117
|
+
* The name of the ref.
|
|
118
|
+
* @param {Base} instance
|
|
119
|
+
* A base instance.
|
|
120
|
+
* @param {'add'|'remove'} [mode]
|
|
121
|
+
* The action to perform: add or remove the event listeners.
|
|
122
|
+
*
|
|
123
|
+
* @return {void}
|
|
124
|
+
* @private
|
|
125
|
+
*/
|
|
126
|
+
private __manageChild;
|
|
127
|
+
/**
|
|
128
|
+
* Bind event methods to the given child instance.
|
|
129
|
+
*
|
|
130
|
+
* @param {string} name
|
|
131
|
+
* The name of the ref.
|
|
132
|
+
* @param {Base} instance
|
|
133
|
+
* A base instance.
|
|
134
|
+
*
|
|
135
|
+
* @return {void}
|
|
136
|
+
*/
|
|
137
|
+
bindChild(name: string, instance: Base): void;
|
|
138
|
+
/**
|
|
139
|
+
* Unbind event methods to the given child instance.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} name
|
|
142
|
+
* The name of the ref.
|
|
143
|
+
* @param {Base} instance
|
|
144
|
+
* A base instance.
|
|
145
|
+
*
|
|
146
|
+
* @return {void}
|
|
147
|
+
*/
|
|
148
|
+
unbindChild(name: string, instance: Base): void;
|
|
149
|
+
/**
|
|
150
|
+
* Manage event methods on the root element.
|
|
151
|
+
*
|
|
152
|
+
* @param {'add'|'remove'} [mode]
|
|
153
|
+
* The action to perform: add or remove the event listeners.
|
|
154
|
+
*
|
|
155
|
+
* @return {void}
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
158
|
+
private __manageRootElement;
|
|
159
|
+
/**
|
|
160
|
+
* Bind event methods on the root element.
|
|
161
|
+
*
|
|
162
|
+
* @return {void}
|
|
163
|
+
*/
|
|
164
|
+
bindRootElement(): void;
|
|
165
|
+
/**
|
|
166
|
+
* Unbind event method from the root element.
|
|
167
|
+
*
|
|
168
|
+
* @return {void}
|
|
169
|
+
*/
|
|
170
|
+
unbindRootElement(): void;
|
|
171
|
+
/**
|
|
172
|
+
* Get the event name from the given method and its tied ref or children name.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} method
|
|
175
|
+
* @param {string} name
|
|
176
|
+
* @return {string}
|
|
177
|
+
* @private
|
|
178
|
+
*/
|
|
179
|
+
private __getEventNameByMethod;
|
|
180
|
+
/**
|
|
181
|
+
* Get the base instance methods matching the `onSomething` pattern.
|
|
182
|
+
*
|
|
183
|
+
* @param {string} [name]
|
|
184
|
+
* @return {string[]}
|
|
185
|
+
* @private
|
|
186
|
+
*/
|
|
187
|
+
private __getEventMethodsByName;
|
|
188
|
+
}
|
|
189
|
+
export type Base = import('../index.js').default;
|
|
190
|
+
export type ChildrenManager = import('./ChildrenManager.js').default;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import getAllProperties from '../../utils/object/getAllProperties.js';
|
|
3
|
+
import RefsManager from './RefsManager.js';
|
|
4
|
+
export default class EventsManager {
|
|
5
|
+
constructor(rootElement, base) {
|
|
6
|
+
_defineProperty(this, "__rootElement", void 0);
|
|
7
|
+
|
|
8
|
+
_defineProperty(this, "__base", void 0);
|
|
9
|
+
|
|
10
|
+
_defineProperty(this, "__methodsCache", new Map());
|
|
11
|
+
|
|
12
|
+
_defineProperty(this, "__rootElementHandler", {
|
|
13
|
+
handleEvent: event => {
|
|
14
|
+
var normalizedEventName = EventsManager.normalizeName(event.type);
|
|
15
|
+
var method = "on".concat(normalizedEventName);
|
|
16
|
+
|
|
17
|
+
this.__base[method](event);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
_defineProperty(this, "__refsHandler", {
|
|
22
|
+
handleEvent: event => {
|
|
23
|
+
var ref = event.currentTarget;
|
|
24
|
+
var refName = RefsManager.normalizeRefName(ref.dataset.ref);
|
|
25
|
+
var normalizedRefName = EventsManager.normalizeName(refName);
|
|
26
|
+
var normalizedEventName = EventsManager.normalizeName(event.type);
|
|
27
|
+
var method = "on".concat(normalizedRefName).concat(normalizedEventName);
|
|
28
|
+
var index = 0;
|
|
29
|
+
|
|
30
|
+
if (Array.isArray(this.__base.$refs[refName])) {
|
|
31
|
+
index = this.__base.$refs[refName].indexOf(ref);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.__base[method](event, index);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
_defineProperty(this, "__childrenHandler", {
|
|
39
|
+
handleEvent: event => {
|
|
40
|
+
var child = event.currentTarget.__base__;
|
|
41
|
+
var childName;
|
|
42
|
+
var index = 0;
|
|
43
|
+
var childrenManager = this.__base.$children;
|
|
44
|
+
childrenManager.registeredNames.find(name => {
|
|
45
|
+
if (childrenManager[name].includes(child)) {
|
|
46
|
+
childName = name;
|
|
47
|
+
index = childrenManager[name].indexOf(child);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return false;
|
|
52
|
+
});
|
|
53
|
+
var normalizedRefName = EventsManager.normalizeName(childName);
|
|
54
|
+
var normalizedEventName = EventsManager.normalizeName(event.type);
|
|
55
|
+
var method = "on".concat(normalizedRefName).concat(normalizedEventName);
|
|
56
|
+
var args = Array.isArray(event.detail) ? event.detail : [];
|
|
57
|
+
|
|
58
|
+
this.__base[method](...args, index, event);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
Object.defineProperties(this, {
|
|
63
|
+
__rootElement: {
|
|
64
|
+
enumerable: false,
|
|
65
|
+
writable: false,
|
|
66
|
+
value: rootElement
|
|
67
|
+
},
|
|
68
|
+
__base: {
|
|
69
|
+
enumerable: false,
|
|
70
|
+
writable: false,
|
|
71
|
+
value: base
|
|
72
|
+
},
|
|
73
|
+
__methodsCache: {
|
|
74
|
+
enumerable: false,
|
|
75
|
+
writable: false,
|
|
76
|
+
value: this.__methodsCache
|
|
77
|
+
},
|
|
78
|
+
__rootElementHandler: {
|
|
79
|
+
enumerable: false,
|
|
80
|
+
writable: false,
|
|
81
|
+
value: this.__rootElementHandler
|
|
82
|
+
},
|
|
83
|
+
__refsHandler: {
|
|
84
|
+
enumerable: false,
|
|
85
|
+
writable: false,
|
|
86
|
+
value: this.__refsHandler
|
|
87
|
+
},
|
|
88
|
+
__childrenHandler: {
|
|
89
|
+
enumerable: false,
|
|
90
|
+
writable: false,
|
|
91
|
+
value: this.__childrenHandler
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
__manageRef(name, elements) {
|
|
97
|
+
var mode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'add';
|
|
98
|
+
var action = "".concat(mode, "EventListener");
|
|
99
|
+
|
|
100
|
+
var methods = this.__getEventMethodsByName(name);
|
|
101
|
+
|
|
102
|
+
methods.forEach(method => {
|
|
103
|
+
var event = this.__getEventNameByMethod(method, name);
|
|
104
|
+
|
|
105
|
+
elements.forEach(element => element[action](event, this.__refsHandler));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
bindRef(name, elements) {
|
|
110
|
+
this.__manageRef(name, elements);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
unbindRef(name, elements) {
|
|
114
|
+
this.__manageRef(name, elements, 'remove');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
__manageChild(name, instance) {
|
|
118
|
+
var mode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'add';
|
|
119
|
+
var action = mode === 'add' ? '$on' : '$off';
|
|
120
|
+
|
|
121
|
+
var methods = this.__getEventMethodsByName(name);
|
|
122
|
+
|
|
123
|
+
methods.forEach(method => {
|
|
124
|
+
var event = this.__getEventNameByMethod(method, name);
|
|
125
|
+
|
|
126
|
+
instance[action](event, this.__childrenHandler);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
bindChild(name, instance) {
|
|
131
|
+
this.__manageChild(name, instance);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
unbindChild(name, instance) {
|
|
135
|
+
this.__manageChild(name, instance, 'remove');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
__manageRootElement() {
|
|
139
|
+
var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'add';
|
|
140
|
+
var modeMethod = "".concat(mode, "EventListener");
|
|
141
|
+
|
|
142
|
+
var methods = this.__getEventMethodsByName();
|
|
143
|
+
|
|
144
|
+
methods.forEach(method => {
|
|
145
|
+
var event = this.__getEventNameByMethod(method);
|
|
146
|
+
|
|
147
|
+
this.__rootElement[modeMethod](event, this.__rootElementHandler);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
bindRootElement() {
|
|
152
|
+
this.__manageRootElement();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
unbindRootElement() {
|
|
156
|
+
this.__manageRootElement('remove');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
__getEventNameByMethod(method) {
|
|
160
|
+
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
161
|
+
var normalizedName = EventsManager.normalizeName(name);
|
|
162
|
+
var regex = new RegExp("^on".concat(normalizedName, "([A-Z].*)$"));
|
|
163
|
+
var [, event] = method.match(regex);
|
|
164
|
+
return EventsManager.normalizeEventName(event);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
__getEventMethodsByName() {
|
|
168
|
+
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
169
|
+
var normalizedName = EventsManager.normalizeName(name);
|
|
170
|
+
var regex = new RegExp("^on".concat(normalizedName, "[A-Z].*$"));
|
|
171
|
+
var key = regex.toString();
|
|
172
|
+
|
|
173
|
+
var methods = this.__methodsCache.get(key);
|
|
174
|
+
|
|
175
|
+
if (!methods) {
|
|
176
|
+
methods = Array.from(getAllProperties(this.__base, [], method => regex.test(method)).reduce((set, _ref) => {
|
|
177
|
+
var [method] = _ref;
|
|
178
|
+
return set.add(method);
|
|
179
|
+
}, new Set()));
|
|
180
|
+
|
|
181
|
+
this.__methodsCache.set(key, methods);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return methods;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static normalizeName(name) {
|
|
188
|
+
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();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static normalizeEventName(name) {
|
|
192
|
+
return name.replace(/[A-Z]/g, c => "-".concat(c.toLowerCase())).replace(/^-/, '');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../index.js').BaseConfig} BaseConfig
|
|
3
|
+
* @typedef {StringConstructor|NumberConstructor|BooleanConstructor|ArrayConstructor|ObjectConstructor} OptionType
|
|
4
|
+
* @typedef {{ type: OptionType, default: String|Number|Boolean|(() => Array|Object)}} OptionWithDefault
|
|
5
|
+
* @typedef {OptionType | OptionWithDefault} OptionValue
|
|
6
|
+
* @typedef {{ [name:string]: OptionValue }} OptionsSchema
|
|
7
|
+
* @typedef {{ [optionName:string]: any }} OptionsInterface
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Class options to manage options as data attributes on an HTML element.
|
|
11
|
+
* @augments {OptionsInterface}
|
|
12
|
+
*/
|
|
13
|
+
export default class OptionsManager {
|
|
14
|
+
/**
|
|
15
|
+
* List of allowed types.
|
|
16
|
+
* @type {Array}
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
private static types;
|
|
20
|
+
/**
|
|
21
|
+
* Property name cache.
|
|
22
|
+
* @type {Map}
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
private static __propertyNameCache;
|
|
26
|
+
/**
|
|
27
|
+
* Get a property name.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} name
|
|
30
|
+
* @return {string}
|
|
31
|
+
* @private
|
|
32
|
+
*/
|
|
33
|
+
private static __getPropertyName;
|
|
34
|
+
/**
|
|
35
|
+
* Class constructor.
|
|
36
|
+
*
|
|
37
|
+
* @param {HTMLElement} element The HTML element storing the options.
|
|
38
|
+
* @param {OptionsSchema} schema A Base class config options.
|
|
39
|
+
* @param {BaseConfig} baseConfig A Base class config.
|
|
40
|
+
*/
|
|
41
|
+
constructor(element: HTMLElement, schema: OptionsSchema, baseConfig: BaseConfig);
|
|
42
|
+
/**
|
|
43
|
+
* The HTML element holding the options attributes.
|
|
44
|
+
* @type {HTMLElement}
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
private __element;
|
|
48
|
+
/**
|
|
49
|
+
* An object to store Array and Object values for reference.
|
|
50
|
+
* @type {Object}
|
|
51
|
+
* @private
|
|
52
|
+
*/
|
|
53
|
+
private __values;
|
|
54
|
+
/**
|
|
55
|
+
* The default values to return for each available type.
|
|
56
|
+
* @type {Object}
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
private __defaultValues;
|
|
60
|
+
/**
|
|
61
|
+
* Default value for the name property.
|
|
62
|
+
* @type {string}
|
|
63
|
+
*/
|
|
64
|
+
name: string;
|
|
65
|
+
/**
|
|
66
|
+
* Default value for the debug property.
|
|
67
|
+
* @type {boolean}
|
|
68
|
+
*/
|
|
69
|
+
debug: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Default value for the log property.
|
|
72
|
+
* @type {Boolean}
|
|
73
|
+
*/
|
|
74
|
+
log: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Register an option.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} name
|
|
79
|
+
* @param {OptionValue} config
|
|
80
|
+
* @return {void}
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
private __register;
|
|
84
|
+
/**
|
|
85
|
+
* Get an option value.
|
|
86
|
+
*
|
|
87
|
+
* @param {String} name The option name.
|
|
88
|
+
* @param {OptionType} type The option data's type.
|
|
89
|
+
* @param {any} defaultValue The default value for this option.
|
|
90
|
+
*/
|
|
91
|
+
get(name: string, type: OptionType, defaultValue: any): any;
|
|
92
|
+
/**
|
|
93
|
+
* Set an option value.
|
|
94
|
+
*
|
|
95
|
+
* @param {String} name The option name.
|
|
96
|
+
* @param {OptionType} type The option data's type.
|
|
97
|
+
* @param {any} value The new value for this option.
|
|
98
|
+
*/
|
|
99
|
+
set(name: string, type: OptionType, value: any, defaultValue: any): void;
|
|
100
|
+
/**
|
|
101
|
+
* Test if the element has a given property.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} prop
|
|
104
|
+
* @return {boolean}
|
|
105
|
+
*/
|
|
106
|
+
__hasProperty(prop: string): boolean;
|
|
107
|
+
}
|
|
108
|
+
export type BaseConfig = import('../index.js').BaseConfig;
|
|
109
|
+
export type OptionType = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor;
|
|
110
|
+
export type OptionWithDefault = {
|
|
111
|
+
type: OptionType;
|
|
112
|
+
default: string | number | boolean | (() => any[] | any);
|
|
113
|
+
};
|
|
114
|
+
export type OptionValue = OptionType | OptionWithDefault;
|
|
115
|
+
export type OptionsSchema = {
|
|
116
|
+
[name: string]: OptionValue;
|
|
117
|
+
};
|
|
118
|
+
export type OptionsInterface = {
|
|
119
|
+
[optionName: string]: any;
|
|
120
|
+
};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import deepmerge from 'deepmerge';
|
|
3
|
+
import isObject from '../../utils/object/isObject.js';
|
|
4
|
+
export default class OptionsManager {
|
|
5
|
+
constructor(element, schema, baseConfig) {
|
|
6
|
+
var _baseConfig$debug, _baseConfig$log;
|
|
7
|
+
|
|
8
|
+
_defineProperty(this, "__element", void 0);
|
|
9
|
+
|
|
10
|
+
_defineProperty(this, "__values", {});
|
|
11
|
+
|
|
12
|
+
_defineProperty(this, "__defaultValues", {
|
|
13
|
+
String: '',
|
|
14
|
+
Number: 0,
|
|
15
|
+
Boolean: false,
|
|
16
|
+
Array: () => [],
|
|
17
|
+
Object: () => ({})
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
_defineProperty(this, "name", 'Base');
|
|
21
|
+
|
|
22
|
+
_defineProperty(this, "debug", false);
|
|
23
|
+
|
|
24
|
+
_defineProperty(this, "log", false);
|
|
25
|
+
|
|
26
|
+
Object.defineProperties(this, {
|
|
27
|
+
__element: {
|
|
28
|
+
enumerable: false,
|
|
29
|
+
writable: false,
|
|
30
|
+
value: element
|
|
31
|
+
},
|
|
32
|
+
__values: {
|
|
33
|
+
enumerable: false,
|
|
34
|
+
writable: false,
|
|
35
|
+
value: this.__values
|
|
36
|
+
},
|
|
37
|
+
__defaultValues: {
|
|
38
|
+
enumerable: false,
|
|
39
|
+
writable: false,
|
|
40
|
+
value: this.__defaultValues
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
this.name = baseConfig.name;
|
|
44
|
+
schema.debug = {
|
|
45
|
+
type: Boolean,
|
|
46
|
+
default: (_baseConfig$debug = baseConfig.debug) !== null && _baseConfig$debug !== void 0 ? _baseConfig$debug : false
|
|
47
|
+
};
|
|
48
|
+
schema.log = {
|
|
49
|
+
type: Boolean,
|
|
50
|
+
default: (_baseConfig$log = baseConfig.log) !== null && _baseConfig$log !== void 0 ? _baseConfig$log : false
|
|
51
|
+
};
|
|
52
|
+
Object.entries(schema).forEach(_ref => {
|
|
53
|
+
var [name, config] = _ref;
|
|
54
|
+
|
|
55
|
+
this.__register(name, config);
|
|
56
|
+
});
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
__register(name, config) {
|
|
61
|
+
var hasDefaultValue = !OptionsManager.types.includes(config);
|
|
62
|
+
var type = hasDefaultValue ? config.type : config;
|
|
63
|
+
|
|
64
|
+
if (!OptionsManager.types.includes(type)) {
|
|
65
|
+
throw new Error("The \"".concat(name, "\" option has an invalid type. The allowed types are: String, Number, Boolean, Array and Object."));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
var defaultValue = hasDefaultValue ? config.default : this.__defaultValues[type.name];
|
|
69
|
+
|
|
70
|
+
if ((type === Array || type === Object) && typeof defaultValue !== 'function') {
|
|
71
|
+
throw new Error("The default value for options of type \"".concat(type.name, "\" must be returned by a function."));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
Object.defineProperty(this, name, {
|
|
75
|
+
get: () => {
|
|
76
|
+
return this.get(name, type, defaultValue);
|
|
77
|
+
},
|
|
78
|
+
set: value => {
|
|
79
|
+
this.set(name, type, value, defaultValue);
|
|
80
|
+
},
|
|
81
|
+
enumerable: true
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get(name, type, defaultValue) {
|
|
86
|
+
var propertyName = OptionsManager.__getPropertyName(name);
|
|
87
|
+
|
|
88
|
+
var hasProperty = this.__hasProperty(propertyName);
|
|
89
|
+
|
|
90
|
+
if (type === Boolean) {
|
|
91
|
+
if (defaultValue) {
|
|
92
|
+
var negatedPropertyName = OptionsManager.__getPropertyName(name, 'No');
|
|
93
|
+
|
|
94
|
+
var hasNegatedProperty = this.__hasProperty(negatedPropertyName);
|
|
95
|
+
|
|
96
|
+
return !hasNegatedProperty;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return hasProperty || defaultValue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
var value = this.__element.dataset[propertyName];
|
|
103
|
+
|
|
104
|
+
if (type === Number) {
|
|
105
|
+
return hasProperty ? Number(value) : defaultValue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (type === Array || type === Object) {
|
|
109
|
+
var val = deepmerge(defaultValue(), hasProperty ? JSON.parse(value) : this.__defaultValues[type.name]());
|
|
110
|
+
|
|
111
|
+
if (!this.__values[name]) {
|
|
112
|
+
this.__values[name] = val;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return this.__values[name];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return hasProperty ? value : defaultValue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
set(name, type, value, defaultValue) {
|
|
122
|
+
var propertyName = OptionsManager.__getPropertyName(name);
|
|
123
|
+
|
|
124
|
+
if (value.constructor.name !== type.name) {
|
|
125
|
+
var val = Array.isArray(value) || isObject(value) ? JSON.stringify(value) : value;
|
|
126
|
+
throw new TypeError("The \"".concat(val, "\" value for the \"").concat(name, "\" option must be of type \"").concat(type.name, "\""));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
switch (type) {
|
|
130
|
+
case Boolean:
|
|
131
|
+
if (defaultValue) {
|
|
132
|
+
var negatedPropertyName = OptionsManager.__getPropertyName(name, 'No');
|
|
133
|
+
|
|
134
|
+
if (value) {
|
|
135
|
+
delete this.__element.dataset[negatedPropertyName];
|
|
136
|
+
} else {
|
|
137
|
+
this.__element.dataset[negatedPropertyName] = '';
|
|
138
|
+
}
|
|
139
|
+
} else if (value) {
|
|
140
|
+
this.__element.dataset[propertyName] = '';
|
|
141
|
+
} else {
|
|
142
|
+
delete this.__element.dataset[propertyName];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
break;
|
|
146
|
+
|
|
147
|
+
case Array:
|
|
148
|
+
case Object:
|
|
149
|
+
this.__values[name] = value;
|
|
150
|
+
break;
|
|
151
|
+
|
|
152
|
+
default:
|
|
153
|
+
this.__element.dataset[propertyName] = value;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
__hasProperty(prop) {
|
|
158
|
+
return typeof this.__element.dataset[prop] !== 'undefined';
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
static __getPropertyName(name) {
|
|
162
|
+
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
163
|
+
var key = name + prefix;
|
|
164
|
+
|
|
165
|
+
if (OptionsManager.__propertyNameCache.has(key)) {
|
|
166
|
+
return OptionsManager.__propertyNameCache.get(key);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
var propertyName = "option".concat(prefix).concat(name.replace(/^\w/, c => c.toUpperCase()));
|
|
170
|
+
|
|
171
|
+
OptionsManager.__propertyNameCache.set(key, propertyName);
|
|
172
|
+
|
|
173
|
+
return propertyName;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
_defineProperty(OptionsManager, "types", [String, Number, Boolean, Array, Object]);
|
|
179
|
+
|
|
180
|
+
_defineProperty(OptionsManager, "__propertyNameCache", new Map());
|