index.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. 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. readonly cwd: string;
  204. /**
  205. * Handle `'activate-request'` messages.
  206. */
  207. protected onActivateRequest(msg: Message): void {
  208. this.node.tabIndex = -1;
  209. this.node.focus();
  210. }
  211. /**
  212. * Render the launcher to virtual DOM nodes.
  213. */
  214. protected render(): vdom.VirtualNode | vdom.VirtualNode[] {
  215. // First group-by categories
  216. let categories = Object.create(null);
  217. each(this.model.items(), (item, index) => {
  218. let cat = item.category || 'Other';
  219. if (!(cat in categories)) {
  220. categories[cat] = [];
  221. }
  222. categories[cat].push(item);
  223. });
  224. // Within each category sort by rank
  225. for (let cat in categories) {
  226. categories[cat] = categories[cat].sort(Private.sortCmp);
  227. }
  228. // Variable to help create sections
  229. let sections: vdom.VirtualNode[] = [];
  230. let section: vdom.VirtualNode;
  231. // Assemble the final ordered list of categories, beginning with
  232. // KNOWN_CATEGORIES.
  233. let orderedCategories: string[] = [];
  234. each(KNOWN_CATEGORIES, (cat, index) => {
  235. orderedCategories.push(cat);
  236. });
  237. for (let cat in categories) {
  238. if (KNOWN_CATEGORIES.indexOf(cat) === -1) {
  239. orderedCategories.push(cat);
  240. }
  241. }
  242. // Now create the sections for each category
  243. each(orderedCategories, (cat, index) => {
  244. let iconClass = '${(categories[cat][0] as ILauncherItem).iconClass} ' +
  245. 'jp-Launcher-sectionIcon jp-Launcher-icon';
  246. let kernel = KERNEL_CATEGORIES.indexOf(cat) > -1;
  247. if (cat in categories) {
  248. section = (
  249. <div className='jp-Launcher-section'>
  250. <div className='jp-Launcher-sectionHeader'>
  251. {kernel && <div className={iconClass} />}
  252. <h2 className='jp-Launcher-sectionTitle'>{cat}</h2>
  253. </div>
  254. <div className='jp-Launcher-cardContainer'>
  255. {toArray(map(categories[cat], (item: ILauncherItem) => {
  256. return Card(kernel, item, this, this._callback);
  257. }))}
  258. </div>
  259. </div>
  260. );
  261. sections.push(section);
  262. }
  263. });
  264. // Wrap the sections in body and content divs.
  265. return (
  266. <div className='jp-Launcher-body'>
  267. <div className='jp-Launcher-content'>
  268. {sections}
  269. </div>
  270. </div>
  271. );
  272. }
  273. private _callback: (widget: Widget) => void;
  274. }
  275. /**
  276. * The namespace for `Launcher` class statics.
  277. */
  278. export
  279. namespace Launcher {
  280. /**
  281. * The options used to create a Launcher.
  282. */
  283. export
  284. interface IOptions {
  285. /**
  286. * The cwd of the launcher.
  287. */
  288. cwd: string;
  289. /**
  290. * The callback used when an item is launched.
  291. */
  292. callback: (widget: Widget) => void;
  293. }
  294. }
  295. export
  296. function Card(kernel: boolean, item: ILauncherItem, launcher: Launcher, launcherCallback: (widget: Widget) => void): vdom.VirtualElement {
  297. // Build the onclick handler.
  298. let onclick = () => {
  299. let callback = item.callback as any;
  300. let value = callback(launcher.cwd, item.name);
  301. Promise.resolve(value).then(widget => {
  302. launcherCallback(widget);
  303. launcher.dispose();
  304. });
  305. };
  306. // Add a data attribute for the category
  307. let dataset = {category: item.category};
  308. // Return the VDOM element.
  309. return (
  310. <div className='jp-LauncherCard'
  311. title={item.displayName}
  312. onclick={onclick}
  313. dataset={dataset}>
  314. <div className='jp-LauncherCard-icon'>
  315. {(item.kernelIconUrl && kernel) &&
  316. <img src={item.kernelIconUrl} className='jp-Launcher-kernelIcon' />}
  317. {(!item.kernelIconUrl && !kernel) &&
  318. <div className={`${item.iconClass} jp-Launcher-icon`} />}
  319. {(!item.kernelIconUrl && kernel) &&
  320. <div className='jp-LauncherCard-noKernelIcon'>
  321. {item.displayName[0].toUpperCase()}
  322. </div>}
  323. </div>
  324. <div className='jp-LauncherCard-label' title={item.displayName}>
  325. {item.displayName}
  326. </div>
  327. </div>
  328. );
  329. }
  330. /**
  331. * The namespace for module private data.
  332. */
  333. namespace Private {
  334. /**
  335. * Create an item given item options.
  336. */
  337. export
  338. function createItem(options: ILauncherItem): ILauncherItem {
  339. return {
  340. ...options,
  341. category: options.category || '',
  342. name: options.name || options.name,
  343. iconClass: options.iconClass || '',
  344. iconLabel: options.iconLabel || '',
  345. rank: options.rank !== undefined ? options.rank : Infinity
  346. };
  347. }
  348. /**
  349. * A sort comparison function for a launcher item.
  350. */
  351. export
  352. function sortCmp(a: ILauncherItem, b: ILauncherItem): number {
  353. // First, compare by rank.
  354. let r1 = a.rank;
  355. let r2 = b.rank;
  356. if (r1 !== r2) {
  357. return r1 < r2 ? -1 : 1; // Infinity safe
  358. }
  359. // Finally, compare by display name.
  360. return a.displayName.localeCompare(b.displayName);
  361. }
  362. }