2020-04-06 03:54:16 +02:00
|
|
|
import { alias, or, gt } from "@ember/object/computed";
|
2020-03-24 10:35:46 +01:00
|
|
|
import { computed } from "@ember/object";
|
2021-02-24 08:43:35 +01:00
|
|
|
import {
|
|
|
|
default as discourseComputed,
|
|
|
|
observes,
|
|
|
|
on,
|
|
|
|
} from "discourse-common/utils/decorators";
|
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
|
|
|
import { defaultSelectionType, selectionTypes } from "../lib/wizard-mapper";
|
|
|
|
import { snakeCase, generateName, 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({
|
2021-02-24 08:43:35 +01:00
|
|
|
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,
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-06 03:54:16 +02:00
|
|
|
didInsertElement() {
|
2021-02-24 08:43:35 +01:00
|
|
|
if (
|
|
|
|
!this.activeType ||
|
|
|
|
(this.activeType && !this[`${this.activeType}Enabled`])
|
|
|
|
) {
|
2020-04-15 07:22:21 +02:00
|
|
|
later(() => this.resetActiveType());
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
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) {
|
2020-04-11 08:22:12 +02:00
|
|
|
if (this._state == "destroying") return;
|
2020-04-06 03:54:16 +02:00
|
|
|
let $target = $(e.target);
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
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
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-03-29 09:49:33 +02:00
|
|
|
@discourseComputed
|
2020-04-06 03:54:16 +02:00
|
|
|
selectorTypes() {
|
2021-02-24 08:43:35 +01:00
|
|
|
return selectionTypes
|
|
|
|
.filter((type) => this[`${type}Enabled`])
|
|
|
|
.map((type) => ({ type, label: this.typeLabel(type) }));
|
2020-04-01 07:03:26 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01: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
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-06 03:54:16 +02:00
|
|
|
typeLabel(type) {
|
2021-02-24 08:43:35 +01:00
|
|
|
return type
|
|
|
|
? I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`)
|
|
|
|
: null;
|
2020-04-06 03:54:16 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
comboBoxAllowAny: or("showWizardField", "showWizardAction"),
|
|
|
|
|
2020-07-17 09:02:48 +02:00
|
|
|
@discourseComputed
|
|
|
|
showController() {
|
2021-02-24 08:43:35 +01:00
|
|
|
return getOwner(this).lookup("controller:admin-wizards-wizard-show");
|
2020-07-17 09:02:48 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-07-17 09:02:48 +02:00
|
|
|
@discourseComputed(
|
2021-02-24 08:43:35 +01:00
|
|
|
"activeType",
|
|
|
|
"showController.wizardFields.[]",
|
|
|
|
"showController.wizard.actions.[]",
|
|
|
|
"showController.userFields.[]",
|
|
|
|
"showController.currentField.id",
|
|
|
|
"showController.currentAction.id",
|
|
|
|
"showController.customFields"
|
2020-07-17 09:02:48 +02:00
|
|
|
)
|
|
|
|
comboBoxContent(
|
|
|
|
activeType,
|
|
|
|
wizardFields,
|
|
|
|
wizardActions,
|
|
|
|
userFields,
|
|
|
|
currentFieldId,
|
2020-10-20 07:40:23 +02:00
|
|
|
currentActionId,
|
|
|
|
customFields
|
2020-07-17 09:02:48 +02:00
|
|
|
) {
|
2020-04-19 13:02:14 +02:00
|
|
|
let content;
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (activeType === "wizardField") {
|
2020-04-19 13:02:14 +02:00
|
|
|
content = wizardFields;
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (this.options.context === "field") {
|
|
|
|
content = content.filter((field) => field.id !== currentFieldId);
|
2020-07-17 09:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (activeType === "wizardAction") {
|
|
|
|
content = wizardActions.map((a) => ({
|
2020-07-17 09:02:48 +02:00
|
|
|
id: a.id,
|
|
|
|
label: `${generateName(a.type)} (${a.id})`,
|
2021-02-24 08:43:35 +01:00
|
|
|
type: a.type,
|
2020-07-17 09:02:48 +02:00
|
|
|
}));
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (this.options.context === "action") {
|
|
|
|
content = content.filter((a) => a.id !== currentActionId);
|
2020-04-19 13:02:14 +02:00
|
|
|
}
|
2020-04-10 10:51:01 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01: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
|
|
|
|
);
|
2020-04-19 13:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (activeType === "userFieldOptions") {
|
2020-04-19 13:02:14 +02:00
|
|
|
content = userFields;
|
2020-04-06 03:54:16 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (activeType === "customField") {
|
2020-10-20 07:40:23 +02:00
|
|
|
content = customFields;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-06 03:54:16 +02:00
|
|
|
return content;
|
2020-03-24 10:35:46 +01:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
@discourseComputed("activeType")
|
2020-04-05 03:37:09 +02:00
|
|
|
multiSelectContent(activeType) {
|
|
|
|
return {
|
|
|
|
category: this.categories,
|
|
|
|
group: this.groups,
|
2021-02-24 08:43:35 +01:00
|
|
|
list: "",
|
2020-04-05 03:37:09 +02:00
|
|
|
}[activeType];
|
2020-03-24 10:35:46 +01:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
@discourseComputed("activeType", "inputType")
|
2020-04-06 03:54:16 +02:00
|
|
|
placeholderKey(activeType, inputType) {
|
2021-02-24 08:43:35 +01:00
|
|
|
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)}`;
|
2021-02-24 08:43:35 +01:00
|
|
|
}
|
2020-04-05 03:37:09 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
@discourseComputed("activeType")
|
2020-04-05 03:37:09 +02:00
|
|
|
multiSelectOptions(activeType) {
|
|
|
|
let result = {
|
2021-02-24 08:43:35 +01:00
|
|
|
none: this.placeholderKey,
|
2020-04-05 03:37:09 +02:00
|
|
|
};
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
if (activeType === "list") {
|
2020-04-05 03:37:09 +02:00
|
|
|
result.allowAny = true;
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-05 03:37:09 +02:00
|
|
|
return result;
|
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-03-24 10:35:46 +01:00
|
|
|
optionEnabled(type) {
|
|
|
|
const options = this.options;
|
|
|
|
if (!options) return false;
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-03-24 10:35:46 +01:00
|
|
|
const option = options[type];
|
|
|
|
if (option === true) return true;
|
2021-02-24 08:43:35 +01:00
|
|
|
if (typeof option !== "string") return false;
|
|
|
|
|
|
|
|
return option.split(",").filter((option) => {
|
2020-04-05 03:37:09 +02:00
|
|
|
return [this.selectorType, this.inputType].indexOf(option) !== -1;
|
|
|
|
}).length;
|
2020-03-24 10:35:46 +01:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-05 03:37:09 +02:00
|
|
|
showInput(type) {
|
|
|
|
return this.activeType === type && this[`${type}Enabled`];
|
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-29 03:42:39 +02:00
|
|
|
changeValue(value) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("value", value);
|
|
|
|
this.onUpdate("selector", this.activeType);
|
2020-04-29 03:42:39 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
|
|
|
@observes("inputType")
|
2020-04-29 03:42:39 +02:00
|
|
|
resetActiveType() {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set(
|
|
|
|
"activeType",
|
|
|
|
defaultSelectionType(this.selectorType, this.options)
|
|
|
|
);
|
2020-04-20 11:41:13 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-03-24 10:35:46 +01:00
|
|
|
actions: {
|
|
|
|
toggleType(type) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("activeType", type);
|
|
|
|
this.set("showTypes", false);
|
|
|
|
this.set("value", null);
|
|
|
|
this.onUpdate("selector");
|
2020-04-06 03:54:16 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
toggleTypes() {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.toggleProperty("showTypes");
|
2020-04-29 03:42:39 +02:00
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-29 03:42:39 +02:00
|
|
|
changeValue(value) {
|
|
|
|
this.changeValue(value);
|
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-29 03:42:39 +02:00
|
|
|
changeInputValue(event) {
|
|
|
|
this.changeValue(event.target.value);
|
|
|
|
},
|
2021-02-24 08:43:35 +01:00
|
|
|
|
2020-04-29 03:42:39 +02:00
|
|
|
changeUserValue(previousValue, value) {
|
|
|
|
this.changeValue(value);
|
2021-02-24 08:43:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|