UNPKG

6.88 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5import { nullTranslator } from '@jupyterlab/translation';
6import { notTrustedIcon, trustedIcon, VDomModel, VDomRenderer } from '@jupyterlab/ui-components';
7import React from 'react';
8const TRUST_CLASS = 'jp-StatusItem-trust';
9/**
10 * Determine the notebook trust status message.
11 */
12function 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 * A pure function for a notebook trust status component.
27 *
28 * @param props: the props for the component.
29 *
30 * @returns a tsx component for notebook trust.
31 */
32function 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 * The NotebookTrust status item.
42 */
43export class NotebookTrustStatus extends VDomRenderer {
44 /**
45 * Construct a new status item.
46 */
47 constructor(translator) {
48 super(new NotebookTrustStatus.Model());
49 this.translator = translator || nullTranslator;
50 this.node.classList.add(TRUST_CLASS);
51 }
52 /**
53 * Render the NotebookTrust status item.
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 * A namespace for NotebookTrust statics.
68 */
69(function (NotebookTrustStatus) {
70 /**
71 * A VDomModel for the NotebookTrust status item.
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 * The number of trusted code cells in the current notebook.
83 */
84 get trustedCells() {
85 return this._trustedCells;
86 }
87 /**
88 * The total number of code cells in the current notebook.
89 */
90 get totalCells() {
91 return this._totalCells;
92 }
93 /**
94 * Whether the active cell is trusted.
95 */
96 get activeCellTrusted() {
97 return this._activeCellTrusted;
98 }
99 /**
100 * The current notebook for the model.
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 // Add listeners
120 this._notebook.activeCellChanged.connect(this._onActiveCellChanged, this);
121 this._notebook.modelContentChanged.connect(this._onModelChanged, this);
122 // Derive values
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 * When the notebook model changes, update the trust state.
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 * When the active cell changes, update the trust state.
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 * Given a notebook model, figure out how many of the code cells are trusted.
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 * Get the current state of the model.
180 */
181 _getAllState() {
182 return [this._trustedCells, this._totalCells, this.activeCellTrusted];
183 }
184 /**
185 * Trigger a change in the renderer.
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//# sourceMappingURL=truststatus.js.map
\No newline at end of file