This repository has been archived by the owner on Dec 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Application.ts
179 lines (140 loc) · 5.29 KB
/
Application.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
import { Module } from './Module';
import { Env } from './Env';
import * as uuid from 'uuid/v4';
import DeviceManager from '@pageworks/device-manager';
import Pjax from '@pageworks/pjax';
export class Application{
public static modules:Array<Module> = [];
constructor(){
new DeviceManager(Env.isDebug, true);
// new Pjax({ debug: Env.isDebug });
new Env();
Env.stopLoading();
}
/**
* Called when the application must mount any pending modules
*/
public static mountModules():void{
/** Get any modules that are pending that have not been mounted */
const pendingModules:Array<HTMLElement> = Array.from(document.body.querySelectorAll('[data-module]:not([data-uuid])'));
pendingModules.forEach((requestedModule)=>{
/** Get what module is being requested */
const moduleIndex = requestedModule.dataset.module;
/** Create the module */
Application.createModule(moduleIndex, requestedModule);
});
/** Manage the lazy (loaded) parents */
Application.manageLazyParents();
}
/**
* Called when lazy parents need to be checked.
* Parents are lazy when the child (sub) module loads before them.
*/
public static manageLazyParents():void{
/** Loop though all modules */
Application.modules.forEach((module)=>{
/** Check if the module has a future parent element */
if(module.futureParent){
/** Get the module attached to the parent */
module.parent = Application.getModuleByUUID(module.futureParent.getAttribute('data-uuid'));
/** Check if the parent was set */
if(module.parent){
module.futureParent = null;
/** Register the module as a child of the parent */
module.parent.register(module);
}
}
});
}
/**
* Attempt to create a new module.
* @param index - the public static index string for the class
* @param view - the `HTMLElement` that the module will be attached to
* @returns `Module` or `null`
*/
public static createModule(index:string, view:HTMLElement):Module{
/** Assume the module can't be created */
let newModule:Module = null;
try{
/** Create a new UUID */
const id = uuid();
/** Create the module */
newModule = new modules[index].prototype.constructor(view, id);
/** Attempt to mount the module */
newModule.mount();
}catch(e){
/** If the error wasn't caused by an undefined module report it */
if(modules[index] !== undefined){
console.error('Failed to create module', e);
}
}
/** Check if the module was successfully created */
if(newModule){
Application.modules.push(newModule);
/** Trigger the modules after mount event */
newModule.afterMount();
}
return newModule;
}
/**
* Called when a module needs to be destroyed.
* @param uuid - the unique ID of the module
*/
public static destroyModule(uuid:string):void{
/** Verify a UUID was provided */
if(uuid){
/** Loop through the modules */
this.modules.forEach((module)=>{
/** Check if the UUIDs match */
if(module.uuid === uuid){
/** Get the array index of the module */
const index = this.modules.indexOf(module);
/** Fire the before destroy event */
module.beforeDestroy().then(() => {
/** Fire the destroy event */
module.destroy();
/** Splice the module from the array */
this.modules.splice(index, 1);
})
.catch(e => {
if(Env.isDebug){
console.error('Something went wrong while destroying a module', e);
}
/** Fire the destroy event */
module.destroy();
/** Splice the module from the array */
this.modules.splice(index, 1);
});
}
});
}else{
console.warn('No UUID provided');
}
}
/**
* Used to get a module by its UUID.
* @param uuid - the unique ID for the module
* @returns `Module` or `null`
*/
public static getModuleByUUID(uuid:string):Module{
/** Verify a UUID was provided */
if(!uuid){
console.warn('No UUID provided');
return null;
}
/** Assume the module doesn't exist */
let returnModule:Module = null;
/** Attempt to find the module */
this.modules.forEach((module)=>{
/** Check if the UUIDs match */
if(module.uuid === uuid){
returnModule = module;
}
});
return returnModule;
}
}
/** Starts the runtime application */
new Application();
/** Mount the initial modules */
Application.mountModules();