jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
420 lines (419 loc) • 13.4 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 phosphor_panel_1 = require('phosphor-panel');
var phosphor_signaling_1 = require('phosphor-signaling');
var phosphor_tabs_1 = require('phosphor-tabs');
var toolbar_1 = require('../notebook/notebook/toolbar');
/**
* The class name added to inspector panels.
*/
var PANEL_CLASS = 'jp-Inspector';
/**
* The class name added to inspector child item widgets.
*/
var ITEM_CLASS = 'jp-InspectorItem';
/**
* The class name added to inspector child item widgets' content.
*/
var CONTENT_CLASS = 'jp-InspectorItem-content';
/**
* The history clear button class name.
*/
var CLEAR_CLASS = 'jp-InspectorItem-clear';
/**
* The back button class name.
*/
var BACK_CLASS = 'jp-InspectorItem-back';
/**
* The forward button class name.
*/
var FORWARD_CLASS = 'jp-InspectorItem-forward';
/**
* The orientation toggle bottom button class name.
*/
var BOTTOM_TOGGLE_CLASS = 'jp-InspectorItem-bottom';
/**
* The orientation toggle right button class name.
*/
var RIGHT_TOGGLE_CLASS = 'jp-InspectorItem-right';
/**
* A panel which contains a set of inspectors.
*/
var Inspector = (function (_super) {
__extends(Inspector, _super);
/**
* Construct an inspector.
*/
function Inspector(options) {
var _this = this;
_super.call(this);
this._items = Object.create(null);
this._orientation = 'horizontal';
this._source = null;
this.addClass(PANEL_CLASS);
// Create inspector child items and add them to the inspectors panel.
(options.items || []).forEach(function (value) {
var widget = value.widget || new InspectorItem();
widget.orientation = _this._orientation;
widget.orientationToggled.connect(function () {
_this.orientation = 'vertical' ? 'horizontal' : 'vertical';
});
widget.rank = value.rank;
widget.remembers = !!value.remembers;
widget.title.closable = false;
widget.title.text = value.name;
if (value.className) {
widget.addClass(value.className);
}
_this._items[value.type] = widget;
_this.addChild(widget);
});
}
Object.defineProperty(Inspector.prototype, "orientation", {
/**
* Set the orientation of the inspector panel.
*/
get: function () {
return this._orientation;
},
set: function (orientation) {
var _this = this;
if (this._orientation === orientation) {
return;
}
this._orientation = orientation;
Object.keys(this._items).forEach(function (i) {
_this._items[i].orientation = orientation;
});
},
enumerable: true,
configurable: true
});
Object.defineProperty(Inspector.prototype, "source", {
/**
* Set the source of events the inspector panel listens for.
*/
get: function () {
return this._source;
},
set: function (source) {
var _this = this;
if (this._source === source) {
return;
}
// Disconnect old signal handler.
if (this.source) {
this._source.inspected.disconnect(this.onInspectorUpdate, this);
this._source.disposed.disconnect(this.onSourceDisposed, this);
}
// Clear the inspector child items (but maintain history).
Object.keys(this._items).forEach(function (i) { return _this._items[i].content = null; });
this._source = source;
// Connect new signal handler.
if (this.source) {
this._source.inspected.connect(this.onInspectorUpdate, this);
this._source.disposed.connect(this.onSourceDisposed, this);
}
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources held by the widget.
*/
Inspector.prototype.dispose = function () {
var _this = this;
if (this.isDisposed) {
return;
}
// Dispose the inspector child items.
Object.keys(this._items).forEach(function (i) { return _this._items[i].dispose(); });
this._items = null;
// Disconnect from source.
this.source = null;
_super.prototype.dispose.call(this);
};
/**
* Handle inspector update signals.
*/
Inspector.prototype.onInspectorUpdate = function (sender, args) {
var widget = this._items[args.type];
if (!widget) {
return;
}
// Update the content of the inspector widget.
widget.content = args.content;
var items = this._items;
// If any inspector with a higher rank has content, do not change focus.
if (args.content) {
for (var type in items) {
var inspector = this._items[type];
if (inspector.rank < widget.rank && inspector.content) {
return;
}
}
this.currentWidget = widget;
return;
}
// If the inspector was emptied, show the next best ranked inspector.
var lowest = Infinity;
widget = null;
for (var type in items) {
var inspector = this._items[type];
if (inspector.rank < lowest && inspector.content) {
lowest = inspector.rank;
widget = inspector;
}
}
if (widget) {
this.currentWidget = widget;
}
};
/**
* Handle source disposed signals.
*/
Inspector.prototype.onSourceDisposed = function (sender, args) {
this.source = null;
};
return Inspector;
}(phosphor_tabs_1.TabPanel));
exports.Inspector = Inspector;
/**
* A code inspector child widget.
*/
var InspectorItem = (function (_super) {
__extends(InspectorItem, _super);
/**
* Construct an inspector widget.
*/
function InspectorItem() {
_super.call(this);
/**
* This flag is a temporary placeholder and will be removed when we switch to
* the phosphor mono-repo and have support for multiple inspector locations.
*/
this.toggleEnabled = false;
this._content = null;
this._history = null;
this._index = -1;
this._orientation = 'horizontal';
this._rank = Infinity;
this._remembers = false;
this._toolbar = null;
this.addClass(ITEM_CLASS);
this.update();
}
Object.defineProperty(InspectorItem.prototype, "content", {
/**
* The text of the inspector.
*/
get: function () {
return this._content;
},
set: function (newValue) {
if (newValue === this._content) {
return;
}
if (this._content) {
if (this._remembers) {
this._content.hide();
}
else {
this._content.dispose();
}
}
this._content = newValue;
if (this._content) {
this._content.addClass(CONTENT_CLASS);
this.addChild(this._content);
if (this.remembers) {
this._history.push(newValue);
this._index++;
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectorItem.prototype, "orientation", {
/**
* The display orientation of the inspector widget.
*/
get: function () {
return this._orientation;
},
set: function (newValue) {
if (newValue === this._orientation) {
return;
}
this._orientation = newValue;
this.update();
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectorItem.prototype, "orientationToggled", {
/**
* A signal emitted when an inspector widget's orientation is toggled.
*/
get: function () {
return Private.orientationToggledSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectorItem.prototype, "remembers", {
/**
* A flag that indicates whether the inspector remembers history.
*/
get: function () {
return this._remembers;
},
set: function (newValue) {
if (newValue === this._remembers) {
return;
}
this._clear();
this._remembers = newValue;
if (!this._remembers) {
this._history = null;
}
this.update();
},
enumerable: true,
configurable: true
});
Object.defineProperty(InspectorItem.prototype, "rank", {
/**
* The display rank of the inspector.
*/
get: function () {
return this._rank;
},
set: function (newValue) {
this._rank = newValue;
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources held by the widget.
*/
InspectorItem.prototype.dispose = function () {
if (this.isDisposed) {
return;
}
phosphor_signaling_1.clearSignalData(this);
if (this._history) {
this._history.forEach(function (widget) { return widget.dispose(); });
this._history = null;
}
if (this._toolbar) {
this._toolbar.dispose();
}
_super.prototype.dispose.call(this);
};
/**
* Handle `update_request` messages.
*/
InspectorItem.prototype.onUpdateRequest = function (msg) {
if (this._toolbar) {
this._toolbar.dispose();
}
this._toolbar = this._createToolbar();
this.insertChild(0, this._toolbar);
};
/**
* Navigate back in history.
*/
InspectorItem.prototype._back = function () {
if (this._history.length) {
this._navigateTo(Math.max(this._index - 1, 0));
}
};
/**
* Clear history.
*/
InspectorItem.prototype._clear = function () {
if (this._history) {
this._history.forEach(function (widget) { return widget.dispose(); });
}
this._history = [];
this._index = -1;
};
/**
* Navigate forward in history.
*/
InspectorItem.prototype._forward = function () {
if (this._history.length) {
this._navigateTo(Math.min(this._index + 1, this._history.length - 1));
}
};
/**
* Create a history toolbar.
*/
InspectorItem.prototype._createToolbar = function () {
var _this = this;
var toolbar = new toolbar_1.NotebookToolbar();
if (this.toggleEnabled) {
var toggle = new toolbar_1.ToolbarButton({
className: this.orientation === 'vertical' ? RIGHT_TOGGLE_CLASS
: BOTTOM_TOGGLE_CLASS,
onClick: function () { return _this.orientationToggled.emit(void 0); },
tooltip: 'Toggle the inspector orientation.'
});
toolbar.add('toggle', toggle);
}
if (!this._remembers) {
return toolbar;
}
var clear = new toolbar_1.ToolbarButton({
className: CLEAR_CLASS,
onClick: function () { return _this._clear(); },
tooltip: 'Clear history.'
});
toolbar.add('clear', clear);
var back = new toolbar_1.ToolbarButton({
className: BACK_CLASS,
onClick: function () { return _this._back(); },
tooltip: 'Navigate back in history.'
});
toolbar.add('back', back);
var forward = new toolbar_1.ToolbarButton({
className: FORWARD_CLASS,
onClick: function () { return _this._forward(); },
tooltip: 'Navigate forward in history.'
});
toolbar.add('forward', forward);
return toolbar;
};
/**
* Navigate to a known index in history.
*/
InspectorItem.prototype._navigateTo = function (index) {
if (this._content) {
this._content.hide();
}
this._content = this._history[index];
this._index = index;
this._content.show();
};
return InspectorItem;
}(phosphor_panel_1.Panel));
exports.InspectorItem = InspectorItem;
/**
* A namespace for inspector private data.
*/
var Private;
(function (Private) {
/**
* A signal emitted when an inspector's orientation is toggled.
*/
Private.orientationToggledSignal = new phosphor_signaling_1.Signal();
})(Private || (Private = {}));