Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-10 04:12:53 +01:00
173 Zeilen
Kein EOL
3,9 KiB
JavaScript
173 Zeilen
Kein EOL
3,9 KiB
JavaScript
import { schema, listProperties, camelCase, snakeCase } from '../lib/wizard';
|
|
import EmberObject from '@ember/object';
|
|
import { A } from "@ember/array";
|
|
|
|
function present(val) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
function mapped(property, type) {
|
|
return schema[type].mapped.indexOf(property) > -1;
|
|
}
|
|
|
|
function castCase(property, value) {
|
|
return property.indexOf('_type') > -1 ? camelCase(value) : value;
|
|
}
|
|
|
|
function buildProperty(json, property, type) {
|
|
if (mapped(property, type) && present(json[property])) {
|
|
let inputs = [];
|
|
|
|
json[property].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);
|
|
} else {
|
|
return json[property];
|
|
}
|
|
}
|
|
|
|
function buildObject(json, type) {
|
|
let props = {
|
|
isNew: false
|
|
}
|
|
|
|
Object.keys(json).forEach(prop => {
|
|
props[prop] = buildProperty(json, prop, type)
|
|
});
|
|
|
|
return EmberObject.create(props);
|
|
}
|
|
|
|
function buildObjectArray(json, type) {
|
|
let array = A();
|
|
|
|
if (present(json)) {
|
|
json.forEach((objJson) => {
|
|
let object = buildObject(objJson, type);
|
|
|
|
if (hasAdvancedProperties(object, type)) {
|
|
object.set('showAdvanced', true);
|
|
}
|
|
|
|
array.pushObject(object);
|
|
});
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
function buildBasicProperties(json, type, props) {
|
|
listProperties(type).forEach((p) => {
|
|
props[p] = buildProperty(json, p, type);
|
|
|
|
if (hasAdvancedProperties(json, type)) {
|
|
result.showAdvanced = true;
|
|
}
|
|
});
|
|
|
|
return props;
|
|
}
|
|
|
|
function hasAdvancedProperties(object, type) {
|
|
return Object.keys(object).some(p => {
|
|
return schema[type].advanced.indexOf(p) > -1 && present(object[p]);
|
|
});
|
|
}
|
|
|
|
/// to be removed: necessary due to action array being moved from step to wizard
|
|
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;
|
|
}
|
|
///
|
|
|
|
function buildProperties(json) {
|
|
let props = {
|
|
steps: A(),
|
|
actions: A()
|
|
};
|
|
|
|
if (present(json)) {
|
|
props.existingId = true;
|
|
props = buildBasicProperties(json, 'wizard', props);
|
|
|
|
if (present(json.steps)) {
|
|
json.steps.forEach((stepJson) => {
|
|
let stepProps = {
|
|
isNew: false
|
|
};
|
|
|
|
stepProps = buildBasicProperties(stepJson, 'step', stepProps);
|
|
stepProps.fields = buildObjectArray(stepJson.fields, 'field');
|
|
|
|
props.steps.pushObject(EmberObject.create(stepProps));
|
|
});
|
|
};
|
|
|
|
json = actionPatch(json); // to be removed - see above
|
|
props.actions = buildObjectArray(json.actions, 'action');
|
|
} else {
|
|
listProperties('wizard').forEach(prop => {
|
|
props[prop] = schema.wizard.basic[prop];
|
|
});
|
|
}
|
|
|
|
return props;
|
|
}
|
|
|
|
export {
|
|
buildProperties,
|
|
present,
|
|
mapped
|
|
} |