0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2025-02-01 02:48:56 +01:00
discourse-custom-wizard/assets/javascripts/discourse/lib/wizard-json.js.es6

190 Zeilen
4,2 KiB
Text

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