dialogs.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Contents, Kernel
  5. } from '@jupyterlab/services';
  6. import {
  7. each
  8. } from '@phosphor/algorithm';
  9. import {
  10. Widget
  11. } from '@phosphor/widgets';
  12. import {
  13. ClientSession, Dialog, showDialog
  14. } from '@jupyterlab/apputils';
  15. import {
  16. DocumentManager
  17. } from '@jupyterlab/docmanager';
  18. import {
  19. DocumentRegistry
  20. } from '@jupyterlab/docregistry';
  21. import {
  22. FileBrowserModel
  23. } from './model';
  24. /**
  25. * The class name added for a file conflict.
  26. */
  27. const FILE_CONFLICT_CLASS = 'jp-mod-conflict';
  28. /**
  29. * Open a file using a dialog.
  30. */
  31. export
  32. function openWithDialog(path: string, manager: DocumentManager, host?: HTMLElement): Promise<Widget> {
  33. let handler: OpenWithHandler;
  34. return manager.services.ready.then(() => {
  35. handler = new OpenWithHandler(path, manager);
  36. return showDialog({
  37. title: 'Open File',
  38. body: handler.node,
  39. primaryElement: handler.inputNode,
  40. buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OPEN' })]
  41. });
  42. }).then(result => {
  43. if (result.accept) {
  44. return handler.open();
  45. }
  46. });
  47. }
  48. /**
  49. * Create a new file using a dialog.
  50. */
  51. export
  52. function createNewDialog(model: FileBrowserModel, manager: DocumentManager, host?: HTMLElement): Promise<Widget> {
  53. let handler: CreateNewHandler;
  54. return manager.services.ready.then(() => {
  55. handler = new CreateNewHandler(model, manager);
  56. return showDialog({
  57. title: 'Create New File',
  58. host,
  59. body: handler.node,
  60. primaryElement: handler.inputNode,
  61. buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OPEN' })]
  62. });
  63. }).then(result => {
  64. if (result.accept) {
  65. return handler.open();
  66. }
  67. });
  68. }
  69. /**
  70. * A widget used to open files with a specific widget/kernel.
  71. */
  72. class OpenWithHandler extends Widget {
  73. /**
  74. * Construct a new "open with" dialog.
  75. */
  76. constructor(path: string, manager: DocumentManager) {
  77. super({ node: Private.createOpenWithNode() });
  78. this._manager = manager;
  79. this.inputNode.textContent = path;
  80. this._ext = DocumentRegistry.extname(path);
  81. this.populateFactories();
  82. this.widgetDropdown.onchange = this.widgetChanged.bind(this);
  83. }
  84. /**
  85. * Dispose of the resources used by the widget.
  86. */
  87. dispose(): void {
  88. this._manager = null;
  89. super.dispose();
  90. }
  91. /**
  92. * Get the input text node.
  93. */
  94. get inputNode(): HTMLElement {
  95. return this.node.firstChild as HTMLElement;
  96. }
  97. /**
  98. * Get the widget dropdown node.
  99. */
  100. get widgetDropdown(): HTMLSelectElement {
  101. return this.node.children[1] as HTMLSelectElement;
  102. }
  103. /**
  104. * Get the kernel dropdown node.
  105. */
  106. get kernelDropdownNode(): HTMLSelectElement {
  107. return this.node.children[2] as HTMLSelectElement;
  108. }
  109. /**
  110. * Open the file and return the document widget.
  111. */
  112. open(): Widget {
  113. let path = this.inputNode.textContent;
  114. let widgetName = this.widgetDropdown.value;
  115. let kernelValue = this.kernelDropdownNode.value;
  116. let kernelId: Kernel.IModel;
  117. if (kernelValue !== 'null') {
  118. kernelId = JSON.parse(kernelValue) as Kernel.IModel;
  119. }
  120. return this._manager.open(path, widgetName, kernelId);
  121. }
  122. /**
  123. * Populate the widget factories.
  124. */
  125. protected populateFactories(): void {
  126. let factories = this._manager.registry.preferredWidgetFactories(this._ext);
  127. let widgetDropdown = this.widgetDropdown;
  128. each(factories, factory => {
  129. let option = document.createElement('option');
  130. option.text = factory.name;
  131. widgetDropdown.appendChild(option);
  132. });
  133. this.widgetChanged();
  134. }
  135. /**
  136. * Handle a change to the widget.
  137. */
  138. protected widgetChanged(): void {
  139. let widgetName = this.widgetDropdown.value;
  140. let preference = this._manager.registry.getKernelPreference(
  141. this._ext, widgetName
  142. );
  143. let services = this._manager.services;
  144. ClientSession.populateKernelSelect(this.kernelDropdownNode, {
  145. specs: services.specs,
  146. sessions: services.sessions.running(),
  147. preference
  148. });
  149. }
  150. private _ext = '';
  151. private _manager: DocumentManager = null;
  152. }
  153. /**
  154. * A widget used to create new files.
  155. */
  156. class CreateNewHandler extends Widget {
  157. /**
  158. * Construct a new "create new" dialog.
  159. */
  160. constructor(model: FileBrowserModel, manager: DocumentManager) {
  161. super({ node: Private.createCreateNewNode() });
  162. this._model = model;
  163. this._manager = manager;
  164. // Create a file name based on the current time.
  165. let time = new Date();
  166. time.setMinutes(time.getMinutes() - time.getTimezoneOffset());
  167. let name = time.toJSON().slice(0, 10);
  168. name += '-' + time.getHours() + time.getMinutes() + time.getSeconds();
  169. this.inputNode.value = name + '.txt';
  170. this.inputNode.setSelectionRange(0, name.length);
  171. // Check for name conflicts when the inputNode changes.
  172. this.inputNode.addEventListener('input', () => {
  173. this.inputNodeChanged();
  174. });
  175. // Update the widget choices when the file type changes.
  176. this.fileTypeDropdown.addEventListener('change', () => {
  177. this.fileTypeChanged();
  178. });
  179. // Update the kernel choices when the widget changes.
  180. this.widgetDropdown.addEventListener('change', () => {
  181. this.widgetDropdownChanged();
  182. });
  183. // Populate the lists of file types and widget factories.
  184. this.populateFileTypes();
  185. this.populateFactories();
  186. }
  187. /**
  188. * Dispose of the resources used by the widget.
  189. */
  190. dispose(): void {
  191. this._model = null;
  192. this._manager = null;
  193. super.dispose();
  194. }
  195. /**
  196. * Get the input text node.
  197. */
  198. get inputNode(): HTMLInputElement {
  199. return this.node.firstChild as HTMLInputElement;
  200. }
  201. /**
  202. * Get the file type dropdown node.
  203. */
  204. get fileTypeDropdown(): HTMLSelectElement {
  205. return this.node.children[1] as HTMLSelectElement;
  206. }
  207. /**
  208. * Get the widget dropdown node.
  209. */
  210. get widgetDropdown(): HTMLSelectElement {
  211. return this.node.children[2] as HTMLSelectElement;
  212. }
  213. /**
  214. * Get the kernel dropdown node.
  215. */
  216. get kernelDropdownNode(): HTMLSelectElement {
  217. return this.node.children[3] as HTMLSelectElement;
  218. }
  219. /**
  220. * Get the current extension for the file.
  221. */
  222. get ext(): string {
  223. return DocumentRegistry.extname(this.inputNode.value);
  224. }
  225. /**
  226. * Open the file and return the document widget.
  227. */
  228. open(): Widget {
  229. let path = this.inputNode.textContent;
  230. let widgetName = this.widgetDropdown.value;
  231. let kernelValue = this.kernelDropdownNode.value;
  232. let kernelId: Kernel.IModel;
  233. if (kernelValue !== 'null') {
  234. kernelId = JSON.parse(kernelValue) as Kernel.IModel;
  235. }
  236. return this._manager.createNew(path, widgetName, kernelId);
  237. }
  238. /**
  239. * Handle a change to the inputNode.
  240. */
  241. protected inputNodeChanged(): void {
  242. let path = this.inputNode.value;
  243. each(this._model.items(), item => {
  244. if (item.path === path) {
  245. this.addClass(FILE_CONFLICT_CLASS);
  246. return;
  247. }
  248. });
  249. let ext = this.ext;
  250. if (ext === this._prevExt) {
  251. return;
  252. }
  253. // Update the file type dropdown and the factories.
  254. if (this._extensions.indexOf(ext) === -1) {
  255. this.fileTypeDropdown.value = this._sentinel;
  256. } else {
  257. this.fileTypeDropdown.value = ext;
  258. }
  259. this.populateFactories();
  260. }
  261. /**
  262. * Populate the file types.
  263. */
  264. protected populateFileTypes(): void {
  265. let dropdown = this.fileTypeDropdown;
  266. let option = document.createElement('option');
  267. option.text = 'File';
  268. option.value = this._sentinel;
  269. each(this._manager.registry.fileTypes(), ft => {
  270. option = document.createElement('option');
  271. option.text = `${ft.name} (${ft.extension})`;
  272. option.value = ft.extension;
  273. dropdown.appendChild(option);
  274. this._extensions.push(ft.extension);
  275. });
  276. if (this.ext in this._extensions) {
  277. dropdown.value = this.ext;
  278. } else {
  279. dropdown.value = this._sentinel;
  280. }
  281. }
  282. /**
  283. * Populate the widget factories.
  284. */
  285. protected populateFactories(): void {
  286. let ext = this.ext;
  287. let factories = this._manager.registry.preferredWidgetFactories(ext);
  288. let widgetDropdown = this.widgetDropdown;
  289. each(factories, factory => {
  290. let option = document.createElement('option');
  291. option.text = factory.name;
  292. widgetDropdown.appendChild(option);
  293. });
  294. this.widgetDropdownChanged();
  295. this._prevExt = ext;
  296. }
  297. /**
  298. * Handle changes to the file type dropdown.
  299. */
  300. protected fileTypeChanged(): void {
  301. // Update the current inputNode.
  302. let oldExt = this.ext;
  303. let newExt = this.fileTypeDropdown.value;
  304. if (oldExt === newExt || newExt === '') {
  305. return;
  306. }
  307. let oldName = this.inputNode.value;
  308. let base = oldName.slice(0, oldName.length - oldExt.length - 1);
  309. this.inputNode.value = base + newExt;
  310. }
  311. /**
  312. * Handle a change to the widget dropdown.
  313. */
  314. protected widgetDropdownChanged(): void {
  315. let ext = this.ext;
  316. let widgetName = this.widgetDropdown.value;
  317. let manager = this._manager;
  318. let preference = manager.registry.getKernelPreference(ext, widgetName);
  319. let services = this._manager.services;
  320. ClientSession.populateKernelSelect(this.kernelDropdownNode, {
  321. specs: services.specs,
  322. sessions: services.sessions.running(),
  323. preference
  324. });
  325. }
  326. private _model: FileBrowserModel = null;
  327. private _manager: DocumentManager = null;
  328. private _sentinel = 'UNKNOWN_EXTENSION';
  329. private _prevExt = '';
  330. private _extensions: string[] = [];
  331. }
  332. /**
  333. * A namespace for private data.
  334. */
  335. namespace Private {
  336. /**
  337. * Create the node for an open with handler.
  338. */
  339. export
  340. function createOpenWithNode(): HTMLElement {
  341. let body = document.createElement('div');
  342. let nameTitle = document.createElement('label');
  343. nameTitle.textContent = 'File Name';
  344. let name = document.createElement('div');
  345. let widgetTitle = document.createElement('label');
  346. widgetTitle.textContent = 'Widget Type';
  347. let widgetDropdown = document.createElement('select');
  348. let kernelTitle = document.createElement('label');
  349. kernelTitle.textContent = 'Kernel';
  350. let kernelDropdownNode = document.createElement('select');
  351. body.appendChild(nameTitle);
  352. body.appendChild(name);
  353. body.appendChild(widgetTitle);
  354. body.appendChild(widgetDropdown);
  355. body.appendChild(kernelTitle);
  356. body.appendChild(kernelDropdownNode);
  357. return body;
  358. }
  359. /**
  360. * Create the node for a create new handler.
  361. */
  362. export
  363. function createCreateNewNode(): HTMLElement {
  364. let body = document.createElement('div');
  365. let nameTitle = document.createElement('label');
  366. nameTitle.textContent = 'File Name';
  367. let name = document.createElement('input');
  368. let typeTitle = document.createElement('label');
  369. typeTitle.textContent = 'File Type';
  370. let fileTypeDropdown = document.createElement('select');
  371. let widgetTitle = document.createElement('label');
  372. widgetTitle.textContent = 'Widget Type';
  373. let widgetDropdown = document.createElement('select');
  374. let kernelTitle = document.createElement('label');
  375. kernelTitle.textContent = 'Kernel';
  376. let kernelDropdownNode = document.createElement('select');
  377. body.appendChild(nameTitle);
  378. body.appendChild(name);
  379. body.appendChild(typeTitle);
  380. body.appendChild(fileTypeDropdown);
  381. body.appendChild(widgetTitle);
  382. body.appendChild(widgetDropdown);
  383. body.appendChild(kernelTitle);
  384. body.appendChild(kernelDropdownNode);
  385. return body;
  386. }
  387. /**
  388. * Create the node for a create from handler.
  389. */
  390. export
  391. function createCreateFromNode(): HTMLElement {
  392. let body = document.createElement('div');
  393. let nameTitle = document.createElement('label');
  394. nameTitle.textContent = 'File Name';
  395. let name = document.createElement('input');
  396. let kernelTitle = document.createElement('label');
  397. kernelTitle.textContent = 'Kernel';
  398. let kernelDropdownNode = document.createElement('select');
  399. body.appendChild(nameTitle);
  400. body.appendChild(name);
  401. body.appendChild(kernelTitle);
  402. body.appendChild(kernelDropdownNode);
  403. return body;
  404. }
  405. }