UNPKG

4.56 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, { tabIndex: 0, spacing: HALF_SPACING, onClick: props.handleClick, onKeyDown: props.handleKeyDown },
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._handleKeyDown = opts.onKeyDown;
39 this.translator = opts.translator || nullTranslator;
40 this._trans = this.translator.load('jupyterlab');
41 this._serviceManager.sessions.runningChanged.connect(this._onSessionsRunningChanged, this);
42 this._serviceManager.terminals.runningChanged.connect(this._onTerminalsRunningChanged, this);
43 this.addClass('jp-mod-highlighted');
44 }
45 /**
46 * Render the running sessions widget.
47 */
48 render() {
49 if (!this.model) {
50 return null;
51 }
52 // TODO-TRANS: Should probably be handled differently.
53 // This is more localizable friendly: "Terminals: %1 | Kernels: %2"
54 this.title.caption = this._trans.__('%1 Terminals, %2 Kernel sessions', this.model.terminals, this.model.sessions);
55 return (React.createElement(RunningSessionsComponent, { sessions: this.model.sessions, terminals: this.model.terminals, handleClick: this._handleClick, handleKeyDown: this._handleKeyDown }));
56 }
57 /**
58 * Dispose of the status item.
59 */
60 dispose() {
61 super.dispose();
62 this._serviceManager.sessions.runningChanged.disconnect(this._onSessionsRunningChanged, this);
63 this._serviceManager.terminals.runningChanged.disconnect(this._onTerminalsRunningChanged, this);
64 }
65 /**
66 * Set the number of kernel sessions when the list changes.
67 */
68 _onSessionsRunningChanged(manager, sessions) {
69 this.model.sessions = sessions.length;
70 }
71 /**
72 * Set the number of terminal sessions when the list changes.
73 */
74 _onTerminalsRunningChanged(manager, terminals) {
75 this.model.terminals = terminals.length;
76 }
77}
78/**
79 * A namespace for RunningSessions statics.
80 */
81(function (RunningSessions) {
82 /**
83 * A VDomModel for the RunningSessions status item.
84 */
85 class Model extends VDomModel {
86 constructor() {
87 super(...arguments);
88 this._terminals = 0;
89 this._sessions = 0;
90 }
91 /**
92 * The number of active kernel sessions.
93 */
94 get sessions() {
95 return this._sessions;
96 }
97 set sessions(sessions) {
98 const oldSessions = this._sessions;
99 this._sessions = sessions;
100 if (oldSessions !== this._sessions) {
101 this.stateChanged.emit(void 0);
102 }
103 }
104 /**
105 * The number of active terminal sessions.
106 */
107 get terminals() {
108 return this._terminals;
109 }
110 set terminals(terminals) {
111 const oldTerminals = this._terminals;
112 this._terminals = terminals;
113 if (oldTerminals !== this._terminals) {
114 this.stateChanged.emit(void 0);
115 }
116 }
117 }
118 RunningSessions.Model = Model;
119})(RunningSessions || (RunningSessions = {}));
120//# sourceMappingURL=runningSessions.js.map
\No newline at end of file