-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
528 lines (419 loc) · 15.6 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
let
AccountType,
storeAttr
const
__EXTS__ = {},
__EXTNS__ = {},
AutoLoadedExts = {},
TempoLoadedExts = {},
MimeTypeSupportExts = {}
function isExtension(){
return true
}
function getFavicon( dataset ){
// Retreive favicon of an extension from marketplace asset server
if( !dataset ) return ''
const { namespace, nsi, version, favicon } = dataset
return `${window.marketplace}/${namespace}/${nsi}~${version}/${favicon}`
}
function throwAlert( type, extname, options ){
switch( type ){
case 'EXTENSION_EXIST': if( !options )
options = {
status: 'Info',
message: `<span class="text-primary">${extname}</span> application is already installed in your workspace.`,
actions: false
}; break
case 'EXTENSION_NOT_FOUND': if( !options )
options = {
status: 'Alert',
message: `<span class="text-primary">${extname}</span> application is not available in your workspace. Install it from the marketplace to continue.`,
actions: {
passive: {
label: 'Go to Marketplace',
gstate: {
target: 'marketplace',
call: 'open',
arguments: [{ open: { name: extname } }]
}
}
}
}; break
}
options && GState.global.alert( options )
}
function runExt( id, payload ){
const
actives = GState.get('activeExtensions'),
values = Object.values( actives ),
maxIndex = values.length > 1 ?
Math.max( ...( values.map( ({ zindex }) => { return zindex } ) ) )
: values.length
// Clear notification badge this has on the toolbar
GState.notification.clear( id )
// Default workspace view mode
let WSMode = false
// Load new extension
if( !actives.hasOwnProperty( id ) ){
actives[ id ] = __EXTS__[ id ]
// Extension has a default workspace view mode
const { runscript } = actives[ id ]
WSMode = runscript
&& ( runscript.workspace
|| ( runscript[ AccountType ] && runscript[ AccountType ].workspace )
|| ( runscript['*'] && runscript['*'].workspace ) )
}
// No re-position required for single view block
else if( maxIndex <= 1 ){
// Add specified operation payload to loaded extension
if( payload ){
// actives[ id ].payload = payload
GState.dirty( id, payload )
GState.dirty( 'activeExtensions', actives )
uiStore.set( storeAttr, actives )
}
return
}
// Add specified operation payload to extension
if( payload ) GState.dirty( id, payload )
actives[ id ].zindex = maxIndex + 1 // Position targeted view block to the top
GState.dirty( 'activeExtensions', actives )
uiStore.set( storeAttr, actives )
// Show Aside in default/auto mode
;( !values.length || WSMode ) && GState.workspace.layout({ mode: WSMode || 'auto' })
}
function quitExt( id ){
const actives = GState.get('activeExtensions')
// Is not active
if( !actives.hasOwnProperty( id ) ) return
// Send quit signal to the application
GState.extension.signal( id, 'USER:QUIT' )
delete actives[ id ]
GState.set( 'activeExtensions', actives )
uiStore.set( storeAttr, actives )
// Clear in case of temporary loaded extension
if( TempoLoadedExts[ id ] ){
delete AutoLoadedExts[ id ]
delete TempoLoadedExts[ id ]
GState.dirty('Extensions', AutoLoadedExts )
uiStore.set( storeAttr +'-tempo', TempoLoadedExts )
}
// Hide Aside when all extension & marketplace are closed
!GState.get('marketplace')
&& GState.workspace.layout({ mode: !Object.keys(actives).length ? 'ns' : 'auto' })
}
async function refreshExt( id, payload ){
try {
// Get latest version of its metadata
const metadata = await get( id )
if( !metadata ) throw new Error('Unexpected Error Occured')
// Replace extension metadata
__EXTS__[ id ] = metadata
__EXTNS__[`${metadata.nsi}~${metadata.version}`] = metadata
// Re-run the extension with current payload if active
const actives = GState.get('activeExtensions')
if( actives.hasOwnProperty( id ) ){
delete actives[ id ]
runExt( id, payload )
}
}
catch( error ){ console.log('Failed Refreshing Extension: ', error ) }
}
async function getPlugin( id ){
// Fetch dependency plugin dataset from marketplace
try {
const { error, message, extension } = await window.MPSRequest(`/extension/${id}`)
if( error ) throw new Error( message )
return extension
}
catch( error ){ console.log('Failed Retreiving plugin dataset: ', error ) }
}
async function assignDependencies( extension ){
// Check and load an application/plugin dependencies
const deps = extension.resource
&& extension.resource.dependencies
&& extension.resource.dependencies.length
&& extension.resource.dependencies.filter( each => { return /^plugin:(.+)$/.test( each ) } )
if( !Array.isArray( deps ) || !deps.length ) return extension
for( const x in deps ){
let plugin = await getPlugin( deps[x] )
// No found
if( !plugin )
throw new Error(`<${deps[x]}> not found`)
// Also assign required plugin dependencies to this plugin if there is
plugin = await assignDependencies( plugin )
if( !extension.plugins ) extension.plugins = {}
extension.plugins[ plugin.nsi ] = plugin
}
return extension
}
function requirePermission({ resource }){
// Check whether an application requires or have a missing permissions
return resource
&& resource.permissions
&& resource.permissions.scope
&& resource.permissions.scope.length
&& resource.permissions.scope.filter( each => {
return typeof each == 'string'
|| ( typeof each == 'object' && each.type && !each.access )
} ).length
}
async function askPermission( type, requestor, list, __callback ){
function exec( resolve ){
function callback( list ){
GState.set('permissionRequest', null )
resolve( list )
}
GState.set('permissionRequest', { type, requestor, list, callback })
}
// JS callback method
if( typeof __callback == 'function' )
return exec( __callback )
// or return promise: async/await
return new Promise( exec )
}
// Extension handler API class
function ExtensionManager( id, metadata ){
this.id = id
this.meta = metadata
this.payload = null
this.run = payload => {
this.payload = payload
runExt( this.id, payload )
// Temporary load application to autoloaded list: Get removed when quit
if( !AutoLoadedExts[ this.id ] ){
AutoLoadedExts[ this.id ] =
TempoLoadedExts[ this.id ] = this.meta
GState.dirty('Extensions', AutoLoadedExts )
uiStore.set( storeAttr +'-tempo', TempoLoadedExts )
}
}
this.quit = () => {
this.payload = null
quitExt( this.id )
}
this.refresh = async () => refreshExt( this.id, this.payload )
}
GState
.define('extension')
.action( 'open', runExt )
.action( 'close', quitExt )
.action( 'signal', ( appId, code ) => GState.set( 'extension:signal', { appId, code } ) )
// Ask for data or hook access permissions
GState
.define('permission')
.action( 'ask', askPermission )
.action( 'check', () => {} )
window.Extensions = {
list: {},
favicon: getFavicon,
run: ( name, payload ) => {
if( !window.Extensions.list.hasOwnProperty( name ) ){
// Throw no found extension dialog
throwAlert( 'EXTENSION_NOT_FOUND', name )
return false
}
window.Extensions.list[ name ].run( payload )
return true
},
quit: name => {
if( !window.Extensions.list.hasOwnProperty( name ) ){
// Throw no found extension dialog
throwAlert( 'EXTENSION_NOT_FOUND', name )
return false
}
window.Extensions.list[ name ].quit()
return true
},
meta: query => {
// Retreive a given extension details by id or name or nsi
for( let id in __EXTS__ )
if( query == id
|| __EXTS__[ id ].nsi == query
|| __EXTS__[ id ].name == query )
return Object.assign( {}, __EXTS__[ id ], { id, favicon: getFavicon( __EXTS__[ id ] ) } )
// Extension not found
let
byAccount = 'Install it from the marketplace to continue', // Default (Admin)
actions = {
passive: {
label: 'Go to Marketplace',
gstate: {
target: 'marketplace',
call: 'open',
arguments: [{ open: { name: query } }]
}
}
}
// Learner or Admin account get different message: Not allow to install themeselves
if( GState.get('user').accounttype != 'ADMIN' ){
byAccount = 'Contact your administrators for support'
actions = false
}
// Workspace alert message
throwAlert( 'EXTENSION_NOT_FOUND', query, {
status: 'Alert',
message: `The Extension <span class="text-primary">${query}</span> is not available in your workspace. ${byAccount}.`,
actions
} )
},
install: async extension => {
if( !isExtension( extension ) ) return
if( window.Extensions.list.hasOwnProperty( extension.name ) ){
// Throw extension already exist dialog
throwAlert( 'EXTENSION_EXIST', extension.name )
return false
}
/** Ask user to grant permission requested by the
* extension before to proceed with the installation
*/
if( requirePermission( extension ) ){
const list = await askPermission( 'scope', extension, extension.resource.permissions.scope )
if( Array.isArray( list ) )
extension.resource.permissions.scope = list
}
/**
* Assign required plugin dependencies to this extension
*
* NOTE: Regular mode only. Plugin are directly added to
* `config.json` file in sandbox mode
*/
if( !window.SANDBOX ) extension = await assignDependencies( extension )
try {
const { error, message, extensionId } = await window.Request('/extension/install', 'POST', extension )
if( error ) throw new Error( message )
// Register extension globally
window.Extensions.register({ id: extensionId, ...extension })
return extensionId
}
catch( error ){
console.log('Error Installing Extension: ', error )
return false
}
},
uninstall: async id => {
if( !id ) return
try {
const { error, message } = await Request(`/extension/${id}/uninstall`, 'DELETE')
if( error ) throw new Error( message )
// Unregister extension globally
window.Extensions.unregister( id )
return true
}
catch( error ){
console.log('Error Uninstalling Extension: ', error )
return false
}
},
register: extension => {
const { id, type, name, nsi, version, runscript, resource } = extension
// Add extension to loaded list
__EXTS__[ id ] =
__EXTNS__[`${nsi}~${version}`] = extension
// List of intalled and registered applications: Auto-loadable or not
if( type == 'application' ){
window.Extensions.list[ name ] = new ExtensionManager( id, extension )
if( resource && resource.services && !isEmpty( resource.services ) ){
// Extensions capable of reading particular type of file or data
Array.isArray( resource.services.editor )
&& resource.services.editor.map( mime => {
if( !MimeTypeSupportExts[ mime ] ) MimeTypeSupportExts[ mime ] = []
MimeTypeSupportExts[ mime ].push({ id, name: extension.name, type: 'editor' })
})
// Extensions capable of editing particular type of file or data
Array.isArray( resource.services.reader )
&& resource.services.reader.map( mime => {
if( !MimeTypeSupportExts[ mime ] ) MimeTypeSupportExts[ mime ] = []
MimeTypeSupportExts[ mime ].push({ id, name: extension.name, type: 'reader' })
})
}
}
/** Register globally all auto-loadable extensions
* that can show on toolbar by checking "runscript"
* configuration rules
*
* NOTE: Some extensions are not meant to
* display in the toolbar/Aside.
*/
if( runscript
&& ( ( runscript[ AccountType ] && runscript[ AccountType ].autoload ) // Specific account
|| ( runscript['*'] && runscript['*'].autoload ) ) ){ // All account
AutoLoadedExts[ id ] = extension
GState.dirty('Extensions', AutoLoadedExts )
}
},
unregister: id => {
if( !__EXTS__[ id ] ) return
const { name, nsi, version } = __EXTS__[ id ]
delete __EXTS__[ id ]
delete __EXTNS__[`${nsi}~${version}`]
// Close auto-loaded application if running
if( !AutoLoadedExts[ id ] || !window.Extensions.quit( name ) ) return
// Delete from workspace
delete window.Extensions.list[ name ]
delete AutoLoadedExts[ id ]
// Refresh workspace extensions
GState.dirty('Extensions', AutoLoadedExts )
},
open: ( type, payload ) => {
if( !Array.isArray( MimeTypeSupportExts.hasOwnProperty( type ) ) ){
console.log('[EXT]: No extension to read this datatype found')
return false
}
for( let o = 0; o < MimeTypeSupportExts[ type ].length; o++ )
if( MimeTypeSupportExts[ type ][ o ].defaultHandler ){
window.Extensions.run( MimeTypeSupportExts[ type ][ o ].name, payload )
return true
}
// Select first handler by default
window.Extensions.run( MimeTypeSupportExts[ type ][0].name, payload )
return true
},
installed: () => { return Object.values( __EXTS__ ) },
isInstalled: arg => { return __EXTS__.hasOwnProperty( arg ) || __EXTNS__.hasOwnProperty( arg ) }
}
export const load = async accountType => {
AccountType = accountType
storeAttr = 'active-extensions-'+ AccountType.toLowerCase()
// Initialize extensions state handler
GState.set( 'activeExtensions', uiStore.get( storeAttr ) || {} )
// Fetch all installed extensions
const list = await fetch()
if( !isEmpty( list ) )
list.map( ({ extensionId, ...rest }) => window.Extensions.register({ id: extensionId, ...rest }) )
// Close all temporary loaded apps
Object.keys( uiStore.get( storeAttr +'-tempo') || {} ).map( id => quitExt( id ) )
// List of auto-loaded extensions
GState.set('Extensions', AutoLoadedExts )
return AutoLoadedExts
}
export const get = async id => {
// Get an installed extension info
try {
/**---------- Sandbox mode ----------**/
if( window.SANDBOX ) return require('root/../config.json')
/**---------- Regular mode ----------**/
const { error, message, extension } = await window.Request(`/extension/${id}`)
if( error ) throw new Error( message )
return extension
}
catch( error ){
console.log('Failed Retreiving an Extension: ', error )
return
}
}
export const fetch = async query => {
// Fetch all installed extension or query a specific category
try {
/**---------- Sandbox mode ----------**/
if( window.SANDBOX ) return []
/**---------- Regular mode ----------**/
const { error, message, extensions, results } = await window.Request(`/extension/${query ? 'search?query='+ query : 'list'}`)
if( error ) throw new Error( message )
return extensions || results
}
catch( error ){
console.log('Failed Fetching Extensions: ', error )
return []
}
}