dialogs.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Contents, Kernel, Session
  5. } from 'jupyter-js-services';
  6. import {
  7. Widget
  8. } from 'phosphor/lib/ui/widget';
  9. import {
  10. showDialog
  11. } from '../dialog';
  12. import {
  13. DocumentManager
  14. } from '../docmanager';
  15. import {
  16. IKernelPreference, populateKernels
  17. } from '../docregistry';
  18. import {
  19. FileBrowserModel
  20. } from './model';
  21. /**
  22. * The class name added to file dialogs.
  23. */
  24. const FILE_DIALOG_CLASS = 'jp-FileDialog';
  25. /**
  26. * The class name added for a file conflict.
  27. */
  28. const FILE_CONFLICT_CLASS = 'jp-mod-conflict';
  29. /**
  30. * Create a file using a file creator.
  31. */
  32. export
  33. function createFromDialog(model: FileBrowserModel, manager: DocumentManager, creatorName: string): Promise<Widget> {
  34. let handler = new CreateFromHandler(model, manager, creatorName);
  35. return handler.populate().then(() => {
  36. return handler.showDialog();
  37. });
  38. }
  39. /**
  40. * Open a file using a dialog.
  41. */
  42. export
  43. function openWithDialog(path: string, manager: DocumentManager, host?: HTMLElement): Promise<Widget> {
  44. let handler: OpenWithHandler;
  45. return manager.listSessions().then(sessions => {
  46. handler = new OpenWithHandler(path, manager, sessions);
  47. return showDialog({
  48. title: 'Open File',
  49. body: handler.node,
  50. okText: 'OPEN'
  51. });
  52. }).then(result => {
  53. if (result.text === 'OPEN') {
  54. return handler.open();
  55. }
  56. });
  57. }
  58. /**
  59. * Create a new file using a dialog.
  60. */
  61. export
  62. function createNewDialog(model: FileBrowserModel, manager: DocumentManager, host?: HTMLElement): Promise<Widget> {
  63. let handler: CreateNewHandler;
  64. return manager.listSessions().then(sessions => {
  65. handler = new CreateNewHandler(model, manager, sessions);
  66. return showDialog({
  67. title: 'Create New File',
  68. host,
  69. body: handler.node,
  70. okText: 'CREATE'
  71. });
  72. }).then(result => {
  73. if (result.text === 'CREATE') {
  74. return handler.open();
  75. }
  76. });
  77. }
  78. /**
  79. * Rename a file with optional dialog.
  80. */
  81. export
  82. function renameFile(model: FileBrowserModel, oldPath: string, newPath: string): Promise<Contents.IModel> {
  83. return model.rename(oldPath, newPath).catch(error => {
  84. if (error.xhr) {
  85. error.message = `${error.xhr.statusText} ${error.xhr.status}`;
  86. }
  87. if (error.message.indexOf('409') !== -1) {
  88. let options = {
  89. title: 'Overwrite file?',
  90. body: `"${newPath}" already exists, overwrite?`,
  91. okText: 'OVERWRITE'
  92. };
  93. return showDialog(options).then(button => {
  94. if (button.text === 'OVERWRITE') {
  95. return model.deleteFile(newPath).then(() => {
  96. return model.rename(oldPath, newPath);
  97. });
  98. }
  99. });
  100. } else {
  101. throw error;
  102. }
  103. });
  104. }
  105. /**
  106. * A widget used to open files with a specific widget/kernel.
  107. */
  108. class OpenWithHandler extends Widget {
  109. /**
  110. * Construct a new "open with" dialog.
  111. */
  112. constructor(path: string, manager: DocumentManager, sessions: Session.IModel[], host?: HTMLElement) {
  113. super({ node: Private.createOpenWithNode() });
  114. this._manager = manager;
  115. this._host = host;
  116. this._sessions = sessions;
  117. this.inputNode.textContent = path;
  118. this._ext = path.split('.').pop();
  119. this.populateFactories();
  120. this.widgetDropdown.onchange = this.widgetChanged.bind(this);
  121. }
  122. /**
  123. * Dispose of the resources used by the widget.
  124. */
  125. dispose(): void {
  126. this._host = null;
  127. this._manager = null;
  128. this._sessions = null;
  129. super.dispose();
  130. }
  131. /**
  132. * Get the input text node.
  133. */
  134. get inputNode(): HTMLElement {
  135. return this.node.firstChild as HTMLElement;
  136. }
  137. /**
  138. * Get the widget dropdown node.
  139. */
  140. get widgetDropdown(): HTMLSelectElement {
  141. return this.node.children[1] as HTMLSelectElement;
  142. }
  143. /**
  144. * Get the kernel dropdown node.
  145. */
  146. get kernelDropdownNode(): HTMLSelectElement {
  147. return this.node.children[2] as HTMLSelectElement;
  148. }
  149. /**
  150. * Open the file and return the document widget.
  151. */
  152. open(): Widget {
  153. let path = this.inputNode.textContent;
  154. let widgetName = this.widgetDropdown.value;
  155. let kernelValue = this.kernelDropdownNode.value;
  156. let kernelId: Kernel.IModel;
  157. if (kernelValue !== 'null') {
  158. kernelId = JSON.parse(kernelValue) as Kernel.IModel;
  159. }
  160. return this._manager.open(path, widgetName, kernelId);
  161. }
  162. /**
  163. * Populate the widget factories.
  164. */
  165. protected populateFactories(): void {
  166. let factories = this._manager.registry.listWidgetFactories(this._ext);
  167. let widgetDropdown = this.widgetDropdown;
  168. for (let factory of factories) {
  169. let option = document.createElement('option');
  170. option.text = factory;
  171. widgetDropdown.appendChild(option);
  172. }
  173. this.widgetChanged();
  174. }
  175. /**
  176. * Handle a change to the widget.
  177. */
  178. protected widgetChanged(): void {
  179. let widgetName = this.widgetDropdown.value;
  180. let preference = this._manager.registry.getKernelPreference(
  181. this._ext, widgetName
  182. );
  183. let specs = this._manager.kernelspecs;
  184. let sessions = this._sessions;
  185. Private.updateKernels(this.kernelDropdownNode,
  186. { preference, specs, sessions }
  187. );
  188. }
  189. private _ext = '';
  190. private _manager: DocumentManager = null;
  191. private _host: HTMLElement = null;
  192. private _sessions: Session.IModel[] = null;
  193. }
  194. /**
  195. * A widget used to create a file using a creator.
  196. */
  197. class CreateFromHandler extends Widget {
  198. /**
  199. * Construct a new "create from" dialog.
  200. */
  201. constructor(model: FileBrowserModel, manager: DocumentManager, creatorName: string) {
  202. super({ node: Private.createCreateFromNode() });
  203. this.addClass(FILE_DIALOG_CLASS);
  204. this._model = model;
  205. this._manager = manager;
  206. this._creatorName = creatorName;
  207. // Check for name conflicts when the inputNode changes.
  208. this.inputNode.addEventListener('input', () => {
  209. let value = this.inputNode.value;
  210. if (value !== this._orig) {
  211. for (let item of this._model.items) {
  212. if (item.name === value) {
  213. this.addClass(FILE_CONFLICT_CLASS);
  214. return;
  215. }
  216. }
  217. }
  218. this.removeClass(FILE_CONFLICT_CLASS);
  219. });
  220. }
  221. /**
  222. * Dispose of the resources used by the widget.
  223. */
  224. dispose(): void {
  225. this._model = null;
  226. this._manager = null;
  227. super.dispose();
  228. }
  229. /**
  230. * Get the input text node.
  231. */
  232. get inputNode(): HTMLInputElement {
  233. return this.node.getElementsByTagName('input')[0] as HTMLInputElement;
  234. }
  235. /**
  236. * Get the kernel dropdown node.
  237. */
  238. get kernelDropdownNode(): HTMLSelectElement {
  239. return this.node.getElementsByTagName('select')[0] as HTMLSelectElement;
  240. }
  241. /**
  242. * Show the createNew dialog.
  243. */
  244. showDialog(): Promise<Widget> {
  245. return showDialog({
  246. title: `Create New ${this._creatorName}`,
  247. body: this.node,
  248. okText: 'CREATE'
  249. }).then(result => {
  250. if (result.text === 'CREATE') {
  251. return this._open().then(widget => {
  252. if (!widget) {
  253. return this.showDialog();
  254. }
  255. return widget;
  256. });
  257. }
  258. this._model.deleteFile(this._orig.path);
  259. return null;
  260. });
  261. }
  262. /**
  263. * Populate the create from widget.
  264. */
  265. populate(): Promise<void> {
  266. let model = this._model;
  267. let manager = this._manager;
  268. let registry = manager.registry;
  269. let creator = registry.getCreator(this._creatorName);
  270. let { fileType, widgetName, kernelName } = creator;
  271. let fType = registry.getFileType(fileType);
  272. let ext = '.txt';
  273. let type: Contents.FileType = 'file';
  274. if (fType) {
  275. ext = fType.extension;
  276. type = fType.fileType || 'file';
  277. }
  278. if (!widgetName || widgetName === 'default') {
  279. this._widgetName = widgetName = registry.defaultWidgetFactory(ext);
  280. }
  281. // Handle the kernel preferences.
  282. let preference = registry.getKernelPreference(ext, widgetName);
  283. if (preference.canStartKernel) {
  284. let specs = this._manager.kernelspecs;
  285. let sessions = this._sessions;
  286. let preferredKernel = kernelName;
  287. Private.updateKernels(this.kernelDropdownNode,
  288. { specs, sessions, preferredKernel, preference }
  289. );
  290. } else {
  291. this.node.removeChild(this.kernelDropdownNode);
  292. }
  293. return manager.listSessions().then(sessions => {
  294. this._sessions = sessions;
  295. return model.newUntitled({ ext, type });
  296. }).then((contents: Contents.IModel) => {
  297. this.inputNode.value = contents.name;
  298. this._orig = contents;
  299. });
  300. }
  301. /**
  302. * Open the file and return the document widget.
  303. */
  304. private _open(): Promise<Widget> {
  305. let file = this.inputNode.value;
  306. let widgetName = this._widgetName;
  307. let kernelValue = this.kernelDropdownNode ? this.kernelDropdownNode.value
  308. : 'null';
  309. let kernelId: Kernel.IModel;
  310. if (kernelValue !== 'null') {
  311. kernelId = JSON.parse(kernelValue) as Kernel.IModel;
  312. }
  313. if (file !== this._orig.name) {
  314. let promise = renameFile(this._model, this._orig.name, file);
  315. return promise.then((contents: Contents.IModel) => {
  316. if (!contents) {
  317. return null;
  318. }
  319. return this._manager.open(contents.path, widgetName, kernelId);
  320. });
  321. }
  322. let path = this._orig.path;
  323. return Promise.resolve(this._manager.createNew(path, widgetName, kernelId));
  324. }
  325. private _model: FileBrowserModel = null;
  326. private _creatorName: string;
  327. private _widgetName: string;
  328. private _orig: Contents.IModel = null;
  329. private _manager: DocumentManager;
  330. private _sessions: Session.IModel[] = [];
  331. }
  332. /**
  333. * A widget used to create new files.
  334. */
  335. class CreateNewHandler extends Widget {
  336. /**
  337. * Construct a new "create new" dialog.
  338. */
  339. constructor(model: FileBrowserModel, manager: DocumentManager, sessions: Session.IModel[]) {
  340. super({ node: Private.createCreateNewNode() });
  341. this._model = model;
  342. this._manager = manager;
  343. this._sessions = sessions;
  344. // Create a file name based on the current time.
  345. let time = new Date();
  346. time.setMinutes(time.getMinutes() - time.getTimezoneOffset());
  347. let name = time.toJSON().slice(0, 10);
  348. name += '-' + time.getHours() + time.getMinutes() + time.getSeconds();
  349. this.inputNode.value = name + '.txt';
  350. // Check for name conflicts when the inputNode changes.
  351. this.inputNode.addEventListener('input', () => {
  352. this.inputNodeChanged();
  353. });
  354. // Update the widget choices when the file type changes.
  355. this.fileTypeDropdown.addEventListener('change', () => {
  356. this.fileTypeChanged();
  357. });
  358. // Update the kernel choices when the widget changes.
  359. this.widgetDropdown.addEventListener('change', () => {
  360. this.widgetDropdownChanged();
  361. });
  362. // Populate the lists of file types and widget factories.
  363. this.populateFileTypes();
  364. this.populateFactories();
  365. }
  366. /**
  367. * Dispose of the resources used by the widget.
  368. */
  369. dispose(): void {
  370. this._model = null;
  371. this._sessions = null;
  372. this._manager = null;
  373. super.dispose();
  374. }
  375. /**
  376. * Get the input text node.
  377. */
  378. get inputNode(): HTMLInputElement {
  379. return this.node.firstChild as HTMLInputElement;
  380. }
  381. /**
  382. * Get the file type dropdown node.
  383. */
  384. get fileTypeDropdown(): HTMLSelectElement {
  385. return this.node.children[1] as HTMLSelectElement;
  386. }
  387. /**
  388. * Get the widget dropdown node.
  389. */
  390. get widgetDropdown(): HTMLSelectElement {
  391. return this.node.children[2] as HTMLSelectElement;
  392. }
  393. /**
  394. * Get the kernel dropdown node.
  395. */
  396. get kernelDropdownNode(): HTMLSelectElement {
  397. return this.node.children[3] as HTMLSelectElement;
  398. }
  399. /**
  400. * Get the current extension for the file.
  401. */
  402. get ext(): string {
  403. return this.inputNode.value.split('.').pop();
  404. }
  405. /**
  406. * Open the file and return the document widget.
  407. */
  408. open(): Widget {
  409. let path = this.inputNode.textContent;
  410. let widgetName = this.widgetDropdown.value;
  411. let kernelValue = this.kernelDropdownNode.value;
  412. let kernelId: Kernel.IModel;
  413. if (kernelValue !== 'null') {
  414. kernelId = JSON.parse(kernelValue) as Kernel.IModel;
  415. }
  416. return this._manager.createNew(path, widgetName, kernelId);
  417. }
  418. /**
  419. * Handle a change to the inputNode.
  420. */
  421. protected inputNodeChanged(): void {
  422. let path = this.inputNode.value;
  423. for (let item of this._model.items) {
  424. if (item.path === path) {
  425. this.addClass(FILE_CONFLICT_CLASS);
  426. return;
  427. }
  428. }
  429. let ext = this.ext;
  430. if (ext === this._prevExt) {
  431. return;
  432. }
  433. // Update the file type dropdown and the factories.
  434. if (this._extensions.indexOf(ext) === -1) {
  435. this.fileTypeDropdown.value = this._sentinal;
  436. } else {
  437. this.fileTypeDropdown.value = ext;
  438. }
  439. this.populateFactories();
  440. }
  441. /**
  442. * Populate the file types.
  443. */
  444. protected populateFileTypes(): void {
  445. let fileTypes = this._manager.registry.listFileTypes();
  446. let dropdown = this.fileTypeDropdown;
  447. let option = document.createElement('option');
  448. option.text = 'File';
  449. option.value = this._sentinal;
  450. for (let ft of fileTypes) {
  451. option = document.createElement('option');
  452. option.text = `${ft.name} (${ft.extension})`;
  453. option.value = ft.extension;
  454. dropdown.appendChild(option);
  455. this._extensions.push(ft.extension);
  456. }
  457. if (this.ext in this._extensions) {
  458. dropdown.value = this.ext;
  459. } else {
  460. dropdown.value = this._sentinal;
  461. }
  462. }
  463. /**
  464. * Populate the widget factories.
  465. */
  466. protected populateFactories(): void {
  467. let ext = this.ext;
  468. let factories = this._manager.registry.listWidgetFactories(ext);
  469. let widgetDropdown = this.widgetDropdown;
  470. for (let factory of factories) {
  471. let option = document.createElement('option');
  472. option.text = factory;
  473. widgetDropdown.appendChild(option);
  474. }
  475. this.widgetDropdownChanged();
  476. this._prevExt = ext;
  477. }
  478. /**
  479. * Handle changes to the file type dropdown.
  480. */
  481. protected fileTypeChanged(): void {
  482. // Update the current inputNode.
  483. let oldExt = this.ext;
  484. let newExt = this.fileTypeDropdown.value;
  485. if (oldExt === newExt || newExt === '') {
  486. return;
  487. }
  488. let oldName = this.inputNode.value;
  489. let base = oldName.slice(0, oldName.length - oldExt.length - 1);
  490. this.inputNode.value = base + newExt;
  491. }
  492. /**
  493. * Handle a change to the widget dropdown.
  494. */
  495. protected widgetDropdownChanged(): void {
  496. let ext = this.ext;
  497. let widgetName = this.widgetDropdown.value;
  498. let preference = this._manager.registry.getKernelPreference(ext, widgetName);
  499. let specs = this._manager.kernelspecs;
  500. let sessions = this._sessions;
  501. Private.updateKernels(this.kernelDropdownNode,
  502. { preference, sessions, specs }
  503. );
  504. }
  505. private _model: FileBrowserModel = null;
  506. private _manager: DocumentManager = null;
  507. private _sessions: Session.IModel[] = null;
  508. private _sentinal = 'UNKNOWN_EXTENSION';
  509. private _prevExt = '';
  510. private _extensions: string[] = [];
  511. }
  512. /**
  513. * A namespace for private data.
  514. */
  515. namespace Private {
  516. /**
  517. * Create the node for an open with handler.
  518. */
  519. export
  520. function createOpenWithNode(): HTMLElement {
  521. let body = document.createElement('div');
  522. let name = document.createElement('span');
  523. let widgetDropdown = document.createElement('select');
  524. let kernelDropdownNode = document.createElement('select');
  525. body.appendChild(name);
  526. body.appendChild(widgetDropdown);
  527. body.appendChild(kernelDropdownNode);
  528. return body;
  529. }
  530. /**
  531. * Create the node for a create new handler.
  532. */
  533. export
  534. function createCreateNewNode(): HTMLElement {
  535. let body = document.createElement('div');
  536. let name = document.createElement('input');
  537. let fileTypeDropdown = document.createElement('select');
  538. let widgetDropdown = document.createElement('select');
  539. let kernelDropdownNode = document.createElement('select');
  540. body.appendChild(name);
  541. body.appendChild(fileTypeDropdown);
  542. body.appendChild(widgetDropdown);
  543. body.appendChild(kernelDropdownNode);
  544. return body;
  545. }
  546. /**
  547. * Create the node for a create from handler.
  548. */
  549. export
  550. function createCreateFromNode(): HTMLElement {
  551. let body = document.createElement('div');
  552. let name = document.createElement('input');
  553. let kernelDropdownNode = document.createElement('select');
  554. body.appendChild(name);
  555. body.appendChild(kernelDropdownNode);
  556. return body;
  557. }
  558. /**
  559. * Update a kernel listing based on a kernel preference.
  560. */
  561. export
  562. function updateKernels(node: HTMLSelectElement, options: IKernelOptions): void {
  563. let { preference, specs, sessions, preferredKernel } = options;
  564. if (!preference.canStartKernel) {
  565. while (node.firstChild) {
  566. node.removeChild(node.firstChild);
  567. }
  568. node.disabled = true;
  569. return;
  570. }
  571. let preferredLanguage = preference.language;
  572. node.disabled = false;
  573. populateKernels(node,
  574. { specs, sessions, preferredLanguage, preferredKernel }
  575. );
  576. // Select the "null" valued kernel if we do not prefer a kernel.
  577. if (!preference.preferKernel) {
  578. node.value = 'null';
  579. }
  580. }
  581. /**
  582. * The options for updating kernels.
  583. */
  584. export
  585. interface IKernelOptions {
  586. /**
  587. * The kernel preference.
  588. */
  589. preference: IKernelPreference;
  590. /**
  591. * The kernel specs.
  592. */
  593. specs: Kernel.ISpecModels;
  594. /**
  595. * The running sessions.
  596. */
  597. sessions: Session.IModel[];
  598. /**
  599. * The preferred kernel name.
  600. */
  601. preferredKernel?: string;
  602. }
  603. }