2017-03-23 18:52:20 +01:00
|
|
|
import Dish from "./Dish.js";
|
|
|
|
import Ingredient from "./Ingredient.js";
|
2017-03-06 13:45:51 +01:00
|
|
|
|
|
|
|
|
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
|
2017-01-31 19:24:56 +01:00
|
|
|
* @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) {
|
2017-01-31 19:24:56 +01:00
|
|
|
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
|
|
|
|
2017-01-31 19:24:56 +01:00
|
|
|
if (operationConfig) {
|
|
|
|
this._parseConfig(operationConfig);
|
2016-11-28 11:42:58 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads and parses the given config.
|
|
|
|
*
|
|
|
|
* @private
|
2017-01-31 19:24:56 +01:00
|
|
|
* @param {Object} operationConfig
|
2016-11-28 11:42:58 +01:00
|
|
|
*/
|
2017-01-31 19:24:56 +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);
|
2017-01-31 19:24:56 +01:00
|
|
|
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}
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
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++) {
|
2017-01-31 19:24:56 +01:00
|
|
|
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,
|
2017-01-31 19:24:56 +01:00
|
|
|
"args": ingredientConfig
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
2017-02-09 16:09:33 +01:00
|
|
|
|
2017-01-31 19:24:56 +01:00
|
|
|
return operationConfig;
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new Ingredient to this Operation.
|
|
|
|
*
|
|
|
|
* @param {Ingredient} ingredient
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
Operation.prototype.addIngredient = function(ingredient) {
|
|
|
|
this.ingList.push(ingredient);
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Ingredient values for this Operation.
|
|
|
|
*
|
2017-01-31 19:24:56 +01:00
|
|
|
* @param {Object[]} ingValues
|
2016-11-28 11:42:58 +01:00
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
Operation.prototype.setIngValues = function(ingValues) {
|
2017-04-13 19:08:50 +02:00
|
|
|
for (let i = 0; i < ingValues.length; i++) {
|
2017-01-31 19:24:56 +01:00
|
|
|
this.ingList[i].setValue(ingValues[i]);
|
2016-11-28 11:42:58 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Ingredient values for this Operation.
|
|
|
|
*
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
Operation.prototype.getIngValues = function() {
|
2017-04-13 19:08:50 +02:00
|
|
|
const ingValues = [];
|
|
|
|
for (let i = 0; i < this.ingList.length; i++) {
|
2017-01-31 19:24:56 +01:00
|
|
|
ingValues.push(this.ingList[i].value);
|
2016-11-28 11:42:58 +01:00
|
|
|
}
|
2017-01-31 19:24:56 +01:00
|
|
|
return ingValues;
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this Operation has a breakpoint.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
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}
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
Operation.prototype.isBreakpoint = function() {
|
2016-11-28 11:42:58 +01:00
|
|
|
return this.breakpoint;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this Operation is disabled.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
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}
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
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}
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
Operation.prototype.isFlowControl = function() {
|
|
|
|
return this.flowControl;
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
2017-03-23 18:52:20 +01:00
|
|
|
|
|
|
|
export default Operation;
|