OperationsWaiter.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. let searchResultsEl = document.getElementById("search-results"),
  65. el = e.target,
  66. 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. let matchedOps = this.filterOperations(str, true),
  76. matchedOpsHtml = "";
  77. for (let i = 0; i < matchedOps.length; i++) {
  78. matchedOpsHtml += matchedOps[i].toStubHtml();
  79. }
  80. searchResultsEl.innerHTML = matchedOpsHtml;
  81. searchResultsEl.dispatchEvent(this.manager.oplistcreate);
  82. }
  83. }
  84. };
  85. /**
  86. * Filters operations based on the search string and returns the matching ones.
  87. *
  88. * @param {string} searchStr
  89. * @param {boolean} highlight - Whether or not to highlight the matching string in the operation
  90. * name and description
  91. * @returns {string[]}
  92. */
  93. OperationsWaiter.prototype.filterOperations = function(searchStr, highlight) {
  94. let matchedOps = [],
  95. matchedDescs = [];
  96. searchStr = searchStr.toLowerCase();
  97. for (const opName in this.app.operations) {
  98. let op = this.app.operations[opName],
  99. namePos = opName.toLowerCase().indexOf(searchStr),
  100. descPos = op.description.toLowerCase().indexOf(searchStr);
  101. if (namePos >= 0 || descPos >= 0) {
  102. const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
  103. if (highlight) {
  104. operation.highlightSearchString(searchStr, namePos, descPos);
  105. }
  106. if (namePos < 0) {
  107. matchedOps.push(operation);
  108. } else {
  109. matchedDescs.push(operation);
  110. }
  111. }
  112. }
  113. return matchedDescs.concat(matchedOps);
  114. };
  115. /**
  116. * Finds the operation which has been selected using keyboard shortcuts. This will have the class
  117. * 'selected-op' set. Returns the index of the operation within the given list.
  118. *
  119. * @param {element[]} ops
  120. * @returns {number}
  121. */
  122. OperationsWaiter.prototype.getSelectedOp = function(ops) {
  123. for (let i = 0; i < ops.length; i++) {
  124. if (ops[i].classList.contains("selected-op")) {
  125. return i;
  126. }
  127. }
  128. return -1;
  129. };
  130. /**
  131. * Handler for oplistcreate events.
  132. *
  133. * @listens Manager#oplistcreate
  134. * @param {event} e
  135. */
  136. OperationsWaiter.prototype.opListCreate = function(e) {
  137. this.manager.recipe.createSortableSeedList(e.target);
  138. $("[data-toggle=popover]").popover();
  139. };
  140. /**
  141. * Handler for operation doubleclick events.
  142. * Adds the operation to the recipe and auto bakes.
  143. *
  144. * @param {event} e
  145. */
  146. OperationsWaiter.prototype.operationDblclick = function(e) {
  147. const li = e.target;
  148. this.manager.recipe.addOperation(li.textContent);
  149. this.app.autoBake();
  150. };
  151. /**
  152. * Handler for edit favourites click events.
  153. * Sets up the 'Edit favourites' pane and displays it.
  154. *
  155. * @param {event} e
  156. */
  157. OperationsWaiter.prototype.editFavouritesClick = function(e) {
  158. e.preventDefault();
  159. e.stopPropagation();
  160. // Add favourites to modal
  161. const favCat = this.app.categories.filter(function(c) {
  162. return c.name === "Favourites";
  163. })[0];
  164. let html = "";
  165. for (let i = 0; i < favCat.ops.length; i++) {
  166. const opName = favCat.ops[i];
  167. const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
  168. html += operation.toStubHtml(true);
  169. }
  170. const editFavouritesList = document.getElementById("edit-favourites-list");
  171. editFavouritesList.innerHTML = html;
  172. this.removeIntent = false;
  173. const editableList = Sortable.create(editFavouritesList, {
  174. filter: ".remove-icon",
  175. onFilter: function (evt) {
  176. const el = editableList.closest(evt.item);
  177. if (el) {
  178. $(el).popover("destroy");
  179. el.parentNode.removeChild(el);
  180. }
  181. },
  182. onEnd: function(evt) {
  183. if (this.removeIntent) {
  184. $(evt.item).popover("destroy");
  185. evt.item.remove();
  186. }
  187. }.bind(this),
  188. });
  189. Sortable.utils.on(editFavouritesList, "dragleave", function() {
  190. this.removeIntent = true;
  191. }.bind(this));
  192. Sortable.utils.on(editFavouritesList, "dragover", function() {
  193. this.removeIntent = false;
  194. }.bind(this));
  195. $("#edit-favourites-list [data-toggle=popover]").popover();
  196. $("#favourites-modal").modal();
  197. };
  198. /**
  199. * Handler for save favourites click events.
  200. * Saves the selected favourites and reloads them.
  201. */
  202. OperationsWaiter.prototype.saveFavouritesClick = function() {
  203. let favouritesList = [],
  204. favs = document.querySelectorAll("#edit-favourites-list li");
  205. for (let i = 0; i < favs.length; i++) {
  206. favouritesList.push(favs[i].textContent);
  207. }
  208. this.app.saveFavourites(favouritesList);
  209. this.app.loadFavourites();
  210. this.app.populateOperationsList();
  211. this.manager.recipe.initialiseOperationDragNDrop();
  212. };
  213. /**
  214. * Handler for reset favourites click events.
  215. * Resets favourites to their defaults.
  216. */
  217. OperationsWaiter.prototype.resetFavouritesClick = function() {
  218. this.app.resetFavourites();
  219. };
  220. /**
  221. * Handler for opIcon mouseover events.
  222. * Hides any popovers already showing on the operation so that there aren't two at once.
  223. *
  224. * @param {event} e
  225. */
  226. OperationsWaiter.prototype.opIconMouseover = function(e) {
  227. const opEl = e.target.parentNode;
  228. if (e.target.getAttribute("data-toggle") === "popover") {
  229. $(opEl).popover("hide");
  230. }
  231. };
  232. /**
  233. * Handler for opIcon mouseleave events.
  234. * If this icon created a popover and we're moving back to the operation element, display the
  235. * operation popover again.
  236. *
  237. * @param {event} e
  238. */
  239. OperationsWaiter.prototype.opIconMouseleave = function(e) {
  240. let opEl = e.target.parentNode,
  241. toEl = e.toElement || e.relatedElement;
  242. if (e.target.getAttribute("data-toggle") === "popover" && toEl === opEl) {
  243. $(opEl).popover("show");
  244. }
  245. };
  246. export default OperationsWaiter;