Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize ObservableMap in models and rendermime #1709

Merged
merged 98 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
ffa5085
Update rendermime interface
blink1073 Feb 13, 2017
e34295b
wip interface changes
blink1073 Feb 13, 2017
6f3a9a1
Finish changes to rendermime
blink1073 Feb 13, 2017
43ecaeb
Move default mime bundle handling to rendermime
blink1073 Feb 13, 2017
15ddaf0
Update inspector and markdownwidget
blink1073 Feb 13, 2017
0972ccc
Update cells and output area
blink1073 Feb 13, 2017
6580a4c
wip update tests
blink1073 Feb 13, 2017
88d8506
Use a constant for the mime type
blink1073 Feb 14, 2017
bf43bd1
wip clean up output area model
blink1073 Feb 14, 2017
50ddd64
Expose convertbundle
blink1073 Feb 14, 2017
ae12666
Finish output area model refactor
blink1073 Feb 14, 2017
7cc5efb
Rename IMimeBundle -> IMimeModel
blink1073 Feb 14, 2017
f75116b
Finish output area model and update cell model
blink1073 Feb 14, 2017
1a8ff43
Clean up cell and outputarea widgets
blink1073 Feb 14, 2017
dcbfeb7
Finish outputarea and cells
blink1073 Feb 14, 2017
c95368f
renam ibaseoutput -> ibasemodel
blink1073 Feb 14, 2017
de0c79a
wip refactoring
blink1073 Feb 14, 2017
b39e936
Update notebook model to use observablemap
blink1073 Feb 14, 2017
d463ae0
Finish api changes
blink1073 Feb 14, 2017
8b206e3
Make trusted optional and default to false
blink1073 Feb 15, 2017
f5d8ce7
Update existing rendermime tests
blink1073 Feb 15, 2017
67e1196
Update tooltip widget
blink1073 Feb 15, 2017
4145a92
Update renderers tests
blink1073 Feb 15, 2017
3071705
Clean up the outputarea widget apis
blink1073 Feb 15, 2017
13d50e7
wip update tests
blink1073 Feb 15, 2017
e39d034
Output area widget updates
blink1073 Feb 15, 2017
a8b94ad
Move stderr handling to rendermime
blink1073 Feb 15, 2017
b6f0e90
wip move output model to rendermime
blink1073 Feb 15, 2017
75d5172
Finish refactor of outputmodel and outputareamodel
blink1073 Feb 16, 2017
6511b34
Move prompt handling to gutter using execution count
blink1073 Feb 16, 2017
0c1b872
Use proper names
blink1073 Feb 16, 2017
c962576
Add equality checking to observable map
blink1073 Feb 16, 2017
6de2176
wip update tests
blink1073 Feb 16, 2017
192a29c
Use deepEqual itemCmp for mimemodel
blink1073 Feb 16, 2017
2f7d07f
Allow initial value setting for observable map
blink1073 Feb 16, 2017
10ffcbd
Make trusted arg optional and default to false
blink1073 Feb 16, 2017
151f6ab
update cell widget tests
blink1073 Feb 16, 2017
e241f33
Add type aliases in metadata namespace
blink1073 Feb 16, 2017
8912d2c
Simplify mimemodel creation
blink1073 Feb 16, 2017
d7a4110
Update metadata tests
blink1073 Feb 16, 2017
dd0d40b
Update celltools tests
blink1073 Feb 16, 2017
91b8e04
Update notebook tests
blink1073 Feb 16, 2017
3b59cac
Remove unused disposed signal
blink1073 Feb 16, 2017
b7e9182
Update outputareamodel tests and add output test module
blink1073 Feb 16, 2017
8655fa4
Add tests for output model and mime model
blink1073 Feb 16, 2017
563b566
Allow initial items and itemcmp for observablevector'
blink1073 Feb 16, 2017
31e1e3c
Allow intial values for outputareamodel
blink1073 Feb 16, 2017
362b4ea
wip outputarea tests
blink1073 Feb 16, 2017
00044a0
Add an observable JSON object and widget
blink1073 Feb 16, 2017
f8e207c
Clean up observeablejson construction
blink1073 Feb 16, 2017
bd0c7aa
Finish rendermime models
blink1073 Feb 16, 2017
0fa3ea6
Use updated interfaces
blink1073 Feb 16, 2017
64027a7
wip clean up
blink1073 Feb 16, 2017
3b757f0
wip update outputareamodel
blink1073 Feb 16, 2017
af4a5e9
Back out cell attachments for now
blink1073 Feb 16, 2017
1c8eed8
Finish api changes
blink1073 Feb 16, 2017
83d0f92
wip update tests
blink1073 Feb 16, 2017
d0329e0
create observablejson test file
blink1073 Feb 16, 2017
81b7bb5
wip update tests
blink1073 Feb 16, 2017
cbbd633
update outputmodel tests
blink1073 Feb 16, 2017
f762a83
Add intial outputs to the widget
blink1073 Feb 16, 2017
7b9c1ed
update output widget tests
blink1073 Feb 16, 2017
6809a92
Clean up handling of cell trust
blink1073 Feb 16, 2017
8426dc2
Restore handling of execute result styling
blink1073 Feb 17, 2017
1caa897
Perform cleaner extraction of source data
blink1073 Feb 17, 2017
df3f02f
Use itemCmp in remove
blink1073 Feb 17, 2017
36b1c97
Give back copies in observablejson parse
blink1073 Feb 17, 2017
fbc279e
Clean up handling of raw data
blink1073 Feb 17, 2017
f491228
wip update tests
blink1073 Feb 17, 2017
b6a3b4f
Fix mimetype lookup
blink1073 Feb 17, 2017
9a6a429
Finish rendermime tests
blink1073 Feb 17, 2017
ee45c65
Update renderers tests
blink1073 Feb 17, 2017
90c9a64
Update observablejson and finish its tests
blink1073 Feb 17, 2017
82fc2ef
Fix mimemodel signal and update tests
blink1073 Feb 17, 2017
1211d80
Remove extraneous signals from the rendermime models
blink1073 Feb 17, 2017
a959d16
Clean up mimemodel
blink1073 Feb 17, 2017
22d25b1
Update mimemodel tests
blink1073 Feb 17, 2017
217d6fa
More cleanup of output model
blink1073 Feb 17, 2017
597c5cb
Finish outputarea model tests
blink1073 Feb 17, 2017
91ce607
Finish output area
blink1073 Feb 17, 2017
a664a12
Finish outputarea widget tests
blink1073 Feb 17, 2017
ab3ba05
Clean up handling of cell trusted state
blink1073 Feb 17, 2017
5660b9d
Clean up notebook metadata handling
blink1073 Feb 17, 2017
2d08777
Finish updating tests
blink1073 Feb 17, 2017
b381e6c
Fix css import
blink1073 Feb 17, 2017
dbbc10b
Clean up rendermime interface
blink1073 Feb 17, 2017
a7cffa7
More rendermime api cleanup
blink1073 Feb 17, 2017
351b494
Update examples
blink1073 Feb 17, 2017
9b8eff5
Update rendermime tests
blink1073 Feb 17, 2017
145abac
Use explicit setTimeout and try to avoid merge conflict
blink1073 Feb 17, 2017
bfca508
Another attempt to avoid merge conflict
blink1073 Feb 17, 2017
69aa496
Another merge attempt
blink1073 Feb 17, 2017
7b3b875
Another attempt
blink1073 Feb 17, 2017
3c32a78
Update notebook example
blink1073 Feb 17, 2017
20a91ae
Clean up css and remove keydown suppression
blink1073 Feb 17, 2017
bdcf1cb
remove dead links
blink1073 Feb 17, 2017
9e0e8be
remove more dead links
blink1073 Feb 17, 2017
3d7fd0a
Address review comments
blink1073 Feb 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 63 additions & 129 deletions src/cells/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import {
} from '@jupyterlab/services';

