UNPKG

4.64 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { PathExt } from '@jupyterlab/coreutils';
4import { TextItem } from '@jupyterlab/statusbar';
5import { VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
6import React from 'react';
7/**
8 * A pure component for rendering a file path (or activity name).
9 *
10 * @param props - the props for the component.
11 *
12 * @returns a tsx component for a file path.
13 */
14function PathStatusComponent(props) {
15 return React.createElement(TextItem, { source: props.name, title: props.fullPath });
16}
17/**
18 * A status bar item for the current file path (or activity name).
19 */
20export class PathStatus extends VDomRenderer {
21 /**
22 * Construct a new PathStatus status item.
23 */
24 constructor(opts) {
25 super(new PathStatus.Model(opts.docManager));
26 this.node.title = this.model.path;
27 }
28 /**
29 * Render the status item.
30 */
31 render() {
32 return (React.createElement(PathStatusComponent, { fullPath: this.model.path, name: this.model.name }));
33 }
34}
35/**
36 * A namespace for PathStatus statics.
37 */
38(function (PathStatus) {
39 /**
40 * A VDomModel for rendering the PathStatus status item.
41 */
42 class Model extends VDomModel {
43 /**
44 * Construct a new model.
45 *
46 * @param docManager the application document manager. Used to check
47 * whether the current widget is a document.
48 */
49 constructor(docManager) {
50 super();
51 /**
52 * React to a title change for the current widget.
53 */
54 this._onTitleChange = (title) => {
55 const oldState = this._getAllState();
56 this._name = title.label;
57 this._triggerChange(oldState, this._getAllState());
58 };
59 /**
60 * React to a path change for the current document.
61 */
62 this._onPathChange = (_documentModel, newPath) => {
63 const oldState = this._getAllState();
64 this._path = newPath;
65 this._name = PathExt.basename(newPath);
66 this._triggerChange(oldState, this._getAllState());
67 };
68 this._path = '';
69 this._name = '';
70 this._widget = null;
71 this._docManager = docManager;
72 }
73 /**
74 * The current path for the application.
75 */
76 get path() {
77 return this._path;
78 }
79 /**
80 * The name of the current activity.
81 */
82 get name() {
83 return this._name;
84 }
85 /**
86 * The current widget for the application.
87 */
88 get widget() {
89 return this._widget;
90 }
91 set widget(widget) {
92 const oldWidget = this._widget;
93 if (oldWidget !== null) {
94 const oldContext = this._docManager.contextForWidget(oldWidget);
95 if (oldContext) {
96 oldContext.pathChanged.disconnect(this._onPathChange);
97 }
98 else {
99 oldWidget.title.changed.disconnect(this._onTitleChange);
100 }
101 }
102 const oldState = this._getAllState();
103 this._widget = widget;
104 if (this._widget === null) {
105 this._path = '';
106 this._name = '';
107 }
108 else {
109 const widgetContext = this._docManager.contextForWidget(this._widget);
110 if (widgetContext) {
111 this._path = widgetContext.path;
112 this._name = PathExt.basename(widgetContext.path);
113 widgetContext.pathChanged.connect(this._onPathChange);
114 }
115 else {
116 this._path = '';
117 this._name = this._widget.title.label;
118 this._widget.title.changed.connect(this._onTitleChange);
119 }
120 }
121 this._triggerChange(oldState, this._getAllState());
122 }
123 /**
124 * Get the current state of the model.
125 */
126 _getAllState() {
127 return [this._path, this._name];
128 }
129 /**
130 * Trigger a state change to rerender.
131 */
132 _triggerChange(oldState, newState) {
133 if (oldState[0] !== newState[0] || oldState[1] !== newState[1]) {
134 this.stateChanged.emit(void 0);
135 }
136 }
137 }
138 PathStatus.Model = Model;
139})(PathStatus || (PathStatus = {}));
140//# sourceMappingURL=pathstatus.js.map
\No newline at end of file