1
0
Fork 0
discourse-custom-wizard-unl.../assets/javascripts/discourse/lib/wizard-mapper.js.es6

171 Zeilen
3,6 KiB
Text

2020-04-02 07:21:57 +02:00
import EmberObject from "@ember/object";
2020-04-05 03:37:09 +02:00
import { A } from "@ember/array";
2020-05-28 05:06:06 +02:00
import I18n from "I18n";
2020-04-01 12:58:30 +02:00
// Inputs
function defaultInputType(options = {}) {
return options.inputTypes.split(',')[0];
}
function mapInputTypes(types) {
return types.map(function(type) {
return {
id: type,
name: I18n.t(`admin.wizard.input.${type}.name`)
};
});
}
function inputTypesContent(options = {}) {
return options.inputTypes ?
mapInputTypes(options.inputTypes.split(',')) :
mapInputTypes(selectableInputTypes);
}
2020-04-15 07:22:21 +02:00
// connectorTypes
2020-04-01 12:58:30 +02:00
const connectors = {
pair: [
'equal',
'greater',
'less',
'greater_or_equal',
2020-04-06 10:36:38 +02:00
'less_or_equal',
2020-04-30 11:17:37 +02:00
'regex',
2020-05-01 11:12:58 +02:00
'is'
2020-04-15 07:22:21 +02:00
],
output: [
'then',
'set',
],
2020-04-01 12:58:30 +02:00
}
2020-04-15 07:22:21 +02:00
function defaultConnector(connectorType, inputType, options={}) {
if (connectorType === 'input') {
return defaultInputType(options);
}
if (connectorType === 'pair') {
if (inputType === 'conditional') return 'equal';
if (inputType === 'association') return 'association';
if (inputType === 'validation') return 'equal';
}
if (connectorType === 'output') {
if (inputType === 'conditional') return 'then';
if (inputType === 'assignment') return 'set';
}
2020-04-06 10:36:38 +02:00
return 'equal';
2020-04-01 12:58:30 +02:00
}
function connectorContent(connectorType, inputType, opts) {
2020-04-02 07:21:57 +02:00
let connector = opts[`${connectorType}Connector`];
2020-04-07 09:54:30 +02:00
if ((!connector && connectorType === 'output') || inputType === 'association') {
2020-04-15 07:22:21 +02:00
connector = defaultConnector(connectorType, inputType);
2020-04-02 07:21:57 +02:00
}
2020-04-01 12:58:30 +02:00
2020-04-02 07:21:57 +02:00
let content = connector ? [connector] : connectors[connectorType];
return content.map(function(item) {
return {
id: item,
name: I18n.t(`admin.wizard.connector.${item}`)
};
2020-04-01 12:58:30 +02:00
});
}
// Selectors
const selectionTypes = [
'text',
2020-04-06 03:54:16 +02:00
'list',
2020-04-05 03:37:09 +02:00
'wizardField',
2020-04-02 10:21:03 +02:00
'userField',
'userFieldOptions',
2020-04-01 12:58:30 +02:00
'group',
'category',
2020-04-02 10:21:03 +02:00
'tag',
2020-04-06 03:54:16 +02:00
'user'
2020-04-01 12:58:30 +02:00
]
function defaultSelectionType(inputType, options = {}) {
if (options[`${inputType}DefaultSelection`]) {
return options[`${inputType}DefaultSelection`];
}
let type = selectionTypes[0];
for (let t of selectionTypes) {
let inputTypes = options[`${t}Selection`];
if (inputTypes === true ||
((typeof inputTypes === 'string') &&
inputTypes.split(',').indexOf(inputType) > -1)) {
2020-04-02 07:21:57 +02:00
2020-04-01 12:58:30 +02:00
type = t;
break;
}
}
return type;
}
// items
function newPair(inputType, options = {}) {
let params = {
index: options.index,
pairCount: options.pairCount,
key: '',
key_type: defaultSelectionType('key', options),
value: '',
value_type: defaultSelectionType('value', options),
connector: defaultConnector('pair', inputType, options)
}
2020-04-02 07:21:57 +02:00
return EmberObject.create(params);
2020-04-01 12:58:30 +02:00
}
2020-04-06 10:36:38 +02:00
function newInput(options = {}, count) {
2020-04-01 12:58:30 +02:00
const inputType = defaultInputType(options);
2020-04-06 03:54:16 +02:00
2020-04-01 12:58:30 +02:00
let params = {
type: inputType,
2020-04-05 03:37:09 +02:00
pairs: A(
2020-04-01 12:58:30 +02:00
[
newPair(
inputType,
Object.assign({},
options,
{ index: 0, pairCount: 1 }
)
)
]
)
}
2020-04-06 10:36:38 +02:00
if (count > 0) {
params.connector = options.inputConnector;
}
2020-04-01 12:58:30 +02:00
2020-04-06 03:54:16 +02:00
if (['conditional', 'assignment'].indexOf(inputType) > -1 ||
options.outputDefaultSelection ||
options.outputConnector) {
2020-04-01 12:58:30 +02:00
params['output_type'] = defaultSelectionType('output', options);
2020-04-06 10:36:38 +02:00
params['output_connector'] = defaultConnector('output', inputType, options);
2020-04-01 12:58:30 +02:00
}
2020-04-02 07:21:57 +02:00
return EmberObject.create(params);
2020-04-01 12:58:30 +02:00
}
export {
defaultInputType,
defaultSelectionType,
2020-04-07 12:28:39 +02:00
defaultConnector,
2020-04-01 12:58:30 +02:00
connectorContent,
inputTypesContent,
2020-04-05 03:37:09 +02:00
selectionTypes,
2020-04-01 12:58:30 +02:00
newInput,
2020-04-05 03:37:09 +02:00
newPair
2020-04-01 12:58:30 +02:00
}