@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.
Files changed (237) hide show
  1. package/Base/index.d.ts +217 -0
  2. package/Base/index.js +237 -0
  3. package/Base/managers/ChildrenManager.d.ts +108 -0
  4. package/Base/managers/ChildrenManager.js +124 -0
  5. package/Base/managers/EventsManager.d.ts +190 -0
  6. package/Base/managers/EventsManager.js +195 -0
  7. package/Base/managers/OptionsManager.d.ts +120 -0
  8. package/Base/managers/OptionsManager.js +180 -0
  9. package/Base/managers/RefsManager.d.ts +84 -0
  10. package/Base/managers/RefsManager.js +83 -0
  11. package/{abstracts/Base/classes/Services.d.ts → Base/managers/ServicesManager.d.ts} +22 -7
  12. package/Base/managers/ServicesManager.js +115 -0
  13. package/{abstracts/Base → Base}/utils.d.ts +0 -30
  14. package/Base/utils.js +43 -0
  15. package/README.md +12 -70
  16. package/decorators/index.js +1 -2
  17. package/decorators/withBreakpointManager.d.ts +5 -5
  18. package/decorators/withBreakpointManager.js +41 -79
  19. package/decorators/withBreakpointObserver.d.ts +4 -4
  20. package/decorators/withBreakpointObserver.js +54 -77
  21. package/decorators/withDrag.d.ts +6 -6
  22. package/decorators/withDrag.js +17 -41
  23. package/decorators/withExtraConfig.d.ts +6 -6
  24. package/decorators/withExtraConfig.js +2 -24
  25. package/decorators/withIntersectionObserver.d.ts +4 -4
  26. package/decorators/withIntersectionObserver.js +17 -60
  27. package/decorators/withMountWhenInView.d.ts +8 -8
  28. package/decorators/withMountWhenInView.js +23 -51
  29. package/helpers/createApp.d.ts +4 -4
  30. package/helpers/createApp.js +10 -12
  31. package/helpers/importOnInteraction.d.ts +6 -6
  32. package/helpers/importOnInteraction.js +7 -8
  33. package/helpers/importWhenIdle.d.ts +4 -4
  34. package/helpers/importWhenIdle.js +7 -8
  35. package/helpers/importWhenVisible.d.ts +6 -6
  36. package/helpers/importWhenVisible.js +9 -16
  37. package/helpers/index.d.ts +1 -3
  38. package/helpers/index.js +1 -4
  39. package/helpers/utils.js +2 -3
  40. package/index.d.ts +19 -5
  41. package/index.js +5 -6
  42. package/index.modern.js +1 -3230
  43. package/index.umd.js +1 -3586
  44. package/package.json +3 -6
  45. package/{abstracts → services}/Service.d.ts +0 -0
  46. package/services/Service.js +63 -0
  47. package/services/drag.js +144 -180
  48. package/services/index.js +1 -2
  49. package/services/key.js +56 -98
  50. package/services/load.d.ts +1 -1
  51. package/services/load.js +22 -51
  52. package/services/pointer.js +118 -157
  53. package/services/raf.js +37 -65
  54. package/services/resize.js +46 -98
  55. package/services/scroll.js +81 -120
  56. package/utils/collide/boundingRectToCircle.js +7 -6
  57. package/utils/collide/collideCircleCircle.js +1 -2
  58. package/utils/collide/collideCircleRect.js +1 -2
  59. package/utils/collide/collidePointCircle.js +1 -2
  60. package/utils/collide/collidePointRect.js +1 -2
  61. package/utils/collide/collideRectRect.js +1 -2
  62. package/utils/collide/index.js +1 -2
  63. package/utils/css/classes.js +2 -3
  64. package/utils/css/index.js +1 -2
  65. package/utils/css/matrix.js +1 -2
  66. package/utils/css/styles.js +3 -8
  67. package/utils/css/transition.js +67 -140
  68. package/utils/debounce.js +3 -4
  69. package/utils/focusTrap.js +4 -5
  70. package/utils/history.js +12 -19
  71. package/utils/index.d.ts +2 -1
  72. package/utils/index.js +2 -2
  73. package/utils/keyCodes.js +1 -2
  74. package/utils/math/clamp.js +1 -2
  75. package/utils/math/clamp01.js +1 -2
  76. package/utils/math/damp.js +1 -2
  77. package/utils/math/index.js +1 -2
  78. package/utils/math/inertiaFinalValue.js +1 -2
  79. package/utils/math/lerp.js +1 -2
  80. package/utils/math/map.js +1 -2
  81. package/utils/math/round.js +1 -2
  82. package/utils/memoize.d.ts +30 -0
  83. package/utils/memoize.js +30 -0
  84. package/utils/nextFrame.js +4 -9
  85. package/utils/object/autoBind.js +11 -19
  86. package/utils/object/getAllProperties.d.ts +6 -3
  87. package/utils/object/getAllProperties.js +10 -8
  88. package/utils/object/index.js +1 -2
  89. package/utils/object/isObject.js +2 -4
  90. package/utils/string/index.js +1 -2
  91. package/utils/string/withLeadingCharacters.js +1 -2
  92. package/utils/string/withLeadingSlash.js +1 -2
  93. package/utils/string/withTrailingCharacters.js +1 -2
  94. package/utils/string/withTrailingSlash.js +1 -2
  95. package/utils/string/withoutLeadingCharacters.js +1 -2
  96. package/utils/string/withoutLeadingCharactersRecursive.js +1 -2
  97. package/utils/string/withoutLeadingSlash.js +1 -2
  98. package/utils/string/withoutTrailingCharacters.js +1 -2
  99. package/utils/string/withoutTrailingCharactersRecursive.js +1 -2
  100. package/utils/string/withoutTrailingSlash.js +1 -2
  101. package/utils/throttle.js +2 -3
  102. package/abstracts/Base/children.d.ts +0 -16
  103. package/abstracts/Base/children.js +0 -66
  104. package/abstracts/Base/children.js.map +0 -1
  105. package/abstracts/Base/classes/Options.d.ts +0 -37
  106. package/abstracts/Base/classes/Options.js +0 -182
  107. package/abstracts/Base/classes/Options.js.map +0 -1
  108. package/abstracts/Base/classes/Services.js +0 -120
  109. package/abstracts/Base/classes/Services.js.map +0 -1
  110. package/abstracts/Base/components.d.ts +0 -22
  111. package/abstracts/Base/components.js +0 -67
  112. package/abstracts/Base/components.js.map +0 -1
  113. package/abstracts/Base/events.d.ts +0 -8
  114. package/abstracts/Base/events.js +0 -185
  115. package/abstracts/Base/events.js.map +0 -1
  116. package/abstracts/Base/index.d.ts +0 -176
  117. package/abstracts/Base/index.js +0 -215
  118. package/abstracts/Base/index.js.map +0 -1
  119. package/abstracts/Base/options.d.ts +0 -17
  120. package/abstracts/Base/options.js +0 -112
  121. package/abstracts/Base/options.js.map +0 -1
  122. package/abstracts/Base/refs.d.ts +0 -30
  123. package/abstracts/Base/refs.js +0 -60
  124. package/abstracts/Base/refs.js.map +0 -1
  125. package/abstracts/Base/utils.js +0 -102
  126. package/abstracts/Base/utils.js.map +0 -1
  127. package/abstracts/EventManager.d.ts +0 -41
  128. package/abstracts/EventManager.js +0 -88
  129. package/abstracts/EventManager.js.map +0 -1
  130. package/abstracts/Service.js +0 -82
  131. package/abstracts/Service.js.map +0 -1
  132. package/abstracts/index.d.ts +0 -4
  133. package/abstracts/index.js +0 -5
  134. package/abstracts/index.js.map +0 -1
  135. package/components/Accordion/Accordion.d.ts +0 -83
  136. package/components/Accordion/Accordion.js +0 -121
  137. package/components/Accordion/Accordion.js.map +0 -1
  138. package/components/Accordion/AccordionItem.d.ts +0 -122
  139. package/components/Accordion/AccordionItem.js +0 -327
  140. package/components/Accordion/AccordionItem.js.map +0 -1
  141. package/components/Accordion/index.d.ts +0 -27
  142. package/components/Accordion/index.js +0 -39
  143. package/components/Accordion/index.js.map +0 -1
  144. package/components/Cursor.d.ts +0 -138
  145. package/components/Cursor.js +0 -167
  146. package/components/Cursor.js.map +0 -1
  147. package/components/Draggable.d.ts +0 -49
  148. package/components/Draggable.js +0 -77
  149. package/components/Draggable.js.map +0 -1
  150. package/components/Modal.d.ts +0 -158
  151. package/components/Modal.js +0 -270
  152. package/components/Modal.js.map +0 -1
  153. package/components/Tabs.d.ts +0 -108
  154. package/components/Tabs.js +0 -195
  155. package/components/Tabs.js.map +0 -1
  156. package/components/index.d.ts +0 -6
  157. package/components/index.js +0 -7
  158. package/components/index.js.map +0 -1
  159. package/decorators/index.js.map +0 -1
  160. package/decorators/withBreakpointManager.js.map +0 -1
  161. package/decorators/withBreakpointObserver.js.map +0 -1
  162. package/decorators/withDrag.js.map +0 -1
  163. package/decorators/withExtraConfig.js.map +0 -1
  164. package/decorators/withIntersectionObserver.js.map +0 -1
  165. package/decorators/withMountWhenInView.js.map +0 -1
  166. package/full.d.ts +0 -10
  167. package/full.js +0 -12
  168. package/full.js.map +0 -1
  169. package/full.modern.js +0 -7523
  170. package/full.modern.js.map +0 -1
  171. package/full.umd.js +0 -8277
  172. package/full.umd.js.map +0 -1
  173. package/helpers/createApp.js.map +0 -1
  174. package/helpers/createBase.d.ts +0 -7
  175. package/helpers/createBase.js +0 -7
  176. package/helpers/createBase.js.map +0 -1
  177. package/helpers/defineComponent.d.ts +0 -11
  178. package/helpers/defineComponent.js +0 -68
  179. package/helpers/defineComponent.js.map +0 -1
  180. package/helpers/importOnInteraction.js.map +0 -1
  181. package/helpers/importWhenIdle.js.map +0 -1
  182. package/helpers/importWhenVisible.js.map +0 -1
  183. package/helpers/index.js.map +0 -1
  184. package/helpers/utils.js.map +0 -1
  185. package/index.js.map +0 -1
  186. package/index.modern.js.map +0 -1
  187. package/index.umd.js.map +0 -1
  188. package/services/drag.js.map +0 -1
  189. package/services/index.js.map +0 -1
  190. package/services/key.js.map +0 -1
  191. package/services/load.js.map +0 -1
  192. package/services/pointer.js.map +0 -1
  193. package/services/raf.js.map +0 -1
  194. package/services/resize.js.map +0 -1
  195. package/services/scroll.js.map +0 -1
  196. package/utils/collide/boundingRectToCircle.js.map +0 -1
  197. package/utils/collide/collideCircleCircle.js.map +0 -1
  198. package/utils/collide/collideCircleRect.js.map +0 -1
  199. package/utils/collide/collidePointCircle.js.map +0 -1
  200. package/utils/collide/collidePointRect.js.map +0 -1
  201. package/utils/collide/collideRectRect.js.map +0 -1
  202. package/utils/collide/index.js.map +0 -1
  203. package/utils/css/classes.js.map +0 -1
  204. package/utils/css/index.js.map +0 -1
  205. package/utils/css/matrix.js.map +0 -1
  206. package/utils/css/styles.js.map +0 -1
  207. package/utils/css/transition.js.map +0 -1
  208. package/utils/debounce.js.map +0 -1
  209. package/utils/focusTrap.js.map +0 -1
  210. package/utils/history.js.map +0 -1
  211. package/utils/index.js.map +0 -1
  212. package/utils/keyCodes.js.map +0 -1
  213. package/utils/math/clamp.js.map +0 -1
  214. package/utils/math/clamp01.js.map +0 -1
  215. package/utils/math/damp.js.map +0 -1
  216. package/utils/math/index.js.map +0 -1
  217. package/utils/math/inertiaFinalValue.js.map +0 -1
  218. package/utils/math/lerp.js.map +0 -1
  219. package/utils/math/map.js.map +0 -1
  220. package/utils/math/round.js.map +0 -1
  221. package/utils/nextFrame.js.map +0 -1
  222. package/utils/object/autoBind.js.map +0 -1
  223. package/utils/object/getAllProperties.js.map +0 -1
  224. package/utils/object/index.js.map +0 -1
  225. package/utils/object/isObject.js.map +0 -1
  226. package/utils/string/index.js.map +0 -1
  227. package/utils/string/withLeadingCharacters.js.map +0 -1
  228. package/utils/string/withLeadingSlash.js.map +0 -1
  229. package/utils/string/withTrailingCharacters.js.map +0 -1
  230. package/utils/string/withTrailingSlash.js.map +0 -1
  231. package/utils/string/withoutLeadingCharacters.js.map +0 -1
  232. package/utils/string/withoutLeadingCharactersRecursive.js.map +0 -1
  233. package/utils/string/withoutLeadingSlash.js.map +0 -1
  234. package/utils/string/withoutTrailingCharacters.js.map +0 -1
  235. package/utils/string/withoutTrailingCharactersRecursive.js.map +0 -1
  236. package/utils/string/withoutTrailingSlash.js.map +0 -1
  237. package/utils/throttle.js.map +0 -1
