index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ArrayExt, ArrayIterator, IIterator, map, each, toArray
  5. } from '@phosphor/algorithm';
  6. import {
  7. Token
  8. } from '@phosphor/coreutils';
  9. import {
  10. DisposableDelegate, IDisposable
  11. } from '@phosphor/disposable';
  12. import {
  13. Message
  14. } from '@phosphor/messaging';
  15. import {
  16. Widget
  17. } from '@phosphor/widgets';
  18. import * as vdom from '@phosphor/virtualdom';
  19. import {
  20. showErrorMessage, VDomModel, VDomRenderer
  21. } from '@jupyterlab/apputils';
  22. import '../style/index.css';
  23. /* tslint:disable */
  24. /**
  25. * We have configured the TSX transform to look for the h function in the local
  26. * module.
  27. */
  28. const h = vdom.h;
  29. /* tslint:enable */
  30. /**
  31. * The class name added to Launcher instances.
  32. */
  33. const LAUNCHER_CLASS = 'jp-Launcher';
  34. /**
  35. * The known categories of launcher items and their default ordering.
  36. */
  37. const KNOWN_CATEGORIES = ['Notebook', 'Console', 'Other'];
  38. /**
  39. * These laucher item categories are known to have kernels, so the kernel icons
  40. * are used.
  41. */
  42. const KERNEL_CATEGORIES = ['Notebook', 'Console'];
  43. /**
  44. * The command IDs used by the launcher plugin.
  45. */
  46. export
  47. namespace CommandIDs {
  48. export
  49. const show: string = 'launcher:show';
  50. };
  51. /* tslint:disable */
  52. /**
  53. * The launcher token.
  54. */
  55. export
  56. const ILauncher = new Token<ILauncher>('jupyter.services.launcher');
  57. /* tslint:enable */
  58. /**
  59. * The launcher interface.
  60. */
  61. export
  62. interface ILauncher {
  63. /**
  64. * Add a command item to the launcher, and trigger re-render event for parent
  65. * widget.
  66. *
  67. * @param options - The specification options for a launcher item.
  68. *
  69. * @returns A disposable that will remove the item from Launcher, and trigger
  70. * re-render event for parent widget.
  71. *
  72. */
  73. add(options: ILauncherItem): IDisposable;
  74. }
  75. /**
  76. * The specification for a launcher item.
  77. */
  78. export
  79. interface ILauncherItem {
  80. /**
  81. * The display name for the launcher item.
  82. */
  83. displayName: string;
  84. /**
  85. * The callback invoked to launch the item.
  86. *
  87. * The callback is invoked with a current working directory and the
  88. * name of the selected launcher item. When the function returns
  89. * the launcher will close.
  90. */
  91. callback: (cwd: string, name: string) => Widget | Promise<Widget>;
  92. /**
  93. * The icon class for the launcher item.
  94. *
  95. * #### Notes
  96. * This class name will be added to the icon node for the visual
  97. * representation of the launcher item.
  98. *
  99. * Multiple class names can be separated with white space.
  100. *
  101. * The default value is an empty string.
  102. */
  103. iconClass?: string;
  104. /**
  105. * The icon label for the launcher item.
  106. *
  107. * #### Notes
  108. * This label will be added as text to the icon node for the visual
  109. * representation of the launcher item.
  110. *
  111. * The default value is an empty string.
  112. */
  113. iconLabel?: string;
  114. /**
  115. * The identifier for the launcher item.
  116. *
  117. * The default value is the displayName.
  118. */
  119. name?: string;
  120. /**
  121. * The category for the launcher item.
  122. *
  123. * The default value is the an empty string.
  124. */
  125. category?: string;
  126. /**
  127. * The rank for the launcher item.
  128. *
  129. * The rank is used when ordering launcher items for display. After grouping
  130. * into categories, items are sorted in the following order:
  131. * 1. Rank (lower is better)
  132. * 3. Display Name (locale order)
  133. *
  134. * The default rank is `Infinity`.
  135. */
  136. rank?: number;
  137. /**
  138. * For items that have a kernel associated with them, the URL of the kernel
  139. * icon.
  140. *
  141. * This is not a CSS class, but the URL that points to the icon in the kernel
  142. * spec.
  143. */
  144. kernelIconUrl?: string;
  145. }
  146. /**
  147. * LauncherModel keeps track of the path to working directory and has a list of
  148. * LauncherItems, which the Launcher will render.
  149. */
  150. export
  151. class LauncherModel extends VDomModel implements ILauncher {
  152. /**
  153. * Create a new launcher model.
  154. */
  155. constructor() {
  156. super();
  157. }
  158. /**
  159. * Add a command item to the launcher, and trigger re-render event for parent
  160. * widget.
  161. *
  162. * @param options - The specification options for a launcher item.
  163. *
  164. * @returns A disposable that will remove the item from Launcher, and trigger
  165. * re-render event for parent widget.
  166. *
  167. */
  168. add(options: ILauncherItem): IDisposable {
  169. // Create a copy of the options to circumvent mutations to the original.
  170. let item = Private.createItem(options);
  171. this._items.push(item);
  172. this.stateChanged.emit(void 0);
  173. return new DisposableDelegate(() => {
  174. ArrayExt.removeFirstOf(this._items, item);
  175. this.stateChanged.emit(void 0);
  176. });
  177. }
  178. /**
  179. * Return an iterator of launcher items.
  180. */
  181. items(): IIterator<ILauncherItem> {
  182. return new ArrayIterator(this._items);
  183. }
  184. private _items: ILauncherItem[] = [];
  185. }
  186. /**
  187. * A virtual-DOM-based widget for the Launcher.
  188. */
  189. export
  190. class Launcher extends VDomRenderer<LauncherModel> {
  191. /**
  192. * Construct a new launcher widget.
  193. */
  194. constructor(options: Launcher.IOptions) {
  195. super();
  196. this._cwd = options.cwd;
  197. this._callback = options.callback;
  198. this.addClass(LAUNCHER_CLASS);
  199. }
  200. /**
  201. * The cwd of the launcher.
  202. */
  203. get cwd(): string {
  204. return this._cwd;
  205. }
  206. set cwd(value: string) {
  207. this._cwd = value;
  208. this.update();
  209. }
  210. /**
  211. * Whether there is a pending item being launched.
  212. */
  213. get pending(): boolean {
  214. return this._pending;
  215. }
  216. set pending(value: boolean) {
  217. this._pending = value;
  218. }
  219. /**
  220. * Handle `'activate-request'` messages.
  221. */
  222. protected onActivateRequest(msg: Message): void {
  223. this.node.tabIndex = -1;
  224. this.node.focus();
  225. }
  226. /**
  227. * Render the launcher to virtual DOM nodes.
  228. */
  229. protected render(): vdom.VirtualNode | vdom.VirtualNode[] {
  230. // Bail if there is no model.
  231. if (!this.model) {
  232. return [];
  233. }
  234. // First group-by categories
  235. let categories = Object.create(null);
  236. each(this.model.items(), (item, index) => {
  237. let cat = item.category || 'Other';
  238. if (!(cat in categories)) {
  239. categories[cat] = [];
  240. }
  241. categories[cat].push(item);
  242. });
  243. // Within each category sort by rank
  244. for (let cat in categories) {
  245. categories[cat] = categories[cat].sort(Private.sortCmp);
  246. }
  247. // Variable to help create sections
  248. let sections: vdom.VirtualNode[] = [];
  249. let section: vdom.VirtualNode;
  250. // Assemble the final ordered list of categories, beginning with
  251. // KNOWN_CATEGORIES.
  252. let orderedCategories: string[] = [];
  253. each(KNOWN_CATEGORIES, (cat, index) => {
  254. orderedCategories.push(cat);
  255. });
  256. for (let cat in categories) {
  257. if (KNOWN_CATEGORIES.indexOf(cat) === -1) {
  258. orderedCategories.push(cat);
  259. }
  260. }
  261. // Now create the sections for each category
  262. each(orderedCategories, (cat, index) => {
  263. let iconClass = `${(categories[cat][0] as ILauncherItem).iconClass} ` +
  264. 'jp-Launcher-sectionIcon jp-Launcher-icon';
  265. let kernel = KERNEL_CATEGORIES.indexOf(cat) > -1;
  266. if (cat in categories) {
  267. section = (
  268. <div className='jp-Launcher-section'>
  269. <div className='jp-Launcher-sectionHeader'>
  270. {kernel && <div className={iconClass} />}
  271. <h2 className='jp-Launcher-sectionTitle'>{cat}</h2>
  272. </div>
  273. <div className='jp-Launcher-cardContainer'>
  274. {toArray(map(categories[cat], (item: ILauncherItem) => {
  275. return Card(kernel, item, this, this._callback);
  276. }))}
  277. </div>
  278. </div>
  279. );
  280. sections.push(section);
  281. }
  282. });
  283. // Wrap the sections in body and content divs.
  284. return (
  285. <div className='jp-Launcher-body'>
  286. <div className='jp-Launcher-content'>
  287. <div className='jp-Launcher-cwd'>
  288. <h3>{this.cwd}</h3>
  289. </div>
  290. {sections}
  291. </div>
  292. </div>
  293. );
  294. }
  295. private _callback: (widget: Widget) => void;
  296. private _pending = false;
  297. private _cwd = '';
  298. }
  299. /**
  300. * The namespace for `Launcher` class statics.
  301. */
  302. export
  303. namespace Launcher {
  304. /**
  305. * The options used to create a Launcher.
  306. */
  307. export
  308. interface IOptions {
  309. /**
  310. * The cwd of the launcher.
  311. */
  312. cwd: string;
  313. /**
  314. * The callback used when an item is launched.
  315. */
  316. callback: (widget: Widget) => void;
  317. }
  318. }
  319. /**
  320. * A pure tsx component for a launcher card.
  321. *
  322. * @param kernel - whether the item takes uses a kernel.
  323. *
  324. * @param item - the launcher item to render.
  325. *
  326. * @param launcher - the Launcher instance to which this is added.
  327. *
  328. * @param launcherCallback - a callback to call after an item has been launched.
  329. *
  330. * @returns a vdom `VirtualElement` for the launcher card.
  331. */
  332. function Card(kernel: boolean, item: ILauncherItem, launcher: Launcher, launcherCallback: (widget: Widget) => void): vdom.VirtualElement {
  333. // Build the onclick handler.
  334. let onclick = () => {
  335. // If an item has already been launched,
  336. // don't try to launch another.
  337. if (launcher.pending === true) {
  338. return;
  339. }
  340. launcher.pending = true;
  341. let callback = item.callback as any;
  342. let value = callback(launcher.cwd, item.name);
  343. Promise.resolve(value).then(widget => {
  344. launcherCallback(widget);
  345. launcher.dispose();
  346. }).catch(err => {
  347. launcher.pending = false;
  348. showErrorMessage('Launcher Error', err);
  349. });
  350. };
  351. // Add a data attribute for the category
  352. let dataset = { category: item.category || 'Other' };
  353. // Return the VDOM element.
  354. return (
  355. <div className='jp-LauncherCard'
  356. title={item.displayName}
  357. onclick={onclick}
  358. dataset={dataset}>
  359. <div className='jp-LauncherCard-icon'>
  360. {(item.kernelIconUrl && kernel) &&
  361. <img src={item.kernelIconUrl} className='jp-Launcher-kernelIcon' />}
  362. {(!item.kernelIconUrl && !kernel) &&
  363. <div className={`${item.iconClass} jp-Launcher-icon`} />}
  364. {(!item.kernelIconUrl && kernel) &&
  365. <div className='jp-LauncherCard-noKernelIcon'>
  366. {item.displayName[0].toUpperCase()}
  367. </div>}
  368. </div>
  369. <div className='jp-LauncherCard-label' title={item.displayName}>
  370. {item.displayName}
  371. </div>
  372. </div>
  373. );
  374. }
  375. /**
  376. * The namespace for module private data.
  377. */
  378. namespace Private {
  379. /**
  380. * Create an item given item options.
  381. */
  382. export
  383. function createItem(options: ILauncherItem): ILauncherItem {
  384. return {
  385. ...options,
  386. category: options.category || '',
  387. name: options.name || options.name,
  388. iconClass: options.iconClass || '',
  389. iconLabel: options.iconLabel || '',
  390. rank: options.rank !== undefined ? options.rank : Infinity
  391. };
  392. }
  393. /**
  394. * A sort comparison function for a launcher item.
  395. */
  396. export
  397. function sortCmp(a: ILauncherItem, b: ILauncherItem): number {
  398. // First, compare by rank.
  399. let r1 = a.rank;
  400. let r2 = b.rank;
  401. if (r1 !== r2 && r1 !== undefined && r2 !== undefined) {
  402. return r1 < r2 ? -1 : 1; // Infinity safe
  403. }
  404. // Finally, compare by display name.
  405. return a.displayName.localeCompare(b.displayName);
  406. }
  407. }