jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
166 lines (165 loc) • 5.53 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var phosphor_disposable_1 = require('phosphor-disposable');
var phosphor_signaling_1 = require('phosphor-signaling');
var phosphor_tabs_1 = require('phosphor-tabs');
/**
* The class name added to the currently active widget's title.
*/
var SEMANTIC_FOCUS_CLASS = 'jp-mod-semanticFocus';
/**
* An object that tracks the active widget in an application.
*/
var WidgetTracker = (function () {
/**
* Construct a new widget tracker.
*/
function WidgetTracker() {
this._widgets = [];
this._activeWidget = null;
// TODO: Replace this with message filters on semantic-focus
// events when available.
document.body.addEventListener('focus', this, true);
}
Object.defineProperty(WidgetTracker.prototype, "activeWidgetChanged", {
/**
* A signal emitted when the active widget changes.
*/
get: function () {
return activeWidgetChangedSignal.bind(this);
},
enumerable: true,
configurable: true
});
Object.defineProperty(WidgetTracker.prototype, "isDisposed", {
/**
* Test whether the widget tracker has been disposed.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return this._widgets === null;
},
enumerable: true,
configurable: true
});
Object.defineProperty(WidgetTracker.prototype, "widgets", {
/**
* The read-only list of tracked widgets.
*
* #### Notes
* This is a read-only property.
*/
get: function () {
return this._widgets.slice();
},
enumerable: true,
configurable: true
});
Object.defineProperty(WidgetTracker.prototype, "activeWidget", {
/**
* The currently active widget.
*
* #### Notes
* This is set automatically due to user events but can
* also be set programatically.
* The widget must be one of the current widgets.
* The widget will be activated in the application shell.
* The [[activeWidgetChanged]] signal will be emitted.
*/
get: function () {
return this._activeWidget;
},
set: function (widget) {
if (this._activeWidget === widget) {
return;
}
if (this._widgets.indexOf(widget) === -1) {
return;
}
// Toggle the active class in the widget titles.
if (this._activeWidget) {
var className = this._activeWidget.title.className;
className = className.replace(SEMANTIC_FOCUS_CLASS, '');
this._activeWidget.title.className = className;
}
this._activeWidget = widget;
if (widget) {
widget.title.className += " " + SEMANTIC_FOCUS_CLASS;
}
this.activeWidgetChanged.emit(widget);
// Activate the widget in the dock panel.
// TODO: Use an API for this for this when available.
var stack = widget.parent;
if (!stack) {
return;
}
var tabs = stack.parent;
if (tabs instanceof phosphor_tabs_1.TabPanel) {
tabs.currentWidget = widget;
}
},
enumerable: true,
configurable: true
});
/**
* Dispose of the resources used by the tracker.
*/
WidgetTracker.prototype.dispose = function () {
if (this.isDisposed) {
return;
}
this._widgets = null;
this._activeWidget = null;
document.body.removeEventListener('focus', this, true);
};
/**
* Add a widget to the widget tracker.
*
* @param widget - The widget to add to the tracker.
*
* @returns A disposable that can be used to remove the widget from
* the tracker.
*
* #### Notes
* The new widget will be set as the active widget.
*/
WidgetTracker.prototype.addWidget = function (widget) {
var _this = this;
this._widgets.push(widget);
this.activeWidget = widget;
var disposal = function () {
var index = _this._widgets.indexOf(widget);
_this._widgets.splice(index, 1);
};
widget.disposed.connect(function () { disposal(); });
return new phosphor_disposable_1.DisposableDelegate(function () { disposal(); });
};
/**
* Handle the DOM events for the widget tracker.
*
* @param event - The DOM event sent to the widget.
*/
WidgetTracker.prototype.handleEvent = function (event) {
if (event.type === 'focus') {
for (var _i = 0, _a = this._widgets; _i < _a.length; _i++) {
var widget = _a[_i];
var target = event.target;
if (widget.isAttached && widget.isVisible) {
if (widget.node.contains(target)) {
this.activeWidget = widget;
return;
}
}
}
}
};
return WidgetTracker;
}());
exports.WidgetTracker = WidgetTracker;
/**
* A signal emitted when the active widget changes.
*/
var activeWidgetChangedSignal = new phosphor_signaling_1.Signal();