handler.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. 'use strict';
  4. import {
  5. IContentsModel, IContentsManager, IContentsOpts
  6. } from 'jupyter-js-services';
  7. import {
  8. IMessageFilter, IMessageHandler, Message, installMessageFilter
  9. } from 'phosphor-messaging';
  10. import {
  11. Property
  12. } from 'phosphor-properties';
  13. import {
  14. ISignal, Signal
  15. } from 'phosphor-signaling';
  16. import {
  17. Widget
  18. } from 'phosphor-widget';
  19. import {
  20. showDialog
  21. } from '../dialog';
  22. /**
  23. * The class name added to a dirty documents.
  24. */
  25. const DIRTY_CLASS = 'jp-mod-dirty';
  26. /**
  27. * An implementation of a file handler.
  28. */
  29. export
  30. abstract class AbstractFileHandler<T extends Widget> implements IMessageFilter {
  31. /**
  32. * Construct a new source file handler.
  33. *
  34. * @param manager - The contents manager used to save/load files.
  35. */
  36. constructor(manager: IContentsManager) {
  37. this._manager = manager;
  38. }
  39. /**
  40. * A signal emitted when a file opens.
  41. */
  42. get opened(): ISignal<AbstractFileHandler<T>, T> {
  43. return Private.openedSignal.bind(this);
  44. }
  45. /**
  46. * Get the list of file extensions explicitly supported by the handler.
  47. */
  48. get fileExtensions(): string[] {
  49. return [];
  50. }
  51. /**
  52. * Get the list of mime types explicitly supported by the handler.
  53. */
  54. get mimeTypes(): string[] {
  55. return [];
  56. }
  57. /**
  58. * Get the contents manager used by the handler.
  59. */
  60. get manager(): IContentsManager {
  61. return this._manager;
  62. }
  63. /**
  64. * Find a widget given a path.
  65. */
  66. findWidget(path: string): T {
  67. for (let w of this._widgets) {
  68. let model = this._getModel(w);
  69. if (model.path === path) {
  70. return w;
  71. }
  72. }
  73. }
  74. /**
  75. * Find a model given a widget. The model itself will have a
  76. * null `content` field.
  77. */
  78. findModel(widget: T): IContentsModel {
  79. return Private.modelProperty.get(widget);
  80. }
  81. /**
  82. * Open a contents model and return a widget.
  83. */
  84. open(model: IContentsModel): T {
  85. let widget = this.findWidget(model.path);
  86. if (!widget) {
  87. widget = this.createWidget(model);
  88. widget.title.closable = true;
  89. this._setModel(widget, model);
  90. this._widgets.push(widget);
  91. installMessageFilter(widget, this);
  92. }
  93. // Fetch the contents and populate the widget asynchronously.
  94. let opts = this.getFetchOptions(model);
  95. this.manager.get(model.path, opts).then(contents => {
  96. widget.title.text = this.getTitleText(model);
  97. return this.populateWidget(widget, contents);
  98. }).then(contents => {
  99. this.clearDirty(model.path);
  100. });
  101. this.opened.emit(widget);
  102. return widget;
  103. }
  104. /**
  105. * Rename a file.
  106. */
  107. rename(oldPath: string, newPath: string): boolean {
  108. let widget = this.findWidget(oldPath);
  109. if (widget === void 0) {
  110. return false;
  111. }
  112. if (newPath === void 0) {
  113. this.clearDirty(oldPath);
  114. widget.close();
  115. return true;
  116. }
  117. let model = this._getModel(widget);
  118. model.path = newPath;
  119. let parts = newPath.split('/');
  120. model.name = parts[parts.length - 1];
  121. widget.title.text = this.getTitleText(model);
  122. return true;
  123. }
  124. /**
  125. * Save contents.
  126. *
  127. * @param path - The path of the file to save.
  128. *
  129. * returns A promise that resolves to the contents of the path.
  130. *
  131. * #### Notes
  132. * This clears the dirty state of the file after a successful save.
  133. */
  134. save(path: string): Promise<IContentsModel> {
  135. let widget = this.findWidget(path);
  136. if (!widget) {
  137. return Promise.resolve(void 0);
  138. }
  139. let model = this._getModel(widget);
  140. return this.getSaveOptions(widget, model).then(opts => {
  141. return this.manager.save(model.path, opts);
  142. }).then(contents => {
  143. this.clearDirty(path);
  144. return contents;
  145. });
  146. }
  147. /**
  148. * Revert contents.
  149. *
  150. * @param path - The path of the file to revert.
  151. *
  152. * returns A promise that resolves to the new contents of the path.
  153. *
  154. * #### Notes
  155. * This clears the dirty state of the file after a successful revert.
  156. */
  157. revert(path: string): Promise<IContentsModel> {
  158. let widget = this.findWidget(path);
  159. if (!widget) {
  160. return Promise.resolve(void 0);
  161. }
  162. let model = this._getModel(widget);
  163. let opts = this.getFetchOptions(model);
  164. return this.manager.get(model.path, opts).then(contents => {
  165. return this.populateWidget(widget, contents);
  166. }).then(contents => {
  167. this.clearDirty(path);
  168. return contents;
  169. });
  170. }
  171. /**
  172. * Close a file.
  173. *
  174. * @param path - The path of the file to close.
  175. *
  176. * returns A boolean indicating whether the file was closed.
  177. */
  178. close(path: string): Promise<boolean> {
  179. let widget = this.findWidget(path);
  180. if (!widget) {
  181. return Promise.resolve(false);
  182. }
  183. if (this.isDirty(path)) {
  184. return this._maybeClose(widget);
  185. }
  186. this._close(widget);
  187. return Promise.resolve(true);
  188. }
  189. /**
  190. * Close all files.
  191. */
  192. closeAll(): void {
  193. for (let w of this._widgets) {
  194. w.close();
  195. }
  196. }
  197. /**
  198. * Get whether a file is dirty.
  199. */
  200. isDirty(path: string): boolean {
  201. let widget = this.findWidget(path);
  202. return Private.dirtyProperty.get(widget);
  203. }
  204. /**
  205. * Set the dirty state of a widget (defaults to current active widget).
  206. */
  207. setDirty(path: string): void {
  208. let widget = this.findWidget(path);
  209. Private.dirtyProperty.set(widget, true);
  210. }
  211. /**
  212. * Clear the dirty state of a widget (defaults to current active widget).
  213. */
  214. clearDirty(path: string): void {
  215. let widget = this.findWidget(path);
  216. Private.dirtyProperty.set(widget, false);
  217. }
  218. /**
  219. * Filter messages on the widget.
  220. */
  221. filterMessage(handler: IMessageHandler, msg: Message): boolean {
  222. let widget = handler as T;
  223. if (msg.type === 'close-request' && widget) {
  224. let path = this.findModel(widget).path;
  225. this.close(path);
  226. return true;
  227. }
  228. return false;
  229. }
  230. /**
  231. * Get options use to fetch the model contents from disk.
  232. *
  233. * #### Notes
  234. * Subclasses are free to use any or none of the information in
  235. * the model.
  236. */
  237. protected getFetchOptions(model: IContentsModel): IContentsOpts {
  238. return { type: 'file', format: 'text' };
  239. }
  240. /**
  241. * Get the options used to save the widget content.
  242. */
  243. protected abstract getSaveOptions(widget: T, model: IContentsModel): Promise<IContentsOpts>;
  244. /**
  245. * Create the widget from a model.
  246. */
  247. protected abstract createWidget(model: IContentsModel): T;
  248. /**
  249. * Populate a widget from an `IContentsModel`.
  250. *
  251. * #### Notes
  252. * Subclasses are free to use any or none of the information in
  253. * the model. It is up to subclasses to handle setting dirty state when
  254. * the widget contents change. See [[AbstractFileHandler.dirtyProperty]].
  255. */
  256. protected abstract populateWidget(widget: T, model: IContentsModel): Promise<IContentsModel>;
  257. /**
  258. * Set the appropriate title text based on a model.
  259. */
  260. protected getTitleText(model: IContentsModel): string {
  261. return model.name;
  262. }
  263. /**
  264. * Get the model for a given widget.
  265. */
  266. private _getModel(widget: T): IContentsModel {
  267. return Private.modelProperty.get(widget);
  268. }
  269. /**
  270. * Set the model for a widget.
  271. */
  272. private _setModel(widget: T, model: IContentsModel) {
  273. Private.modelProperty.set(widget, model);
  274. }
  275. /**
  276. * Ask the user whether to close an unsaved file.
  277. */
  278. private _maybeClose(widget: T): Promise<boolean> {
  279. return showDialog({
  280. title: 'Close without saving?',
  281. body: `File "${widget.title.text}" has unsaved changes, close without saving?`,
  282. host: widget.node
  283. }).then(value => {
  284. if (value.text === 'OK') {
  285. this._close(widget);
  286. return true;
  287. }
  288. return false;
  289. });
  290. }
  291. /**
  292. * Actually close the file.
  293. */
  294. private _close(widget: T): void {
  295. let model = Private.modelProperty.get(widget);
  296. widget.dispose();
  297. let index = this._widgets.indexOf(widget);
  298. this._widgets.splice(index, 1);
  299. }
  300. private _manager: IContentsManager = null;
  301. private _widgets: T[] = [];
  302. private _cb: (widget: T) => void = null;
  303. }
  304. /**
  305. * A private namespace for AbstractFileHandler data.
  306. */
  307. namespace Private {
  308. /**
  309. * A signal emitted when a model is opened.
  310. */
  311. export
  312. const openedSignal = new Signal<AbstractFileHandler<Widget>, Widget>();
  313. /**
  314. * An attached property with the widget model.
  315. */
  316. export
  317. const modelProperty = new Property<Widget, IContentsModel>({
  318. name: 'model',
  319. value: null
  320. });
  321. /**
  322. * An attached property with the widget dirty state.
  323. */
  324. export
  325. const dirtyProperty = new Property<Widget, boolean>({
  326. name: 'dirty',
  327. value: false,
  328. changed: (widget: Widget, oldValue: boolean, newValue: boolean) => {
  329. if (newValue) {
  330. widget.title.className += ` ${DIRTY_CLASS}`;
  331. } else {
  332. widget.title.className = widget.title.className.replace(DIRTY_CLASS, '');
  333. }
  334. }
  335. });
  336. }