2021-04-12 07:10:02 +02:00
|
|
|
import { camelCase, listProperties, snakeCase } from "../lib/wizard";
|
2021-03-28 11:06:49 +02:00
|
|
|
import wizardSchema from "../lib/wizard-schema";
|
|
|
|
import EmberObject from "@ember/object";
|
2020-04-05 03:37:09 +02:00
|
|
|
import { A } from "@ember/array";
|
2020-04-01 12:58:30 +02:00
|
|
|
|
|
|
|
function present(val) {
|
2020-04-01 14:16:26 +02:00
|
|
|
if (val === null || val === undefined) {
|
|
|
|
return false;
|
2021-03-28 11:06:49 +02:00
|
|
|
} else if (typeof val === "object") {
|
2020-04-01 14:16:26 +02:00
|
|
|
return Object.keys(val).length !== 0;
|
2021-03-28 11:06:49 +02:00
|
|
|
} else if (typeof val === "string" || val.constructor === Array) {
|
2020-04-01 14:16:26 +02:00
|
|
|
return val && val.length;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-01 12:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapped(property, type) {
|
2020-04-16 04:04:27 +02:00
|
|
|
return wizardSchema[type].mapped.indexOf(property) > -1;
|
2020-04-01 12:58:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:36:38 +02:00
|
|
|
function castCase(property, value) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return property.indexOf("_type") > -1 ? camelCase(value) : value;
|
2020-04-02 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 07:41:27 +02:00
|
|
|
function buildProperty(json, property, type) {
|
|
|
|
let value = json[property];
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
if (mapped(property, type) && present(value) && value.constructor === Array) {
|
2020-04-15 07:41:27 +02:00
|
|
|
let inputs = [];
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
value.forEach((inputJson) => {
|
|
|
|
let input = {};
|
|
|
|
|
|
|
|
Object.keys(inputJson).forEach((inputKey) => {
|
|
|
|
if (inputKey === "pairs") {
|
2020-04-06 10:36:38 +02:00
|
|
|
let pairs = [];
|
|
|
|
let pairCount = inputJson.pairs.length;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
inputJson.pairs.forEach((pairJson) => {
|
2020-04-06 10:36:38 +02:00
|
|
|
let pair = {};
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
Object.keys(pairJson).forEach((pairKey) => {
|
|
|
|
pair[pairKey] = castCase(pairKey, pairJson[pairKey]);
|
2020-04-02 07:21:57 +02:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-06 10:36:38 +02:00
|
|
|
pair.pairCount = pairCount;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
pairs.push(EmberObject.create(pair));
|
2020-04-06 10:36:38 +02:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-06 10:36:38 +02:00
|
|
|
input.pairs = pairs;
|
|
|
|
} else {
|
2021-03-28 11:06:49 +02:00
|
|
|
input[inputKey] = castCase(inputKey, inputJson[inputKey]);
|
2020-04-06 10:36:38 +02:00
|
|
|
}
|
2020-04-02 07:21:57 +02:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
inputs.push(EmberObject.create(input));
|
2020-04-06 10:36:38 +02:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-06 10:36:38 +02:00
|
|
|
return A(inputs);
|
|
|
|
} else {
|
2020-04-15 07:41:27 +02:00
|
|
|
return value;
|
2020-04-06 10:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildObject(json, type) {
|
2020-04-10 09:57:49 +02:00
|
|
|
let props = {
|
2021-03-28 11:06:49 +02:00
|
|
|
isNew: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(json).forEach((prop) => {
|
|
|
|
props[prop] = buildProperty(json, prop, type);
|
2020-04-01 12:58:30 +02:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
return EmberObject.create(props);
|
2020-04-01 12:58:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
function buildObjectArray(json, type) {
|
|
|
|
let array = A();
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (present(json)) {
|
|
|
|
json.forEach((objJson) => {
|
|
|
|
let object = buildObject(objJson, type);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (hasAdvancedProperties(object, type)) {
|
2021-03-28 11:06:49 +02:00
|
|
|
object.set("showAdvanced", true);
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
array.pushObject(object);
|
|
|
|
});
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
return array;
|
2020-04-02 07:21:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
function buildBasicProperties(json, type, props) {
|
|
|
|
listProperties(type).forEach((p) => {
|
|
|
|
props[p] = buildProperty(json, p, type);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
if (hasAdvancedProperties(json, type)) {
|
2020-04-15 10:58:10 +02:00
|
|
|
props.showAdvanced = true;
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
return props;
|
2020-04-02 07:21:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
function hasAdvancedProperties(object, type) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return Object.keys(object).some((p) => {
|
2020-04-16 04:04:27 +02:00
|
|
|
return wizardSchema[type].advanced.indexOf(p) > -1 && present(object[p]);
|
2020-04-02 11:29:22 +02:00
|
|
|
});
|
2020-04-02 07:21:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
/// to be removed: necessary due to action array being moved from step to wizard
|
2020-04-08 09:59:54 +02:00
|
|
|
function actionPatch(json) {
|
|
|
|
let actions = json.actions || [];
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
json.steps.forEach((step) => {
|
2020-04-08 09:59:54 +02:00
|
|
|
if (step.actions && step.actions.length) {
|
2021-03-28 11:06:49 +02:00
|
|
|
step.actions.forEach((action) => {
|
|
|
|
action.run_after = "wizard_completion";
|
2020-04-08 09:59:54 +02:00
|
|
|
actions.push(action);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-08 09:59:54 +02:00
|
|
|
json.actions = actions;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-08 09:59:54 +02:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
///
|
|
|
|
|
2020-04-01 14:16:26 +02:00
|
|
|
function buildProperties(json) {
|
2021-03-28 11:06:49 +02:00
|
|
|
let props = {
|
2020-04-11 08:22:12 +02:00
|
|
|
steps: A(),
|
2021-03-28 11:06:49 +02:00
|
|
|
actions: A(),
|
2020-04-01 12:58:30 +02:00
|
|
|
};
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-01 12:58:30 +02:00
|
|
|
if (present(json)) {
|
|
|
|
props.existingId = true;
|
2021-03-28 11:06:49 +02:00
|
|
|
props = buildBasicProperties(json, "wizard", props);
|
|
|
|
|
2020-04-01 12:58:30 +02:00
|
|
|
if (present(json.steps)) {
|
2020-04-01 14:16:26 +02:00
|
|
|
json.steps.forEach((stepJson) => {
|
2020-04-10 09:57:49 +02:00
|
|
|
let stepProps = {
|
2021-03-28 11:06:49 +02:00
|
|
|
isNew: false,
|
2020-04-01 14:16:26 +02:00
|
|
|
};
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
stepProps = buildBasicProperties(stepJson, "step", stepProps);
|
|
|
|
stepProps.fields = buildObjectArray(stepJson.fields, "field");
|
|
|
|
|
2020-04-10 09:57:49 +02:00
|
|
|
props.steps.pushObject(EmberObject.create(stepProps));
|
2020-04-08 04:52:07 +02:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
}
|
2020-04-10 09:57:49 +02:00
|
|
|
|
|
|
|
json = actionPatch(json); // to be removed - see above
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
props.actions = buildObjectArray(json.actions, "action");
|
2020-04-01 12:58:30 +02:00
|
|
|
} else {
|
2021-03-28 11:06:49 +02:00
|
|
|
listProperties("wizard").forEach((prop) => {
|
2020-04-16 04:04:27 +02:00
|
|
|
props[prop] = wizardSchema.wizard.basic[prop];
|
2020-04-10 09:57:49 +02:00
|
|
|
});
|
2020-04-01 12:58:30 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-01 12:58:30 +02:00
|
|
|
return props;
|
2020-04-01 14:16:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
export { buildProperties, present, mapped };
|