1
0
Fork 0
discourse-custom-wizard-unl.../assets/javascripts/discourse/lib/wizard.js.es6
Angus McLeod af3e61fe75
Add custom field improvements (#115)
* Add custom field improvements

This PR does a few things to improve our support of custom fields
1. Adds custom fields added by other plugins to the list in admin/wizards/custom-fields and the custom field list in the mapper selector
2. Adds support for json custom fields in the wizard actions

* Make eslint happy

* Make prettier happy

* Make rubocop happy

* Make ember template lint happy

* Don't assume we have the context in the selector

* Ensure custom fields don't require optional attributes (with tests)
2021-06-08 17:09:49 +05:30

124 Zeilen
2,5 KiB
JavaScript

import EmberObject from "@ember/object";
import wizardSchema from "./wizard-schema";
function selectKitContent(content) {
return content.map((i) => ({ id: i, name: i }));
}
function generateName(id) {
return id ? sentenceCase(id) : "";
}
function generateId(name) {
return name ? snakeCase(name) : "";
}
function sentenceCase(string) {
return string
.replace(/[_\-]+/g, " ")
.toLowerCase()
.replace(/(^\w|\b\w)/g, (m) => m.toUpperCase());
}
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("_");
}
function camelCase(string) {
return string.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace("-", "").replace("_", "");
});
}
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",
];
const notificationLevels = [
"regular",
"watching",
"tracking",
"watching_first_post",
"muted",
];
function listProperties(itemType, opts = {}) {
let properties = Object.keys(wizardSchema[itemType].basic);
const types = wizardSchema[itemType].types;
if (types) {
let typeProperties = [];
if (opts.allTypes) {
Object.keys(types).forEach((type) => {
typeProperties = typeProperties.concat(Object.keys(types[type]));
});
} else if (opts.objectType && types[opts.objectType]) {
typeProperties = Object.keys(types[opts.objectType]);
}
properties = properties.concat(typeProperties);
}
return properties;
}
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,
label: `${field.label} (${field.id})`,
type: field.type,
});
})
);
}
}
return result;
}, []);
}
export {
selectKitContent,
generateName,
generateId,
camelCase,
snakeCase,
userProperties,
listProperties,
notificationLevels,
wizardFieldList,
sentenceCase,
};