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

312 Zeilen
6,8 KiB
Text

2020-04-02 10:21:03 +02:00
import {
properties,
mappedProperties,
2020-04-02 11:29:22 +02:00
advancedProperties,
2020-04-02 10:21:03 +02:00
camelCase,
snakeCase
} from '../lib/wizard';
2020-04-01 14:16:26 +02:00
import EmberObject from '@ember/object';
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;
}
function buildJson(object, type) {
let result = {};
properties[type].forEach((p) => {
let value = object.get(p);
2020-04-01 14:16:26 +02:00
if (mapped(p, type)) {
value = buildMappedJson(value);
}
2020-04-01 12:58:30 +02:00
if (value) {
2020-04-01 14:16:26 +02:00
result[p] = value;
2020-04-01 12:58:30 +02:00
}
});
return result;
2020-04-01 14:16:26 +02:00
}
2020-04-01 12:58:30 +02:00
function buildMappedJson(inputs) {
if (!inputs || !inputs.length) return false;
let result = [];
inputs.forEach(inpt => {
let input = {
type: inpt.type,
};
if (present(inpt.output)) {
input.output = inpt.output;
2020-04-02 10:21:03 +02:00
input.output_type = snakeCase(inpt.output_type);
2020-04-01 12:58:30 +02:00
input.connector = inpt.connector;
}
if (present(inpt.pairs)) {
input.pairs = [];
inpt.pairs.forEach(pr => {
if (present(pr.key) && present(pr.value)) {
let pairParams = {
index: pr.index,
key: pr.key,
2020-04-02 10:21:03 +02:00
key_type: snakeCase(pr.key_type),
2020-04-01 12:58:30 +02:00
value: pr.value,
2020-04-02 10:21:03 +02:00
value_type: snakeCase(pr.value_type),
2020-04-01 12:58:30 +02:00
connector: pr.connector
}
input.pairs.push(pairParams);
}
});
}
if ((input.type === 'assignment' && present(input.output)) ||
(input.type === 'conditional' && present(input.pairs)) ||
(input.type === 'pair' && present(input.pairs))) {
result.push(input);
}
});
if (!result.length) {
result = false;
}
return result;
}
2020-04-01 14:16:26 +02:00
function buildStepJson(object) {
2020-04-01 12:58:30 +02:00
let steps = [];
let error = null;
object.some((s) => {
let step = buildJson(s, 'step');
let fields = s.fields;
if (fields.length) {
step.fields = [];
fields.some((f) => {
if (!f.type) {
error = 'type_required';
return;
}
step.fields.push(
buildJson(f, 'field')
);
});
if (error) return;
}
let actions = s.actions;
if (actions.length) {
step.actions = [];
actions.some((a) => {
if (a.api_body) {
try {
JSON.parse(a.api_body);
} catch (e) {
error = 'invalid_api_body';
return;
}
}
step.actions.push(
buildJson(a, 'action')
);
});
if (error) return;
}
steps.push(step);
});
if (error) {
return { error };
} else {
return { steps };
};
}
2020-04-02 10:21:03 +02:00
function mappedProperty(property, value) {
return property.indexOf('_type') > -1 ? camelCase(value) : value;
}
2020-04-01 12:58:30 +02:00
function buildObject(json, type) {
let params = {
isNew: false
}
Object.keys(json).forEach(prop => {
2020-04-02 07:21:57 +02:00
if (mapped(prop, type) && present(json[prop])) {
2020-04-01 12:58:30 +02:00
let inputs = [];
2020-04-02 07:21:57 +02:00
json[prop].forEach(inputJson => {
let input = {}
Object.keys(inputJson).forEach(inputKey => {
if (inputKey === 'pairs') {
let pairs = [];
let pairCount = inputJson.pairs.length;
inputJson.pairs.forEach(pairJson => {
2020-04-02 10:21:03 +02:00
let pair = {};
Object.keys(pairJson).forEach(pairKey => {
pair[pairKey] = mappedProperty(pairKey, pairJson[pairKey]);
});
2020-04-02 07:21:57 +02:00
pair.pairCount = pairCount;
2020-04-01 14:16:26 +02:00
2020-04-02 07:21:57 +02:00
pairs.push(
EmberObject.create(pair)
);
});
input.pairs = pairs;
} else {
2020-04-02 10:21:03 +02:00
input[inputKey] = mappedProperty(inputKey, inputJson[inputKey]);
2020-04-02 07:21:57 +02:00
}
2020-04-01 12:58:30 +02:00
});
2020-04-02 07:21:57 +02:00
inputs.push(
EmberObject.create(input)
);
});
2020-04-01 12:58:30 +02:00
params[prop] = Ember.A(inputs);
} else {
params[prop] = json[prop];
}
});
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) {
return advancedProperties.step[property] && present(value);
2020-04-02 07:21:57 +02:00
}
2020-04-02 11:29:22 +02:00
function hasAdvanced(params, type) {
return Object.keys(params).some(p => {
return advancedProperties[type].indexOf(p) > -1 && present(params[p]);
});
2020-04-02 07:21:57 +02:00
}
2020-04-01 14:16:26 +02:00
function buildProperties(json) {
2020-04-01 12:58:30 +02:00
let steps = Ember.A();
let props = {
steps
};
2020-04-01 14:16:26 +02:00
2020-04-01 12:58:30 +02:00
if (present(json)) {
props.id = json.id;
props.existingId = true;
properties.wizard.forEach((p) => {
props[p] = json[p];
2020-04-02 07:21:57 +02:00
2020-04-02 11:29:22 +02:00
if (wizardHasAdvanced(p, json[p])) {
2020-04-02 07:21:57 +02:00
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
};
properties.step.forEach((p) => {
stepParams[p] = stepJson[p];
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
});
stepParams.fields = Ember.A();
if (present(stepJson.fields)) {
stepJson.fields.forEach((f) => {
2020-04-02 07:21:57 +02:00
let params = buildObject(f, 'field');
2020-04-02 11:29:22 +02:00
if (hasAdvanced(params, 'field')) {
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-01 14:16:26 +02:00
stepParams.actions = Ember.A();
if (present(stepJson.actions)) {
stepJson.actions.forEach((a) => {
2020-04-02 07:21:57 +02:00
let params = buildObject(a, 'action');
2020-04-02 11:29:22 +02:00
if (hasAdvanced(params, 'action')) {
2020-04-02 07:21:57 +02:00
params.showAdvanced = true;
}
stepParams.actions.pushObject(params);
2020-04-01 12:58:30 +02:00
});
}
2020-04-01 14:16:26 +02:00
steps.pushObject(
EmberObject.create(stepParams)
);
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;
props.steps = Ember.A();
2020-04-01 12:58:30 +02:00
}
return props;
2020-04-01 14:16:26 +02:00
}
export {
buildStepJson,
buildJson,
buildProperties
2020-04-01 12:58:30 +02:00
}