/**
* 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 {App} app - The main view object for CyberChef.
* @param {Manager} manager - The CyberChef event manager.
*/
const HTMLIngredient = 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() {
let inline = (this.type === "boolean" ||
this.type === "number" ||
this.type === "option" ||
this.type === "shortString" ||
this.type === "binaryShortString"),
html = inline ? "" : "
";
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) {
const el = e.target;
const op = el.parentNode.parentNode;
const args = op.querySelectorAll(".arg-group");
for (let i = 0; i < this.disableArgs.length; i++) {
const els = args[this.disableArgs[i]].querySelectorAll("input, select, button");
for (let 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) {
const el = e.target;
const op = el.parentNode.parentNode;
const 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) {
let select = e.target,
input = select.nextSibling;
input.value = select.childNodes[select.selectedIndex].value;
this.manager.recipe.ingChange();
};
export default HTMLIngredient;