HTMLIngredient.js 8.4 KB

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