0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 04:12:53 +01:00
discourse-custom-wizard/assets/javascripts/discourse/lib/wizard-json.js.es6

209 Zeilen
5 KiB
Text

2020-04-05 03:37:09 +02:00
import { properties, mappedProperties, advancedProperties, camelCase, snakeCase } from '../lib/wizard';
2020-04-01 14:16:26 +02:00
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;
} else if (typeof val === 'object') {
return Object.keys(val).length !== 0;
} else if (typeof val === 'string' || val.constructor === Array) {
return val && val.length;
} else {
return false;
}
2020-04-01 12:58:30 +02:00
}
function mapped(property, type) {
return mappedProperties[type] &&
mappedProperties[type].indexOf(property) > -1;
}
2020-04-06 10:36:38 +02:00
function castCase(property, value) {
2020-04-02 10:21:03 +02:00
return property.indexOf('_type') > -1 ? camelCase(value) : value;
}
2020-04-06 10:36:38 +02:00
function buildProperty(json, property, type) {
if (mapped(property, type) && present(json[property])) {
let inputs = [];
json[property].forEach(inputJson => {
let input = {}
2020-04-01 12:58:30 +02:00
2020-04-06 10:36:38 +02:00
Object.keys(inputJson).forEach(inputKey => {
if (inputKey === 'pairs') {
let pairs = [];
let pairCount = inputJson.pairs.length;
inputJson.pairs.forEach(pairJson => {
let pair = {};
2020-04-02 07:21:57 +02:00
2020-04-06 10:36:38 +02:00
Object.keys(pairJson).forEach(pairKey => {
pair[pairKey] = castCase(pairKey, pairJson[pairKey]);
2020-04-02 07:21:57 +02:00
});
2020-04-06 10:36:38 +02:00
pair.pairCount = pairCount;
pairs.push(
EmberObject.create(pair)
);
});
input.pairs = pairs;
} else {
input[inputKey] = castCase(inputKey, inputJson[inputKey]);
}
2020-04-02 07:21:57 +02:00
});
2020-04-01 12:58:30 +02:00
2020-04-06 10:36:38 +02:00
inputs.push(
EmberObject.create(input)
);
});
return A(inputs);
} else {
return json[property];
}
}
function buildObject(json, type) {
let params = {
isNew: false
}
Object.keys(json).forEach(prop => {
params[prop] = buildProperty(json, prop, type)
2020-04-01 12:58:30 +02:00
});
2020-04-01 14:16:26 +02:00
return EmberObject.create(params);
2020-04-01 12:58:30 +02:00
}
2020-04-02 11:29:22 +02:00
function wizardHasAdvanced(property, value) {
2020-04-02 07:21:57 +02:00
if (property === 'save_submissions' && value == false) return true;
if (property === 'restart_on_revisit' && value == true) return true;
return false;
}
2020-04-02 11:29:22 +02:00
function stepHasAdvanced(property, value) {
2020-04-08 09:59:54 +02:00
return advancedProperties.steps[property] && present(value);
2020-04-02 07:21:57 +02:00
}
2020-04-07 06:56:16 +02:00
function objectHasAdvanced(params, type) {
2020-04-02 11:29:22 +02:00
return Object.keys(params).some(p => {
2020-04-07 06:56:16 +02:00
let value = params[p];
let advanced = advancedProperties[type][params.type];
return advanced && advanced.indexOf(p) > -1 && present(value);
2020-04-02 11:29:22 +02:00
});
2020-04-02 07:21:57 +02:00
}
2020-04-08 09:59:54 +02:00
/// to be removed
function actionPatch(json) {
let actions = json.actions || [];
json.steps.forEach(step => {
if (step.actions && step.actions.length) {
step.actions.forEach(action => {
action.run_after = 'wizard_completion';
actions.push(action);
});
}
});
json.actions = actions;
return json;
}
///
2020-04-01 14:16:26 +02:00
function buildProperties(json) {
2020-04-01 12:58:30 +02:00
let props = {
2020-04-08 09:59:54 +02:00
steps: A(),
actions: A()
2020-04-01 12:58:30 +02:00
};
2020-04-08 09:59:54 +02:00
2020-04-01 12:58:30 +02:00
if (present(json)) {
props.id = json.id;
props.existingId = true;
2020-04-08 09:59:54 +02:00
// to fix
properties.wizard
.filter(p => ['steps', 'actions'].indexOf(p) === -1)
.forEach((p) => {
props[p] = buildProperty(json, p, 'wizard');
if (wizardHasAdvanced(p, json[p])) {
props.showAdvanced = true;
}
});
2020-04-01 12:58:30 +02:00
if (present(json.steps)) {
2020-04-01 14:16:26 +02:00
json.steps.forEach((stepJson) => {
let stepParams = {
isNew: false
};
2020-04-08 09:59:54 +02:00
properties.steps.forEach((p) => {
2020-04-07 06:56:16 +02:00
stepParams[p] = buildProperty(stepJson, p, 'wizard');
2020-04-02 07:21:57 +02:00
2020-04-02 11:29:22 +02:00
if (stepHasAdvanced(p, stepJson[p])) {
2020-04-02 07:21:57 +02:00
stepParams.showAdvanced = true;
}
2020-04-01 14:16:26 +02:00
});
2020-04-05 03:37:09 +02:00
stepParams.fields = A();
2020-04-01 14:16:26 +02:00
if (present(stepJson.fields)) {
stepJson.fields.forEach((f) => {
2020-04-08 09:59:54 +02:00
let params = buildObject(f, 'fields');
2020-04-07 06:56:16 +02:00
2020-04-08 09:59:54 +02:00
if (objectHasAdvanced(params, 'fields')) {
2020-04-02 07:21:57 +02:00
params.showAdvanced = true;
}
stepParams.fields.pushObject(params);
2020-04-01 12:58:30 +02:00
});
}
2020-04-08 09:59:54 +02:00
props.steps.pushObject(
2020-04-01 14:16:26 +02:00
EmberObject.create(stepParams)
);
2020-04-01 12:58:30 +02:00
});
};
2020-04-08 09:59:54 +02:00
// to be removed
json = actionPatch(json);
// to be removed
2020-04-08 04:52:07 +02:00
if (present(json.actions)) {
json.actions.forEach((a) => {
2020-04-08 09:59:54 +02:00
let params = buildObject(a, 'actions');
2020-04-08 04:52:07 +02:00
2020-04-08 09:59:54 +02:00
if (objectHasAdvanced(params, 'actions')) {
2020-04-08 04:52:07 +02:00
params.showAdvanced = true;
}
props.actions.pushObject(params);
});
}
2020-04-01 12:58:30 +02:00
} else {
2020-04-01 14:16:26 +02:00
props.id = '';
props.name = '';
props.background = '';
props.save_submissions = true;
props.multiple_submissions = false;
props.after_signup = false;
props.after_time = false;
props.required = false;
props.prompt_completion = false;
props.restart_on_revisit = false;
props.permitted = null;
2020-04-01 12:58:30 +02:00
}
2020-04-08 09:59:54 +02:00
2020-04-01 12:58:30 +02:00
return props;
2020-04-01 14:16:26 +02:00
}
export {
2020-04-08 09:59:54 +02:00
buildProperties,
present,
mapped
2020-04-01 12:58:30 +02:00
}