2020-04-14 07:46:06 +02:00
|
|
|
import EmberObject from "@ember/object";
|
2021-03-28 11:06:49 +02:00
|
|
|
import wizardSchema from "./wizard-schema";
|
2020-04-14 07:46:06 +02:00
|
|
|
|
2020-04-02 10:21:03 +02:00
|
|
|
function selectKitContent(content) {
|
2021-03-28 11:06:49 +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) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return id ? sentenceCase(id) : "";
|
2020-04-02 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
function generateId(name, opts = {}) {
|
|
|
|
return name ? snakeCase(name) : "";
|
2020-04-02 10:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function sentenceCase(string) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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 = [
|
2021-03-28 11:06:49 +02:00
|
|
|
"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
|
|
|
];
|
|
|
|
|
2020-05-25 15:59:31 +02:00
|
|
|
const notificationLevels = [
|
2021-03-28 11:06:49 +02:00
|
|
|
"regular",
|
|
|
|
"watching",
|
|
|
|
"tracking",
|
|
|
|
"watching_first_post",
|
|
|
|
"muted",
|
2020-05-25 15:59:31 +02:00
|
|
|
];
|
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
function listProperties(type, opts = {}) {
|
2020-04-16 04:04:27 +02:00
|
|
|
let properties = Object.keys(wizardSchema[type].basic);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
const types = wizardSchema[type].types;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
if (types) {
|
|
|
|
let typeProperties = [];
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
if (opts.allTypes) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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]);
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
properties = properties.concat(typeProperties);
|
2020-04-10 09:57:49 +02:00
|
|
|
}
|
2021-03-28 11:06: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;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-14 07:46:06 +02:00
|
|
|
if (opts.upTo) {
|
2021-03-28 11:06:49 +02:00
|
|
|
upToIndex = steps.map((s) => s.id).indexOf(opts.upTo);
|
2020-04-14 07:46:06 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-14 07:46:06 +02:00
|
|
|
return steps.reduce((result, step, index) => {
|
|
|
|
let fields = step.fields;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-14 07:46:06 +02:00
|
|
|
if (fields && fields.length > 0) {
|
|
|
|
if (upToIndex === null || index < upToIndex) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-03-28 11:06:49 +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,
|
2020-05-25 15:59:31 +02:00
|
|
|
notificationLevels,
|
2021-03-28 11:06:49 +02:00
|
|
|
wizardFieldList,
|
2020-07-02 11:33:58 +02:00
|
|
|
};
|