/** * Object to handle the creation of operation ingredients. * * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright 2016 * @license Apache-2.0 * * @constructor * @param {Object} config - The configuration object for this ingredient. * @param {HTMLApp} app - The main view object for CyberChef. * @param {Manager} manager - The CyberChef event manager. */ var HTMLIngredient = module.exports = function(config, app, manager) { this.app = app; this.manager = manager; this.name = config.name; this.type = config.type; this.value = config.value; this.disabled = config.disabled || false; this.disableArgs = config.disableArgs || false; this.placeholder = config.placeholder || false; this.target = config.target; this.toggleValues = config.toggleValues; this.id = "ing-" + this.app.nextIngId(); }; /** * Renders the ingredient in HTML. * * @returns {string} */ HTMLIngredient.prototype.toHtml = function() { var inline = (this.type === "boolean" || this.type === "number" || this.type === "option" || this.type === "shortString" || this.type === "binaryShortString"), html = inline ? "" : "
 
", i, m; html += "
"; switch (this.type) { case "string": case "binaryString": case "byteArray": html += ""; break; case "shortString": case "binaryShortString": html += ""; break; case "toggleString": html += "
\
"; break; case "number": html += ""; break; case "boolean": html += ""; if (this.disableArgs) { this.manager.addDynamicListener("#" + this.id, "click", this.toggleDisableArgs, this); } break; case "option": html += ""; break; case "populateOption": html += ""; this.manager.addDynamicListener("#" + this.id, "change", this.populateOptionChange, this); break; case "editableOption": html += "
"; html += ""; html += ""; html += "
"; this.manager.addDynamicListener("#sel-" + this.id, "change", this.editableOptionChange, this); break; case "text": html += ""; break; default: break; } html += "
"; return html; }; /** * Handler for argument disable toggle. * Toggles disabled state for all arguments in the disableArgs list for this ingredient. * * @param {event} e */ HTMLIngredient.prototype.toggleDisableArgs = function(e) { var el = e.target, op = el.parentNode.parentNode, args = op.querySelectorAll(".arg-group"), els; for (var i = 0; i < this.disableArgs.length; i++) { els = args[this.disableArgs[i]].querySelectorAll("input, select, button"); for (var j = 0; j < els.length; j++) { if (els[j].getAttribute("disabled")) { els[j].removeAttribute("disabled"); } else { els[j].setAttribute("disabled", "disabled"); } } } this.manager.recipe.ingChange(); }; /** * Handler for populate option changes. * Populates the relevant argument with the specified value. * * @param {event} e */ HTMLIngredient.prototype.populateOptionChange = function(e) { var el = e.target, op = el.parentNode.parentNode, target = op.querySelectorAll(".arg-group")[this.target].querySelector("input, select, textarea"); target.value = el.childNodes[el.selectedIndex].getAttribute("populate-value"); this.manager.recipe.ingChange(); }; /** * Handler for editable option changes. * Populates the input box with the selected value. * * @param {event} e */ HTMLIngredient.prototype.editableOptionChange = function(e) { var select = e.target, input = select.nextSibling; input.value = select.childNodes[select.selectedIndex].value; this.manager.recipe.ingChange(); };