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/custom-wizard.js.es6

196 Zeilen
3,8 KiB
Text

2020-02-02 11:42:05 +01:00
function generateSelectKitContent(content) {
return content.map(i => ({id: i, name: i}))
}
2020-03-21 18:30:11 +01:00
function generateName(id) {
return id.replace(/[_\-]+/g, ' ')
.toLowerCase()
.replace(/(^\w|\b\w)/g, (m) => m.toUpperCase())
}
2020-04-01 07:03:26 +02:00
function generateId(name) {
return name.replace(/[^\w ]/g, '')
.replace(/ /g,"_")
.toLowerCase();
}
2020-03-21 18:30:11 +01:00
const profileFields = [
'name',
2020-03-30 08:16:03 +02:00
'username',
'email',
2020-03-21 18:30:11 +01:00
'date_of_birth',
'title',
'locale',
'location',
'website',
'bio_raw',
2020-03-30 08:16:03 +02:00
'trust_level'
2020-03-21 18:30:11 +01:00
];
const actionTypes = [
'create_topic',
'update_profile',
'create_topic',
'update_profile',
'send_message',
'send_to_api',
'add_to_group',
'route_to',
'open_composer'
];
2020-04-01 07:03:26 +02:00
// Inputs
2020-03-24 10:35:46 +01:00
2020-04-01 07:03:26 +02:00
const selectableInputTypes = [
2020-03-24 10:35:46 +01:00
'conditional',
'assignment'
]
function defaultInputType(options = {}) {
2020-03-30 08:16:03 +02:00
if (!options.hasOutput) return 'pair';
2020-04-01 07:03:26 +02:00
if (!options.inputTypes) return selectableInputTypes[0];
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-03-24 10:35:46 +01:00
}
2020-04-01 07:03:26 +02:00
// Connectors
const connectors = {
output: [
'then',
'set',
],
pair: [
'equal',
'greater',
'less',
'greater_or_equal',
'less_or_equal'
]
}
function connectorItem(connector) {
return {
id: connector,
name: I18n.t(`admin.wizard.connector.${connector}`)
};
}
function defaultConnector(connectorType, inputType, opts = {}) {
if (opts[`${connectorType}Connector`]) return opts[`${connectorType}Connector`];
if (inputType === 'assignment') return 'set';
return connectorType === 'output' ? 'then' : 'equal';
}
function connectorContent(connectorType, inputType, opts) {
let connector = opts[`${connectorType}Connector`] || defaultConnector(connectorType, inputType, opts);
if (connector) return [connectorItem(connector)];
return connectors[connectorType].map(function(connector) {
return connectorItem(connector);
});
}
// Selectors
const selectionTypes = [
'text',
'wizard',
'user',
'group',
'category',
'tag'
]
2020-03-24 10:35:46 +01:00
function defaultSelectionType(inputType, options = {}) {
2020-04-01 07:03:26 +02:00
if (options[`${inputType}DefaultSelection`]) {
return options[`${inputType}DefaultSelection`];
2020-03-31 10:30:53 +02:00
}
2020-03-24 10:35:46 +01:00
2020-04-01 07:03:26 +02:00
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)) {
type = t;
break;
2020-03-24 10:35:46 +01:00
}
}
2020-04-01 07:03:26 +02:00
2020-03-24 10:35:46 +01:00
return type;
}
2020-04-01 07:03:26 +02:00
// 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)
}
return Ember.Object.create(params);
}
2020-03-21 18:30:11 +01:00
function newInput(options = {}) {
2020-04-01 07:03:26 +02:00
const inputType = defaultInputType(options);
2020-03-24 10:35:46 +01:00
let params = {
2020-04-01 07:03:26 +02:00
type: inputType,
2020-03-31 10:30:53 +02:00
pairs: Ember.A(
[
newPair(
2020-04-01 07:03:26 +02:00
inputType,
Object.assign({},
2020-03-31 10:30:53 +02:00
options,
{ index: 0, pairCount: 1 }
)
)
]
)
2020-03-21 18:30:11 +01:00
}
if (options.hasOutput) {
2020-03-24 10:35:46 +01:00
params['output_type'] = defaultSelectionType('output', options);
2020-04-01 07:03:26 +02:00
params['connector'] = defaultConnector('output', inputType, options);
2020-03-21 18:30:11 +01:00
}
2020-04-01 07:03:26 +02:00
2020-03-21 18:30:11 +01:00
return Ember.Object.create(params);
}
2020-04-01 07:03:26 +02:00
//
2020-03-30 01:53:28 +02:00
2020-03-21 18:30:11 +01:00
export {
generateSelectKitContent,
profileFields,
actionTypes,
generateName,
2020-03-24 10:35:46 +01:00
defaultInputType,
defaultSelectionType,
2020-04-01 07:03:26 +02:00
connectorContent,
inputTypesContent,
2020-03-21 18:30:11 +01:00
newInput,
2020-03-30 01:53:28 +02:00
newPair,
generateId
2020-03-21 18:30:11 +01:00
};