diff --git a/assets/javascripts/discourse/components/custom-field-input.js.es6 b/assets/javascripts/discourse/components/custom-field-input.js.es6 index a673112b..bd6c78a1 100644 --- a/assets/javascripts/discourse/components/custom-field-input.js.es6 +++ b/assets/javascripts/discourse/components/custom-field-input.js.es6 @@ -4,27 +4,12 @@ import { alias, equal, or } from "@ember/object/computed"; import { computed } from "@ember/object"; import I18n from "I18n"; -const klasses = ["topic", "post", "group", "category"]; -const types = ["string", "boolean", "integer", "json"]; -const subscriptionTypes = { - klass: ["group", "category"], - type: ["json"], -}; - -const generateContent = function (array, type, subscribed = false) { - return array.reduce((result, key) => { - let subArr = subscriptionTypes[type]; - let subscription = subArr && subArr.includes(key); - if (!subscription || subscribed) { - result.push({ - id: key, - name: I18n.t(`admin.wizard.custom_field.${type}.${key}`), - subscription, - }); - } - return result; - }, []); -}; +import wizardSchema, { + customFieldsKlassesRequiringAdditionalSubscription, + customFieldsKlassSubscriptionLevel, + customFieldsTypesRequiringAdditionalSubscription, + customFieldsTypeSubscriptionLevel, +} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema"; export default Component.extend({ tagName: "tr", @@ -32,12 +17,6 @@ export default Component.extend({ postSerializers: ["post"], groupSerializers: ["basic_group"], categorySerializers: ["basic_category"], - klassContent: computed("subscribed", function () { - return generateContent(klasses, "klass", this.subscribed); - }), - typeContent: computed("subscribed", function () { - return generateContent(types, "type", this.subscribed); - }), showInputs: or("field.new", "field.edit"), classNames: ["custom-field-input"], loading: or("saving", "destroying"), @@ -49,15 +28,49 @@ export default Component.extend({ this.set("originalField", JSON.parse(JSON.stringify(this.field))); }, - @discourseComputed("field.klass") - serializerContent(klass) { - const serializers = this.get(`${klass}Serializers`); + // @discourseComputed("field.klass") + // serializerContent(klass) { + // const serializers = this.get(`${klass}Serializers`); - if (serializers) { - return generateContent(serializers, "serializers", this.subscribed); - } else { - return []; - } + // if (serializers) { + // return generateContent(serializers, "serializers", this.subscribed); + // } else { + // return []; + // } + // }, + + @discourseComputed("subscription") + customFieldTypes(subscription) { + let unsubscribedCustomFields = customFieldsTypesRequiringAdditionalSubscription( + subscription + ); + return wizardSchema.custom_field.types.reduce((result, type) => { + let disabled = unsubscribedCustomFields.includes(type); + result.push({ + id: type, + name: I18n.t(`admin.wizard.custom_field.type.${type}`), + subscription: customFieldsTypeSubscriptionLevel(type), + disabled: disabled, + }); + return result; + }, []); + }, + + @discourseComputed("subscription") + customFieldKlasses(subscription) { + let unsubscribedCustomFields = customFieldsKlassesRequiringAdditionalSubscription( + subscription + ); + return wizardSchema.custom_field.klasses.reduce((result, klass) => { + let disabled = unsubscribedCustomFields.includes(klass); + result.push({ + id: klass, + name: I18n.t(`admin.wizard.custom_field.klass.${klass}`), + subscription: customFieldsKlassSubscriptionLevel(klass), + disabled: disabled, + }); + return result; + }, []); }, @observes("field.klass") diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index 250df371..a49dec14 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -208,10 +208,24 @@ const action = { objectArrays: {}, }; +const custom_field = { + klasses: ["topic", "post", "group", "category"], + types: ["string", "boolean", "integer", "json"], + customFieldKlassWithSubscription: { + standard: [], + business: ["group", "category"], + }, + customFieldTypeWithSubscription: { + standard: ["json"], + business: [], + }, +} + const wizardSchema = { wizard, step, field, + custom_field, action, }; @@ -242,6 +256,58 @@ export function actionSubscriptionLevel(type) { } } + + +export function customFieldsKlassesRequiringAdditionalSubscription( + currentSubscription +) { + switch (currentSubscription) { + case "business": + return []; + case "standard": + return custom_field.customFieldKlassWithSubscription["business"]; + default: + return custom_field.customFieldKlassWithSubscription["business"].concat(custom_field.customFieldKlassWithSubscription["standard"]); + } +} + +export function customFieldsKlassSubscriptionLevel(type) { + if (custom_field.customFieldKlassWithSubscription["business"].includes(type)) { + return "business" + } else { + if (custom_field.customFieldKlassWithSubscription["standard"].includes(type)) { + return "standard" + } else { + return "" + } + } +} + +export function customFieldsTypesRequiringAdditionalSubscription( + currentSubscription +) { + switch (currentSubscription) { + case "business": + return []; + case "standard": + return custom_field.customFieldTypeWithSubscription["business"]; + default: + return custom_field.customFieldTypeWithSubscription["business"].concat(custom_field.customFieldTypeWithSubscription["standard"]); + } +} + +export function customFieldsTypeSubscriptionLevel(type) { + if (custom_field.customFieldTypeWithSubscription["business"].includes(type)) { + return "business" + } else { + if (custom_field.customFieldTypeWithSubscription["standard"].includes(type)) { + return "standard" + } else { + return "" + } + } +} + export function buildFieldTypes(types) { wizardSchema.field.types = types; } diff --git a/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 index ca9c4c40..4992f92d 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 @@ -10,10 +10,12 @@ export default DiscourseRoute.extend({ setupController(controller, model) { const customFields = A(model.custom_fields || []); const subscribed = model.subscribed; + const subscription = model.subscription; controller.setProperties({ customFields, subscribed, + subscription, }); }, }); diff --git a/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs b/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs index 361d2d44..dceed458 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs @@ -33,7 +33,8 @@ field=field removeField=(action "removeField") saveField=(action "saveField") - subscribed=subscribed}} + subscribed=subscribed + subscription=subscription}} {{/each}} diff --git a/assets/javascripts/discourse/templates/components/custom-field-input.hbs b/assets/javascripts/discourse/templates/components/custom-field-input.hbs index ac7e8689..b4ddcc81 100644 --- a/assets/javascripts/discourse/templates/components/custom-field-input.hbs +++ b/assets/javascripts/discourse/templates/components/custom-field-input.hbs @@ -2,14 +2,14 @@