1 |
|
2 |
|
3 | import { TextItem } from '@jupyterlab/statusbar';
|
4 | import { nullTranslator } from '@jupyterlab/translation';
|
5 | import { VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
|
6 | import { JSONExt } from '@lumino/coreutils';
|
7 | import React from 'react';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export 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 |
|
37 |
|
38 | function 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 |
|
49 |
|
50 | export class KernelStatus extends VDomRenderer {
|
51 | |
52 |
|
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 |
|
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 |
|
75 |
|
76 | (function (KernelStatus) {
|
77 | |
78 |
|
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 |
|
93 |
|
94 | get kernelName() {
|
95 | return this._kernelName;
|
96 | }
|
97 | |
98 |
|
99 |
|
100 | get status() {
|
101 | return this._kernelStatus
|
102 | ? this._statusNames[this._kernelStatus]
|
103 | : undefined;
|
104 | }
|
105 | |
106 |
|
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 |
|
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 |
|
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 |
|
150 |
|
151 | _onKernelChanged(_sessionContext, change) {
|
152 | var _a;
|
153 | const oldState = this._getAllState();
|
154 |
|
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 |
|
\ | No newline at end of file |