HTMLIngredient.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Object to handle the creation of operation ingredients.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2016
  6. * @license Apache-2.0
  7. *
  8. * @constructor
  9. * @param {Object} config - The configuration object for this ingredient.
  10. * @param {App} app - The main view object for CyberChef.
  11. * @param {Manager} manager - The CyberChef event manager.
  12. */
  13. const HTMLIngredient = function(config, app, manager) {
  14. this.app = app;
  15. this.manager = manager;
  16. this.name = config.name;
  17. this.type = config.type;
  18. this.value = config.value;
  19. this.disabled = config.disabled || false;
  20. this.disableArgs = config.disableArgs || false;
  21. this.placeholder = config.placeholder || false;
  22. this.target = config.target;
  23. this.toggleValues = config.toggleValues;
  24. this.id = "ing-" + this.app.nextIngId();
  25. };
  26. /**
  27. * Renders the ingredient in HTML.
  28. *
  29. * @returns {string}
  30. */
  31. HTMLIngredient.prototype.toHtml = function() {
  32. const inline = (
  33. this.type === "boolean" ||
  34. this.type === "number" ||
  35. this.type === "option" ||
  36. this.type === "shortString" ||
  37. this.type === "binaryShortString"
  38. );
  39. let html = inline ? "" : "<div class='clearfix'>&nbsp;</div>",
  40. i, m;
  41. html += "<div class='arg-group" + (inline ? " inline-args" : "") +
  42. (this.type === "text" ? " arg-group-text" : "") + "'><label class='arg-label' for='" +
  43. this.id + "'>" + this.name + "</label>";
  44. switch (this.type) {
  45. case "string":
  46. case "binaryString":
  47. case "byteArray":
  48. html += "<input type='text' id='" + this.id + "' class='arg arg-input' arg-name='" +
  49. this.name + "' value='" + this.value + "'" +
  50. (this.disabled ? " disabled='disabled'" : "") +
  51. (this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
  52. break;
  53. case "shortString":
  54. case "binaryShortString":
  55. html += "<input type='text' id='" + this.id +
  56. "'class='arg arg-input short-string' arg-name='" + this.name + "'value='" +
  57. this.value + "'" + (this.disabled ? " disabled='disabled'" : "") +
  58. (this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
  59. break;
  60. case "toggleString":
  61. html += "<div class='input-group'><div class='input-group-btn'>\
  62. <button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown'\
  63. aria-haspopup='true' aria-expanded='false'" +
  64. (this.disabled ? " disabled='disabled'" : "") + ">" + this.toggleValues[0] +
  65. " <span class='caret'></span></button><ul class='dropdown-menu'>";
  66. for (i = 0; i < this.toggleValues.length; i++) {
  67. html += "<li><a href='#'>" + this.toggleValues[i] + "</a></li>";
  68. }
  69. html += "</ul></div><input type='text' class='arg arg-input toggle-string'" +
  70. (this.disabled ? " disabled='disabled'" : "") +
  71. (this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + "></div>";
  72. break;
  73. case "number":
  74. html += "<input type='number' id='" + this.id + "'class='arg arg-input' arg-name='" +
  75. this.name + "'value='" + this.value + "'" +
  76. (this.disabled ? " disabled='disabled'" : "") +
  77. (this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
  78. break;
  79. case "boolean":
  80. html += "<input type='checkbox' id='" + this.id + "'class='arg' arg-name='" +
  81. this.name + "'" + (this.value ? " checked='checked' " : "") +
  82. (this.disabled ? " disabled='disabled'" : "") + ">";
  83. if (this.disableArgs) {
  84. this.manager.addDynamicListener("#" + this.id, "click", this.toggleDisableArgs, this);
  85. }
  86. break;
  87. case "option":
  88. html += "<select class='arg' id='" + this.id + "'arg-name='" + this.name + "'" +
  89. (this.disabled ? " disabled='disabled'" : "") + ">";
  90. for (i = 0; i < this.value.length; i++) {
  91. if ((m = this.value[i].match(/\[([a-z0-9 -()^]+)\]/i))) {
  92. html += "<optgroup label='" + m[1] + "'>";
  93. } else if ((m = this.value[i].match(/\[\/([a-z0-9 -()^]+)\]/i))) {
  94. html += "</optgroup>";
  95. } else {
  96. html += "<option>" + this.value[i] + "</option>";
  97. }
  98. }
  99. html += "</select>";
  100. break;
  101. case "populateOption":
  102. html += "<select class='arg' id='" + this.id + "'arg-name='" + this.name + "'" +
  103. (this.disabled ? " disabled='disabled'" : "") + ">";
  104. for (i = 0; i < this.value.length; i++) {
  105. if ((m = this.value[i].name.match(/\[([a-z0-9 -()^]+)\]/i))) {
  106. html += "<optgroup label='" + m[1] + "'>";
  107. } else if ((m = this.value[i].name.match(/\[\/([a-z0-9 -()^]+)\]/i))) {
  108. html += "</optgroup>";
  109. } else {
  110. html += "<option populate-value='" + this.value[i].value + "'>" +
  111. this.value[i].name + "</option>";
  112. }
  113. }
  114. html += "</select>";
  115. this.manager.addDynamicListener("#" + this.id, "change", this.populateOptionChange, this);
  116. break;
  117. case "editableOption":
  118. html += "<div class='editable-option'>";
  119. html += "<select class='editable-option-select' id='sel-" + this.id + "'" +
  120. (this.disabled ? " disabled='disabled'" : "") + ">";
  121. for (i = 0; i < this.value.length; i++) {
  122. html += "<option value='" + this.value[i].value + "'>" + this.value[i].name + "</option>";
  123. }
  124. html += "</select>";
  125. html += "<input class='arg arg-input editable-option-input' id='" + this.id +
  126. "'arg-name='" + this.name + "'" + " value='" + this.value[0].value + "'" +
  127. (this.disabled ? " disabled='disabled'" : "") +
  128. (this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
  129. html += "</div>";
  130. this.manager.addDynamicListener("#sel-" + this.id, "change", this.editableOptionChange, this);
  131. break;
  132. case "text":
  133. html += "<textarea id='" + this.id + "' class='arg' arg-name='" +
  134. this.name + "'" + (this.disabled ? " disabled='disabled'" : "") +
  135. (this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">" +
  136. this.value + "</textarea>";
  137. break;
  138. default:
  139. break;
  140. }
  141. html += "</div>";
  142. return html;
  143. };
  144. /**
  145. * Handler for argument disable toggle.
  146. * Toggles disabled state for all arguments in the disableArgs list for this ingredient.
  147. *
  148. * @param {event} e
  149. */
  150. HTMLIngredient.prototype.toggleDisableArgs = function(e) {
  151. const el = e.target;
  152. const op = el.parentNode.parentNode;
  153. const args = op.querySelectorAll(".arg-group");
  154. for (let i = 0; i < this.disableArgs.length; i++) {
  155. const els = args[this.disableArgs[i]].querySelectorAll("input, select, button");
  156. for (let j = 0; j < els.length; j++) {
  157. if (els[j].getAttribute("disabled")) {
  158. els[j].removeAttribute("disabled");
  159. } else {
  160. els[j].setAttribute("disabled", "disabled");
  161. }
  162. }
  163. }
  164. this.manager.recipe.ingChange();
  165. };
  166. /**
  167. * Handler for populate option changes.
  168. * Populates the relevant argument with the specified value.
  169. *
  170. * @param {event} e
  171. */
  172. HTMLIngredient.prototype.populateOptionChange = function(e) {
  173. const el = e.target;
  174. const op = el.parentNode.parentNode;
  175. const target = op.querySelectorAll(".arg-group")[this.target].querySelector("input, select, textarea");
  176. target.value = el.childNodes[el.selectedIndex].getAttribute("populate-value");
  177. this.manager.recipe.ingChange();
  178. };
  179. /**
  180. * Handler for editable option changes.
  181. * Populates the input box with the selected value.
  182. *
  183. * @param {event} e
  184. */
  185. HTMLIngredient.prototype.editableOptionChange = function(e) {
  186. const select = e.target,
  187. input = select.nextSibling;
  188. input.value = select.childNodes[select.selectedIndex].value;
  189. this.manager.recipe.ingChange();
  190. };
  191. export default HTMLIngredient;