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

194 Zeilen
3,8 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
2021-04-12 07:44:47 +02:00
const selectableInputTypes = [
2021-04-12 08:12:20 +02:00
"conditional",
"assignment",
"association",
"validation",
2021-04-12 07:44:47 +02:00
];
2020-04-01 12:58:30 +02:00
function defaultInputType(options = {}) {
return options.inputTypes.split(",")[0];
2020-04-01 12:58:30 +02:00
}
function mapInputTypes(types) {
return types.map(function (type) {
2020-04-01 12:58:30 +02:00
return {
id: type,
name: I18n.t(`admin.wizard.input.${type}.name`),
2020-04-01 12:58:30 +02:00
};
});
}
function inputTypesContent(options = {}) {
return options.inputTypes
? mapInputTypes(options.inputTypes.split(","))
: mapInputTypes(selectableInputTypes);
2020-04-01 12:58:30 +02:00
}
2020-04-15 07:22:21 +02:00
// connectorTypes
2020-04-01 12:58:30 +02:00
const connectors = {
pair: [
"equal",
2023-05-03 11:01:54 +02:00
"not_equal",
"greater",
"less",
"greater_or_equal",
"less_or_equal",
"regex",
"is",
2020-04-15 07:22:21 +02:00
],
output: ["then", "set"],
};
2020-04-01 12:58:30 +02:00
function defaultConnector(connectorType, inputType, options = {}) {
if (connectorType === "input") {
2020-04-15 07:22:21 +02:00
return defaultInputType(options);
}
if (connectorType === "pair") {
2021-04-12 08:12:20 +02:00
if (inputType === "conditional") {
return "equal";
}
if (inputType === "association") {
return "association";
}
if (inputType === "validation") {
return "equal";
}
2020-04-15 07:22:21 +02:00
}
if (connectorType === "output") {
2021-04-12 08:12:20 +02:00
if (inputType === "conditional") {
return "then";
}
if (inputType === "assignment") {
return "set";
}
2020-04-15 07:22:21 +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`];
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-02 07:21:57 +02:00
let content = connector ? [connector] : connectors[connectorType];
return content.map(function (item) {
2020-04-02 07:21:57 +02:00
return {
id: item,
name: I18n.t(`admin.wizard.connector.${item}`),
2020-04-02 07:21:57 +02:00
};
2020-04-01 12:58:30 +02:00
});
}
// Selectors
const selectionTypes = [
"text",
"list",
"wizardField",
"wizardAction",
"userField",
"userFieldOptions",
"group",
"category",
"tag",
"user",
"customField",
2021-10-20 16:00:30 +02:00
"value",
];
2020-04-01 12:58:30 +02:00
function defaultSelectionType(inputType, options = {}, connector = null) {
2020-04-01 12:58:30 +02:00
if (options[`${inputType}DefaultSelection`]) {
return options[`${inputType}DefaultSelection`];
}
if (connector === "is") {
return "value";
}
2020-04-01 12:58:30 +02:00
let type = selectionTypes[0];
2020-04-01 12:58:30 +02:00
for (let t of selectionTypes) {
let inputTypes = options[`${t}Selection`];
if (
inputTypes === true ||
(typeof inputTypes === "string" &&
inputTypes.split(",").indexOf(inputType) > -1)
) {
2020-04-01 12:58:30 +02:00
type = t;
break;
}
}
2020-04-01 12:58:30 +02:00
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-01 12:58:30 +02:00
let params = {
type: inputType,
pairs: A([
newPair(
inputType,
Object.assign({}, options, { index: 0, pairCount: 1 })
),
]),
};
2020-04-06 10:36:38 +02:00
if (count > 0) {
params.connector = options.inputConnector;
}
if (
["conditional", "assignment"].indexOf(inputType) > -1 ||
options.outputDefaultSelection ||
options.outputConnector
) {
params["output_type"] = defaultSelectionType("output", options);
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,
newPair,
};