-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
305 lines (252 loc) · 8.49 KB
/
index.js
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
/* global TD */
TD.extensions = {}
TD.extensions._list = []
TD.extensions._init = []
/**
* Adds an extension to the global extension list
*
* @param {object} extension TD.extension object
* @param {boolean} enable enable extension right after addition
* @param {boolean} init initialize extension right after addition
*/
TD.extensions.add = (extension, enable, init) => {
if (TD.extensions.getExtension(extension.name) === undefined) {
TD.extensions._list.push(extension)
try {
TD.extensions.reorderExtensions()
} catch (exception) {
// Silent catch, most likely so because a dependency we require is not here yet
}
if (enable) {
TD.extensions.enable(extension.name, init)
}
}
}
/**
* Removes an extension from the global extension list
* Also disables and destroys it in the progress (if not already done)
*
* @param {string} extensionName name of the extension to be removed
*/
TD.extensions.remove = (extensionName) => {
let extension = TD.extensions.getExtension(extensionName)
let extensionIndex = TD.extensions._list.indexOf(extension)
TD.extensions.disable(extensionName)
TD.extensions._list.splice(extensionIndex, 1)
TD.extensions.reorderExtensions()
}
/**
* Get an extension object returned by name
*
* @param {string} extensionName name of the extension to get
*/
TD.extensions.getExtension = (extensionName) => {
return TD.extensions._list.filter(extension => extension.name === extensionName)[0]
}
/**
* Returns the names of all extensions in the global extension list
*
* @returns {array}
*/
TD.extensions.getAll = () => {
return TD.extensions._list.map(extension => extension.name)
}
/**
* Returns the names of all extensions currently enabled from localStorage
*
* @returns {array}
*/
TD.extensions.getAllEnabled = () => {
let enabled = JSON.parse(window.localStorage.getItem('TD.extensions.enabled'))
if (enabled === null) {
window.localStorage.setItem('TD.extensions.enabled', JSON.stringify([]))
return []
} else {
return enabled
}
}
/**
* Returns the names of all extensions currently initialized
*
* @returns {array}
*/
TD.extensions.getAllInitialized = () => {
return TD.extensions._init
}
/**
* Enables an extension, so it will be run the next time .init() is executed
* or the `init` parameter is passed
*
* Rules so that an extension will be enabled:
* - it isn't already enabled
* - the dependencies it requires are already enabled
* - it has no conflicts with currently enabled extensions
*
* @param {string} extensionName name of the extension to be enabled
* @param {boolean} init initialize the extension right after enabling
*/
TD.extensions.enable = (extensionName, init) => {
let enabledExtensions = TD.extensions.getAllEnabled()
let extension = TD.extensions.getExtension(extensionName)
if (!TD.extensions.isEnabled(extension.name) &&
TD.extensions.checkDependencies(extension.name) &&
TD.extensions.checkConflicts(extension.name)) {
enabledExtensions.push(extensionName)
enabledExtensions = TD.extensions.reorderEnabledExtensions(enabledExtensions)
window.localStorage.setItem('TD.extensions.enabled', JSON.stringify(enabledExtensions))
}
if (init) {
TD.extensions.initializeExtension(extension.name)
}
}
/**
* Disables an extension and all extensions that depend on it
*
* @param {string} extensionName name of the extension to be disabled
*/
TD.extensions.disable = (extensionName) => {
let enabledExtensions = TD.extensions.getAllEnabled()
if (TD.extensions.isEnabled(extensionName)) {
let extensionIndex = enabledExtensions.indexOf(extensionName)
enabledExtensions.splice(extensionIndex, 1)
enabledExtensions = TD.extensions.reorderEnabledExtensions(enabledExtensions)
window.localStorage.setItem('TD.extensions.enabled', JSON.stringify(enabledExtensions))
enabledExtensions.forEach((enabledExtension) => {
TD.extensions.checkDependencies(enabledExtension)
})
}
if (TD.extensions.isInitialized(extensionName)) {
TD.extensions.destroyExtension(extensionName)
}
}
/**
* Initialize all extensions
*/
TD.extensions.init = () => {
let enabledExtensions = TD.extensions.getAllEnabled()
enabledExtensions.forEach((extension) => {
TD.extensions.initializeExtension(extension)
})
}
/**
* Checks the dependencies for a given extension
*
* @param {string} extensionName name of the extension to be checked
*
* @returns {boolean} true if dependencies could be resolved, false if any dependency is missing
*/
TD.extensions.checkDependencies = (extensionName) => {
let extension = TD.extensions.getExtension(extensionName)
if (extension.dependencies === undefined) {
return true
}
let extensionDependencyStatus = extension.dependencies.every((dependency) => TD.extensions.isEnabled(dependency))
if (extensionDependencyStatus === false && TD.extensions.isEnabled(extensionName)) {
TD.extensions.disable(extensionName)
return false
} else {
return extensionDependencyStatus
}
}
/**
* Checks the conflicts of a given extension
*
* @param {string} extensionName name of the extension to be checked
*
* @returns {boolean} true if no conflicts exist with enabled extensions, false if there are conflicts
*/
TD.extensions.checkConflicts = (extensionName) => {
let extension = TD.extensions.getExtension(extensionName)
if (extension.conflicts === undefined) {
return true
}
let extensionConflictStatus = extension.conflicts.some(conflict => TD.extensions.isEnabled(conflict))
return !extensionConflictStatus
}
/**
* Check if given extension is enabled
*
* @param {string} extensionName name of the extension to be checked
*
* @returns {boolean} boolean value representing if an extension is enabled
*/
TD.extensions.isEnabled = (extensionName) => {
return TD.extensions.getAllEnabled().includes(extensionName)
}
/**
* Check if given extension is initialized
*
* @param {string} extensionName name of the extension to be checked
*
* @returns {boolean} boolean value representing if an extension is initialized
*/
TD.extensions.isInitialized = (extensionName) => {
return TD.extensions.getAllInitialized().includes(extensionName)
}
/**
* Resolves the extensions list dependency tree and returns a sorted list of extension names
*
* @returns {array} sorted array of extension names
*/
TD.extensions.resolveDependencyGraph = () => {
let sorted = []
let visited = {}
TD.extensions._list.forEach(function visit (extension, ancestors) {
if (!Array.isArray(ancestors)) ancestors = []
if (extension.name === undefined) return
ancestors.push(extension.name)
visited[extension.name] = true
if (extension.dependencies) {
extension.dependencies.forEach(function (dependency) {
if (ancestors.indexOf(dependency) >= 0) {
throw new Error('Circular dependency "' + dependency + '" is required by "' + extension.name + '": ' + ancestors.join(' -> '))
}
if (visited[dependency]) return
visit(TD.extensions.getExtension(dependency), ancestors.slice(0))
})
}
if (sorted.indexOf(extension.name) < 0) sorted.push(extension.name)
})
return sorted
}
/**
* Initializes the given extension
*
* @param {string} extensionName name of extension to be initialized
*/
TD.extensions.initializeExtension = (extensionName) => {
let extension = TD.extensions.getExtension(extensionName)
if (!TD.extensions.isInitialized(extensionName)) {
extension.create()
TD.extensions._init.push(extensionName)
}
}
/**
* Destroys the given extension
*
* @param {string} extensionName name of extension to be destroyed
*/
TD.extensions.destroyExtension = (extensionName) => {
let extension = TD.extensions.getExtension(extensionName)
if (TD.extensions.isInitialized(extensionName)) {
let extensionIndex = TD.extensions._init.indexOf(extensionName)
extension.destroy()
TD.extensions._init.splice(extensionIndex, 1)
}
}
/**
* Reorders the extension list using the resolved dependency graph
*/
TD.extensions.reorderExtensions = () => {
let extensionOrder = TD.extensions.resolveDependencyGraph()
TD.extensions._list = extensionOrder.map((extension) => TD.extensions.getExtension(extension))
}
/**
* Reorders the enabled extension list using the resolved dependency graph
*
* @returns the ordered list of enabled extensions
*/
TD.extensions.reorderEnabledExtensions = (enabledExtensions) => {
let extensionOrder = TD.extensions.resolveDependencyGraph()
return extensionOrder.filter((extension) => enabledExtensions.includes(extension))
}