@@ -0,0 +1,217 @@
1
+ /**
2
+ * @typedef {typeof Base} BaseConstructor
3
+ * @typedef {(Base) => Promise<BaseConstructor | { default: BaseConstructor }>} BaseAsyncConstructor
4
+ * @typedef {OptionsManager & { [name:string]: any }} BaseOptions
5
+ * @typedef {{ [name:string]: HTMLElement | HTMLElement[] }} BaseRefs
6
+ * @typedef {{ [nameOrSelector:string]: Base[] | Promise<Base>[] }} BaseChildren
7
+ * @typedef {{ [nameOrSelector:string]: BaseConstructor | BaseAsyncConstructor }} BaseConfigComponents
8
+ * @typedef {import('./managers/OptionsManager').OptionsSchema} BaseConfigOptions
9
+ * @typedef {import('./managers/ServicesManager.js').ServiceName} ServiceName
10
+ */
11
+ /**
12
+ * @typedef {Object} BaseConfig
13
+ * @property {String} name
14
+ * @property {Boolean} [debug]
15
+ * @property {Boolean} [log]
16
+ * @property {String[]} [refs]
17
+ * @property {BaseConfigComponents} [components]
18
+ * @property {BaseConfigOptions} [options]
19
+ */
20
+ /**
21
+ * Base class.
22
+ */
23
+ export default class Base {
24
+ /**
25
+ * This is a Base instance.
26
+ * @type {Boolean}
27
+ * @readonly
28
+ */
29
+ static readonly $isBase: boolean;
30
+ /**
31
+ * @type {BaseConfig}
32
+ */
33
+ static config: BaseConfig;
34
+ /**
35
+ * Factory method to generate multiple instance of the class.
36
+ *
37
+ * @param {String} nameOrSelector The selector on which to mount each instance.
38
+ * @return {Array<Base>} A list of the created instance.
39
+ */
40
+ static $factory(nameOrSelector: string): Array<Base>;
41
+ /**
42
+ * Class constructor where all the magic takes place.
43
+ *
44
+ * @param {HTMLElement} element The component's root element dd.
45
+ */
46
+ constructor(element: HTMLElement);
47
+ /**
48
+ * The instance parent.
49
+ * @type {Base}
50
+ */
51
+ $parent: Base;
52
+ /**
53
+ * The instance id.
54
+ * @type {string}
55
+ */
56
+ $id: string;
57
+ /**
58
+ * The root element.
59
+ * @type {HTMLElement}
60
+ */
61
+ $el: HTMLElement;
62
+ /**
63
+ * The state of the component.
64
+ * @type {Boolean}
65
+ */
66
+ $isMounted: boolean;
67
+ /**
68
+ * Get the root instance of the app.
69
+ * @return {Base}
70
+ */
71
+ get $root(): Base;
72
+ /**
73
+ * Merge configuration with the parents' configurations.
74
+ *
75
+ * @return {BaseConfig}
76
+ * @private
77
+ */
78
+ private get __config();
79
+ /**
80
+ * @type {ServicesManager}
81
+ * @private
82
+ */
83
+ private __services;
84
+ /**
85
+ * @type {ServicesManager}
86
+ */
87
+ get $services(): ServicesManager;
88
+ /**
89
+ * @type {RefsManager}
90
+ * @private
91
+ */
92
+ private __refs;
93
+ /**
94
+ * @return {RefsManager}
95
+ */
96
+ get $refs(): RefsManager;
97
+ /**
98
+ * @type {BaseOptions}
99
+ * @private
100
+ */
101
+ private __options;
102
+ /**
103
+ * @return {BaseOptions}
104
+ */
105
+ get $options(): BaseOptions;
106
+ /**
107
+ * @type {ChildrenManager}
108
+ * @private
109
+ */
110
+ private __children;
111
+ /**
112
+ * @return {ChildrenManager}
113
+ */
114
+ get $children(): ChildrenManager;
115
+ /**
116
+ * @type {EventsManager}
117
+ * @private
118
+ */
119
+ private __events;
120
+ /**
121
+ * Small helper to log stuff.
122
+ *
123
+ * @return {(...args: any) => void}
124
+ */
125
+ get $log(): (...args: any) => void;
126
+ /**
127
+ * Small helper to debug information.
128
+ *
129
+ * @return {(...args:any) => void}
130
+ */
131
+ get __debug(): (...args: any) => void;
132
+ /**
133
+ * Trigger the `mounted` callback.
134
+ * @return {this}
135
+ */
136
+ $mount(): this;
137
+ /**
138
+ * Update the instance children.
139
+ * @return {this}
140
+ */
141
+ $update(): this;
142
+ /**
143
+ * Trigger the `destroyed` callback.
144
+ * @return {this}
145
+ */
146
+ $destroy(): this;
147
+ /**
148
+ * Terminate a child instance when it is not needed anymore.
149
+ * @return {void}
150
+ */
151
+ $terminate(): void;
152
+ /**
153
+ * Bind a listener function to an event.
154
+ *
155
+ * @param {string} event
156
+ * Name of the event.
157
+ * @param {EventListenerOrEventListenerObject} listener
158
+ * Function to be called.
159
+ * @param {boolean|AddEventListenerOptions} [options]
160
+ * Options for the `removeEventListener` method.
161
+ * @return {() => void}
162
+ * A function to unbind the listener.
163
+ */
164
+ $on(event: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): () => void;
165
+ /**
166
+ * Unbind a listener function from an event.
167
+ *
168
+ * @param {string} event
169
+ * Name of the event.
170
+ * @param {EventListenerOrEventListenerObject} listener
171
+ * Function to be removed.
172
+ * @param {boolean|EventListenerOptions} [options]
173
+ * Options for the `removeEventListener` method.
174
+ *
175
+ * @return {void}
176
+ */
177
+ $off(event: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
178
+ /**
179
+ * Emits an event.
180
+ *
181
+ * @param {string} event
182
+ * Name of the event.
183
+ * @param {any[]} args The arguments to apply to the functions bound to this event.
184
+ * @return {void}
185
+ */
186
+ $emit(event: string, ...args: any[]): void;
187
+ }
188
+ export type BaseConstructor = typeof Base;
189
+ export type BaseAsyncConstructor = (Base: any) => Promise<BaseConstructor | {
190
+ default: BaseConstructor;
191
+ }>;
192
+ export type BaseOptions = OptionsManager & {
193
+ [name: string]: any;
194
+ };
195
+ export type BaseRefs = {
196
+ [name: string]: HTMLElement | HTMLElement[];
197
+ };
198
+ export type BaseChildren = {
199
+ [nameOrSelector: string]: Base[] | Promise<Base>[];
200
+ };
201
+ export type BaseConfigComponents = {
202
+ [nameOrSelector: string]: typeof Base | BaseAsyncConstructor;
203
+ };
204
+ export type BaseConfigOptions = import('./managers/OptionsManager').OptionsSchema;
205
+ export type ServiceName = import('./managers/ServicesManager.js').ServiceName;
206
+ export type BaseConfig = {
207
+ name: string;
208
+ debug?: boolean;
209
+ log?: boolean;
210
+ refs?: string[];
211
+ components?: BaseConfigComponents;
212
+ options?: BaseConfigOptions;
213
+ };
214
+ import ServicesManager from "./managers/ServicesManager.js";
215
+ import RefsManager from "./managers/RefsManager.js";
216
+ import ChildrenManager from "./managers/ChildrenManager.js";
217
+ import OptionsManager from "./managers/OptionsManager.js";
package/Base/index.js ADDED
@@ -0,0 +1,237 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
6
+
7
+ import { callMethod, getComponentElements } from './utils.js';
8
+ import ChildrenManager from './managers/ChildrenManager.js';
9
+ import RefsManager from './managers/RefsManager.js';
10
+ import ServicesManager from './managers/ServicesManager.js';
11
+ import EventsManager from './managers/EventsManager.js';
12
+ import OptionsManager from './managers/OptionsManager.js';
13
+ var id = 0;
14
+
15
+ function noop() {}
16
+
17
+ export default class Base {
18
+ get $root() {
19
+ if (!this.$parent) {
20
+ return this;
21
+ }
22
+
23
+ var parent = this.$parent;
24
+ var root = this.$parent;
25
+
26
+ while (parent) {
27
+ if (!parent.$parent) {
28
+ root = parent;
29
+ }
30
+
31
+ parent = parent.$parent;
32
+ }
33
+
34
+ return root;
35
+ }
36
+
37
+ get __config() {
38
+ var proto = Object.getPrototypeOf(this);
39
+ var {
40
+ config
41
+ } = proto.constructor;
42
+
43
+ while (proto.constructor.config && proto.constructor.$isBase) {
44
+ config = _objectSpread(_objectSpread({}, proto.constructor.config), config);
45
+
46
+ if (proto.constructor.config.options) {
47
+ config.options = _objectSpread(_objectSpread({}, proto.constructor.config.options), config.options);
48
+ }
49
+
50
+ proto = Object.getPrototypeOf(proto);
51
+ }
52
+
53
+ return config;
54
+ }
55
+
56
+ get $services() {
57
+ var services = this.__services;
58
+ this.$emit('get:services', services);
59
+ return services;
60
+ }
61
+
62
+ get $refs() {
63
+ var refs = this.__refs;
64
+ this.$emit('get:refs', refs);
65
+ return refs;
66
+ }
67
+
68
+ get $options() {
69
+ var options = this.__options;
70
+ this.$emit('get:options', options);
71
+ return options;
72
+ }
73
+
74
+ get $children() {
75
+ var children = this.__children;
76
+ this.$emit('get:children', children);
77
+ return children;
78
+ }
79
+
80
+ get $log() {
81
+ return this.__options.log ? window.console.log.bind(window, "[".concat(this.__config.name, "]")) : noop;
82
+ }
83
+
84
+ get __debug() {
85
+ return this.__options.debug && typeof window.__DEV__ !== 'undefined' && window.__DEV__ ? window.console.log.bind(window, "[debug] [".concat(this.$id, "]")) : noop;
86
+ }
87
+
88
+ constructor(element) {
89
+ _defineProperty(this, "$parent", null);
90
+
91
+ _defineProperty(this, "$id", void 0);
92
+
93
+ _defineProperty(this, "$el", void 0);
94
+
95
+ _defineProperty(this, "$isMounted", false);
96
+
97
+ _defineProperty(this, "__services", void 0);
98
+
99
+ _defineProperty(this, "__refs", void 0);
100
+
101
+ _defineProperty(this, "__options", void 0);
102
+
103
+ _defineProperty(this, "__children", void 0);
104
+
105
+ _defineProperty(this, "__events", void 0);
106
+
107
+ if (!element) {
108
+ throw new Error('The root element must be defined.');
109
+ }
110
+
111
+ var {
112
+ __config
113
+ } = this;
114
+
115
+ if (!__config) {
116
+ throw new Error('The `config` static property must be defined.');
117
+ }
118
+
119
+ if (!__config.name) {
120
+ throw new Error('The `config.name` property is required.');
121
+ }
122
+
123
+ this.$id = "".concat(__config.name, "-").concat(id);
124
+ id += 1;
125
+ this.$el = element;
126
+
127
+ if (!('__base__' in this.$el)) {
128
+ Object.defineProperty(this.$el, '__base__', {
129
+ get: () => this,
130
+ configurable: true
131
+ });
132
+ }
133
+
134
+ this.__options = new OptionsManager(element, __config.options || {}, __config);
135
+ this.__services = new ServicesManager(this);
136
+ this.__events = new EventsManager(element, this);
137
+ this.__children = new ChildrenManager(this, element, __config.components || {}, this.__events);
138
+ this.__refs = new RefsManager(this, element, __config.refs || [], this.__events);
139
+
140
+ this.__debug('constructor', this);
141
+
142
+ return this;
143
+ }
144
+
145
+ $mount() {
146
+ this.__debug('$mount');
147
+
148
+ this.$children.registerAll();
149
+ this.$refs.registerAll();
150
+
151
+ this.__events.bindRootElement();
152
+
153
+ this.$services.enableAll();
154
+ this.$children.mountAll();
155
+ callMethod(this, 'mounted');
156
+ this.$isMounted = true;
157
+ return this;
158
+ }
159
+
160
+ $update() {
161
+ this.__debug('$update');
162
+
163
+ this.$refs.unregisterAll();
164
+ this.$services.disableAll();
165
+ this.$children.registerAll();
166
+ this.$refs.registerAll();
167
+ this.$services.enableAll();
168
+ this.$children.updateAll();
169
+ callMethod(this, 'updated');
170
+ return this;
171
+ }
172
+
173
+ $destroy() {
174
+ this.__debug('$destroy');
175
+
176
+ this.__events.unbindRootElement();
177
+
178
+ this.$refs.unregisterAll();
179
+ this.$services.disableAll();
180
+ this.$children.destroyAll();
181
+ callMethod(this, 'destroyed');
182
+ this.$isMounted = false;
183
+ return this;
184
+ }
185
+
186
+ $terminate() {
187
+ this.__debug('$terminate');
188
+
189
+ this.$destroy();
190
+ callMethod(this, 'terminated');
191
+ Object.defineProperty(this.$el, '__base__', {
192
+ value: 'terminated',
193
+ configurable: false,
194
+ writable: false
195
+ });
196
+ }
197
+
198
+ static $factory(nameOrSelector) {
199
+ if (!nameOrSelector) {
200
+ throw new Error('The $factory method requires a component’s name or selector to be specified.');
201
+ }
202
+
203
+ return getComponentElements(nameOrSelector).map(el => new this(el).$mount());
204
+ }
205
+
206
+ $on(event, listener, options) {
207
+ this.__debug('$on', event, listener, options);
208
+
209
+ this.$el.addEventListener(event, listener, options);
210
+ return () => {
211
+ this.$off(event, listener, options);
212
+ };
213
+ }
214
+
215
+ $off(event, listener, options) {
216
+ this.__debug('$off', event, listener);
217
+
218
+ this.$el.removeEventListener(event, listener, options);
219
+ }
220
+
221
+ $emit(event) {
222
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
223
+ args[_key - 1] = arguments[_key];
224
+ }
225
+
226
+ this.__debug('$emit', event, args);
227
+
228
+ this.$el.dispatchEvent(new CustomEvent(event, {
229
+ detail: args
230
+ }));
231
+ }
232
+
233
+ }
234
+
235
+ _defineProperty(Base, "$isBase", true);
236
+
237
+ _defineProperty(Base, "config", void 0);
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @typedef {import('../index.js').default} Base
3
+ * @typedef {import('../index.js').BaseConstructor} BaseConstructor
4
+ * @typedef {import('../index.js').BaseAsyncConstructor} BaseAsyncConstructor
5
+ * @typedef {import('../index.js').BaseConfigComponents} BaseConfigComponents
6
+ * @typedef {import('./EventsManager.js').default} EventsManager
7
+ */
8
+ /**
9
+ * Children manager.
10
+ */
11
+ export default class ChildrenManager {
12
+ /**
13
+ * Class constructor.
14
+ * @param {Base} base
15
+ * The component's instance.
16
+ * @param {HTMLElement} element
17
+ * The component's root element
18
+ * @param {BaseConfigComponents} components
19
+ * The children components' classes
20
+ * @param {EventsManager} eventsManager
21
+ */
22
+ constructor(base: Base, element: HTMLElement, components: BaseConfigComponents, eventsManager: EventsManager);
23
+ /**
24
+ * @type {Base}
25
+ * @private
26
+ */
27
+ private __base;
28
+ /**
29
+ * @type {HTMLElement}
30
+ * @private
31
+ */
32
+ private __element;
33
+ /**
34
+ * @type {BaseConfigComponents}
35
+ * @private
36
+ */
37
+ private __components;
38
+ /**
39
+ * @type {EventsManager}
40
+ * @private
41
+ */
42
+ private __eventsManager;
43
+ /**
44
+ * @return {string[]}
45
+ */
46
+ get registeredNames(): string[];
47
+ /**
48
+ * Register instances of all children components.
49
+ */
50
+ registerAll(): void;
51
+ /**
52
+ * Register instance of a child component.
53
+ *
54
+ * @param {string} name
55
+ * The name of the child component.
56
+ * @param {BaseConstructor|BaseAsyncConstructor} component
57
+ * A Base class or a Promise for async components.
58
+ * @private
59
+ */
60
+ private __register;
61
+ /**
62
+ * Get a child component's instance.
63
+ *
64
+ * @param {HTMLElement & { __base__?: Base | 'terminated' }} el
65
+ * The root element of the child component.
66
+ * @param {BaseConstructor|BaseAsyncConstructor} ComponentClass
67
+ * A Base class or a Promise for async components.
68
+ * @param {string} name
69
+ * The name of the child component.
70
+ * @return {Base|Promise|'terminated'}
71
+ * A Base instance or a Promise resolving to a Base instance.
72
+ * @private
73
+ */
74
+ private __getChild;
75
+ /**
76
+ * Mount all child component instances.
77
+ */
78
+ mountAll(): void;
79
+ /**
80
+ * Update all child component instances.
81
+ */
82
+ updateAll(): void;
83
+ /**
84
+ * Destroy all child component instances.
85
+ */
86
+ destroyAll(): void;
87
+ /**
88
+ * Execute the given hook for all children instances.
89
+ *
90
+ * @param {'$mount'|'$update'|'$destroy'} hook The hook to execute.
91
+ * @private
92
+ */
93
+ private __triggerHookForAll;
94
+ /**
95
+ * Execute the given hook for the given instance.
96
+ *
97
+ * @param {'$mount'|'$update'|'$destroy'} hook The hook to trigger.
98
+ * @param {Base} instance The target instance.
99
+ * @param {string} name The name of the component.
100
+ * @private
101
+ */
102
+ private __triggerHook;
103
+ }
104
+ export type Base = import('../index.js').default;
105
+ export type BaseConstructor = import('../index.js').BaseConstructor;
106
+ export type BaseAsyncConstructor = import('../index.js').BaseAsyncConstructor;
107
+ export type BaseConfigComponents = import('../index.js').BaseConfigComponents;
108
+ export type EventsManager = import('./EventsManager.js').default;
@@ -0,0 +1,124 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import { getComponentElements } from '../utils.js';
3
+ export default class ChildrenManager {
4
+ get registeredNames() {
5
+ return Object.keys(this).filter(key => !key.startsWith('__'));
6
+ }
7
+
8
+ constructor(base, element, components, eventsManager) {
9
+ _defineProperty(this, "__base", void 0);
10
+
11
+ _defineProperty(this, "__element", void 0);
12
+
13
+ _defineProperty(this, "__components", void 0);
14
+
15
+ _defineProperty(this, "__eventsManager", void 0);
16
+
17
+ Object.defineProperties(this, {
18
+ __base: {
19
+ enumerable: false,
20
+ writable: false,
21
+ value: base
22
+ },
23
+ __element: {
24
+ enumerable: false,
25
+ writable: false,
26
+ value: element
27
+ },
28
+ __components: {
29
+ enumerable: false,
30
+ writable: false,
31
+ value: components
32
+ },
33
+ __eventsManager: {
34
+ enumerable: false,
35
+ writable: false,
36
+ value: eventsManager
37
+ }
38
+ });
39
+ }
40
+
41
+ registerAll() {
42
+ Object.entries(this.__components).forEach(_ref => {
43
+ var [name, component] = _ref;
44
+ return this.__register(name, component);
45
+ });
46
+ }
47
+
48
+ __register(name, component) {
49
+ Object.defineProperty(this, name, {
50
+ enumerable: true,
51
+ configurable: true,
52
+ get: () => {
53
+ var elements = getComponentElements(name, this.__element);
54
+
55
+ if (elements.length === 0) {
56
+ return [];
57
+ }
58
+
59
+ return elements.map(element => this.__getChild(element, component, name)).filter(instance => instance !== 'terminated');
60
+ }
61
+ });
62
+ }
63
+
64
+ __getChild(el, ComponentClass, name) {
65
+ if (el.__base__) {
66
+ return el.__base__;
67
+ }
68
+
69
+ if ('$isBase' in ComponentClass) {
70
+ var child = new ComponentClass(el);
71
+ Object.defineProperty(child, '$parent', {
72
+ get: () => this.__base
73
+ });
74
+ return child;
75
+ }
76
+
77
+ return ComponentClass(this.__base).then(module => {
78
+ var _module$default;
79
+
80
+ return this.__getChild(el, (_module$default = module.default) !== null && _module$default !== void 0 ? _module$default : module, name);
81
+ });
82
+ }
83
+
84
+ mountAll() {
85
+ this.__triggerHookForAll('$mount');
86
+ }
87
+
88
+ updateAll() {
89
+ this.__triggerHookForAll('$update');
90
+ }
91
+
92
+ destroyAll() {
93
+ this.__triggerHookForAll('$destroy');
94
+ }
95
+
96
+ __triggerHookForAll(hook) {
97
+ this.registeredNames.forEach(name => {
98
+ this[name].forEach(instance => {
99
+ if (instance instanceof Promise) {
100
+ instance.then(resolvedInstance => this.__triggerHook(hook, resolvedInstance, name));
101
+ } else {
102
+ this.__triggerHook(hook, instance, name);
103
+ }
104
+ });
105
+ });
106
+ }
107
+
108
+ __triggerHook(hook, instance, name) {
109
+ if (hook === '$update' && !instance.$isMounted) {
110
+ hook = '$mount';
111
+ }
112
+
113
+ if (hook === '$update' || hook === '$destroy') {
114
+ this.__eventsManager.unbindChild(name, instance);
115
+ }
116
+
117
+ if (hook === '$update' || hook === '$mount') {
118
+ this.__eventsManager.bindChild(name, instance);
119
+ }
120
+
121
+ instance[hook]();
122
+ }
123
+
124
+ }