UNPKG

4.43 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { nullTranslator } from '@jupyterlab/translation';
4import { kernelIcon, terminalIcon, VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
5import React from 'react';
6import { GroupItem, TextItem } from '@jupyterlab/statusbar';
7/**
8 * Half spacing between subitems in a status item.
9 */
10const HALF_SPACING = 4;
11/**
12 * A pure functional component for rendering kernel and terminal sessions.
13 *
14 * @param props: the props for the component.
15 *
16 * @returns a tsx component for the running sessions.
17 */
18function RunningSessionsComponent(props) {
19 return (React.createElement(GroupItem, { spacing: HALF_SPACING, onClick: props.handleClick },
20 React.createElement(GroupItem, { spacing: HALF_SPACING },
21 React.createElement(TextItem, { source: props.terminals }),
22 React.createElement(terminalIcon.react, { left: '1px', top: '3px', stylesheet: 'statusBar' })),
23 React.createElement(GroupItem, { spacing: HALF_SPACING },
24 React.createElement(TextItem, { source: props.sessions }),
25 React.createElement(kernelIcon.react, { top: '2px', stylesheet: 'statusBar' }))));
26}
27/**
28 * A VDomRenderer for a RunningSessions status item.
29 */
30export class RunningSessions extends VDomRenderer {
31 /**
32 * Create a new RunningSessions widget.
33 */
34 constructor(opts) {
35 super(new RunningSessions.Model());
36 this._serviceManager = opts.serviceManager;
37 this._handleClick = opts.onClick;
38 this.translator = opts.translator || nullTranslator;
39 this._trans = this.translator.load('jupyterlab');
40 this._serviceManager.sessions.runningChanged.connect(this._onSessionsRunningChanged, this);
41 this._serviceManager.terminals.runningChanged.connect(this._onTerminalsRunningChanged, this);
42 this.addClass('jp-mod-highlighted');
43 }
44 /**
45 * Render the running sessions widget.
46 */
47 render() {
48 if (!this.model) {
49 return null;
50 }
51 // TODO-TRANS: Should probably be handled differently.
52 // This is more localizable friendly: "Terminals: %1 | Kernels: %2"
53 this.title.caption = this._trans.__('%1 Terminals, %2 Kernel sessions', this.model.terminals, this.model.sessions);
54 return (React.createElement(RunningSessionsComponent, { sessions: this.model.sessions, terminals: this.model.terminals, handleClick: this._handleClick }));
55 }
56 /**
57 * Dispose of the status item.
58 */
59 dispose() {
60 super.dispose();
61 this._serviceManager.sessions.runningChanged.disconnect(this._onSessionsRunningChanged, this);
62 this._serviceManager.terminals.runningChanged.disconnect(this._onTerminalsRunningChanged, this);
63 }
64 /**
65 * Set the number of kernel sessions when the list changes.
66 */
67 _onSessionsRunningChanged(manager, sessions) {
68 this.model.sessions = sessions.length;
69 }
70 /**
71 * Set the number of terminal sessions when the list changes.
72 */
73 _onTerminalsRunningChanged(manager, terminals) {
74 this.model.terminals = terminals.length;
75 }
76}
77/**
78 * A namespace for RunningSessions statics.
79 */
80(function (RunningSessions) {
81 /**
82 * A VDomModel for the RunningSessions status item.
83 */
84 class Model extends VDomModel {
85 constructor() {
86 super(...arguments);
87 this._terminals = 0;
88 this._sessions = 0;
89 }
90 /**
91 * The number of active kernel sessions.
92 */
93 get sessions() {
94 return this._sessions;
95 }
96 set sessions(sessions) {
97 const oldSessions = this._sessions;
98 this._sessions = sessions;
99 if (oldSessions !== this._sessions) {
100 this.stateChanged.emit(void 0);
101 }
102 }
103 /**
104 * The number of active terminal sessions.
105 */
106 get terminals() {
107 return this._terminals;
108 }
109 set terminals(terminals) {
110 const oldTerminals = this._terminals;
111 this._terminals = terminals;
112 if (oldTerminals !== this._terminals) {
113 this.stateChanged.emit(void 0);
114 }
115 }
116 }
117 RunningSessions.Model = Model;
118})(RunningSessions || (RunningSessions = {}));
119//# sourceMappingURL=runningSessions.js.map
\No newline at end of file