Browse Source

Add trust handling for the notebook

Steven Silvester 8 years ago
parent
commit
2b59897a92
3 changed files with 31 additions and 7 deletions
  1. 7 2
      src/notebook/notebook/model.ts
  2. 8 3
      src/notebook/notebook/trust.ts
  3. 16 2
      src/notebook/plugin.ts

+ 7 - 2
src/notebook/notebook/model.ts

@@ -260,6 +260,8 @@ class NotebookModel extends DocumentModel implements INotebookModel {
       cells.push(cell.toJSON());
     }
     let metadata = utils.copy(this._metadata) as nbformat.INotebookMetadata;
+    // orig_nbformat should not be written to file per spec.
+    delete metadata['orig_nbformat'];
     return {
       metadata,
       nbformat_minor: this._nbformatMinor,
@@ -298,13 +300,16 @@ class NotebookModel extends DocumentModel implements INotebookModel {
 
     let oldValue = 0;
     let newValue = 0;
+    this._nbformatMinor = nbformat.MINOR_VERSION;
+    this._nbformat = nbformat.MAJOR_VERSION;
+
     if (value.nbformat !== this._nbformat) {
       oldValue = this._nbformat;
       this._nbformat = newValue = value.nbformat;
       this.stateChanged.emit({ name: 'nbformat', oldValue, newValue });
     }
-    if (value.nbformat_minor !== this._nbformatMinor) {
-      oldValue = this._nbformat;
+    if (value.nbformat_minor > this._nbformatMinor) {
+      oldValue = this._nbformatMinor;
       this._nbformatMinor = newValue = value.nbformat_minor;
       this.stateChanged.emit({ name: 'nbformatMinor', oldValue, newValue });
     }

+ 8 - 3
src/notebook/notebook/trust.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  showDialog
+  showDialog, okButton, cancelButton, warnButton
 } from '../../dialog';
 
 import {
@@ -41,11 +41,16 @@ function trustNotebook(model: INotebookModel, host?: HTMLElement): Promise<void>
     }
   }
   if (trusted) {
-    return Promise.resolve(void 0);
+    return showDialog({
+      body: 'Notebook is already trusted',
+      buttons: [okButton]
+    }).then(() => void 0);
   }
   return showDialog({
     body: TRUST_MESSAGE,
-    title: 'Trust this notebook?'
+    title: 'Trust this notebook?',
+    okText: 'TRUST',
+    buttons: [cancelButton, warnButton]
   }).then(result => {
     if (result.text === 'OK') {
       for (let i = 0; i < cells.length; i++) {

+ 16 - 2
src/notebook/plugin.ts

@@ -48,7 +48,7 @@ import {
 
 import {
   INotebookTracker, NotebookModelFactory, NotebookPanel, NotebookTracker,
-  NotebookWidgetFactory, NotebookActions, Notebook
+  NotebookWidgetFactory, NotebookActions, Notebook, trustNotebook
 } from './index';
 
 
@@ -73,6 +73,7 @@ const cmdIds = {
   switchKernel: 'notebook:switch-kernel',
   clearAllOutputs: 'notebook:clear-outputs',
   closeAndHalt: 'notebook:close-and-halt',
+  trust: 'notebook:trust',
   run: 'notebook-cells:run',
   runAndAdvance: 'notebook-cells:run-and-advance',
   runAndInsert: 'notebook-cells:run-and-insert',
@@ -296,6 +297,17 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       }
     }
   });
+  commands.addCommand(cmdIds.trust, {
+    label: 'Trust Notebook',
+    execute: () => {
+      let current = tracker.currentWidget;
+      if (current) {
+        return trustNotebook(current.context.model).then(() => {
+          return current.context.save();
+        });
+      }
+    }
+  });
   commands.addCommand(cmdIds.restartClear, {
     label: 'Restart Kernel & Clear Outputs',
     execute: () => {
@@ -646,7 +658,8 @@ function populatePalette(palette: ICommandPalette): void {
     cmdIds.editMode,
     cmdIds.commandMode,
     cmdIds.switchKernel,
-    cmdIds.closeAndHalt
+    cmdIds.closeAndHalt,
+    cmdIds.trust
   ].forEach(command => { palette.addItem({ command, category }); });
 
   category = 'Notebook Cell Operations';
@@ -705,6 +718,7 @@ function createMenu(app: JupyterLab): Menu {
   menu.addItem({ command: cmdIds.restart });
   menu.addItem({ command: cmdIds.switchKernel });
   menu.addItem({ command: cmdIds.closeAndHalt });
+  menu.addItem({ command: cmdIds.trust });
   menu.addItem({ type: 'separator' });
   menu.addItem({ type: 'submenu', menu: settings });