jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
349 lines (348 loc) • 9.94 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var CodeMirror = require('codemirror');
require('codemirror/mode/meta');
var phosphor_signaling_1 = require('phosphor-signaling');
/**
* The default implementation of a document model.
*/
var DocumentModel = (function () {
/**
* Construct a new document model.
*/
function DocumentModel(languagePreference) {
this._text = '';
this._defaultLang = '';
this._dirty = false;
this._readOnly = false;
this._isDisposed = false;
this._defaultLang = languagePreference || '';
}
Object.defineProperty(DocumentModel.prototype, "isDisposed", {
/**
* Get whether the model factory has been disposed.
*/
get: function () {
return this._isDisposed;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentModel.prototype, "contentChanged", {
/**
* A signal emitted when the document content changes.
*/
get: function () {
return Private.contentChangedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentModel.prototype, "stateChanged", {
/**
* A signal emitted when the document state changes.
*/
get: function () {
return Private.stateChangedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentModel.prototype, "dirty", {
/**
* The dirty state of the document.
*/
get: function () {
return this._dirty;
},
set: function (newValue) {
if (newValue === this._dirty) {
return;
}
var oldValue = this._dirty;
this._dirty = newValue;
this.stateChanged.emit({ name: 'dirty', oldValue: oldValue, newValue: newValue });
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentModel.prototype, "readOnly", {
/**
* The read only state of the document.
*/
get: function () {
return this._readOnly;
},
set: function (newValue) {
if (newValue === this._readOnly) {
return;
}
var oldValue = this._readOnly;
this._readOnly = newValue;
this.stateChanged.emit({ name: 'readOnly', oldValue: oldValue, newValue: newValue });
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentModel.prototype, "defaultKernelName", {
/**
* The default kernel name of the document.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return '';
},
enumerable: true,
configurable: true
});
Object.defineProperty(DocumentModel.prototype, "defaultKernelLanguage", {
/**
* The default kernel language of the document.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return this._defaultLang;
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources held by the document manager.
*/
DocumentModel.prototype.dispose = function () {
this._isDisposed = true;
};
/**
* Serialize the model to a string.
*/
DocumentModel.prototype.toString = function () {
return this._text;
};
/**
* Deserialize the model from a string.
*
* #### Notes
* Should emit a [contentChanged] signal.
*/
DocumentModel.prototype.fromString = function (value) {
if (this._text === value) {
return;
}
this._text = value;
this.contentChanged.emit(void 0);
this.dirty = true;
};
/**
* Serialize the model to JSON.
*/
DocumentModel.prototype.toJSON = function () {
return JSON.stringify(this._text);
};
/**
* Deserialize the model from JSON.
*
* #### Notes
* Should emit a [contentChanged] signal.
*/
DocumentModel.prototype.fromJSON = function (value) {
this.fromString(JSON.parse(value));
};
return DocumentModel;
}());
exports.DocumentModel = DocumentModel;
/**
* An implementation of a model factory for text files.
*/
var TextModelFactory = (function () {
function TextModelFactory() {
this._isDisposed = false;
}
Object.defineProperty(TextModelFactory.prototype, "name", {
/**
* The name of the model type.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return 'text';
},
enumerable: true,
configurable: true
});
Object.defineProperty(TextModelFactory.prototype, "fileType", {
/**
* The type of the file.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return 'file';
},
enumerable: true,
configurable: true
});
Object.defineProperty(TextModelFactory.prototype, "fileFormat", {
/**
* The format of the file.
*
* This is a read-only property.
*/
get: function () {
return 'text';
},
enumerable: true,
configurable: true
});
Object.defineProperty(TextModelFactory.prototype, "isDisposed", {
/**
* Get whether the model factory has been disposed.
*/
get: function () {
return this._isDisposed;
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources held by the model factory.
*/
TextModelFactory.prototype.dispose = function () {
this._isDisposed = true;
};
/**
* Create a new model.
*
* @param languagePreference - An optional kernel language preference.
*
* @returns A new document model.
*/
TextModelFactory.prototype.createNew = function (languagePreference) {
return new DocumentModel(languagePreference);
};
/**
* Get the preferred kernel language given an extension.
*/
TextModelFactory.prototype.preferredLanguage = function (ext) {
var mode = CodeMirror.findModeByExtension(ext.slice(1));
if (mode) {
return mode.mode;
}
};
return TextModelFactory;
}());
exports.TextModelFactory = TextModelFactory;
/**
* An implementation of a model factory for base64 files.
*/
var Base64ModelFactory = (function (_super) {
__extends(Base64ModelFactory, _super);
function Base64ModelFactory() {
_super.apply(this, arguments);
}
Object.defineProperty(Base64ModelFactory.prototype, "name", {
/**
* The name of the model type.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return 'base64';
},
enumerable: true,
configurable: true
});
Object.defineProperty(Base64ModelFactory.prototype, "fileType", {
/**
* The type of the file.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return 'file';
},
enumerable: true,
configurable: true
});
Object.defineProperty(Base64ModelFactory.prototype, "fileFormat", {
/**
* The format of the file.
*
* This is a read-only property.
*/
get: function () {
return 'base64';
},
enumerable: true,
configurable: true
});
return Base64ModelFactory;
}(TextModelFactory));
exports.Base64ModelFactory = Base64ModelFactory;
/**
* The default implemetation of a widget factory.
*/
var ABCWidgetFactory = (function () {
function ABCWidgetFactory() {
this._isDisposed = false;
}
Object.defineProperty(ABCWidgetFactory.prototype, "widgetCreated", {
/**
* A signal emitted when a widget is created.
*/
get: function () {
return Private.widgetCreatedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(ABCWidgetFactory.prototype, "isDisposed", {
/**
* Get whether the model factory has been disposed.
*/
get: function () {
return this._isDisposed;
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources held by the document manager.
*/
ABCWidgetFactory.prototype.dispose = function () {
this._isDisposed = true;
};
return ABCWidgetFactory;
}());
exports.ABCWidgetFactory = ABCWidgetFactory;
/**
* A private namespace for data.
*/
var Private;
(function (Private) {
/**
* A signal emitted when a document content changes.
*/
Private.contentChangedSignal = new phosphor_signaling_1.Signal();
/**
* A signal emitted when a widget is created.
*/
Private.widgetCreatedSignal = new phosphor_signaling_1.Signal();
/**
* A signal emitted when a document dirty state changes.
*/
Private.stateChangedSignal = new phosphor_signaling_1.Signal();
})(Private || (Private = {}));