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

119 Zeilen
2,4 KiB
Text

2020-04-14 07:46:06 +02:00
import EmberObject from "@ember/object";
import wizardSchema from './wizard-schema';
2020-04-14 07:46:06 +02:00
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
];
const notificationLevels = [
'regular',
'watching',
'tracking',
'watching_first_post',
'muted'
];
2020-04-20 11:41:13 +02:00
function listProperties(type, opts={}) {
let properties = Object.keys(wizardSchema[type].basic);
2020-04-20 11:41:13 +02:00
const types = wizardSchema[type].types;
if (types) {
let typeProperties = [];
if (opts.allTypes) {
Object.keys(types).forEach(type => {
typeProperties = typeProperties.concat(Object.keys(types[type]));
});
2020-04-29 07:24:33 +02:00
} else if (opts.objectType && types[opts.objectType]) {
2020-04-20 11:41:13 +02:00
typeProperties = Object.keys(types[opts.objectType]);
}
properties = properties.concat(typeProperties);
2020-04-10 09:57:49 +02:00
}
return properties;
2020-04-07 06:56:16 +02:00
}
2020-04-14 07:46:06 +02:00
function wizardFieldList(steps = [], opts = {}) {
let upToIndex = null;
if (opts.upTo) {
upToIndex = steps.map((s) => (s.id)).indexOf(opts.upTo);
}
return steps.reduce((result, step, index) => {
let fields = step.fields;
if (fields && fields.length > 0) {
if (upToIndex === null || index < upToIndex) {
result.push(...fields.map((field) => {
return EmberObject.create({
id: field.id,
2020-04-16 05:34:49 +02:00
label: `${field.label} (${field.id})`,
2020-04-14 07:46:06 +02:00
type: field.type
});
}));
}
}
return result;
}, []);
}
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
userProperties,
2020-04-14 07:46:06 +02:00
listProperties,
notificationLevels,
2020-04-14 07:46:06 +02:00
wizardFieldList
2020-03-21 18:30:11 +01:00
};