UNPKG

4.03 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5import { TextItem } from '@jupyterlab/statusbar';
6import { nullTranslator } from '@jupyterlab/translation';
7import { VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
8import * as React from 'react';
9/**
10 * A pure function for rendering a Command/Edit mode component.
11 *
12 * @param props: the props for rendering the component.
13 *
14 * @returns a tsx component for command/edit mode.
15 */
16function CommandEditComponent(props) {
17 const trans = (props.translator || nullTranslator).load('jupyterlab');
18 return (React.createElement(TextItem, { source: trans.__('Mode: %1', props.modeNames[props.notebookMode]) }));
19}
20/**
21 * StatusBar item to display which notebook mode user is in.
22 */
23export class CommandEditStatus extends VDomRenderer {
24 /**
25 * Construct a new CommandEdit status item.
26 */
27 constructor(translator) {
28 super(new CommandEditStatus.Model());
29 this.translator = translator || nullTranslator;
30 this._trans = this.translator.load('jupyterlab');
31 this._modeNames = {
32 command: this._trans.__('Command'),
33 edit: this._trans.__('Edit')
34 };
35 }
36 /**
37 * Render the CommandEdit status item.
38 */
39 render() {
40 if (!this.model) {
41 return null;
42 }
43 this.node.title = this._trans.__('Notebook is in %1 mode', this._modeNames[this.model.notebookMode]);
44 return (React.createElement(CommandEditComponent, { notebookMode: this.model.notebookMode, translator: this.translator, modeNames: this._modeNames }));
45 }
46}
47/**
48 * A namespace for CommandEdit statics.
49 */
50(function (CommandEditStatus) {
51 /**
52 * A VDomModel for the CommandEdit renderer.
53 */
54 class Model extends VDomModel {
55 constructor() {
56 super(...arguments);
57 /**
58 * On a change to the notebook, update the mode.
59 */
60 this._onChanged = (_notebook) => {
61 const oldMode = this._notebookMode;
62 if (this._notebook) {
63 this._notebookMode = _notebook.mode;
64 }
65 else {
66 this._notebookMode = 'command';
67 }
68 this._triggerChange(oldMode, this._notebookMode);
69 };
70 this._notebookMode = 'command';
71 this._notebook = null;
72 }
73 /**
74 * The current mode of the current notebook.
75 */
76 get notebookMode() {
77 return this._notebookMode;
78 }
79 /**
80 * Set the current notebook for the model.
81 */
82 set notebook(notebook) {
83 const oldNotebook = this._notebook;
84 if (oldNotebook !== null) {
85 oldNotebook.stateChanged.disconnect(this._onChanged, this);
86 oldNotebook.activeCellChanged.disconnect(this._onChanged, this);
87 oldNotebook.modelContentChanged.disconnect(this._onChanged, this);
88 }
89 const oldMode = this._notebookMode;
90 this._notebook = notebook;
91 if (this._notebook === null) {
92 this._notebookMode = 'command';
93 }
94 else {
95 this._notebookMode = this._notebook.mode;
96 this._notebook.stateChanged.connect(this._onChanged, this);
97 this._notebook.activeCellChanged.connect(this._onChanged, this);
98 this._notebook.modelContentChanged.connect(this._onChanged, this);
99 }
100 this._triggerChange(oldMode, this._notebookMode);
101 }
102 /**
103 * Trigger a state change for the renderer.
104 */
105 _triggerChange(oldState, newState) {
106 if (oldState !== newState) {
107 this.stateChanged.emit(void 0);
108 }
109 }
110 }
111 CommandEditStatus.Model = Model;
112})(CommandEditStatus || (CommandEditStatus = {}));
113//# sourceMappingURL=modestatus.js.map
\No newline at end of file