widgetmanager.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ArrayExt, each, map, find, filter, toArray } from '@lumino/algorithm';
  4. import { DisposableSet, IDisposable } from '@lumino/disposable';
  5. import { IMessageHandler, Message, MessageLoop } from '@lumino/messaging';
  6. import { AttachedProperty } from '@lumino/properties';
  7. import { ISignal, Signal } from '@lumino/signaling';
  8. import { Widget } from '@lumino/widgets';
  9. import { Time } from '@jupyterlab/coreutils';
  10. import { showDialog, Dialog } from '@jupyterlab/apputils';
  11. import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
  12. import { Contents } from '@jupyterlab/services';
  13. /**
  14. * The class name added to document widgets.
  15. */
  16. const DOCUMENT_CLASS = 'jp-Document';
  17. /**
  18. * A class that maintains the lifecycle of file-backed widgets.
  19. */
  20. export class DocumentWidgetManager implements IDisposable {
  21. /**
  22. * Construct a new document widget manager.
  23. */
  24. constructor(options: DocumentWidgetManager.IOptions) {
  25. this._registry = options.registry;
  26. }
  27. /**
  28. * A signal emitted when one of the documents is activated.
  29. */
  30. get activateRequested(): ISignal<this, string> {
  31. return this._activateRequested;
  32. }
  33. /**
  34. * Test whether the document widget manager is disposed.
  35. */
  36. get isDisposed(): boolean {
  37. return this._isDisposed;
  38. }
  39. /**
  40. * Dispose of the resources used by the widget manager.
  41. */
  42. dispose(): void {
  43. if (this.isDisposed) {
  44. return;
  45. }
  46. this._isDisposed = true;
  47. Signal.disconnectReceiver(this);
  48. }
  49. /**
  50. * Create a widget for a document and handle its lifecycle.
  51. *
  52. * @param factory - The widget factory.
  53. *
  54. * @param context - The document context object.
  55. *
  56. * @returns A widget created by the factory.
  57. *
  58. * @throws If the factory is not registered.
  59. */
  60. createWidget(
  61. factory: DocumentRegistry.WidgetFactory,
  62. context: DocumentRegistry.Context
  63. ): IDocumentWidget {
  64. const widget = factory.createNew(context);
  65. this._initializeWidget(widget, factory, context);
  66. return widget;
  67. }
  68. /**
  69. * When a new widget is created, we need to hook it up
  70. * with some signals, update the widget extensions (for
  71. * this kind of widget) in the docregistry, among
  72. * other things.
  73. */
  74. private _initializeWidget(
  75. widget: IDocumentWidget,
  76. factory: DocumentRegistry.WidgetFactory,
  77. context: DocumentRegistry.Context
  78. ) {
  79. Private.factoryProperty.set(widget, factory);
  80. // Handle widget extensions.
  81. const disposables = new DisposableSet();
  82. each(this._registry.widgetExtensions(factory.name), extender => {
  83. disposables.add(extender.createNew(widget, context));
  84. });
  85. Private.disposablesProperty.set(widget, disposables);
  86. widget.disposed.connect(this._onWidgetDisposed, this);
  87. this.adoptWidget(context, widget);
  88. context.fileChanged.connect(this._onFileChanged, this);
  89. context.pathChanged.connect(this._onPathChanged, this);
  90. void context.ready.then(() => {
  91. void this.setCaption(widget);
  92. });
  93. }
  94. /**
  95. * Install the message hook for the widget and add to list
  96. * of known widgets.
  97. *
  98. * @param context - The document context object.
  99. *
  100. * @param widget - The widget to adopt.
  101. */
  102. adoptWidget(
  103. context: DocumentRegistry.Context,
  104. widget: IDocumentWidget
  105. ): void {
  106. const widgets = Private.widgetsProperty.get(context);
  107. widgets.push(widget);
  108. MessageLoop.installMessageHook(widget, this);
  109. widget.addClass(DOCUMENT_CLASS);
  110. widget.title.closable = true;
  111. widget.disposed.connect(this._widgetDisposed, this);
  112. Private.contextProperty.set(widget, context);
  113. }
  114. /**
  115. * See if a widget already exists for the given context and widget name.
  116. *
  117. * @param context - The document context object.
  118. *
  119. * @returns The found widget, or `undefined`.
  120. *
  121. * #### Notes
  122. * This can be used to use an existing widget instead of opening
  123. * a new widget.
  124. */
  125. findWidget(
  126. context: DocumentRegistry.Context,
  127. widgetName: string
  128. ): IDocumentWidget | undefined {
  129. const widgets = Private.widgetsProperty.get(context);
  130. if (!widgets) {
  131. return undefined;
  132. }
  133. return find(widgets, widget => {
  134. const factory = Private.factoryProperty.get(widget);
  135. if (!factory) {
  136. return false;
  137. }
  138. return factory.name === widgetName;
  139. });
  140. }
  141. /**
  142. * Get the document context for a widget.
  143. *
  144. * @param widget - The widget of interest.
  145. *
  146. * @returns The context associated with the widget, or `undefined`.
  147. */
  148. contextForWidget(widget: Widget): DocumentRegistry.Context | undefined {
  149. return Private.contextProperty.get(widget);
  150. }
  151. /**
  152. * Clone a widget.
  153. *
  154. * @param widget - The source widget.
  155. *
  156. * @returns A new widget or `undefined`.
  157. *
  158. * #### Notes
  159. * Uses the same widget factory and context as the source, or throws
  160. * if the source widget is not managed by this manager.
  161. */
  162. cloneWidget(widget: Widget): IDocumentWidget | undefined {
  163. const context = Private.contextProperty.get(widget);
  164. if (!context) {
  165. return undefined;
  166. }
  167. const factory = Private.factoryProperty.get(widget);
  168. if (!factory) {
  169. return undefined;
  170. }
  171. const newWidget = factory.createNew(context, widget as IDocumentWidget);
  172. this._initializeWidget(newWidget, factory, context);
  173. return newWidget;
  174. }
  175. /**
  176. * Close the widgets associated with a given context.
  177. *
  178. * @param context - The document context object.
  179. */
  180. closeWidgets(context: DocumentRegistry.Context): Promise<void> {
  181. const widgets = Private.widgetsProperty.get(context);
  182. return Promise.all(
  183. toArray(map(widgets, widget => this.onClose(widget)))
  184. ).then(() => undefined);
  185. }
  186. /**
  187. * Dispose of the widgets associated with a given context
  188. * regardless of the widget's dirty state.
  189. *
  190. * @param context - The document context object.
  191. */
  192. deleteWidgets(context: DocumentRegistry.Context): Promise<void> {
  193. const widgets = Private.widgetsProperty.get(context);
  194. return Promise.all(
  195. toArray(map(widgets, widget => this.onDelete(widget)))
  196. ).then(() => undefined);
  197. }
  198. /**
  199. * Filter a message sent to a message handler.
  200. *
  201. * @param handler - The target handler of the message.
  202. *
  203. * @param msg - The message dispatched to the handler.
  204. *
  205. * @returns `false` if the message should be filtered, of `true`
  206. * if the message should be dispatched to the handler as normal.
  207. */
  208. messageHook(handler: IMessageHandler, msg: Message): boolean {
  209. switch (msg.type) {
  210. case 'close-request':
  211. void this.onClose(handler as Widget);
  212. return false;
  213. case 'activate-request': {
  214. const context = this.contextForWidget(handler as Widget);
  215. if (context) {
  216. this._activateRequested.emit(context.path);
  217. }
  218. break;
  219. }
  220. default:
  221. break;
  222. }
  223. return true;
  224. }
  225. /**
  226. * Set the caption for widget title.
  227. *
  228. * @param widget - The target widget.
  229. */
  230. protected async setCaption(widget: Widget): Promise<void> {
  231. const context = Private.contextProperty.get(widget);
  232. if (!context) {
  233. return;
  234. }
  235. const model = context.contentsModel;
  236. if (!model) {
  237. widget.title.caption = '';
  238. return;
  239. }
  240. return context
  241. .listCheckpoints()
  242. .then((checkpoints: Contents.ICheckpointModel[]) => {
  243. if (widget.isDisposed) {
  244. return;
  245. }
  246. const last = checkpoints[checkpoints.length - 1];
  247. const checkpoint = last ? Time.format(last.last_modified) : 'None';
  248. let caption = `Name: ${model!.name}\nPath: ${model!.path}\n`;
  249. if (context!.model.readOnly) {
  250. caption += 'Read-only';
  251. } else {
  252. caption +=
  253. `Last Saved: ${Time.format(model!.last_modified)}\n` +
  254. `Last Checkpoint: ${checkpoint}`;
  255. }
  256. widget.title.caption = caption;
  257. });
  258. }
  259. /**
  260. * Handle `'close-request'` messages.
  261. *
  262. * @param widget - The target widget.
  263. *
  264. * @returns A promise that resolves with whether the widget was closed.
  265. */
  266. protected async onClose(widget: Widget): Promise<boolean> {
  267. // Handle dirty state.
  268. const [shouldClose, ignoreSave] = await this._maybeClose(widget);
  269. if (widget.isDisposed) {
  270. return true;
  271. }
  272. if (shouldClose) {
  273. if (!ignoreSave) {
  274. const context = Private.contextProperty.get(widget);
  275. if (!context) {
  276. return true;
  277. }
  278. if (context.contentsModel?.writable) {
  279. await context.save();
  280. } else {
  281. await context.saveAs();
  282. }
  283. }
  284. if (widget.isDisposed) {
  285. return true;
  286. }
  287. widget.dispose();
  288. }
  289. return shouldClose;
  290. }
  291. /**
  292. * Dispose of widget regardless of widget's dirty state.
  293. *
  294. * @param widget - The target widget.
  295. */
  296. protected onDelete(widget: Widget): Promise<void> {
  297. widget.dispose();
  298. return Promise.resolve(void 0);
  299. }
  300. /**
  301. * Ask the user whether to close an unsaved file.
  302. */
  303. private _maybeClose(widget: Widget): Promise<[boolean, boolean]> {
  304. // Bail if the model is not dirty or other widgets are using the model.)
  305. const context = Private.contextProperty.get(widget);
  306. if (!context) {
  307. return Promise.resolve([true, true]);
  308. }
  309. let widgets = Private.widgetsProperty.get(context);
  310. if (!widgets) {
  311. return Promise.resolve([true, true]);
  312. }
  313. // Filter by whether the factories are read only.
  314. widgets = toArray(
  315. filter(widgets, widget => {
  316. const factory = Private.factoryProperty.get(widget);
  317. if (!factory) {
  318. return false;
  319. }
  320. return factory.readOnly === false;
  321. })
  322. );
  323. const factory = Private.factoryProperty.get(widget);
  324. if (!factory) {
  325. return Promise.resolve([true, true]);
  326. }
  327. const model = context.model;
  328. if (!model.dirty || widgets.length > 1 || factory.readOnly) {
  329. return Promise.resolve([true, true]);
  330. }
  331. const fileName = widget.title.label;
  332. const saveLabel = context.contentsModel?.writable ? 'Save' : 'Save as';
  333. return showDialog({
  334. title: 'Save your work',
  335. body: `Save changes in "${fileName}" before closing?`,
  336. buttons: [
  337. Dialog.cancelButton(),
  338. Dialog.warnButton({ label: 'Discard' }),
  339. Dialog.okButton({ label: saveLabel })
  340. ]
  341. }).then(result => {
  342. return [result.button.accept, result.button.displayType === 'warn'];
  343. });
  344. }
  345. /**
  346. * Handle the disposal of a widget.
  347. */
  348. private _widgetDisposed(widget: Widget): void {
  349. const context = Private.contextProperty.get(widget);
  350. if (!context) {
  351. return;
  352. }
  353. const widgets = Private.widgetsProperty.get(context);
  354. if (!widgets) {
  355. return;
  356. }
  357. // Remove the widget.
  358. ArrayExt.removeFirstOf(widgets, widget);
  359. // Dispose of the context if this is the last widget using it.
  360. if (!widgets.length) {
  361. context.dispose();
  362. }
  363. }
  364. /**
  365. * Handle the disposal of a widget.
  366. */
  367. private _onWidgetDisposed(widget: Widget): void {
  368. const disposables = Private.disposablesProperty.get(widget);
  369. disposables.dispose();
  370. }
  371. /**
  372. * Handle a file changed signal for a context.
  373. */
  374. private _onFileChanged(context: DocumentRegistry.Context): void {
  375. const widgets = Private.widgetsProperty.get(context);
  376. each(widgets, widget => {
  377. void this.setCaption(widget);
  378. });
  379. }
  380. /**
  381. * Handle a path changed signal for a context.
  382. */
  383. private _onPathChanged(context: DocumentRegistry.Context): void {
  384. const widgets = Private.widgetsProperty.get(context);
  385. each(widgets, widget => {
  386. void this.setCaption(widget);
  387. });
  388. }
  389. private _registry: DocumentRegistry;
  390. private _activateRequested = new Signal<this, string>(this);
  391. private _isDisposed = false;
  392. }
  393. /**
  394. * A namespace for document widget manager statics.
  395. */
  396. export namespace DocumentWidgetManager {
  397. /**
  398. * The options used to initialize a document widget manager.
  399. */
  400. export interface IOptions {
  401. /**
  402. * A document registry instance.
  403. */
  404. registry: DocumentRegistry;
  405. }
  406. }
  407. /**
  408. * A private namespace for DocumentManager data.
  409. */
  410. namespace Private {
  411. /**
  412. * A private attached property for a widget context.
  413. */
  414. export const contextProperty = new AttachedProperty<
  415. Widget,
  416. DocumentRegistry.Context | undefined
  417. >({
  418. name: 'context',
  419. create: () => undefined
  420. });
  421. /**
  422. * A private attached property for a widget factory.
  423. */
  424. export const factoryProperty = new AttachedProperty<
  425. Widget,
  426. DocumentRegistry.WidgetFactory | undefined
  427. >({
  428. name: 'factory',
  429. create: () => undefined
  430. });
  431. /**
  432. * A private attached property for the widgets associated with a context.
  433. */
  434. export const widgetsProperty = new AttachedProperty<
  435. DocumentRegistry.Context,
  436. IDocumentWidget[]
  437. >({
  438. name: 'widgets',
  439. create: () => []
  440. });
  441. /**
  442. * A private attached property for a widget's disposables.
  443. */
  444. export const disposablesProperty = new AttachedProperty<
  445. Widget,
  446. DisposableSet
  447. >({
  448. name: 'disposables',
  449. create: () => new DisposableSet()
  450. });
  451. }