dialogs.ts 11 KB

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