UNPKG

6.95 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { TextItem } from '@jupyterlab/statusbar';
4import { nullTranslator } from '@jupyterlab/translation';
5import { VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
6import { JSONExt } from '@lumino/coreutils';
7import React from 'react';
8/**
9 * Helper function to translate kernel statuses mapping by using
10 * input translator.
11 *
12 * @param translator - Language translator.
13 * @return The translated kernel status mapping.
14 */
15export function translateKernelStatuses(translator) {
16 translator = translator || nullTranslator;
17 const trans = translator.load('jupyterlab');
18 const translated = {
19 unknown: trans.__('Unknown'),
20 starting: trans.__('Starting'),
21 idle: trans.__('Idle'),
22 busy: trans.__('Busy'),
23 terminating: trans.__('Terminating'),
24 restarting: trans.__('Restarting'),
25 autorestarting: trans.__('Autorestarting'),
26 dead: trans.__('Dead'),
27 connected: trans.__('Connected'),
28 connecting: trans.__('Connecting'),
29 disconnected: trans.__('Disconnected'),
30 initializing: trans.__('Initializing'),
31 '': ''
32 };
33 return translated;
34}
35/**
36 * A pure functional component for rendering kernel status.
37 */
38function KernelStatusComponent(props) {
39 const translator = props.translator || nullTranslator;
40 const trans = translator.load('jupyterlab');
41 let statusText = '';
42 if (props.status) {
43 statusText = ` | ${props.status}`;
44 }
45 return (React.createElement(TextItem, { onClick: props.handleClick, onKeyDown: props.handleKeyDown, source: `${props.kernelName}${statusText}`, title: trans.__('Change kernel for %1', props.activityName), tabIndex: 0 }));
46}
47/**
48 * A VDomRenderer widget for displaying the status of a kernel.
49 */
50export class KernelStatus extends VDomRenderer {
51 /**
52 * Construct the kernel status widget.
53 */
54 constructor(opts, translator) {
55 super(new KernelStatus.Model(translator));
56 this.translator = translator || nullTranslator;
57 this._handleClick = opts.onClick;
58 this._handleKeyDown = opts.onKeyDown;
59 this.addClass('jp-mod-highlighted');
60 }
61 /**
62 * Render the kernel status item.
63 */
64 render() {
65 if (this.model === null) {
66 return null;
67 }
68 else {
69 return (React.createElement(KernelStatusComponent, { status: this.model.status, kernelName: this.model.kernelName, activityName: this.model.activityName, handleClick: this._handleClick, handleKeyDown: this._handleKeyDown, translator: this.translator }));
70 }
71 }
72}
73/**
74 * A namespace for KernelStatus statics.
75 */
76(function (KernelStatus) {
77 /**
78 * A VDomModel for the kernel status indicator.
79 */
80 class Model extends VDomModel {
81 constructor(translator) {
82 super();
83 this._activityName = '';
84 this._kernelName = '';
85 this._kernelStatus = '';
86 this._sessionContext = null;
87 translator = translator !== null && translator !== void 0 ? translator : nullTranslator;
88 this._trans = translator.load('jupyterlab');
89 this._statusNames = translateKernelStatuses(translator);
90 }
91 /**
92 * The name of the kernel.
93 */
94 get kernelName() {
95 return this._kernelName;
96 }
97 /**
98 * The current status of the kernel.
99 */
100 get status() {
101 return this._kernelStatus
102 ? this._statusNames[this._kernelStatus]
103 : undefined;
104 }
105 /**
106 * A display name for the activity.
107 */
108 get activityName() {
109 return this._activityName;
110 }
111 set activityName(val) {
112 const oldVal = this._activityName;
113 if (oldVal === val) {
114 return;
115 }
116 this._activityName = val;
117 this.stateChanged.emit();
118 }
119 /**
120 * The current client session associated with the kernel status indicator.
121 */
122 get sessionContext() {
123 return this._sessionContext;
124 }
125 set sessionContext(sessionContext) {
126 var _a, _b, _c, _d;
127 (_a = this._sessionContext) === null || _a === void 0 ? void 0 : _a.statusChanged.disconnect(this._onKernelStatusChanged, this);
128 (_b = this._sessionContext) === null || _b === void 0 ? void 0 : _b.connectionStatusChanged.disconnect(this._onKernelStatusChanged, this);
129 (_c = this._sessionContext) === null || _c === void 0 ? void 0 : _c.kernelChanged.disconnect(this._onKernelChanged, this);
130 const oldState = this._getAllState();
131 this._sessionContext = sessionContext;
132 this._kernelStatus = sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.kernelDisplayStatus;
133 this._kernelName =
134 (_d = sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.kernelDisplayName) !== null && _d !== void 0 ? _d : this._trans.__('No Kernel');
135 sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.statusChanged.connect(this._onKernelStatusChanged, this);
136 sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.connectionStatusChanged.connect(this._onKernelStatusChanged, this);
137 sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.kernelChanged.connect(this._onKernelChanged, this);
138 this._triggerChange(oldState, this._getAllState());
139 }
140 /**
141 * React to changes to the kernel status.
142 */
143 _onKernelStatusChanged() {
144 var _a;
145 this._kernelStatus = (_a = this._sessionContext) === null || _a === void 0 ? void 0 : _a.kernelDisplayStatus;
146 this.stateChanged.emit(void 0);
147 }
148 /**
149 * React to changes in the kernel.
150 */
151 _onKernelChanged(_sessionContext, change) {
152 var _a;
153 const oldState = this._getAllState();
154 // sync setting of status and display name
155 this._kernelStatus = (_a = this._sessionContext) === null || _a === void 0 ? void 0 : _a.kernelDisplayStatus;
156 this._kernelName = _sessionContext.kernelDisplayName;
157 this._triggerChange(oldState, this._getAllState());
158 }
159 _getAllState() {
160 return [this._kernelName, this._kernelStatus, this._activityName];
161 }
162 _triggerChange(oldState, newState) {
163 if (JSONExt.deepEqual(oldState, newState)) {
164 this.stateChanged.emit(void 0);
165 }
166 }
167 }
168 KernelStatus.Model = Model;
169})(KernelStatus || (KernelStatus = {}));
170//# sourceMappingURL=kernelstatuses.js.map
\No newline at end of file