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

202 Zeilen
3,5 KiB
Text

2020-04-02 10:21:03 +02:00
function selectKitContent(content) {
2020-04-05 03:37:09 +02:00
return content.map(i => ({id: i, name: i}));
2020-02-02 11:42:05 +01:00
}
2020-03-21 18:30:11 +01:00
function generateName(id) {
2020-04-03 00:47:32 +02:00
return id ? sentenceCase(id) : '';
2020-04-02 10:21:03 +02:00
}
2020-04-06 03:54:16 +02:00
function generateId(name, opts={}) {
2020-04-03 00:47:32 +02:00
return name ? snakeCase(name) : '';
2020-04-02 10:21:03 +02:00
}
function sentenceCase(string) {
return string.replace(/[_\-]+/g, ' ')
2020-03-21 18:30:11 +01:00
.toLowerCase()
2020-04-05 03:37:09 +02:00
.replace(/(^\w|\b\w)/g, (m) => m.toUpperCase());
2020-03-21 18:30:11 +01:00
}
2020-04-02 10:21:03 +02:00
function snakeCase(string) {
return string.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
2020-04-05 03:37:09 +02:00
.join('_');
2020-04-02 10:21:03 +02:00
}
function camelCase(string) {
return string.replace(/([-_][a-z])/ig, ($1) => {
return $1.toUpperCase()
.replace('-', '')
.replace('_', '');
});
2020-04-01 07:03:26 +02:00
}
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
];
2020-04-01 12:58:30 +02:00
const wizardProperties = [
'id',
'name',
'background',
'save_submissions',
'multiple_submissions',
'after_signup',
'after_time',
'after_time_scheduled',
'required',
'prompt_completion',
'restart_on_revisit',
'theme_id',
'permitted'
];
const stepProperties = [
'id',
'title',
'key',
'banner',
'raw_description',
'required_data',
'required_data_message',
'permitted_params'
]
const fieldProperties = [
'id',
'label',
'key',
'image',
'description',
'type',
'required',
'min_length',
'file_types',
'property',
'limit',
'prefill',
2020-04-07 06:56:16 +02:00
'content'
2020-04-01 12:58:30 +02:00
]
const actionProperties = [
'id',
'type',
'title',
'post',
2020-04-02 07:21:57 +02:00
'post_builder',
2020-04-01 12:58:30 +02:00
'post_template',
'category',
'tags',
'skip_redirect',
'custom_fields',
'required',
'recipient',
'profile_updates',
'group',
'url',
'code',
'api',
'api_endpoint',
'api_body'
]
const properties = {
wizard: wizardProperties,
step: stepProperties,
field: fieldProperties,
action: actionProperties
}
2020-04-02 07:21:57 +02:00
const mappedProperties = {
wizard: [
'permitted'
],
step: [
'required_data',
'permitted_params'
],
field: [
'prefill',
'content'
],
action: [
'title',
'category',
'tags',
'custom_fields',
'required',
'recipient',
'profile_updates',
'group'
]
}
2020-04-07 06:56:16 +02:00
const advancedFieldProperties = [
'prefill',
'content'
]
2020-04-02 11:29:22 +02:00
2020-03-21 18:30:11 +01:00
const actionTypes = [
'create_topic',
'update_profile',
'send_message',
'send_to_api',
'add_to_group',
'route_to',
'open_composer'
2020-04-06 03:54:16 +02:00
].filter(function(type) {
return Discourse.SiteSettings.wizard_api_features || type !== 'send_to_api';
});
2020-03-21 18:30:11 +01:00
2020-04-07 06:56:16 +02:00
const advancedProperties = {
step: [
'required_data',
'permitted_params'
],
field: advancedFieldProperties.reduce(
function(map, type) {
console.log(map, type);
map[type] = advancedFieldProperties;
if (type === 'category') {
map.push('property');
}
return map;
}, {}
),
action: actionTypes.reduce(
function(map, type) {
if (type === 'route_to') {
map[type] = ['code'];
} else if (['create_topic', 'send_message', 'open_composer'].indexOf(type) > -1) {
map[type] = ['custom_fields'];
} else if (['create_topic', 'send_message'].indexOf(type) > -1) {
map[type].push('skip_redirect');
} else if (type === 'send_message') {
map[type].push('required');
}
return map;
}, {}
)
}
2020-03-21 18:30:11 +01:00
export {
2020-04-02 10:21:03 +02:00
selectKitContent,
2020-03-21 18:30:11 +01:00
generateName,
2020-04-01 12:58:30 +02:00
generateId,
2020-04-02 10:21:03 +02:00
camelCase,
snakeCase,
2020-04-01 12:58:30 +02:00
properties,
wizardProperties,
2020-04-02 07:21:57 +02:00
mappedProperties,
2020-04-01 12:58:30 +02:00
profileFields,
2020-04-02 11:29:22 +02:00
advancedProperties,
2020-04-01 12:58:30 +02:00
actionTypes
2020-03-21 18:30:11 +01:00
};