0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-15 22:32:54 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6

338 Zeilen
8,3 KiB
Text

2021-04-12 07:10:02 +02:00
import { alias, gt, or } from "@ember/object/computed";
2020-03-24 10:35:46 +01:00
import { computed } from "@ember/object";
import {
default as discourseComputed,
2021-04-12 08:12:20 +02:00
observes,
} from "discourse-common/utils/decorators";
import { getOwner } from "discourse-common/lib/get-owner";
import { defaultSelectionType, selectionTypes } from "../lib/wizard-mapper";
2021-04-12 07:10:02 +02:00
import { generateName, snakeCase, userProperties } from "../lib/wizard";
2020-04-05 03:37:09 +02:00
import Component from "@ember/component";
2020-04-15 07:22:21 +02:00
import { bind, later } from "@ember/runloop";
2020-05-28 05:06:06 +02:00
import I18n from "I18n";
2020-03-24 10:35:46 +01:00
2020-04-05 03:37:09 +02:00
export default Component.extend({
classNameBindings: [":mapper-selector", "activeType"],
showText: computed("activeType", function () {
return this.showInput("text");
}),
showWizardField: computed("activeType", function () {
return this.showInput("wizardField");
}),
showWizardAction: computed("activeType", function () {
return this.showInput("wizardAction");
}),
showUserField: computed("activeType", function () {
return this.showInput("userField");
}),
showUserFieldOptions: computed("activeType", function () {
return this.showInput("userFieldOptions");
}),
showCategory: computed("activeType", function () {
return this.showInput("category");
}),
showTag: computed("activeType", function () {
return this.showInput("tag");
}),
showGroup: computed("activeType", function () {
return this.showInput("group");
}),
showUser: computed("activeType", function () {
return this.showInput("user");
}),
showList: computed("activeType", function () {
return this.showInput("list");
}),
showCustomField: computed("activeType", function () {
return this.showInput("customField");
}),
textEnabled: computed("options.textSelection", "inputType", function () {
return this.optionEnabled("textSelection");
}),
wizardFieldEnabled: computed(
"options.wizardFieldSelection",
"inputType",
function () {
return this.optionEnabled("wizardFieldSelection");
}
),
wizardActionEnabled: computed(
"options.wizardActionSelection",
"inputType",
function () {
return this.optionEnabled("wizardActionSelection");
}
),
customFieldEnabled: computed(
"options.customFieldSelection",
"inputType",
function () {
return this.optionEnabled("customFieldSelection");
}
),
userFieldEnabled: computed(
"options.userFieldSelection",
"inputType",
function () {
return this.optionEnabled("userFieldSelection");
}
),
userFieldOptionsEnabled: computed(
"options.userFieldOptionsSelection",
"inputType",
function () {
return this.optionEnabled("userFieldOptionsSelection");
}
),
categoryEnabled: computed(
"options.categorySelection",
"inputType",
function () {
return this.optionEnabled("categorySelection");
}
),
tagEnabled: computed("options.tagSelection", "inputType", function () {
return this.optionEnabled("tagSelection");
}),
groupEnabled: computed("options.groupSelection", "inputType", function () {
return this.optionEnabled("groupSelection");
}),
userEnabled: computed("options.userSelection", "inputType", function () {
return this.optionEnabled("userSelection");
}),
listEnabled: computed("options.listSelection", "inputType", function () {
return this.optionEnabled("listSelection");
}),
groups: alias("site.groups"),
categories: alias("site.categories"),
showComboBox: or(
"showWizardField",
"showWizardAction",
"showUserField",
"showUserFieldOptions",
"showCustomField"
),
showMultiSelect: or("showCategory", "showGroup"),
hasTypes: gt("selectorTypes.length", 1),
2020-04-13 14:17:22 +02:00
showTypes: false,
2020-04-06 03:54:16 +02:00
didInsertElement() {
if (
!this.activeType ||
(this.activeType && !this[`${this.activeType}Enabled`])
) {
2020-04-15 07:22:21 +02:00
later(() => this.resetActiveType());
}
2020-04-06 03:54:16 +02:00
$(document).on("click", bind(this, this.documentClick));
},
willDestroyElement() {
$(document).off("click", bind(this, this.documentClick));
},
documentClick(e) {
2021-04-12 08:12:20 +02:00
if (this._state === "destroying") {
return;
}
2020-04-06 03:54:16 +02:00
let $target = $(e.target);
if (!$target.parents(".type-selector").length && this.showTypes) {
this.set("showTypes", false);
2020-04-06 03:54:16 +02:00
}
2020-04-05 03:37:09 +02:00
},
2020-03-29 09:49:33 +02:00
@discourseComputed
2020-04-06 03:54:16 +02:00
selectorTypes() {
return selectionTypes
.filter((type) => this[`${type}Enabled`])
.map((type) => ({ type, label: this.typeLabel(type) }));
2020-04-01 07:03:26 +02:00
},
@discourseComputed("activeType")
2020-04-06 03:54:16 +02:00
activeTypeLabel(activeType) {
return this.typeLabel(activeType);
2020-03-29 09:49:33 +02:00
},
2020-04-06 03:54:16 +02:00
typeLabel(type) {
return type
? I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`)
: null;
2020-04-06 03:54:16 +02:00
},
comboBoxAllowAny: or("showWizardField", "showWizardAction"),
@discourseComputed
showController() {
return getOwner(this).lookup("controller:admin-wizards-wizard-show");
},
@discourseComputed(
"activeType",
"showController.wizardFields.[]",
"showController.wizard.actions.[]",
"showController.userFields.[]",
"showController.currentField.id",
"showController.currentAction.id",
"showController.customFields"
)
comboBoxContent(
activeType,
wizardFields,
wizardActions,
userFields,
currentFieldId,
2020-10-20 07:40:23 +02:00
currentActionId,
customFields
) {
let content;
if (activeType === "wizardField") {
content = wizardFields;
if (this.options.context === "field") {
content = content.filter((field) => field.id !== currentFieldId);
}
}
if (activeType === "wizardAction") {
content = wizardActions.map((a) => ({
id: a.id,
label: `${generateName(a.type)} (${a.id})`,
type: a.type,
}));
if (this.options.context === "action") {
content = content.filter((a) => a.id !== currentActionId);
}
2020-04-10 10:51:01 +02:00
}
if (activeType === "userField") {
content = userProperties
.map((f) => ({
id: f,
name: generateName(f),
}))
.concat(userFields || []);
if (
this.options.context === "action" &&
this.inputType === "association" &&
this.selectorType === "key"
) {
const excludedFields = ["username", "email", "trust_level"];
content = content.filter(
(userField) => excludedFields.indexOf(userField.id) === -1
);
}
}
if (activeType === "userFieldOptions") {
content = userFields;
2020-04-06 03:54:16 +02:00
}
if (activeType === "customField") {
2020-10-20 07:40:23 +02:00
content = customFields;
}
2020-04-06 03:54:16 +02:00
return content;
2020-03-24 10:35:46 +01:00
},
@discourseComputed("activeType")
2020-04-05 03:37:09 +02:00
multiSelectContent(activeType) {
return {
category: this.categories,
group: this.groups,
list: "",
2020-04-05 03:37:09 +02:00
}[activeType];
2020-03-24 10:35:46 +01:00
},
@discourseComputed("activeType", "inputType")
2021-04-12 07:44:47 +02:00
placeholderKey(activeType) {
if (
activeType === "text" &&
this.options[`${this.selectorType}Placeholder`]
) {
2020-04-05 03:37:09 +02:00
return this.options[`${this.selectorType}Placeholder`];
2020-04-06 03:54:16 +02:00
} else {
return `admin.wizard.selector.placeholder.${snakeCase(activeType)}`;
}
2020-04-05 03:37:09 +02:00
},
@discourseComputed("activeType")
2020-04-05 03:37:09 +02:00
multiSelectOptions(activeType) {
let result = {
none: this.placeholderKey,
2020-04-05 03:37:09 +02:00
};
if (activeType === "list") {
2020-04-05 03:37:09 +02:00
result.allowAny = true;
}
2020-04-05 03:37:09 +02:00
return result;
},
2020-03-24 10:35:46 +01:00
optionEnabled(type) {
const options = this.options;
2021-04-12 08:12:20 +02:00
if (!options) {
return false;
}
2020-03-24 10:35:46 +01:00
const option = options[type];
2021-04-12 08:12:20 +02:00
if (option === true) {
return true;
}
if (typeof option !== "string") {
return false;
}
2021-04-12 07:44:47 +02:00
return option.split(",").filter((o) => {
return [this.selectorType, this.inputType].indexOf(o) !== -1;
2020-04-05 03:37:09 +02:00
}).length;
2020-03-24 10:35:46 +01:00
},
2020-04-05 03:37:09 +02:00
showInput(type) {
return this.activeType === type && this[`${type}Enabled`];
},
changeValue(value) {
this.set("value", value);
this.onUpdate("selector", this.activeType);
},
@observes("inputType")
resetActiveType() {
this.set(
"activeType",
defaultSelectionType(this.selectorType, this.options)
);
2020-04-20 11:41:13 +02:00
},
2020-03-24 10:35:46 +01:00
actions: {
toggleType(type) {
this.set("activeType", type);
this.set("showTypes", false);
this.set("value", null);
this.onUpdate("selector");
2020-04-06 03:54:16 +02:00
},
2020-04-13 14:17:22 +02:00
toggleTypes() {
this.toggleProperty("showTypes");
},
changeValue(value) {
this.changeValue(value);
},
changeInputValue(event) {
this.changeValue(event.target.value);
},
changeUserValue(previousValue, value) {
this.changeValue(value);
},
},
});