CyberChef/src/core/Operation.js

164 lines
3.6 KiB
JavaScript
Raw Normal View History

import Dish from "./Dish.js";
import Ingredient from "./Ingredient.js";
2016-11-28 11:42:58 +01:00
/**
* The Operation specified by the user to be run.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @class
* @param {string} operationName
* @param {Object} operationConfig
2016-11-28 11:42:58 +01:00
*/
2017-04-13 19:08:50 +02:00
const Operation = function(operationName, operationConfig) {
this.name = operationName;
this.description = "";
this.inputType = -1;
this.outputType = -1;
this.run = null;
this.highlight = null;
this.highlightReverse = null;
this.breakpoint = false;
this.disabled = false;
this.ingList = [];
2017-02-09 16:09:33 +01:00
if (operationConfig) {
this._parseConfig(operationConfig);
2016-11-28 11:42:58 +01:00
}
};
/**
* Reads and parses the given config.
*
* @private
* @param {Object} operationConfig
2016-11-28 11:42:58 +01:00
*/
Operation.prototype._parseConfig = function(operationConfig) {
this.description = operationConfig.description;
this.inputType = Dish.typeEnum(operationConfig.inputType);
this.outputType = Dish.typeEnum(operationConfig.outputType);
this.run = operationConfig.run;
this.highlight = operationConfig.highlight;
this.highlightReverse = operationConfig.highlightReverse;
this.flowControl = operationConfig.flowControl;
2017-04-13 19:08:50 +02:00
for (let a = 0; a < operationConfig.args.length; a++) {
const ingredientConfig = operationConfig.args[a];
const ingredient = new Ingredient(ingredientConfig);
this.addIngredient(ingredient);
2016-11-28 11:42:58 +01:00
}
};
/**
* Returns the value of the Operation as it should be displayed in a recipe config.
*
* @returns {Object}
*/
Operation.prototype.getConfig = function() {
2017-04-13 19:08:50 +02:00
const ingredientConfig = [];
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
for (let o = 0; o < this.ingList.length; o++) {
ingredientConfig.push(this.ingList[o].getConfig());
2016-11-28 11:42:58 +01:00
}
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
const operationConfig = {
2016-11-28 11:42:58 +01:00
"op": this.name,
"args": ingredientConfig
2016-11-28 11:42:58 +01:00
};
2017-02-09 16:09:33 +01:00
return operationConfig;
2016-11-28 11:42:58 +01:00
};
/**
* Adds a new Ingredient to this Operation.
*
* @param {Ingredient} ingredient
*/
Operation.prototype.addIngredient = function(ingredient) {
this.ingList.push(ingredient);
2016-11-28 11:42:58 +01:00
};
/**
* Set the Ingredient values for this Operation.
*
* @param {Object[]} ingValues
2016-11-28 11:42:58 +01:00
*/
Operation.prototype.setIngValues = function(ingValues) {
2017-04-13 19:08:50 +02:00
for (let i = 0; i < ingValues.length; i++) {
this.ingList[i].setValue(ingValues[i]);
2016-11-28 11:42:58 +01:00
}
};
/**
* Get the Ingredient values for this Operation.
*
* @returns {Object[]}
*/
Operation.prototype.getIngValues = function() {
2017-04-13 19:08:50 +02:00
const ingValues = [];
for (let i = 0; i < this.ingList.length; i++) {
ingValues.push(this.ingList[i].value);
2016-11-28 11:42:58 +01:00
}
return ingValues;
2016-11-28 11:42:58 +01:00
};
/**
* Set whether this Operation has a breakpoint.
*
* @param {boolean} value
*/
Operation.prototype.setBreakpoint = function(value) {
2016-11-28 11:42:58 +01:00
this.breakpoint = !!value;
};
/**
* Returns true if this Operation has a breakpoint set.
*
* @returns {boolean}
*/
Operation.prototype.isBreakpoint = function() {
2016-11-28 11:42:58 +01:00
return this.breakpoint;
};
/**
* Set whether this Operation is disabled.
*
* @param {boolean} value
*/
Operation.prototype.setDisabled = function(value) {
2016-11-28 11:42:58 +01:00
this.disabled = !!value;
};
/**
* Returns true if this Operation is disabled.
*
* @returns {boolean}
*/
Operation.prototype.isDisabled = function() {
2016-11-28 11:42:58 +01:00
return this.disabled;
};
/**
* Returns true if this Operation is a flow control.
*
* @returns {boolean}
*/
Operation.prototype.isFlowControl = function() {
return this.flowControl;
2016-11-28 11:42:58 +01:00
};
export default Operation;