1 | import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
|
2 | import { nullTranslator } from '@jupyterlab/translation';
|
3 | import { notTrustedIcon, trustedIcon } from '@jupyterlab/ui-components';
|
4 | import { toArray } from '@lumino/algorithm';
|
5 | import React from 'react';
|
6 |
|
7 |
|
8 |
|
9 | function cellTrust(props, translator) {
|
10 | translator = translator || nullTranslator;
|
11 | const trans = translator.load('jupyterlab');
|
12 | if (props.trustedCells === props.totalCells) {
|
13 | return [
|
14 | trans.__('Notebook trusted: %1 of %2 cells trusted.', props.trustedCells, props.totalCells),
|
15 | 'jp-StatusItem-trusted'
|
16 | ];
|
17 | }
|
18 | else if (props.activeCellTrusted) {
|
19 | return [
|
20 | trans.__('Active cell trusted: %1 of %2 cells trusted.', props.trustedCells, props.totalCells),
|
21 | 'jp-StatusItem-trusted'
|
22 | ];
|
23 | }
|
24 | else {
|
25 | return [
|
26 | trans.__('Notebook not trusted: %1 of %2 cells trusted.', props.trustedCells, props.totalCells),
|
27 | 'jp-StatusItem-untrusted'
|
28 | ];
|
29 | }
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function NotebookTrustComponent(props) {
|
39 | if (props.allCellsTrusted) {
|
40 | return React.createElement(trustedIcon.react, { top: '2px', stylesheet: 'statusBar' });
|
41 | }
|
42 | else {
|
43 | return React.createElement(notTrustedIcon.react, { top: '2px', stylesheet: 'statusBar' });
|
44 | }
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 | export class NotebookTrustStatus extends VDomRenderer {
|
50 | |
51 |
|
52 |
|
53 | constructor(translator) {
|
54 | super(new NotebookTrustStatus.Model());
|
55 | this.translator = translator || nullTranslator;
|
56 | }
|
57 | |
58 |
|
59 |
|
60 | render() {
|
61 | if (!this.model) {
|
62 | return null;
|
63 | }
|
64 | this.node.title = cellTrust(this.model, this.translator)[0];
|
65 | return (React.createElement("div", null,
|
66 | React.createElement(NotebookTrustComponent, { allCellsTrusted: this.model.trustedCells === this.model.totalCells, activeCellTrusted: this.model.activeCellTrusted, totalCells: this.model.totalCells, trustedCells: this.model.trustedCells })));
|
67 | }
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 | (function (NotebookTrustStatus) {
|
73 | |
74 |
|
75 |
|
76 | class Model extends VDomModel {
|
77 | constructor() {
|
78 | super(...arguments);
|
79 | this._trustedCells = 0;
|
80 | this._totalCells = 0;
|
81 | this._activeCellTrusted = false;
|
82 | this._notebook = null;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | get trustedCells() {
|
88 | return this._trustedCells;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 | get totalCells() {
|
94 | return this._totalCells;
|
95 | }
|
96 | |
97 |
|
98 |
|
99 | get activeCellTrusted() {
|
100 | return this._activeCellTrusted;
|
101 | }
|
102 | |
103 |
|
104 |
|
105 | get notebook() {
|
106 | return this._notebook;
|
107 | }
|
108 | set notebook(model) {
|
109 | const oldNotebook = this._notebook;
|
110 | if (oldNotebook !== null) {
|
111 | oldNotebook.activeCellChanged.disconnect(this._onActiveCellChanged, this);
|
112 | oldNotebook.modelContentChanged.disconnect(this._onModelChanged, this);
|
113 | }
|
114 | const oldState = this._getAllState();
|
115 | this._notebook = model;
|
116 | if (this._notebook === null) {
|
117 | this._trustedCells = 0;
|
118 | this._totalCells = 0;
|
119 | this._activeCellTrusted = false;
|
120 | }
|
121 | else {
|
122 |
|
123 | this._notebook.activeCellChanged.connect(this._onActiveCellChanged, this);
|
124 | this._notebook.modelContentChanged.connect(this._onModelChanged, this);
|
125 |
|
126 | if (this._notebook.activeCell !== undefined) {
|
127 | this._activeCellTrusted = this._notebook.activeCell.model.trusted;
|
128 | }
|
129 | else {
|
130 | this._activeCellTrusted = false;
|
131 | }
|
132 | const { total, trusted } = this._deriveCellTrustState(this._notebook.model);
|
133 | this._totalCells = total;
|
134 | this._trustedCells = trusted;
|
135 | }
|
136 | this._triggerChange(oldState, this._getAllState());
|
137 | }
|
138 | |
139 |
|
140 |
|
141 | _onModelChanged(notebook) {
|
142 | const oldState = this._getAllState();
|
143 | const { total, trusted } = this._deriveCellTrustState(notebook.model);
|
144 | this._totalCells = total;
|
145 | this._trustedCells = trusted;
|
146 | this._triggerChange(oldState, this._getAllState());
|
147 | }
|
148 | |
149 |
|
150 |
|
151 | _onActiveCellChanged(model, cell) {
|
152 | const oldState = this._getAllState();
|
153 | if (cell) {
|
154 | this._activeCellTrusted = cell.model.trusted;
|
155 | }
|
156 | else {
|
157 | this._activeCellTrusted = false;
|
158 | }
|
159 | this._triggerChange(oldState, this._getAllState());
|
160 | }
|
161 | |
162 |
|
163 |
|
164 | _deriveCellTrustState(model) {
|
165 | if (model === null) {
|
166 | return { total: 0, trusted: 0 };
|
167 | }
|
168 | const cells = toArray(model.cells);
|
169 | const trusted = cells.reduce((accum, current) => {
|
170 | if (current.trusted) {
|
171 | return accum + 1;
|
172 | }
|
173 | else {
|
174 | return accum;
|
175 | }
|
176 | }, 0);
|
177 | const total = cells.length;
|
178 | return {
|
179 | total,
|
180 | trusted
|
181 | };
|
182 | }
|
183 | |
184 |
|
185 |
|
186 | _getAllState() {
|
187 | return [this._trustedCells, this._totalCells, this.activeCellTrusted];
|
188 | }
|
189 | |
190 |
|
191 |
|
192 | _triggerChange(oldState, newState) {
|
193 | if (oldState[0] !== newState[0] ||
|
194 | oldState[1] !== newState[1] ||
|
195 | oldState[2] !== newState[2]) {
|
196 | this.stateChanged.emit(void 0);
|
197 | }
|
198 | }
|
199 | }
|
200 | NotebookTrustStatus.Model = Model;
|
201 | })(NotebookTrustStatus || (NotebookTrustStatus = {}));
|
202 |
|
\ | No newline at end of file |