@@ -39,7 +39,14 @@ define([
39
39
//========================================================================
40
40
// Constructor
41
41
//========================================================================
42
- constructor ( initialData ) {
42
+ /**
43
+ *
44
+ * @param {* } initialData
45
+ * @param {* } extensionType extension type: notebook/chrome/lab
46
+ */
47
+ constructor ( extensionType = 'notebook' , initialData = { } ) {
48
+ // initial mode
49
+ this . extensionType = extensionType ;
43
50
// initial configuration
44
51
this . data = {
45
52
// Configuration
@@ -291,15 +298,22 @@ define([
291
298
}
292
299
293
300
loadData ( configKey = 'vpudf' ) {
301
+ let that = this ;
294
302
return new Promise ( function ( resolve , reject ) {
295
- Jupyter . notebook . config . load ( ) ;
296
- Jupyter . notebook . config . loaded . then ( function ( ) {
297
- var data = Jupyter . notebook . config . data [ configKey ] ;
298
- if ( data == undefined ) {
299
- data = { } ;
300
- }
301
- resolve ( data ) ;
302
- } ) ;
303
+ // CHROME: edited to use chrome.storage
304
+ if ( that . extensionType === 'notebook' ) {
305
+ Jupyter . notebook . config . load ( ) ;
306
+ Jupyter . notebook . config . loaded . then ( function ( ) {
307
+ var data = Jupyter . notebook . config . data [ configKey ] ;
308
+ if ( data == undefined ) {
309
+ data = { } ;
310
+ }
311
+ resolve ( data ) ;
312
+ } ) ;
313
+ } else if ( that . extensionType === 'chrome' ) {
314
+ // CHROME: TODO: chrome.storage
315
+ resolve ( { } ) ;
316
+ }
303
317
} ) ;
304
318
} ;
305
319
@@ -310,39 +324,50 @@ define([
310
324
* @returns
311
325
*/
312
326
getData ( dataKey = '' , configKey = 'vpudf' ) {
327
+ let that = this ;
313
328
return new Promise ( function ( resolve , reject ) {
314
- Jupyter . notebook . config . load ( ) ;
315
- Jupyter . notebook . config . loaded . then ( function ( ) {
316
- var data = Jupyter . notebook . config . data [ configKey ] ;
317
- if ( data == undefined ) {
318
- resolve ( data ) ;
319
- return ;
320
- }
321
- if ( dataKey == '' ) {
322
- resolve ( data ) ;
323
- return ;
324
- }
325
- if ( Object . keys ( data ) . length > 0 ) {
326
- resolve ( data [ dataKey ] ) ;
327
- return ;
328
- }
329
- reject ( 'No data available.' ) ;
330
- } ) ;
329
+ if ( that . extensionType === 'notebook' ) {
330
+ Jupyter . notebook . config . load ( ) ;
331
+ Jupyter . notebook . config . loaded . then ( function ( ) {
332
+ var data = Jupyter . notebook . config . data [ configKey ] ;
333
+ if ( data == undefined ) {
334
+ resolve ( data ) ;
335
+ return ;
336
+ }
337
+ if ( dataKey == '' ) {
338
+ resolve ( data ) ;
339
+ return ;
340
+ }
341
+ if ( Object . keys ( data ) . length > 0 ) {
342
+ resolve ( data [ dataKey ] ) ;
343
+ return ;
344
+ }
345
+ reject ( 'No data available.' ) ;
346
+ } ) ;
347
+ } else if ( that . extensionType === 'chrome' ) {
348
+ // CHROME: TODO: chrome.storage
349
+ resolve ( { } ) ;
350
+ }
331
351
} ) ;
332
352
}
333
353
334
354
getDataSimple ( dataKey = '' , configKey = 'vpudf' ) {
335
- Jupyter . notebook . config . load ( ) ;
336
- var data = Jupyter . notebook . config . data [ configKey ] ;
337
- if ( data == undefined ) {
355
+ if ( this . extensionType === 'notebook' ) {
356
+ Jupyter . notebook . config . load ( ) ;
357
+ var data = Jupyter . notebook . config . data [ configKey ] ;
358
+ if ( data == undefined ) {
359
+ return undefined ;
360
+ }
361
+ if ( dataKey == '' ) {
362
+ return data ;
363
+ }
364
+ if ( Object . keys ( data ) . length > 0 ) {
365
+ return data [ dataKey ] ;
366
+ }
367
+ } else if ( this . extensionType === 'chrome' ) {
368
+ // CHROME: TODO: chrome.storage
338
369
return undefined ;
339
370
}
340
- if ( dataKey == '' ) {
341
- return data ;
342
- }
343
- if ( Object . keys ( data ) . length > 0 ) {
344
- return data [ dataKey ] ;
345
- }
346
371
347
372
return undefined ;
348
373
}
@@ -353,17 +378,27 @@ define([
353
378
* @param {String } configKey
354
379
*/
355
380
setData ( dataObj , configKey = 'vpudf' ) {
356
- // set data using key
357
- Jupyter . notebook . config . loaded . then ( function ( ) {
358
- Jupyter . notebook . config . update ( { [ configKey ] : dataObj } ) ;
359
- } ) ;
381
+ if ( this . extensionType === 'notebook' ) {
382
+ // set data using key
383
+ Jupyter . notebook . config . loaded . then ( function ( ) {
384
+ Jupyter . notebook . config . update ( { [ configKey ] : dataObj } ) ;
385
+ } ) ;
386
+ } else if ( this . extensionType === 'chrome' ) {
387
+ // CHROME: TODO: chrome.storage
388
+
389
+ }
360
390
}
361
391
362
392
removeData ( key , configKey = 'vpudf' ) {
363
- // if set value to null, it removes from config data
364
- Jupyter . notebook . config . loaded . then ( function ( ) {
365
- Jupyter . notebook . config . update ( { [ configKey ] : { [ key ] : null } } ) ;
366
- } ) ;
393
+ if ( this . extensionType === 'notebook' ) {
394
+ // if set value to null, it removes from config data
395
+ Jupyter . notebook . config . loaded . then ( function ( ) {
396
+ Jupyter . notebook . config . update ( { [ configKey ] : { [ key ] : null } } ) ;
397
+ } ) ;
398
+ } else if ( this . extensionType === 'chrome' ) {
399
+ // CHROME: TODO: chrome.storage
400
+
401
+ }
367
402
}
368
403
369
404
/**
@@ -372,18 +407,35 @@ define([
372
407
* @param {String } configKey
373
408
*/
374
409
getMetadata ( dataKey = '' , configKey = 'vp' ) {
375
- let metadata = Jupyter . notebook . metadata [ configKey ] ;
376
- if ( metadata ) {
377
- // update this metadataSetting
378
- this . metadataSettings = {
379
- ...this . metadataSettings ,
380
- ...metadata
381
- } ;
382
- // no datakey, return all metadata
383
- if ( dataKey == '' ) {
384
- return metadata ;
410
+ if ( this . extensionType === 'notebook' ) {
411
+ let metadata = Jupyter . notebook . metadata [ configKey ] ;
412
+ if ( metadata ) {
413
+ // update this metadataSetting
414
+ this . metadataSettings = {
415
+ ...this . metadataSettings ,
416
+ ...metadata
417
+ } ;
418
+ // no datakey, return all metadata
419
+ if ( dataKey == '' ) {
420
+ return metadata ;
421
+ }
422
+ return metadata [ dataKey ] ;
423
+ }
424
+ } else if ( this . extensionType === 'chrome' ) {
425
+ // CHROME: use colab.global.notebookModel.metadata
426
+ let metadata = colab . global . notebookModel . metadata [ configKey ] ;
427
+ if ( metadata ) {
428
+ // update this metadataSetting
429
+ this . metadataSettings = {
430
+ ...this . metadataSettings ,
431
+ ...metadata
432
+ } ;
433
+ // no datakey, return all metadata
434
+ if ( dataKey == '' ) {
435
+ return metadata ;
436
+ }
437
+ return metadata [ dataKey ] ;
385
438
}
386
- return metadata [ dataKey ] ;
387
439
}
388
440
return { } ;
389
441
}
@@ -394,27 +446,41 @@ define([
394
446
* @param {String } configKey
395
447
*/
396
448
setMetadata ( dataObj , configKey = 'vp' ) {
397
- let oldData = Jupyter . notebook . metadata [ configKey ] ;
398
- Jupyter . notebook . metadata [ configKey ] = {
399
- ...oldData ,
400
- ...dataObj
401
- } ;
402
- Jupyter . notebook . set_dirty ( ) ;
449
+ if ( this . extensionType === 'notebook' ) {
450
+ let oldData = Jupyter . notebook . metadata [ configKey ] ;
451
+ Jupyter . notebook . metadata [ configKey ] = {
452
+ ...oldData ,
453
+ ...dataObj
454
+ } ;
455
+ Jupyter . notebook . set_dirty ( ) ;
456
+
457
+ } else if ( this . extensionType === 'chrome' ) {
458
+ // CHROME: use colab.global.notebookModel.metadata
459
+ let oldData = colab . global . notebookModel . metadata [ configKey ] ;
460
+ colab . global . notebookModel . metadata [ configKey ] = {
461
+ ...oldData ,
462
+ ...dataObj
463
+ } ;
464
+ }
403
465
404
466
// update this metadataSetting
405
467
this . metadataSettings = {
406
468
...this . metadataSettings ,
407
469
...dataObj
408
470
} ;
409
-
410
471
}
411
472
412
473
/**
413
474
* Reset metadata (on jupyter file)
414
475
* @param {String } configKey
415
476
*/
416
477
resetMetadata ( configKey = 'vp' ) {
417
- Jupyter . notebook . metadata [ configKey ] = { } ;
478
+ if ( this . extensionType === 'notebook' ) {
479
+ Jupyter . notebook . metadata [ configKey ] = { } ;
480
+ } else if ( this . extensionType === 'chrome' ) {
481
+ // CHROME: use colab.global.notebookModel.metadata
482
+ colab . global . notebookModel . metadata [ configKey ] = { } ;
483
+ }
418
484
}
419
485
420
486
/**
0 commit comments