discourse/app/assets/javascripts/admin/addon/mixins/setting-object.js
Joffrey JAFFEUX c9bd4b4c64
FIX: ensures validValues is an array (#13435)
Before this fix the setting object would have exceptions on 3 fields: computedNameProperty, computedValueProperty and validValues

```
TypeError: Cannot read property 'forEach' of undefined
    at Class.validValues (http://localhost:4200/assets/admin.js:10468:19)
    at Class.<anonymous> (http://localhost:4200/assets/vendor.js:82492:19)
    at http://localhost:4200/assets/vendor.js:28633:34
    at untrack (http://localhost:4200/assets/vendor.js:26641:7)
    at ComputedProperty.get (http://localhost:4200/assets/vendor.js:28632:13)
    at Class.CPGETTER_FUNCTION [as validValues] (http://localhost:4200/assets/vendor.js:26259:25)
    at Class.r (<anonymous>:1:83)
```
2021-06-18 14:02:21 +02:00

70 lines
1.6 KiB
JavaScript

import I18n from "I18n";
import Mixin from "@ember/object/mixin";
import { computed } from "@ember/object";
import discourseComputed from "discourse-common/utils/decorators";
import { isPresent } from "@ember/utils";
export default Mixin.create({
@discourseComputed("value", "default")
overridden(val, defaultVal) {
if (val === null) {
val = "";
}
if (defaultVal === null) {
defaultVal = "";
}
return val.toString() !== defaultVal.toString();
},
computedValueProperty: computed(
"valueProperty",
"validValues.[]",
function () {
if (isPresent(this.valueProperty)) {
return this.valueProperty;
}
if (isPresent(this.validValues.get("firstObject.value"))) {
return "value";
} else {
return null;
}
}
),
computedNameProperty: computed("nameProperty", "validValues.[]", function () {
if (isPresent(this.nameProperty)) {
return this.nameProperty;
}
if (isPresent(this.validValues.get("firstObject.name"))) {
return "name";
} else {
return null;
}
}),
@discourseComputed("valid_values")
validValues(validValues) {
const vals = [],
translateNames = this.translate_names;
(validValues || []).forEach((v) => {
if (v.name && v.name.length > 0 && translateNames) {
vals.addObject({ name: I18n.t(v.name), value: v.value });
} else {
vals.addObject(v);
}
});
return vals;
},
@discourseComputed("valid_values")
allowsNone(validValues) {
if (validValues && validValues.indexOf("") >= 0) {
return "admin.settings.none";
}
},
});