2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Dish from "./Dish.mjs";
|
|
|
|
import Ingredient from "./Ingredient.mjs";
|
2018-03-27 00:14:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Operation specified by the user to be run.
|
|
|
|
*/
|
|
|
|
class Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operation constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
// Private fields
|
|
|
|
this._inputType = -1;
|
|
|
|
this._outputType = -1;
|
2018-04-06 20:11:13 +02:00
|
|
|
this._presentType = -1;
|
2018-03-27 00:14:23 +02:00
|
|
|
this._breakpoint = false;
|
|
|
|
this._disabled = false;
|
|
|
|
this._flowControl = false;
|
2019-02-08 19:02:13 +01:00
|
|
|
this._manualBake = false;
|
2018-03-27 00:14:23 +02:00
|
|
|
this._ingList = [];
|
|
|
|
|
|
|
|
// Public fields
|
|
|
|
this.name = "";
|
|
|
|
this.module = "";
|
|
|
|
this.description = "";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = null;
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for operation runner
|
|
|
|
*
|
|
|
|
* @param {*} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for forward highlighter
|
|
|
|
*
|
|
|
|
* @param {Object[]} pos
|
|
|
|
* @param {number} pos[].start
|
|
|
|
* @param {number} pos[].end
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {Object[]} pos
|
|
|
|
*/
|
|
|
|
highlight(pos, args) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for reverse highlighter
|
|
|
|
*
|
|
|
|
* @param {Object[]} pos
|
|
|
|
* @param {number} pos[].start
|
|
|
|
* @param {number} pos[].end
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {Object[]} pos
|
|
|
|
*/
|
|
|
|
highlightReverse(pos, args) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-06 20:11:13 +02:00
|
|
|
/**
|
|
|
|
* Method to be called when displaying the result of an operation in a human-readable
|
|
|
|
* format. This allows operations to return usable data from their run() method and
|
|
|
|
* only format them when this method is called.
|
|
|
|
*
|
|
|
|
* The default action is to return the data unchanged, but child classes can override
|
|
|
|
* this behaviour.
|
|
|
|
*
|
|
|
|
* @param {*} data - The result of the run() function
|
2018-05-16 12:39:30 +02:00
|
|
|
* @param {Object[]} args - The operation's arguments
|
2018-04-06 20:11:13 +02:00
|
|
|
* @returns {*} - A human-readable version of the data
|
|
|
|
*/
|
2018-05-16 12:39:30 +02:00
|
|
|
present(data, args) {
|
2018-04-06 20:11:13 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* Sets the input type as a Dish enum.
|
|
|
|
*
|
|
|
|
* @param {string} typeStr
|
|
|
|
*/
|
|
|
|
set inputType(typeStr) {
|
|
|
|
this._inputType = Dish.typeEnum(typeStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the input type as a readable string.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
get inputType() {
|
|
|
|
return Dish.enumLookup(this._inputType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the output type as a Dish enum.
|
|
|
|
*
|
|
|
|
* @param {string} typeStr
|
|
|
|
*/
|
|
|
|
set outputType(typeStr) {
|
|
|
|
this._outputType = Dish.typeEnum(typeStr);
|
2018-04-06 20:11:13 +02:00
|
|
|
if (this._presentType < 0) this._presentType = this._outputType;
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the output type as a readable string.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
get outputType() {
|
|
|
|
return Dish.enumLookup(this._outputType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-06 20:11:13 +02:00
|
|
|
/**
|
|
|
|
* Sets the presentation type as a Dish enum.
|
|
|
|
*
|
|
|
|
* @param {string} typeStr
|
|
|
|
*/
|
|
|
|
set presentType(typeStr) {
|
|
|
|
this._presentType = Dish.typeEnum(typeStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the presentation type as a readable string.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
get presentType() {
|
|
|
|
return Dish.enumLookup(this._presentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* Sets the args for the current operation.
|
|
|
|
*
|
|
|
|
* @param {Object[]} conf
|
|
|
|
*/
|
|
|
|
set args(conf) {
|
|
|
|
conf.forEach(arg => {
|
|
|
|
const ingredient = new Ingredient(arg);
|
|
|
|
this.addIngredient(ingredient);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the args for the current operation.
|
|
|
|
*
|
|
|
|
* @param {Object[]} conf
|
|
|
|
*/
|
|
|
|
get args() {
|
|
|
|
return this._ingList.map(ing => {
|
2018-06-09 11:43:36 +02:00
|
|
|
const conf = {
|
2018-03-27 00:14:23 +02:00
|
|
|
name: ing.name,
|
|
|
|
type: ing.type,
|
2018-06-09 11:43:36 +02:00
|
|
|
value: ing.defaultValue
|
2018-03-27 00:14:23 +02:00
|
|
|
};
|
2018-06-09 11:43:36 +02:00
|
|
|
|
|
|
|
if (ing.toggleValues) conf.toggleValues = ing.toggleValues;
|
|
|
|
if (ing.hint) conf.hint = ing.hint;
|
2019-01-09 16:28:50 +01:00
|
|
|
if (ing.rows) conf.rows = ing.rows;
|
2018-06-09 11:43:36 +02:00
|
|
|
if (ing.disabled) conf.disabled = ing.disabled;
|
2018-06-10 13:03:55 +02:00
|
|
|
if (ing.target) conf.target = ing.target;
|
2018-12-25 20:02:05 +01:00
|
|
|
if (ing.defaultIndex) conf.defaultIndex = ing.defaultIndex;
|
2019-03-04 12:46:27 +01:00
|
|
|
if (typeof ing.min === "number") conf.min = ing.min;
|
|
|
|
if (typeof ing.max === "number") conf.max = ing.max;
|
|
|
|
if (ing.step) conf.step = ing.step;
|
2018-06-09 11:43:36 +02:00
|
|
|
return conf;
|
2018-03-27 00:14:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of the Operation as it should be displayed in a recipe config.
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
get config() {
|
|
|
|
return {
|
|
|
|
"op": this.name,
|
2018-05-20 17:49:42 +02:00
|
|
|
"args": this._ingList.map(ing => ing.config)
|
2018-03-27 00:14:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new Ingredient to this Operation.
|
|
|
|
*
|
|
|
|
* @param {Ingredient} ingredient
|
|
|
|
*/
|
|
|
|
addIngredient(ingredient) {
|
|
|
|
this._ingList.push(ingredient);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Ingredient values for this Operation.
|
|
|
|
*
|
|
|
|
* @param {Object[]} ingValues
|
|
|
|
*/
|
|
|
|
set ingValues(ingValues) {
|
|
|
|
ingValues.forEach((val, i) => {
|
|
|
|
this._ingList[i].value = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Ingredient values for this Operation.
|
|
|
|
*
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
|
|
|
get ingValues() {
|
|
|
|
return this._ingList.map(ing => ing.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this Operation has a breakpoint.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
set breakpoint(value) {
|
|
|
|
this._breakpoint = !!value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this Operation has a breakpoint set.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get breakpoint() {
|
|
|
|
return this._breakpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this Operation is disabled.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
set disabled(value) {
|
|
|
|
this._disabled = !!value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this Operation is disabled.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get disabled() {
|
|
|
|
return this._disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this Operation is a flow control.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get flowControl() {
|
|
|
|
return this._flowControl;
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:02:13 +01:00
|
|
|
|
2018-05-20 17:49:42 +02:00
|
|
|
/**
|
|
|
|
* Set whether this Operation is a flowcontrol op.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
set flowControl(value) {
|
|
|
|
this._flowControl = !!value;
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:02:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this Operation should not trigger AutoBake.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get manualBake() {
|
|
|
|
return this._manualBake;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether this Operation should trigger AutoBake.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
set manualBake(value) {
|
|
|
|
this._manualBake = !!value;
|
|
|
|
}
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Operation;
|