import {
deepEqual, JSONValue
JSONValue
} from 'phosphor/lib/algorithm/json';

import {
IIterator, iter
} from 'phosphor/lib/algorithm/iteration';

import {
defineSignal, ISignal
} from 'phosphor/lib/core/signaling';
Expand All @@ -26,12 +22,8 @@ import {
} from '../common/interfaces';

import {
IObservableString, ObservableString
} from '../common/observablestring';

import {
Metadata
} from '../common/metadata';
IObservableMap, ObservableMap
} from '../common/observablemap';

import {
IOutputAreaModel, OutputAreaModel
Expand All @@ -51,40 +43,27 @@ interface ICellModel extends CodeEditor.IModel {
/**
* A signal emitted when the content of the model changes.
*/
contentChanged: ISignal<ICellModel, void>;

/**
* A signal emitted when a metadata field changes.
*/
metadataChanged: ISignal<ICellModel, IChangedArgs<JSONValue>>;
readonly contentChanged: ISignal<ICellModel, void>;

/**
* A signal emitted when a model state changes.
*/
stateChanged: ISignal<ICellModel, IChangedArgs<any>>;
readonly stateChanged: ISignal<ICellModel, IChangedArgs<any>>;

/**
* Serialize the model to JSON.
* Whether the cell is trusted.
*/
toJSON(): nbformat.ICell;
trusted: boolean;

/**
* Get a metadata cursor for the cell.
*
* #### Notes
* Metadata associated with the nbformat spec are set directly
* on the model. This method is used to interact with a namespaced
* set of metadata on the cell.
* The metadata associated with the cell.
*/
getMetadata(name: string): Metadata.ICursor;
readonly metadata: IObservableMap<JSONValue>;

/**
* List the metadata namespace keys for the notebook.
*
* #### Notes
* Metadata associated with the nbformat are not included.
* Serialize the model to JSON.
*/
listMetadata(): IIterator<string>;
toJSON(): nbformat.ICell;
}


Expand Down Expand Up @@ -147,7 +126,7 @@ class CellModel extends CodeEditor.Model implements ICellModel {
*/
constructor(options: CellModel.IOptions) {
super();
this.value.changed.connect(this._onValueChanged, this);
this.value.changed.connect(this.onGenericChange, this);
let cell = options.cell;
if (!cell) {
return;
Expand All @@ -165,125 +144,85 @@ class CellModel extends CodeEditor.Model implements ICellModel {
delete metadata['collapsed'];
delete metadata['scrolled'];
}
this._metadata = metadata;
for (let key in metadata) {
this._metadata.set(key, metadata[key]);
}
this._metadata.changed.connect(this.onGenericChange, this);
}

/**
* A signal emitted when the state of the model changes.
* The type of cell.
*/
contentChanged: ISignal<this, void>;
readonly type: nbformat.CellType;

/**
* A signal emitted when a metadata field changes.
* A signal emitted when the state of the model changes.
*/
metadataChanged: ISignal<this, IChangedArgs<any>>;
readonly contentChanged: ISignal<this, void>;

/**
* A signal emitted when a model state changes.
*/
stateChanged: ISignal<this, IChangedArgs<any>>;
readonly stateChanged: ISignal<this, IChangedArgs<any>>;

/**
* Dispose of the resources held by the model.
* The metadata associated with the cell.
*/
dispose(): void {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
for (let key in this._cursors) {
this._cursors[key].dispose();
}
this._cursors = null;
this._metadata = null;
super.dispose();
get metadata(): IObservableMap<JSONValue> {
return this._metadata;
}

/**
* Serialize the model to JSON.
* Get the trusted state of the model.
*/
toJSON(): nbformat.ICell {
return {
cell_type: this.type,
source: this.value.text,
metadata: utils.copy(this._metadata) as nbformat.IBaseCellMetadata
} as nbformat.ICell;
get trusted(): boolean {
return !!this.metadata.get('trusted');
}

/**
* Get a metadata cursor for the cell.
*
* #### Notes
* Metadata associated with the nbformat spec are set directly
* on the model. This method is used to interact with a namespaced
* set of metadata on the cell.
* Set the trusted state of the model.
*/
getMetadata(name: string): Metadata.ICursor {
if (this.isDisposed) {
return null;
}
if (name in this._cursors) {
return this._cursors[name];
}
if (!this._reader) {
this._reader = this._readCursorData.bind(this);
this._writer = this.setCursorData.bind(this);
set trusted(newValue: boolean) {
let oldValue = this.metadata.get('trusted');
if (oldValue === newValue) {
return;
}
let cursor = new Metadata.Cursor({
name,
read: this._reader,
write: this._writer
});
this._cursors[name] = cursor;
return cursor;
this.metadata.set('trusted', newValue);
this.stateChanged.emit({ name: 'trusted', oldValue, newValue });
}

/**
* List the metadata namespace keys for the notebook.
*
* #### Notes
* Metadata associated with the nbformat are not included.
* Dispose of the resources held by the model.
*/
listMetadata(): IIterator<string> {
return iter(Object.keys(this._metadata));
dispose(): void {
this._metadata.dispose();
super.dispose();
}

/**
* Set the cursor data for a given field.
* Serialize the model to JSON.
*/
protected setCursorData(name: string, newValue: any): void {
let oldValue = this._metadata[name];
if (deepEqual(oldValue, newValue)) {
return;
toJSON(): nbformat.ICell {
let metadata: nbformat.IBaseCellMetadata = Object.create(null);
for (let key in this.metadata.keys()) {
let value = JSON.parse(JSON.stringify(this.metadata.get(key)));
metadata[key] = value as JSONValue;
}
this._metadata[name] = newValue;
this.contentChanged.emit(void 0);
this.metadataChanged.emit({ name, oldValue, newValue });
return {
cell_type: this.type,
source: this.value.text,
metadata,
} as nbformat.ICell;
}

/**
* The type of cell.
*/
type: nbformat.CellType;

/**
* Handle a change to the observable value.
*/
private _onValueChanged(sender: IObservableString, args: ObservableString.IChangedArgs): void {
protected onGenericChange(): void {
this.contentChanged.emit(void 0);
}

/**
* Read the metadata of a given name.
*/
private _readCursorData(name: string): JSONValue {
return this._metadata[name];
}

private _metadata: { [key: string]: any } = Object.create(null);
private _cursors: { [key: string]: Metadata.Cursor } = Object.create(null);
private _reader: (name: string) => JSONValue;
private _writer: (name: string, value: JSONValue) => void;
private _metadata = new ObservableMap<JSONValue>();
}


Expand All @@ -306,7 +245,6 @@ namespace CellModel {

// Define the signals for the `CellModel` class.
defineSignal(CellModel.prototype, 'contentChanged');
defineSignal(CellModel.prototype, 'metadataChanged');
defineSignal(CellModel.prototype, 'stateChanged');


Expand Down Expand Up @@ -360,15 +298,18 @@ class CodeCellModel extends CellModel implements ICodeCellModel {
let factory = (options.contentFactory ||
CodeCellModel.defaultContentFactory
);
this._outputs = factory.createOutputArea();
let trusted = this.trusted;
this._outputs = factory.createOutputArea({ trusted });
let cell = options.cell as nbformat.ICodeCell;
if (cell && cell.cell_type === 'code') {
this.executionCount = cell.execution_count;
for (let output of cell.outputs) {
this._outputs.add(output);
}
}
this._outputs.changed.connect(this._onOutputsChanged, this);
this._outputs.changed.connect(this.onGenericChange, this);
this._outputs.itemChanged.connect(this.onGenericChange, this);
this.metadata.changed.connect(this._onMetadataChanged, this);
}

/**
Expand Down Expand Up @@ -419,22 +360,15 @@ class CodeCellModel extends CellModel implements ICodeCellModel {
toJSON(): nbformat.ICodeCell {
let cell = super.toJSON() as nbformat.ICodeCell;
cell.execution_count = this.executionCount || null;
let outputs = this.outputs;
cell.outputs = [];
for (let i = 0; i < outputs.length; i++) {
let output = outputs.get(i);
if (output.output_type !== 'input_request') {
cell.outputs.push(output as nbformat.IOutput);
}
}
cell.outputs = this.outputs.toJSON();
return cell;
}

/**
* Handle the outputs changing.
* Handle the metadata changing.
*/
private _onOutputsChanged(): void {
this.contentChanged.emit(void 0);
private _onMetadataChanged(sender: ObservableMap<JSONValue>, args: ObservableMap.IChangedArgs<JSONValue>): void {
this._outputs.trusted = !!args.newValue;
}

private _outputs: IOutputAreaModel = null;
Expand Down Expand Up @@ -470,7 +404,7 @@ namespace CodeCellModel {
/**
* Create an output area.
*/
createOutputArea(): IOutputAreaModel;
createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel;
}

/**
Expand All @@ -481,8 +415,8 @@ namespace CodeCellModel {
/**
* Create an output area.
*/
createOutputArea(): IOutputAreaModel {
return new OutputAreaModel();
createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel {
return new OutputAreaModel(options);
}
}

Expand Down
Loading