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