Manager.mjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import WorkerWaiter from "./WorkerWaiter";
  7. import WindowWaiter from "./WindowWaiter";
  8. import ControlsWaiter from "./ControlsWaiter";
  9. import RecipeWaiter from "./RecipeWaiter";
  10. import OperationsWaiter from "./OperationsWaiter";
  11. import InputWaiter from "./InputWaiter";
  12. import OutputWaiter from "./OutputWaiter";
  13. import OptionsWaiter from "./OptionsWaiter";
  14. import HighlighterWaiter from "./HighlighterWaiter";
  15. import SeasonalWaiter from "./SeasonalWaiter";
  16. import BindingsWaiter from "./BindingsWaiter";
  17. import BackgroundWorkerWaiter from "./BackgroundWorkerWaiter";
  18. /**
  19. * This object controls the Waiters responsible for handling events from all areas of the app.
  20. */
  21. class Manager {
  22. /**
  23. * Manager constructor.
  24. *
  25. * @param {App} app - The main view object for CyberChef.
  26. */
  27. constructor(app) {
  28. this.app = app;
  29. // Define custom events
  30. /**
  31. * @event Manager#appstart
  32. */
  33. this.appstart = new CustomEvent("appstart", {bubbles: true});
  34. /**
  35. * @event Manager#apploaded
  36. */
  37. this.apploaded = new CustomEvent("apploaded", {bubbles: true});
  38. /**
  39. * @event Manager#operationadd
  40. */
  41. this.operationadd = new CustomEvent("operationadd", {bubbles: true});
  42. /**
  43. * @event Manager#operationremove
  44. */
  45. this.operationremove = new CustomEvent("operationremove", {bubbles: true});
  46. /**
  47. * @event Manager#oplistcreate
  48. */
  49. this.oplistcreate = new CustomEvent("oplistcreate", {bubbles: true});
  50. /**
  51. * @event Manager#statechange
  52. */
  53. this.statechange = new CustomEvent("statechange", {bubbles: true});
  54. // Define Waiter objects to handle various areas
  55. this.worker = new WorkerWaiter(this.app, this);
  56. this.window = new WindowWaiter(this.app);
  57. this.controls = new ControlsWaiter(this.app, this);
  58. this.recipe = new RecipeWaiter(this.app, this);
  59. this.ops = new OperationsWaiter(this.app, this);
  60. this.input = new InputWaiter(this.app, this);
  61. this.output = new OutputWaiter(this.app, this);
  62. this.options = new OptionsWaiter(this.app, this);
  63. this.highlighter = new HighlighterWaiter(this.app, this);
  64. this.seasonal = new SeasonalWaiter(this.app, this);
  65. this.bindings = new BindingsWaiter(this.app, this);
  66. this.background = new BackgroundWorkerWaiter(this.app, this);
  67. // Object to store dynamic handlers to fire on elements that may not exist yet
  68. this.dynamicHandlers = {};
  69. this.initialiseEventListeners();
  70. }
  71. /**
  72. * Sets up the various components and listeners.
  73. */
  74. setup() {
  75. this.worker.registerChefWorker();
  76. this.recipe.initialiseOperationDragNDrop();
  77. this.controls.autoBakeChange();
  78. this.bindings.updateKeybList();
  79. this.background.registerChefWorker();
  80. this.seasonal.load();
  81. }
  82. /**
  83. * Main function to handle the creation of the event listeners.
  84. */
  85. initialiseEventListeners() {
  86. // Global
  87. window.addEventListener("resize", this.window.windowResize.bind(this.window));
  88. window.addEventListener("blur", this.window.windowBlur.bind(this.window));
  89. window.addEventListener("focus", this.window.windowFocus.bind(this.window));
  90. window.addEventListener("statechange", this.app.stateChange.bind(this.app));
  91. window.addEventListener("popstate", this.app.popState.bind(this.app));
  92. // Controls
  93. document.getElementById("bake").addEventListener("click", this.controls.bakeClick.bind(this.controls));
  94. document.getElementById("auto-bake").addEventListener("change", this.controls.autoBakeChange.bind(this.controls));
  95. document.getElementById("step").addEventListener("click", this.controls.stepClick.bind(this.controls));
  96. document.getElementById("clr-recipe").addEventListener("click", this.controls.clearRecipeClick.bind(this.controls));
  97. document.getElementById("clr-breaks").addEventListener("click", this.controls.clearBreaksClick.bind(this.controls));
  98. document.getElementById("save").addEventListener("click", this.controls.saveClick.bind(this.controls));
  99. document.getElementById("save-button").addEventListener("click", this.controls.saveButtonClick.bind(this.controls));
  100. document.getElementById("save-link-recipe-checkbox").addEventListener("change", this.controls.slrCheckChange.bind(this.controls));
  101. document.getElementById("save-link-input-checkbox").addEventListener("change", this.controls.sliCheckChange.bind(this.controls));
  102. document.getElementById("load").addEventListener("click", this.controls.loadClick.bind(this.controls));
  103. document.getElementById("load-delete-button").addEventListener("click", this.controls.loadDeleteClick.bind(this.controls));
  104. document.getElementById("load-name").addEventListener("change", this.controls.loadNameChange.bind(this.controls));
  105. document.getElementById("load-button").addEventListener("click", this.controls.loadButtonClick.bind(this.controls));
  106. document.getElementById("support").addEventListener("click", this.controls.supportButtonClick.bind(this.controls));
  107. this.addMultiEventListeners("#save-texts textarea", "keyup paste", this.controls.saveTextChange, this.controls);
  108. // Operations
  109. this.addMultiEventListener("#search", "keyup paste search", this.ops.searchOperations, this.ops);
  110. this.addDynamicListener(".op-list li.operation", "dblclick", this.ops.operationDblclick, this.ops);
  111. document.getElementById("edit-favourites").addEventListener("click", this.ops.editFavouritesClick.bind(this.ops));
  112. document.getElementById("save-favourites").addEventListener("click", this.ops.saveFavouritesClick.bind(this.ops));
  113. document.getElementById("reset-favourites").addEventListener("click", this.ops.resetFavouritesClick.bind(this.ops));
  114. this.addDynamicListener(".op-list .op-icon", "mouseover", this.ops.opIconMouseover, this.ops);
  115. this.addDynamicListener(".op-list .op-icon", "mouseleave", this.ops.opIconMouseleave, this.ops);
  116. this.addDynamicListener(".op-list", "oplistcreate", this.ops.opListCreate, this.ops);
  117. this.addDynamicListener("li.operation", "operationadd", this.recipe.opAdd, this.recipe);
  118. // Recipe
  119. this.addDynamicListener(".arg:not(select)", "input", this.recipe.ingChange, this.recipe);
  120. this.addDynamicListener(".arg[type=checkbox], .arg[type=radio], select.arg", "change", this.recipe.ingChange, this.recipe);
  121. this.addDynamicListener(".disable-icon", "click", this.recipe.disableClick, this.recipe);
  122. this.addDynamicListener(".breakpoint", "click", this.recipe.breakpointClick, this.recipe);
  123. this.addDynamicListener("#rec-list li.operation", "dblclick", this.recipe.operationDblclick, this.recipe);
  124. this.addDynamicListener("#rec-list li.operation > div", "dblclick", this.recipe.operationChildDblclick, this.recipe);
  125. this.addDynamicListener("#rec-list .input-group .dropdown-menu a", "click", this.recipe.dropdownToggleClick, this.recipe);
  126. this.addDynamicListener("#rec-list", "operationremove", this.recipe.opRemove.bind(this.recipe));
  127. // Input
  128. this.addMultiEventListener("#input-text", "keyup", this.input.inputChange, this.input);
  129. this.addMultiEventListener("#input-text", "paste", this.input.inputPaste, this.input);
  130. document.getElementById("reset-layout").addEventListener("click", this.app.resetLayout.bind(this.app));
  131. document.getElementById("clr-io").addEventListener("click", this.input.clearIoClick.bind(this.input));
  132. this.addListeners("#input-text,#input-file", "dragover", this.input.inputDragover, this.input);
  133. this.addListeners("#input-text,#input-file", "dragleave", this.input.inputDragleave, this.input);
  134. this.addListeners("#input-text,#input-file", "drop", this.input.inputDrop, this.input);
  135. document.getElementById("input-text").addEventListener("scroll", this.highlighter.inputScroll.bind(this.highlighter));
  136. document.getElementById("input-text").addEventListener("mouseup", this.highlighter.inputMouseup.bind(this.highlighter));
  137. document.getElementById("input-text").addEventListener("mousemove", this.highlighter.inputMousemove.bind(this.highlighter));
  138. this.addMultiEventListener("#input-text", "mousedown dblclick select", this.highlighter.inputMousedown, this.highlighter);
  139. document.querySelector("#input-file .close").addEventListener("click", this.input.clearIoClick.bind(this.input));
  140. // Output
  141. document.getElementById("save-to-file").addEventListener("click", this.output.saveClick.bind(this.output));
  142. document.getElementById("copy-output").addEventListener("click", this.output.copyClick.bind(this.output));
  143. document.getElementById("switch").addEventListener("click", this.output.switchClick.bind(this.output));
  144. document.getElementById("undo-switch").addEventListener("click", this.output.undoSwitchClick.bind(this.output));
  145. document.getElementById("maximise-output").addEventListener("click", this.output.maximiseOutputClick.bind(this.output));
  146. document.getElementById("output-text").addEventListener("scroll", this.highlighter.outputScroll.bind(this.highlighter));
  147. document.getElementById("output-text").addEventListener("mouseup", this.highlighter.outputMouseup.bind(this.highlighter));
  148. document.getElementById("output-text").addEventListener("mousemove", this.highlighter.outputMousemove.bind(this.highlighter));
  149. document.getElementById("output-html").addEventListener("mouseup", this.highlighter.outputHtmlMouseup.bind(this.highlighter));
  150. document.getElementById("output-html").addEventListener("mousemove", this.highlighter.outputHtmlMousemove.bind(this.highlighter));
  151. this.addMultiEventListener("#output-text", "mousedown dblclick select", this.highlighter.outputMousedown, this.highlighter);
  152. this.addMultiEventListener("#output-html", "mousedown dblclick select", this.highlighter.outputHtmlMousedown, this.highlighter);
  153. this.addDynamicListener("#output-file-download", "click", this.output.downloadFile, this.output);
  154. this.addDynamicListener("#output-file-slice", "click", this.output.displayFileSlice, this.output);
  155. document.getElementById("show-file-overlay").addEventListener("click", this.output.showFileOverlayClick.bind(this.output));
  156. // Options
  157. document.getElementById("options").addEventListener("click", this.options.optionsClick.bind(this.options));
  158. document.getElementById("reset-options").addEventListener("click", this.options.resetOptionsClick.bind(this.options));
  159. $(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox", this.options.switchChange.bind(this.options));
  160. $(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox", this.options.setWordWrap.bind(this.options));
  161. $(document).on("switchChange.bootstrapSwitch", ".option-item input:checkbox#useMetaKey", this.bindings.updateKeybList.bind(this.bindings));
  162. this.addDynamicListener(".option-item input[type=number]", "keyup", this.options.numberChange, this.options);
  163. this.addDynamicListener(".option-item input[type=number]", "change", this.options.numberChange, this.options);
  164. this.addDynamicListener(".option-item select", "change", this.options.selectChange, this.options);
  165. document.getElementById("theme").addEventListener("change", this.options.themeChange.bind(this.options));
  166. document.getElementById("logLevel").addEventListener("change", this.options.logLevelChange.bind(this.options));
  167. // Misc
  168. window.addEventListener("keydown", this.bindings.parseInput.bind(this.bindings));
  169. document.getElementById("alert-close").addEventListener("click", this.app.alertCloseClick.bind(this.app));
  170. }
  171. /**
  172. * Adds an event listener to each element in the specified group.
  173. *
  174. * @param {string} selector - A selector string for the element group to add the event to, see
  175. * this.getAll()
  176. * @param {string} eventType - The event to listen for
  177. * @param {function} callback - The function to execute when the event is triggered
  178. * @param {Object} [scope=this] - The object to bind to the callback function
  179. *
  180. * @example
  181. * // Calls the clickable function whenever any element with the .clickable class is clicked
  182. * this.addListeners(".clickable", "click", this.clickable, this);
  183. */
  184. addListeners(selector, eventType, callback, scope) {
  185. scope = scope || this;
  186. [].forEach.call(document.querySelectorAll(selector), function(el) {
  187. el.addEventListener(eventType, callback.bind(scope));
  188. });
  189. }
  190. /**
  191. * Adds multiple event listeners to the specified element.
  192. *
  193. * @param {string} selector - A selector string for the element to add the events to
  194. * @param {string} eventTypes - A space-separated string of all the event types to listen for
  195. * @param {function} callback - The function to execute when the events are triggered
  196. * @param {Object} [scope=this] - The object to bind to the callback function
  197. *
  198. * @example
  199. * // Calls the search function whenever the the keyup, paste or search events are triggered on the
  200. * // search element
  201. * this.addMultiEventListener("search", "keyup paste search", this.search, this);
  202. */
  203. addMultiEventListener(selector, eventTypes, callback, scope) {
  204. const evs = eventTypes.split(" ");
  205. for (let i = 0; i < evs.length; i++) {
  206. document.querySelector(selector).addEventListener(evs[i], callback.bind(scope));
  207. }
  208. }
  209. /**
  210. * Adds multiple event listeners to each element in the specified group.
  211. *
  212. * @param {string} selector - A selector string for the element group to add the events to
  213. * @param {string} eventTypes - A space-separated string of all the event types to listen for
  214. * @param {function} callback - The function to execute when the events are triggered
  215. * @param {Object} [scope=this] - The object to bind to the callback function
  216. *
  217. * @example
  218. * // Calls the save function whenever the the keyup or paste events are triggered on any element
  219. * // with the .saveable class
  220. * this.addMultiEventListener(".saveable", "keyup paste", this.save, this);
  221. */
  222. addMultiEventListeners(selector, eventTypes, callback, scope) {
  223. const evs = eventTypes.split(" ");
  224. for (let i = 0; i < evs.length; i++) {
  225. this.addListeners(selector, evs[i], callback, scope);
  226. }
  227. }
  228. /**
  229. * Adds an event listener to the global document object which will listen on dynamic elements which
  230. * may not exist in the DOM yet.
  231. *
  232. * @param {string} selector - A selector string for the element(s) to add the event to
  233. * @param {string} eventType - The event(s) to listen for
  234. * @param {function} callback - The function to execute when the event(s) is/are triggered
  235. * @param {Object} [scope=this] - The object to bind to the callback function
  236. *
  237. * @example
  238. * // Pops up an alert whenever any button is clicked, even if it is added to the DOM after this
  239. * // listener is created
  240. * this.addDynamicListener("button", "click", alert, this);
  241. */
  242. addDynamicListener(selector, eventType, callback, scope) {
  243. const eventConfig = {
  244. selector: selector,
  245. callback: callback.bind(scope || this)
  246. };
  247. if (this.dynamicHandlers.hasOwnProperty(eventType)) {
  248. // Listener already exists, add new handler to the appropriate list
  249. this.dynamicHandlers[eventType].push(eventConfig);
  250. } else {
  251. this.dynamicHandlers[eventType] = [eventConfig];
  252. // Set up listener for this new type
  253. document.addEventListener(eventType, this.dynamicListenerHandler.bind(this));
  254. }
  255. }
  256. /**
  257. * Handler for dynamic events. This function is called for any dynamic event and decides which
  258. * callback(s) to execute based on the type and selector.
  259. *
  260. * @param {Event} e - The event to be handled
  261. */
  262. dynamicListenerHandler(e) {
  263. const { type, target } = e;
  264. const handlers = this.dynamicHandlers[type];
  265. const matches = target.matches ||
  266. target.webkitMatchesSelector ||
  267. target.mozMatchesSelector ||
  268. target.msMatchesSelector ||
  269. target.oMatchesSelector;
  270. for (let i = 0; i < handlers.length; i++) {
  271. if (matches && matches.call(target, handlers[i].selector)) {
  272. handlers[i].callback(e);
  273. }
  274. }
  275. }
  276. }
  277. export default Manager;