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

124 Zeilen
2,5 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) {
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) {
return id ? sentenceCase(id) : "";
2020-04-02 10:21:03 +02:00
}
2021-04-12 07:44:47 +02:00
function generateId(name) {
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())
.join("_");
2020-04-02 10:21:03 +02:00
}
function camelCase(string) {
return string.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace("-", "").replace("_", "");
2020-04-02 10:21:03 +02:00
});
2020-04-01 07:03:26 +02:00
}
2020-04-10 09:57:49 +02:00
const userProperties = [
"name",
"username",
"email",
"avatar",
"date_of_birth",
"title",
"profile_background",
"card_background",
"locale",
"location",
"website",
"bio_raw",
"trust_level",
"email_level",
"email_messages_level",
"email_digests",
2020-03-21 18:30:11 +01:00
];
const notificationLevels = [
"regular",
"watching",
"tracking",
"watching_first_post",
"muted",
];
2021-04-12 07:44:47 +02:00
function listProperties(itemType, opts = {}) {
let properties = Object.keys(wizardSchema[itemType].basic);
2021-04-12 07:44:47 +02:00
const types = wizardSchema[itemType].types;
2020-04-20 11:41:13 +02:00
if (types) {
let typeProperties = [];
2020-04-20 11:41:13 +02:00
if (opts.allTypes) {
Object.keys(types).forEach((type) => {
2020-04-20 11:41:13 +02:00
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]);
}
2020-04-20 11:41:13 +02:00
properties = properties.concat(typeProperties);
2020-04-10 09:57:49 +02:00
}
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;
2020-04-14 07:46:06 +02:00
if (opts.upTo) {
upToIndex = steps.map((s) => s.id).indexOf(opts.upTo);
2020-04-14 07:46:06 +02:00
}
2020-04-14 07:46:06 +02:00
return steps.reduce((result, step, index) => {
let fields = step.fields;
2020-04-14 07:46:06 +02:00
if (fields && fields.length > 0) {
if (upToIndex === null || index < upToIndex) {
result.push(
...fields.map((field) => {
return EmberObject.create({
id: field.id,
label: `${field.label} (${field.id})`,
type: field.type,
});
})
);
2020-04-14 07:46:06 +02:00
}
}
2020-04-14 07:46:06 +02:00
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,
wizardFieldList,
};