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

302 Zeilen
4,9 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-04-10 09:57:49 +02:00
const userProperties = [
2020-03-21 18:30:11 +01:00
'name',
2020-04-10 10:51:01 +02:00
'username',
2020-03-30 08:16:03 +02:00
'email',
2020-04-07 15:33:11 +02:00
'avatar',
2020-03-21 18:30:11 +01:00
'date_of_birth',
'title',
2020-04-07 16:18:12 +02:00
'profile_background',
'card_background',
2020-03-21 18:30:11 +01:00
'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-10 09:57:49 +02:00
const wizardProperties = {
basic: {
id: null,
name: null,
background: null,
save_submissions: true,
multiple_submissions: null,
after_signup: null,
after_time: null,
after_time_scheduled: null,
required: null,
prompt_completion: null,
restart_on_revisit: null,
theme_id: null,
permitted: null
},
mapped: [
'permitted'
2020-04-08 09:59:54 +02:00
],
2020-04-10 09:57:49 +02:00
advanced: [
'restart_on_revisit',
2020-04-08 09:59:54 +02:00
],
2020-04-10 09:57:49 +02:00
required: [
2020-04-08 09:59:54 +02:00
'id',
],
2020-04-10 09:57:49 +02:00
dependent: {
after_time: 'after_time_scheduled'
},
objectArrays: {
step: {
property: 'steps',
required: false
},
action: {
property: 'actions',
required: false
}
}
};
2020-04-08 04:52:07 +02:00
2020-04-10 09:57:49 +02:00
const stepProperties = {
basic: {
id: null,
title: null,
key: null,
banner: null,
raw_description: null,
required_data: null,
required_data_message: null,
permitted_params: null
},
mapped: [
'required_data',
'permitted_params'
2020-04-02 07:21:57 +02:00
],
2020-04-10 09:57:49 +02:00
advanced: [
2020-04-02 07:21:57 +02:00
'required_data',
'permitted_params'
],
2020-04-10 09:57:49 +02:00
required: [
'id'
],
dependent: {
},
objectArrays: {
field: {
property: 'fields',
required: false
}
}
}
const fieldProperties = {
basic: {
id: null,
label: null,
image: null,
description: null,
required: null,
key: null,
2020-04-11 08:22:12 +02:00
type: null
2020-04-10 09:57:49 +02:00
},
types: {
text: {
min_length: null
},
textarea: {
min_length: null
},
composer: {
min_length: null
},
2020-04-11 08:22:12 +02:00
text_only: {
},
2020-04-10 09:57:49 +02:00
number: {
},
2020-04-11 08:22:12 +02:00
checkbox: {
},
2020-04-10 09:57:49 +02:00
url: {
min_length: null
},
upload: {
file_types: '.jpg,.png'
},
dropdown: {
prefill: null,
content: null
},
tag: {
limit: null,
prefill: null,
content: null
},
category: {
limit: 1,
property: 'id',
prefill: null,
content: null
},
group: {
prefill: null,
content: null
2020-04-11 08:22:12 +02:00
},
user_selector: {
2020-04-10 09:57:49 +02:00
}
},
mapped: [
2020-04-02 07:21:57 +02:00
'prefill',
'content'
],
2020-04-10 09:57:49 +02:00
advanced: [
'prefill',
'content',
'property'
],
required: [
'id',
'type'
],
dependent: {
},
objectArrays: {
}
}
const actionProperties = {
basic: {
id: null,
run_after: 'wizard_completion',
2020-04-11 08:22:12 +02:00
type: null
2020-04-10 09:57:49 +02:00
},
types: {
create_topic: {
title: null,
post: null,
post_builder: null,
post_template: null,
category: null,
tags: null,
custom_fields: null,
skip_redirect: null
},
send_message: {
title: null,
post: null,
post_builder: null,
post_template: null,
skip_redirect: null,
custom_fields: null,
required: null,
recipient: null
},
open_composer: {
title: null,
post: null,
post_builder: null,
post_template: null,
category: null,
tags: null,
custom_fields: null
},
update_profile: {
profile_updates: null,
custom_fields: null
},
add_to_group: {
group: null
},
route_to: {
url: null,
code: null
}
},
mapped: [
2020-04-02 07:21:57 +02:00
'title',
'category',
'tags',
'custom_fields',
'required',
'recipient',
'profile_updates',
'group'
2020-04-10 09:57:49 +02:00
],
advanced: [
'code',
'custom_fields',
'skip_redirect',
'required'
],
required: [
'id',
'type'
],
dependent: {
},
objectArrays: {
}
2020-04-02 07:21:57 +02:00
}
2020-04-10 09:57:49 +02:00
if (Discourse.SiteSettings.wizard_api_features) {
actionProperties.types.send_to_api = {
api: null,
api_endpoint: null,
api_body: null
2020-04-08 09:59:54 +02:00
}
}
2020-04-10 09:57:49 +02:00
const schema = {
wizard: wizardProperties,
step: stepProperties,
field: fieldProperties,
action: actionProperties
}
2020-03-21 18:30:11 +01:00
2020-04-10 09:57:49 +02:00
function listProperties(type, objectType = null) {
let properties = Object.keys(schema[type].basic);
2020-04-11 08:22:12 +02:00
2020-04-10 09:57:49 +02:00
if (schema[type].types && objectType) {
properties = properties.concat(Object.keys(schema[type].types[objectType]));
}
return properties;
2020-04-07 06:56:16 +02:00
}
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-10 09:57:49 +02:00
schema,
userProperties,
listProperties
2020-03-21 18:30:11 +01:00
};