-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
323 lines (265 loc) · 7.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import {
addEventListener,
IAddEventListener,
IAddEventListenerOptions,
ListenerElement,
} from 'vevet-dom';
import { DeepRequired } from 'ts-essentials';
import { NModule } from './types';
import { MutableProps, NMutableProps } from '../MutableProps';
import { Callbacks, NCallbacks } from '../Callbacks';
import { Viewport } from '@/src/Application/events/Viewport';
import { TRequiredModuleProp } from '@/types/utility';
export type { NModule };
type TViewportAdd = Viewport['add'];
/**
* Module base
*/
export class Module<
StaticProps extends NModule.IStaticProps = NModule.IStaticProps,
ChangeableProps extends NModule.IChangeableProps = NModule.IChangeableProps,
CallbacksTypes extends NModule.ICallbacksTypes = NModule.ICallbacksTypes,
> {
/**
* Get Default properties (should be extended)
*/
protected _getDefaultProps() {
return { parent: null } as DeepRequired<StaticProps & ChangeableProps>;
}
/** Current properties */
get props() {
return this._mutableProps.props as TRequiredModuleProp<
StaticProps & ChangeableProps
>;
}
/** Responsive properties */
private _mutableProps: MutableProps<StaticProps, ChangeableProps>;
/** Module Callbacks */
private _callbacks: Callbacks<CallbacksTypes>;
/** Module Callbacks */
get callbacks() {
return this._callbacks;
}
/** Module listeners */
private _listeners: IAddEventListener[];
/**
* Destroyable actions
*/
private _destroyableActions: (() => void)[];
/**
* Classnames to remove
*/
private _classNamesToRemove: NModule.IClassNamesToRemove[];
/** Vevet Application */
private _app!: typeof window.vevetApp;
/** Vevet Application */
get app() {
return this._app;
}
/** Module name */
get name() {
return this.constructor.name;
}
/** Module prefix */
get prefix() {
return '';
}
/** The module is initialized */
private _isInitialized = false;
/** The module is initialized */
get isInitialized() {
return this._isInitialized;
}
/** The module is destroyed */
private _isDestroyed = false;
/** The module is destroyed */
get isDestroyed() {
return this._isDestroyed;
}
/**
* @example
*
* interface IStaticProps extends NModule.IStaticProps {
* staticName: 'My name';
* }
* interface IChangeableProps extends NModule.IChangeableProps {
* weight: number;
* height: number;
* }
* interface ICallbacks extends NModule.ICallbacksTypes {}
* const module = new Module<IStaticProps, IChangeableProps, ICallbacks>(
* {
* staticName: 'My name',
* weight: 70,
* height: 175,
* },
* false
* );
* module.addResponsiveProps({
* breakpoint: 'viewport_phone',
* settings: {
* weight: 80,
* },
* });
* module.init();
* module.addCallback('propsMutate', () => console.log('mutate props'));
* module.addCallback('destroy', () => console.log('destroy'));
*/
constructor(
/** Initial properties */
initialProps?: StaticProps & ChangeableProps,
/**
* Defines if you need to call {@linkcode Module.init} at the constructor's end.
* If you want to add responsive properties, set this parameter to `false`.
*/
canInit = true,
) {
if (window.vevetApp) {
this._app = window.vevetApp;
} else {
throw new Error(
'Vevet.Application does not exist yet. Call "new Vevet.Application()" before using all the stuff',
);
}
this._callbacks = new Callbacks<CallbacksTypes>();
this._listeners = [];
this._destroyableActions = [];
this._classNamesToRemove = [];
const props = {
...this._getDefaultProps(),
...(initialProps || {}),
};
this._mutableProps = new MutableProps(
props as StaticProps & ChangeableProps,
() => this._onPropsMutate(),
this.name,
);
if (canInit) {
this.init();
}
}
/** Add responsive rules */
public addResponsiveProps(rules: NMutableProps.IResponsive<ChangeableProps>) {
if (this.isInitialized) {
throw new Error(
'Responsive properties cannot be added after `init` is called',
);
} else {
this._mutableProps.addResponsiveProps(rules);
}
}
/** Change module properties */
public changeProps(props: Partial<ChangeableProps>) {
if (this.isDestroyed) {
return;
}
this._mutableProps.changeProps(props);
this._callbacks.tbt('propsChange', undefined);
}
/**
* This method is called on properties change
*/
protected _onPropsMutate() {
this._callbacks.tbt('propsMutate', undefined);
}
/** Initializes the class */
public init() {
if (this.isInitialized) {
return;
}
this._isInitialized = true;
this._init();
// destroy the current module on parent destroy
if (this.props.parent) {
this.props.parent.addCallback('destroy', () => this.destroy(), {
isProtected: true,
name: this.name,
});
}
}
/** Inner extra initialization */
protected _init() {}
/** Add destroyable actions */
protected addDestroyableAction(action: () => void) {
this._destroyableActions.push(action);
}
/**
* Add a viewport callback that will be removed on class destroy
*/
public addViewportCallback(
target: Parameters<TViewportAdd>[0],
action: Parameters<TViewportAdd>[1],
data: Parameters<TViewportAdd>[2] = {},
) {
const callback = this._app.viewport.add(target, action, {
...data,
name: this.constructor.name,
});
this.addDestroyableAction(() => callback.remove());
}
/** Add a module callback */
public addCallback<T extends keyof CallbacksTypes>(
target: T,
action: NCallbacks.TAction<CallbacksTypes[T]>,
settings: NCallbacks.ISettings = {},
) {
return this.callbacks.add(target, action, settings);
}
/** Add a DOM event listener */
public addEventListener<
El extends ListenerElement,
Target extends keyof HTMLElementEventMap,
Callback extends (evt: HTMLElementEventMap[Target]) => void,
>(
el: El,
target: Target,
callback: Callback,
options?: IAddEventListenerOptions,
): IAddEventListener {
const listener = addEventListener(el, target, callback, options);
this._listeners.push(listener);
return {
...listener,
remove: () => {
this._listeners = this._listeners.filter(
(item) => item.id !== listener.id,
);
return listener.remove();
},
};
}
protected className(...classNames: string[]) {
return classNames.map((value) => `${this.prefix}${value}`).join(' ');
}
/** Toggle classList */
protected toggleClassName(
element: Element,
className: string,
isActive: boolean,
) {
const isAlreadyExists = element.classList.contains(className);
element.classList.toggle(className, isActive);
if (!isAlreadyExists) {
this._classNamesToRemove.push({ element, className });
}
}
/** Destroy the module */
public destroy() {
if (this.isDestroyed) {
return;
}
this._destroy();
}
/** Destroy the module */
protected _destroy() {
this._callbacks.tbt('destroy', undefined);
this._callbacks.destroy();
this._mutableProps.destroy();
this._destroyableActions.forEach((action) => action());
this._listeners.forEach((listener) => listener.remove());
this._classNamesToRemove.forEach(({ element, className }) =>
element.classList.remove(className),
);
this._isDestroyed = true;
}
}