forked from jupyterlab/jupyterlab
/
status.tsx
362 lines (321 loc) · 8.95 KB
/
status.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
import {
ILogger,
ILoggerRegistry,
IContentChange
} from '@jupyterlab/logconsole';
import { GroupItem, TextItem, interactiveItem } from '@jupyterlab/statusbar';
import { DefaultIconReact } from '@jupyterlab/ui-components';
import { Signal } from '@phosphor/signaling';
import React from 'react';
/**
* A pure functional component for a Log Console status item.
*
* @param props - the props for the component.
*
* @returns a tsx component for rendering the Log Console status.
*/
function LogConsoleStatusComponent(
props: LogConsoleStatusComponent.IProps
): React.ReactElement<LogConsoleStatusComponent.IProps> {
let title = '';
if (props.newMessages > 0) {
title = `${props.newMessages} new messages, `;
}
title += `${props.logEntries} log entries for ${props.source}`;
return (
<GroupItem spacing={0} onClick={props.handleClick} title={title}>
<DefaultIconReact name={'list'} top={'2px'} kind={'statusBar'} />
{props.newMessages > 0 && <TextItem source={props.newMessages} />}
</GroupItem>
);
}
/*
* A namespace for LogConsoleStatusComponent.
*/
namespace LogConsoleStatusComponent {
/**
* The props for the LogConsoleStatusComponent.
*/
export interface IProps {
/**
* A click handler for the item. By default
* Log Console panel is launched.
*/
handleClick: () => void;
/**
* Number of log entries.
*/
logEntries: number;
/**
* Number of new log messages.
*/
newMessages: number;
/**
* Log source name
*/
source: string;
}
}
/**
* A VDomRenderer widget for displaying the status of Log Console logs.
*/
export class LogConsoleStatus extends VDomRenderer<LogConsoleStatus.Model> {
/**
* Construct the log console status widget.
*
* @param options - The status widget initialization options.
*/
constructor(options: LogConsoleStatus.IOptions) {
super();
this._handleClick = options.handleClick;
this.model = new LogConsoleStatus.Model(options.loggerRegistry);
this.addClass(interactiveItem);
this.addClass('jp-LogConsoleStatusItem');
}
/**
* Render the log console status item.
*/
render() {
if (this.model === null || this.model.version === 0) {
this.hide();
return null;
}
this.show();
let {
flashEnabled,
messages,
source,
version,
versionDisplayed,
versionNotified
} = this.model;
if (source !== null && flashEnabled && version > versionNotified) {
this._flashHighlight();
this.model.sourceNotified(source, version);
} else if (source !== null && flashEnabled && version > versionDisplayed) {
this._showHighlighted();
} else {
this._clearHighlight();
}
return (
<LogConsoleStatusComponent
handleClick={this._handleClick}
logEntries={messages}
newMessages={version - versionDisplayed}
source={this.model.source}
/>
);
}
private _flashHighlight() {
this._showHighlighted();
// To make sure the browser triggers the animation, we remove the class,
// wait for an animation frame, then add it back
this.removeClass('jp-LogConsole-flash');
requestAnimationFrame(() => {
this.addClass('jp-LogConsole-flash');
});
}
private _showHighlighted() {
this.addClass('jp-mod-selected');
}
private _clearHighlight() {
this.removeClass('jp-LogConsole-flash');
this.removeClass('jp-mod-selected');
}
private _handleClick: () => void;
}
/**
* A namespace for Log Console log status.
*/
export namespace LogConsoleStatus {
/**
* A VDomModel for the LogConsoleStatus item.
*/
export class Model extends VDomModel {
/**
* Create a new LogConsoleStatus model.
*
* @param loggerRegistry - The logger registry providing the logs.
*/
constructor(loggerRegistry: ILoggerRegistry) {
super();
this._loggerRegistry = loggerRegistry;
this._loggerRegistry.registryChanged.connect(
this._handleLogRegistryChange,
this
);
this._handleLogRegistryChange();
}
/**
* Number of messages currently in the current source.
*/
get messages(): number {
if (this._source === null) {
return 0;
}
const logger = this._loggerRegistry.getLogger(this._source);
return logger.length;
}
/**
* The number of messages ever stored by the current source.
*/
get version(): number {
if (this._source === null) {
return 0;
}
const logger = this._loggerRegistry.getLogger(this._source);
return logger.version;
}
/**
* The name of the active log source
*/
get source(): string | null {
return this._source;
}
set source(name: string | null) {
if (this._source === name) {
return;
}
this._source = name;
// refresh rendering
this.stateChanged.emit();
}
/**
* The last source version that was displayed.
*/
get versionDisplayed(): number {
if (this._source === null) {
return 0;
}
return this._sourceVersion.get(this.source).lastDisplayed;
}
/**
* The last source version we notified the user about.
*/
get versionNotified(): number {
if (this._source === null) {
return 0;
}
return this._sourceVersion.get(this.source).lastNotified;
}
/**
* Flag to toggle flashing when new logs added.
*/
get flashEnabled(): boolean {
return this._flashEnabled;
}
set flashEnabled(enabled: boolean) {
if (this._flashEnabled === enabled) {
return;
}
this._flashEnabled = enabled;
this.flashEnabledChanged.emit();
// refresh rendering
this.stateChanged.emit();
}
/**
* Record the last source version displayed to the user.
*
* @param source - The name of the log source.
* @param version - The version of the log that was displayed.
*
* #### Notes
* This will also update the last notified version so that the last
* notified version is always at least the last displayed version.
*/
sourceDisplayed(source: string | null, version: number) {
if (source === null) {
return;
}
const versions = this._sourceVersion.get(source);
let change = false;
if (versions.lastDisplayed < version) {
versions.lastDisplayed = version;
change = true;
}
if (versions.lastNotified < version) {
versions.lastNotified = version;
change = true;
}
if (change && source === this._source) {
this.stateChanged.emit();
}
}
/**
* Record a source version we notified the user about.
*
* @param source - The name of the log source.
* @param version - The version of the log.
*/
sourceNotified(source: string | null, version: number) {
if (source === null) {
return;
}
const versions = this._sourceVersion.get(source);
if (versions.lastNotified < version) {
versions.lastNotified = version;
if (source === this._source) {
this.stateChanged.emit();
}
}
}
private _handleLogRegistryChange() {
const loggers = this._loggerRegistry.getLoggers();
for (let logger of loggers) {
if (!this._sourceVersion.has(logger.source)) {
logger.contentChanged.connect(this._handleLogContentChange, this);
this._sourceVersion.set(logger.source, {
lastDisplayed: 0,
lastNotified: 0
});
}
}
}
private _handleLogContentChange(
{ source }: ILogger,
change: IContentChange
) {
if (source === this._source) {
this.stateChanged.emit();
}
}
/**
* A signal emitted when the flash enablement changes.
*/
public flashEnabledChanged = new Signal<this, void>(this);
private _flashEnabled: boolean = true;
private _loggerRegistry: ILoggerRegistry;
private _source: string = null;
/**
* The view status of each source.
*
* #### Notes
* Keys are source names, value is a list of two numbers. The first
* represents the version of the messages that was last displayed to the
* user, the second represents the version that we last notified the user
* about.
*/
private _sourceVersion: Map<string, IVersionInfo> = new Map();
}
interface IVersionInfo {
lastDisplayed: number;
lastNotified: number;
}
/**
* Options for creating a new LogConsoleStatus item
*/
export interface IOptions {
/**
* The logger registry providing the logs.
*/
loggerRegistry: ILoggerRegistry;
/**
* A click handler for the item. By default
* Log Console panel is launched.
*/
handleClick: () => void;
}
}