OperationsWaiter.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import HTMLOperation from "./HTMLOperation.js";
  2. import Sortable from "sortablejs";
  3. /**
  4. * Waiter to handle events related to the operations.
  5. *
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2016
  8. * @license Apache-2.0
  9. *
  10. * @constructor
  11. * @param {App} app - The main view object for CyberChef.
  12. * @param {Manager} manager - The CyberChef event manager.
  13. */
  14. const OperationsWaiter = function(app, manager) {
  15. this.app = app;
  16. this.manager = manager;
  17. this.options = {};
  18. this.removeIntent = false;
  19. };
  20. /**
  21. * Handler for search events.
  22. * Finds operations which match the given search term and displays them under the search box.
  23. *
  24. * @param {event} e
  25. */
  26. OperationsWaiter.prototype.searchOperations = function(e) {
  27. let ops, selected;
  28. if (e.type === "search") { // Search
  29. e.preventDefault();
  30. ops = document.querySelectorAll("#search-results li");
  31. if (ops.length) {
  32. selected = this.getSelectedOp(ops);
  33. if (selected > -1) {
  34. this.manager.recipe.addOperation(ops[selected].innerHTML);
  35. this.app.autoBake();
  36. }
  37. }
  38. }
  39. if (e.keyCode === 13) { // Return
  40. e.preventDefault();
  41. } else if (e.keyCode === 40) { // Down
  42. e.preventDefault();
  43. ops = document.querySelectorAll("#search-results li");
  44. if (ops.length) {
  45. selected = this.getSelectedOp(ops);
  46. if (selected > -1) {
  47. ops[selected].classList.remove("selected-op");
  48. }
  49. if (selected === ops.length-1) selected = -1;
  50. ops[selected+1].classList.add("selected-op");
  51. }
  52. } else if (e.keyCode === 38) { // Up
  53. e.preventDefault();
  54. ops = document.querySelectorAll("#search-results li");
  55. if (ops.length) {
  56. selected = this.getSelectedOp(ops);
  57. if (selected > -1) {
  58. ops[selected].classList.remove("selected-op");
  59. }
  60. if (selected === 0) selected = ops.length;
  61. ops[selected-1].classList.add("selected-op");
  62. }
  63. } else {
  64. const searchResultsEl = document.getElementById("search-results");
  65. const el = e.target;
  66. const str = el.value;
  67. while (searchResultsEl.firstChild) {
  68. try {
  69. $(searchResultsEl.firstChild).popover("destroy");
  70. } catch (err) {}
  71. searchResultsEl.removeChild(searchResultsEl.firstChild);
  72. }
  73. $("#categories .in").collapse("hide");
  74. if (str) {
  75. const matchedOps = this.filterOperations(str, true);
  76. const matchedOpsHtml = matchedOps
  77. .map(v => v.toStubHtml())
  78. .join("");
  79. searchResultsEl.innerHTML = matchedOpsHtml;
  80. searchResultsEl.dispatchEvent(this.manager.oplistcreate);
  81. }
  82. }
  83. };
  84. /**
  85. * Filters operations based on the search string and returns the matching ones.
  86. *
  87. * @param {string} searchStr
  88. * @param {boolean} highlight - Whether or not to highlight the matching string in the operation
  89. * name and description
  90. * @returns {string[]}
  91. */
  92. OperationsWaiter.prototype.filterOperations = function(inStr, highlight) {
  93. const matchedOps = [];
  94. const matchedDescs = [];
  95. const searchStr = inStr.toLowerCase();
  96. for (const opName in this.app.operations) {
  97. const op = this.app.operations[opName];
  98. const namePos = opName.toLowerCase().indexOf(searchStr);
  99. const descPos = op.description.toLowerCase().indexOf(searchStr);
  100. if (namePos >= 0 || descPos >= 0) {
  101. const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
  102. if (highlight) {
  103. operation.highlightSearchString(searchStr, namePos, descPos);
  104. }
  105. if (namePos < 0) {
  106. matchedOps.push(operation);
  107. } else {
  108. matchedDescs.push(operation);
  109. }
  110. }
  111. }
  112. return matchedDescs.concat(matchedOps);
  113. };
  114. /**
  115. * Finds the operation which has been selected using keyboard shortcuts. This will have the class
  116. * 'selected-op' set. Returns the index of the operation within the given list.
  117. *
  118. * @param {element[]} ops
  119. * @returns {number}
  120. */
  121. OperationsWaiter.prototype.getSelectedOp = function(ops) {
  122. for (let i = 0; i < ops.length; i++) {
  123. if (ops[i].classList.contains("selected-op")) {
  124. return i;
  125. }
  126. }
  127. return -1;
  128. };
  129. /**
  130. * Handler for oplistcreate events.
  131. *
  132. * @listens Manager#oplistcreate
  133. * @param {event} e
  134. */
  135. OperationsWaiter.prototype.opListCreate = function(e) {
  136. this.manager.recipe.createSortableSeedList(e.target);
  137. $("[data-toggle=popover]").popover();
  138. };
  139. /**
  140. * Handler for operation doubleclick events.
  141. * Adds the operation to the recipe and auto bakes.
  142. *
  143. * @param {event} e
  144. */
  145. OperationsWaiter.prototype.operationDblclick = function(e) {
  146. const li = e.target;
  147. this.manager.recipe.addOperation(li.textContent);
  148. this.app.autoBake();
  149. };
  150. /**
  151. * Handler for edit favourites click events.
  152. * Sets up the 'Edit favourites' pane and displays it.
  153. *
  154. * @param {event} e
  155. */
  156. OperationsWaiter.prototype.editFavouritesClick = function(e) {
  157. e.preventDefault();
  158. e.stopPropagation();
  159. // Add favourites to modal
  160. const favCat = this.app.categories.filter(function(c) {
  161. return c.name === "Favourites";
  162. })[0];
  163. let html = "";
  164. for (let i = 0; i < favCat.ops.length; i++) {
  165. const opName = favCat.ops[i];
  166. const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
  167. html += operation.toStubHtml(true);
  168. }
  169. const editFavouritesList = document.getElementById("edit-favourites-list");
  170. editFavouritesList.innerHTML = html;
  171. this.removeIntent = false;
  172. const editableList = Sortable.create(editFavouritesList, {
  173. filter: ".remove-icon",
  174. onFilter: function (evt) {
  175. const el = editableList.closest(evt.item);
  176. if (el) {
  177. $(el).popover("destroy");
  178. el.parentNode.removeChild(el);
  179. }
  180. },
  181. onEnd: function(evt) {
  182. if (this.removeIntent) {
  183. $(evt.item).popover("destroy");
  184. evt.item.remove();
  185. }
  186. }.bind(this),
  187. });
  188. Sortable.utils.on(editFavouritesList, "dragleave", function() {
  189. this.removeIntent = true;
  190. }.bind(this));
  191. Sortable.utils.on(editFavouritesList, "dragover", function() {
  192. this.removeIntent = false;
  193. }.bind(this));
  194. $("#edit-favourites-list [data-toggle=popover]").popover();
  195. $("#favourites-modal").modal();
  196. };
  197. /**
  198. * Handler for save favourites click events.
  199. * Saves the selected favourites and reloads them.
  200. */
  201. OperationsWaiter.prototype.saveFavouritesClick = function() {
  202. const favs = document.querySelectorAll("#edit-favourites-list li");
  203. const favouritesList = Array.from(favs, e => e.textContent);
  204. this.app.saveFavourites(favouritesList);
  205. this.app.loadFavourites();
  206. this.app.populateOperationsList();
  207. this.manager.recipe.initialiseOperationDragNDrop();
  208. };
  209. /**
  210. * Handler for reset favourites click events.
  211. * Resets favourites to their defaults.
  212. */
  213. OperationsWaiter.prototype.resetFavouritesClick = function() {
  214. this.app.resetFavourites();
  215. };
  216. /**
  217. * Handler for opIcon mouseover events.
  218. * Hides any popovers already showing on the operation so that there aren't two at once.
  219. *
  220. * @param {event} e
  221. */
  222. OperationsWaiter.prototype.opIconMouseover = function(e) {
  223. const opEl = e.target.parentNode;
  224. if (e.target.getAttribute("data-toggle") === "popover") {
  225. $(opEl).popover("hide");
  226. }
  227. };
  228. /**
  229. * Handler for opIcon mouseleave events.
  230. * If this icon created a popover and we're moving back to the operation element, display the
  231. * operation popover again.
  232. *
  233. * @param {event} e
  234. */
  235. OperationsWaiter.prototype.opIconMouseleave = function(e) {
  236. const opEl = e.target.parentNode;
  237. const toEl = e.toElement || e.relatedElement;
  238. if (e.target.getAttribute("data-toggle") === "popover" && toEl === opEl) {
  239. $(opEl).popover("show");
  240. }
  241. };
  242. export default OperationsWaiter;