UNPKG

4.46 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
4import { TextItem } from '@jupyterlab/statusbar';
5import { nullTranslator } from '@jupyterlab/translation';
6import React from 'react';
7/**
8 * A pure functional component for a Saving status item.
9 *
10 * @param props - the props for the component.
11 *
12 * @returns a tsx component for rendering the saving state.
13 */
14function SavingStatusComponent(props) {
15 return React.createElement(TextItem, { source: props.fileStatus });
16}
17/**
18 * The amount of time (in ms) to retain the saving completed message
19 * before hiding the status item.
20 */
21const SAVING_COMPLETE_MESSAGE_MILLIS = 2000;
22/**
23 * A VDomRenderer for a saving status item.
24 */
25export class SavingStatus extends VDomRenderer {
26 /**
27 * Create a new SavingStatus item.
28 */
29 constructor(opts) {
30 super(new SavingStatus.Model(opts.docManager));
31 const translator = opts.translator || nullTranslator;
32 const trans = translator.load('jupyterlab');
33 this._statusMap = {
34 completed: trans.__('Saving completed'),
35 started: trans.__('Saving started'),
36 failed: trans.__('Saving failed')
37 };
38 }
39 /**
40 * Render the SavingStatus item.
41 */
42 render() {
43 if (this.model === null || this.model.status === null) {
44 return null;
45 }
46 else {
47 return (React.createElement(SavingStatusComponent, { fileStatus: this._statusMap[this.model.status] }));
48 }
49 }
50}
51/**
52 * A namespace for SavingStatus statics.
53 */
54(function (SavingStatus) {
55 /**
56 * A VDomModel for the SavingStatus item.
57 */
58 class Model extends VDomModel {
59 /**
60 * Create a new SavingStatus model.
61 */
62 constructor(docManager) {
63 super();
64 /**
65 * React to a saving status change from the current document widget.
66 */
67 this._onStatusChange = (_, newStatus) => {
68 this._status = newStatus;
69 if (this._status === 'completed') {
70 setTimeout(() => {
71 this._status = null;
72 this.stateChanged.emit(void 0);
73 }, SAVING_COMPLETE_MESSAGE_MILLIS);
74 this.stateChanged.emit(void 0);
75 }
76 else {
77 this.stateChanged.emit(void 0);
78 }
79 };
80 this._status = null;
81 this._widget = null;
82 this._status = null;
83 this.widget = null;
84 this._docManager = docManager;
85 }
86 /**
87 * The current status of the model.
88 */
89 get status() {
90 return this._status;
91 }
92 /**
93 * The current widget for the model. Any widget can be assigned,
94 * but it only has any effect if the widget is an IDocument widget
95 * known to the application document manager.
96 */
97 get widget() {
98 return this._widget;
99 }
100 set widget(widget) {
101 var _a, _b;
102 const oldWidget = this._widget;
103 if (oldWidget !== null) {
104 const oldContext = this._docManager.contextForWidget(oldWidget);
105 if (oldContext) {
106 oldContext.saveState.disconnect(this._onStatusChange);
107 }
108 else if ((_a = this._widget.content) === null || _a === void 0 ? void 0 : _a.saveStateChanged) {
109 this._widget.content.saveStateChanged.disconnect(this._onStatusChange);
110 }
111 }
112 this._widget = widget;
113 if (this._widget === null) {
114 this._status = null;
115 }
116 else {
117 const widgetContext = this._docManager.contextForWidget(this._widget);
118 if (widgetContext) {
119 widgetContext.saveState.connect(this._onStatusChange);
120 }
121 else if ((_b = this._widget.content) === null || _b === void 0 ? void 0 : _b.saveStateChanged) {
122 this._widget.content.saveStateChanged.connect(this._onStatusChange);
123 }
124 }
125 }
126 }
127 SavingStatus.Model = Model;
128})(SavingStatus || (SavingStatus = {}));
129//# sourceMappingURL=savingstatus.js.map
\No newline at end of file