commandlinker.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import {
  6. JSONObject
  7. } from 'phosphor/lib/algorithm/json';
  8. import {
  9. IDisposable
  10. } from 'phosphor/lib/core/disposable';
  11. import {
  12. Token
  13. } from 'phosphor/lib/core/token';
  14. import {
  15. CommandRegistry
  16. } from 'phosphor/lib/ui/commandregistry';
  17. import {
  18. IElementAttrs
  19. } from 'phosphor/lib/ui/vdom';
  20. /**
  21. * The command data attribute added to nodes that are connected.
  22. */
  23. const COMMAND_ATTR = 'commandlinker-command';
  24. /**
  25. * The args data attribute added to nodes that are connected.
  26. */
  27. const ARGS_ATTR = 'commandlinker-args';
  28. /* tslint:disable */
  29. /**
  30. * The command linker token.
  31. */
  32. export
  33. const ICommandLinker = new Token<ICommandLinker>('jupyter.services.command-linker');
  34. /* tslint:enable */
  35. /**
  36. * A helper class to generate clickables that execute commands.
  37. */
  38. export
  39. interface ICommandLinker extends IDisposable {
  40. /**
  41. * Connect a command/argument pair to a given node so that when it is clicked,
  42. * the command will execute.
  43. *
  44. * @param node - The node being connected.
  45. *
  46. * @param command - The command ID to execute upon click.
  47. *
  48. * @param args - The arguments with which to invoke the command.
  49. *
  50. * @returns The same node that was passed in, after it has been connected.
  51. *
  52. * #### Notes
  53. * Only `click` events will execute the command on a connected node. So, there
  54. * are two considerations that are relevant:
  55. * 1. If a node is connected, the default click action will be prevented.
  56. * 2. The `HTMLElement` passed in should be clickable.
  57. */
  58. connectNode(node: HTMLElement, command: string, args: JSONObject): HTMLElement;
  59. /**
  60. * Disconnect a node that has been connected to execute a command on click.
  61. *
  62. * @param node - The node being disconnected.
  63. *
  64. * @returns The same node that was passed in, after it has been disconnected.
  65. *
  66. * #### Notes
  67. * This method is safe to call multiple times and is safe to call on nodes
  68. * that were never connected.
  69. *
  70. * This method can be called on rendered virtual DOM nodes that were populated
  71. * using the `populateVNodeAttributes` method in order to disconnect them from
  72. * executing their command/argument pair.
  73. */
  74. disconnectNode(node: HTMLElement): HTMLElement;
  75. /**
  76. * Populate the attributes used to instantiate a virtual DOM node with the
  77. * data set values necessary for its rendered DOM node to respond to clicks by
  78. * executing a command/argument pair
  79. *
  80. * @param attrs - The attributes that will eventually be used to instantiate
  81. * a virtual DOM node.
  82. *
  83. * @param command - The command ID to execute upon click.
  84. *
  85. * @param args - The arguments with which to invoke the command.
  86. *
  87. * #### Notes
  88. * The attributes instance that is returned is identical to the attributes
  89. * instance that was passed in, i.e., this method mutates the original.
  90. */
  91. populateVNodeAttrs(attrs: IElementAttrs, command: string, args: JSONObject): IElementAttrs;
  92. }
  93. /**
  94. * A static class that provides helper methods to generate clickable nodes that
  95. * execute registered commands with pre-populated arguments.
  96. */
  97. export
  98. class CommandLinker implements ICommandLinker {
  99. /**
  100. * Instantiate a new command linker.
  101. */
  102. constructor(options: CommandLinker.IOptions) {
  103. this._commands = options.commands;
  104. document.body.addEventListener('click', this);
  105. }
  106. /**
  107. * Test whether the linker is disposed.
  108. */
  109. get isDisposed(): boolean {
  110. return this._commands === null;
  111. }
  112. /**
  113. * Dispose of the resources held by the linker.
  114. */
  115. dispose(): void {
  116. if (this._commands === null) {
  117. return;
  118. }
  119. this._commands = null;
  120. document.body.removeEventListener('click', this);
  121. }
  122. /**
  123. * Connect a command/argument pair to a given node so that when it is clicked,
  124. * the command will execute.
  125. *
  126. * @param node - The node being connected.
  127. *
  128. * @param command - The command ID to execute upon click.
  129. *
  130. * @param args - The arguments with which to invoke the command.
  131. *
  132. * @returns The same node that was passed in, after it has been connected.
  133. *
  134. * #### Notes
  135. * Only `click` events will execute the command on a connected node. So, there
  136. * are two considerations that are relevant:
  137. * 1. If a node is connected, the default click action will be prevented.
  138. * 2. The `HTMLElement` passed in should be clickable.
  139. */
  140. connectNode(node: HTMLElement, command: string, args: JSONObject): HTMLElement {
  141. let argsValue = JSON.stringify(args);
  142. node.setAttribute(`data-${COMMAND_ATTR}`, command);
  143. if (argsValue) {
  144. node.setAttribute(`data-${ARGS_ATTR}`, argsValue);
  145. }
  146. return node;
  147. }
  148. /**
  149. * Disconnect a node that has been connected to execute a command on click.
  150. *
  151. * @param node - The node being disconnected.
  152. *
  153. * @returns The same node that was passed in, after it has been disconnected.
  154. *
  155. * #### Notes
  156. * This method is safe to call multiple times and is safe to call on nodes
  157. * that were never connected.
  158. *
  159. * This method can be called on rendered virtual DOM nodes that were populated
  160. * using the `populateVNodeAttributes` method in order to disconnect them from
  161. * executing their command/argument pair.
  162. */
  163. disconnectNode(node: HTMLElement): HTMLElement {
  164. node.removeAttribute(`data-${COMMAND_ATTR}`);
  165. node.removeAttribute(`data-${ARGS_ATTR}`);
  166. return node;
  167. }
  168. /**
  169. * Handle the DOM events for the command linker helper class.
  170. *
  171. * @param event - The DOM event sent to the class.
  172. *
  173. * #### Notes
  174. * This method implements the DOM `EventListener` interface and is
  175. * called in response to events on the panel's DOM node. It should
  176. * not be called directly by user code.
  177. */
  178. handleEvent(event: Event): void {
  179. switch (event.type) {
  180. case 'click':
  181. this._evtClick(event as MouseEvent);
  182. break;
  183. default:
  184. return;
  185. }
  186. }
  187. /**
  188. * Populate the attributes used to instantiate a virtual DOM node with the
  189. * data set values necessary for its rendered DOM node to respond to clicks by
  190. * executing a command/argument pair
  191. *
  192. * @param attrs - The attributes that will eventually be used to instantiate
  193. * a virtual DOM node.
  194. *
  195. * @param command - The command ID to execute upon click.
  196. *
  197. * @param args - The arguments with which to invoke the command.
  198. *
  199. * #### Notes
  200. * The attributes instance that is returned is identical to the attributes
  201. * instance that was passed in, i.e., this method mutates the original.
  202. */
  203. populateVNodeAttrs(attrs: IElementAttrs, command: string, args: JSONObject): IElementAttrs {
  204. let argsValue = JSON.stringify(args);
  205. attrs.dataset = attrs.dataset || {};
  206. attrs.dataset[COMMAND_ATTR] = command;
  207. if (argsValue) {
  208. attrs.dataset[ARGS_ATTR] = argsValue;
  209. }
  210. return attrs;
  211. }
  212. /**
  213. * The global click handler that deploys commands/argument pairs that are
  214. * attached to the node being clicked.
  215. */
  216. private _evtClick(event: MouseEvent): void {
  217. let target = event.target as HTMLElement;
  218. while (target && target.parentElement) {
  219. if (target.hasAttribute(`data-${COMMAND_ATTR}`)) {
  220. event.preventDefault();
  221. let command = target.getAttribute(`data-${COMMAND_ATTR}`);
  222. let argsValue = target.getAttribute(`data-${ARGS_ATTR}`);
  223. let args: JSONObject;
  224. if (argsValue) {
  225. args = JSON.parse(argsValue);
  226. }
  227. this._commands.execute(command, args);
  228. return;
  229. }
  230. target = target.parentElement;
  231. }
  232. }
  233. private _commands: CommandRegistry = null;
  234. }
  235. /**
  236. * A namespace for command linker statics.
  237. */
  238. export
  239. namespace CommandLinker {
  240. /**
  241. * The instantiation options for a command linker.
  242. */
  243. export
  244. interface IOptions {
  245. /**
  246. * The command registry instance that all linked commands will use.
  247. */
  248. commands: CommandRegistry;
  249. }
  250. }