2021-09-03 10:46:32 +02:00
|
|
|
import SingleSelectComponent from "select-kit/components/single-select";
|
2023-09-23 19:30:11 +02:00
|
|
|
import { inject as service } from "@ember/service";
|
2023-02-09 14:18:25 +01:00
|
|
|
import { filterValues } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
|
2022-03-25 12:18:54 +01:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2022-03-25 12:22:27 +01:00
|
|
|
import I18n from "I18n";
|
2021-09-03 10:46:32 +02:00
|
|
|
|
2022-03-25 12:22:27 +01:00
|
|
|
const nameKey = function (feature, attribute, value) {
|
|
|
|
if (feature === "action") {
|
2022-03-25 12:18:54 +01:00
|
|
|
return `admin.wizard.action.${value}.label`;
|
|
|
|
} else {
|
|
|
|
return `admin.wizard.${feature}.${attribute}.${value}`;
|
|
|
|
}
|
2022-03-25 12:22:27 +01:00
|
|
|
};
|
2022-03-25 12:18:54 +01:00
|
|
|
|
2023-09-23 19:30:11 +02:00
|
|
|
export default SingleSelectComponent.extend({
|
2021-09-24 11:58:42 +02:00
|
|
|
classNames: ["combo-box", "wizard-subscription-selector"],
|
2023-09-23 19:30:11 +02:00
|
|
|
subscription: service(),
|
2021-09-03 10:46:32 +02:00
|
|
|
|
|
|
|
selectKitOptions: {
|
|
|
|
autoFilterable: false,
|
|
|
|
filterable: false,
|
|
|
|
showFullTitle: true,
|
2021-10-19 14:49:06 +02:00
|
|
|
headerComponent:
|
|
|
|
"wizard-subscription-selector/wizard-subscription-selector-header",
|
2021-09-03 10:46:32 +02:00
|
|
|
caretUpIcon: "caret-up",
|
2021-09-07 14:13:01 +02:00
|
|
|
caretDownIcon: "caret-down",
|
2021-09-03 10:46:32 +02:00
|
|
|
},
|
|
|
|
|
2022-09-23 15:52:05 +02:00
|
|
|
allowedSubscriptionTypes(feature, attribute, value) {
|
2023-09-23 19:30:11 +02:00
|
|
|
let attributes = this.subscription.subscriptionAttributes[feature];
|
2022-03-25 12:22:27 +01:00
|
|
|
if (!attributes || !attributes[attribute]) {
|
2022-09-23 15:54:43 +02:00
|
|
|
return ["none"];
|
2022-03-25 12:22:27 +01:00
|
|
|
}
|
2022-09-23 15:52:05 +02:00
|
|
|
let allowedTypes = [];
|
|
|
|
Object.keys(attributes[attribute]).forEach((subscriptionType) => {
|
2023-09-23 19:38:22 +02:00
|
|
|
let values = attributes[attribute][subscriptionType];
|
2022-03-25 12:18:54 +01:00
|
|
|
if (values[0] === "*" || values.includes(value)) {
|
2022-09-23 15:52:05 +02:00
|
|
|
allowedTypes.push(subscriptionType);
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
|
|
|
});
|
2022-09-23 15:52:05 +02:00
|
|
|
return allowedTypes;
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
|
2023-02-07 12:46:17 +01:00
|
|
|
@discourseComputed("feature", "attribute", "wizard.allowGuests")
|
2023-02-09 14:18:25 +01:00
|
|
|
content(feature, attribute) {
|
2023-09-24 11:19:07 +02:00
|
|
|
debugger;
|
2023-02-09 14:18:25 +01:00
|
|
|
return filterValues(this.wizard, feature, attribute)
|
2022-05-13 12:06:38 +02:00
|
|
|
.map((value) => {
|
2023-09-23 19:30:11 +02:00
|
|
|
let allowedSubscriptionTypes = this.subscription.allowedSubscriptionTypes(
|
2022-05-13 12:06:38 +02:00
|
|
|
feature,
|
|
|
|
attribute,
|
|
|
|
value
|
|
|
|
);
|
2022-09-23 15:52:05 +02:00
|
|
|
|
2022-09-23 15:54:43 +02:00
|
|
|
let subscriptionRequired =
|
|
|
|
allowedSubscriptionTypes.length &&
|
|
|
|
!allowedSubscriptionTypes.includes("none");
|
2022-09-23 15:52:05 +02:00
|
|
|
|
|
|
|
let attrs = {
|
2022-05-13 12:06:38 +02:00
|
|
|
id: value,
|
|
|
|
name: I18n.t(nameKey(feature, attribute, value)),
|
2022-09-23 15:54:43 +02:00
|
|
|
subscriptionRequired,
|
2022-05-13 12:06:38 +02:00
|
|
|
};
|
2022-09-23 15:52:05 +02:00
|
|
|
|
|
|
|
if (subscriptionRequired) {
|
2022-09-23 15:54:43 +02:00
|
|
|
let subscribed = allowedSubscriptionTypes.includes(
|
|
|
|
this.subscriptionType
|
|
|
|
);
|
2022-09-23 15:52:05 +02:00
|
|
|
let selectorKey = subscribed ? "subscribed" : "not_subscribed";
|
|
|
|
let selectorLabel = `admin.wizard.subscription.${selectorKey}.selector`;
|
|
|
|
|
|
|
|
attrs.disabled = !subscribed;
|
|
|
|
attrs.selectorLabel = selectorLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
2022-05-13 12:06:38 +02:00
|
|
|
})
|
|
|
|
.sort(function (a, b) {
|
|
|
|
if (a.subscriptionType && !b.subscriptionType) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!a.subscriptionType && b.subscriptionType) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.subscriptionType === b.subscriptionType) {
|
|
|
|
return a.subscriptionType
|
|
|
|
? a.subscriptionType.localeCompare(b.subscriptionType)
|
|
|
|
: 0;
|
|
|
|
} else {
|
|
|
|
return a.subscriptionType === "standard" ? -1 : 0;
|
|
|
|
}
|
|
|
|
});
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
|
2021-09-03 10:46:32 +02:00
|
|
|
modifyComponentForRow() {
|
2021-09-24 11:58:42 +02:00
|
|
|
return "wizard-subscription-selector/wizard-subscription-selector-row";
|
2021-09-07 14:13:01 +02:00
|
|
|
},
|
|
|
|
});
|