-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
truststatus.tsx
274 lines (241 loc) · 6.65 KB
/
truststatus.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
import React from 'react';
import { VDomRenderer, VDomModel } from '@jupyterlab/apputils';
import { INotebookModel, Notebook } from '.';
import { Cell } from '@jupyterlab/cells';
import { NotTrustedIcon, TrustedIcon } from '@jupyterlab/ui-components';
import { toArray } from '@phosphor/algorithm';
/**
* Determine the notebook trust status message.
*/
function cellTrust(
props: NotebookTrustComponent.IProps | NotebookTrustStatus.Model
): string[] {
if (props.trustedCells === props.totalCells) {
return [
`Notebook trusted: ${props.trustedCells} of ${props.totalCells} cells trusted.`,
'jp-StatusItem-trusted'
];
} else if (props.activeCellTrusted) {
return [
`Active cell trusted: ${props.trustedCells} of ${props.totalCells} cells trusted. `,
'jp-StatusItem-trusted'
];
} else {
return [
`Notebook not trusted: ${props.trustedCells} of ${props.totalCells} cells trusted.`,
'jp-StatusItem-untrusted'
];
}
}
/**
* A pure function for a notebook trust status component.
*
* @param props: the props for the component.
*
* @returns a tsx component for notebook trust.
*/
function NotebookTrustComponent(
props: NotebookTrustComponent.IProps
): React.ReactElement<NotebookTrustComponent.IProps> {
if (props.allCellsTrusted) {
return <TrustedIcon top={'2px'} kind={'statusBar'} />;
} else {
return <NotTrustedIcon top={'2px'} kind={'statusBar'} />;
}
}
/**
* A namespace for NotebookTrustComponent statics.
*/
namespace NotebookTrustComponent {
/**
* Props for the NotebookTrustComponent.
*/
export interface IProps {
/**
* Whether all the cells are trusted.
*/
allCellsTrusted: boolean;
/**
* Whether the currently active cell is trusted.
*/
activeCellTrusted: boolean;
/**
* The total number of cells for the current notebook.
*/
totalCells: number;
/**
* The number of trusted cells for the current notebook.
*/
trustedCells: number;
}
}
/**
* The NotebookTrust status item.
*/
export class NotebookTrustStatus extends VDomRenderer<
NotebookTrustStatus.Model
> {
/**
* Construct a new status item.
*/
constructor() {
super();
this.model = new NotebookTrustStatus.Model();
}
/**
* Render the NotebookTrust status item.
*/
render() {
if (!this.model) {
return null;
}
this.node.title = cellTrust(this.model)[0];
return (
<div>
<NotebookTrustComponent
allCellsTrusted={this.model.trustedCells === this.model.totalCells}
activeCellTrusted={this.model.activeCellTrusted}
totalCells={this.model.totalCells}
trustedCells={this.model.trustedCells}
/>
</div>
);
}
}
/**
* A namespace for NotebookTrust statics.
*/
export namespace NotebookTrustStatus {
/**
* A VDomModel for the NotebookTrust status item.
*/
export class Model extends VDomModel {
/**
* The number of trusted cells in the current notebook.
*/
get trustedCells(): number {
return this._trustedCells;
}
/**
* The total number of cells in the current notebook.
*/
get totalCells() {
return this._totalCells;
}
/**
* Whether the active cell is trusted.
*/
get activeCellTrusted() {
return this._activeCellTrusted;
}
/**
* The current notebook for the model.
*/
get notebook() {
return this._notebook;
}
set notebook(model: Notebook | null) {
const oldNotebook = this._notebook;
if (oldNotebook !== null) {
oldNotebook.activeCellChanged.disconnect(
this._onActiveCellChanged,
this
);
oldNotebook.modelContentChanged.disconnect(this._onModelChanged, this);
}
const oldState = this._getAllState();
this._notebook = model;
if (this._notebook === null) {
this._trustedCells = 0;
this._totalCells = 0;
this._activeCellTrusted = false;
} else {
// Add listeners
this._notebook.activeCellChanged.connect(
this._onActiveCellChanged,
this
);
this._notebook.modelContentChanged.connect(this._onModelChanged, this);
// Derive values
if (this._notebook.activeCell !== undefined) {
this._activeCellTrusted = this._notebook!.activeCell!.model.trusted;
} else {
this._activeCellTrusted = false;
}
const { total, trusted } = this._deriveCellTrustState(
this._notebook.model
);
this._totalCells = total;
this._trustedCells = trusted;
}
this._triggerChange(oldState, this._getAllState());
}
/**
* When the notebook model changes, update the trust state.
*/
private _onModelChanged(notebook: Notebook): void {
const oldState = this._getAllState();
const { total, trusted } = this._deriveCellTrustState(notebook.model);
this._totalCells = total;
this._trustedCells = trusted;
this._triggerChange(oldState, this._getAllState());
}
/**
* When the active cell changes, update the trust state.
*/
private _onActiveCellChanged(model: Notebook, cell: Cell | null): void {
const oldState = this._getAllState();
if (cell) {
this._activeCellTrusted = cell.model.trusted;
} else {
this._activeCellTrusted = false;
}
this._triggerChange(oldState, this._getAllState());
}
/**
* Given a notebook model, figure out how many of the cells are trusted.
*/
private _deriveCellTrustState(
model: INotebookModel
): { total: number; trusted: number } {
let cells = toArray(model.cells);
let trusted = cells.reduce((accum, current) => {
if (current.trusted) {
return accum + 1;
} else {
return accum;
}
}, 0);
let total = cells.length;
return {
total,
trusted
};
}
/**
* Get the current state of the model.
*/
private _getAllState(): [number, number, boolean] {
return [this._trustedCells, this._totalCells, this.activeCellTrusted];
}
/**
* Trigger a change in the renderer.
*/
private _triggerChange(
oldState: [number, number, boolean],
newState: [number, number, boolean]
) {
if (
oldState[0] !== newState[0] ||
oldState[1] !== newState[1] ||
oldState[2] !== newState[2]
) {
this.stateChanged.emit(void 0);
}
}
private _trustedCells: number = 0;
private _totalCells: number = 0;
private _activeCellTrusted: boolean = false;
private _notebook: Notebook | null = null;
}
}