Преглед на файлове

Switch uses of ListChangeType from enum to union type.

Afshin Darian преди 8 години
родител
ревизия
567d711067

+ 17 - 17
src/notebook/common/undo.ts

@@ -10,7 +10,7 @@ import {
 } from 'phosphor-signaling';
 
 import {
-  IObservableList, ListChangeType, IListChangedArgs, ObservableList
+  IObservableList, IListChangedArgs, ObservableList
 } from '../../common/observablelist';
 
 
@@ -176,21 +176,21 @@ class ObservableUndoableList<T extends IJSONable> extends ObservableList<T> impl
   private _undoChange(change: IListChangedArgs<any>): void {
     let value: T;
     switch (change.type) {
-    case ListChangeType.Add:
+    case 'add':
       this.removeAt(change.newIndex);
       break;
-    case ListChangeType.Set:
+    case 'set':
       value = this._createValue(change.oldValue as any);
       this.set(change.oldIndex, value);
       break;
-    case ListChangeType.Remove:
+    case 'remove':
       value = this._createValue(change.oldValue as any);
       this.insert(change.oldIndex, value);
       break;
-    case ListChangeType.Move:
+    case 'move':
       this.move(change.newIndex, change.oldIndex);
       break;
-    case ListChangeType.Replace:
+    case 'replace':
       let len = (change.newValue as any[]).length;
       let values = this._createValues(change.oldValue as any[]);
       this.replace(change.oldIndex, len, values);
@@ -206,21 +206,21 @@ class ObservableUndoableList<T extends IJSONable> extends ObservableList<T> impl
   private _redoChange(change: IListChangedArgs<any>): void {
     let value: T;
     switch (change.type) {
-    case ListChangeType.Add:
+    case 'add':
       value = this._createValue(change.newValue as any);
       this.insert(change.newIndex, value);
       break;
-    case ListChangeType.Set:
+    case 'set':
       value = this._createValue(change.newValue as any);
       this.set(change.newIndex, value);
       break;
-    case ListChangeType.Remove:
+    case 'remove':
       this.removeAt(change.oldIndex);
       break;
-    case ListChangeType.Move:
+    case 'move':
       this.move(change.oldIndex, change.newIndex);
       break;
-    case ListChangeType.Replace:
+    case 'replace':
       let len = (change.oldValue as any[]).length;
       let cells = this._createValues(change.newValue as any[]);
       this.replace(change.oldIndex, len, cells);
@@ -253,15 +253,15 @@ class ObservableUndoableList<T extends IJSONable> extends ObservableList<T> impl
    * Copy a change as JSON.
    */
   private _copyChange(change: IListChangedArgs<T>): IListChangedArgs<any> {
-    if (change.type === ListChangeType.Replace) {
+    if (change.type === 'replace') {
       return this._copyReplace(change);
     }
     let oldValue: any = null;
     let newValue: any = null;
     switch (change.type) {
-    case ListChangeType.Add:
-    case ListChangeType.Set:
-    case ListChangeType.Remove:
+    case 'add':
+    case 'set':
+    case 'remove':
       if (change.oldValue) {
         oldValue = (change.oldValue as T).toJSON();
       }
@@ -269,7 +269,7 @@ class ObservableUndoableList<T extends IJSONable> extends ObservableList<T> impl
         newValue = (change.newValue as T).toJSON();
       }
       break;
-    case ListChangeType.Move:
+    case 'move':
       // Only need the indices.
       break;
     default:
@@ -297,7 +297,7 @@ class ObservableUndoableList<T extends IJSONable> extends ObservableList<T> impl
       newValue.push(value.toJSON());
     }
     return {
-      type: ListChangeType.Replace,
+      type: 'replace',
       oldIndex: change.oldIndex,
       newIndex: change.newIndex,
       oldValue,

+ 5 - 5
src/notebook/notebook/model.ts

@@ -14,7 +14,7 @@ import {
 } from 'phosphor-signaling';
 
 import {
-  IObservableList, ListChangeType, IListChangedArgs
+  IObservableList, IListChangedArgs
 } from '../../common/observablelist';
 
 import {
@@ -411,14 +411,14 @@ class NotebookModel extends DocumentModel implements INotebookModel {
   private _onCellsChanged(list: IObservableList<ICellModel>, change: IListChangedArgs<ICellModel>): void {
     let cell: ICellModel;
     switch (change.type) {
-    case ListChangeType.Add:
+    case 'add':
       cell = change.newValue as ICellModel;
       cell.contentChanged.connect(this._onCellChanged, this);
       break;
-    case ListChangeType.Remove:
+    case 'remove':
       (change.oldValue as ICellModel).dispose();
       break;
-    case ListChangeType.Replace:
+    case 'replace':
       let newValues = change.newValue as ICellModel[];
       for (cell of newValues) {
         cell.contentChanged.connect(this._onCellChanged, this);
@@ -428,7 +428,7 @@ class NotebookModel extends DocumentModel implements INotebookModel {
         cell.dispose();
       }
       break;
-    case ListChangeType.Set:
+    case 'set':
       cell = change.newValue as ICellModel;
       cell.contentChanged.connect(this._onCellChanged, this);
       if (change.oldValue) {

+ 6 - 6
src/notebook/notebook/widget.ts

@@ -26,7 +26,7 @@ import {
 } from 'phosphor-widget';
 
 import {
-  IObservableList, IListChangedArgs, ListChangeType
+  IObservableList, IListChangedArgs
 } from '../../common/observablelist';
 
 import {
@@ -326,16 +326,16 @@ class StaticNotebook extends Widget {
    */
   private _onCellsChanged(sender: IObservableList<ICellModel>, args: IListChangedArgs<ICellModel>) {
     switch (args.type) {
-    case ListChangeType.Add:
+    case 'add':
       this._insertCell(args.newIndex, args.newValue as ICellModel);
       break;
-    case ListChangeType.Move:
+    case 'move':
       this._moveCell(args.newIndex, args.oldIndex);
       break;
-    case ListChangeType.Remove:
+    case 'remove':
       this._removeCell(args.oldIndex);
       break;
-    case ListChangeType.Replace:
+    case 'replace':
       // TODO: reuse existing cell widgets if possible.
       let oldValues = args.oldValue as ICellModel[];
       for (let i = 0; i < oldValues.length; i++) {
@@ -346,7 +346,7 @@ class StaticNotebook extends Widget {
         this._insertCell(args.newIndex, newValues[i - 1]);
       }
       break;
-    case ListChangeType.Set:
+    case 'set':
       // TODO: reuse existing widget if possible.
       this._removeCell(args.newIndex);
       this._insertCell(args.newIndex, args.newValue as ICellModel);

+ 4 - 4
src/notebook/output-area/widget.ts

@@ -26,7 +26,7 @@ import {
 } from 'phosphor-widget';
 
 import {
-  IListChangedArgs, ListChangeType
+  IListChangedArgs
 } from '../../common/observablelist';
 
 import {
@@ -399,11 +399,11 @@ class OutputAreaWidget extends Widget {
    */
   private _onModelStateChanged(sender: OutputAreaModel, args: IListChangedArgs<nbformat.IOutput>) {
     switch (args.type) {
-    case ListChangeType.Add:
+    case 'add':
       // Children are always added at the end.
       this._addChild();
       break;
-    case ListChangeType.Replace:
+    case 'replace':
       // Only "clear" is supported by the model.
       // When an output area is cleared and then quickly replaced with new
       // content (as happens with @interact in widgets, for example), the
@@ -420,7 +420,7 @@ class OutputAreaWidget extends Widget {
         this._removeChild(args.oldIndex);
       }
       break;
-    case ListChangeType.Set:
+    case 'set':
       this._updateChild(args.newIndex);
       break;
     default:

+ 1 - 0
src/tsconfig.json

@@ -14,6 +14,7 @@
     "clipboard/index",
     "clipboard/plugin",
     "commandpalette/plugin",
+    "common/observablelist",
     "dialog/index",
     "docmanager/index",
     "docregistry/plugin",

+ 1 - 5
test/src/notebook/output-area/model.spec.ts

@@ -7,10 +7,6 @@ import {
   MockKernel
 } from 'jupyter-js-services/lib/mockkernel';
 
-import {
-  ListChangeType
-} from '../../../../lib/common/observablelist';
-
 import {
   deepEqual
 } from '../../../../lib/notebook/common/json';
@@ -93,7 +89,7 @@ describe('notebook/output-area/model', () => {
         let called = false;
         model.changed.connect((sender, args) => {
           expect(sender).to.be(model);
-          expect(args.type).to.be(ListChangeType.Add);
+          expect(args.type).to.be('add');
           expect(args.oldIndex).to.be(-1);
           expect(args.newIndex).to.be(0);
           expect(args.oldValue).to.be(void 0);