2020-02-19 17:01:21 +08:00
|
|
|
import { computed } from "@ember/object";
|
2022-03-08 19:18:43 +08:00
|
|
|
import { readOnly } from "@ember/object/computed";
|
2023-10-11 02:38:59 +08:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2020-02-19 17:01:21 +08:00
|
|
|
import { isPresent } from "@ember/utils";
|
2024-03-13 06:52:46 +08:00
|
|
|
import { deepEqual } from "discourse-common/lib/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2023-10-18 18:07:09 +08:00
|
|
|
import I18n from "discourse-i18n";
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-10-31 03:03:08 +08:00
|
|
|
export default Mixin.create({
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("value", "default")
|
2019-04-26 18:16:21 +08:00
|
|
|
overridden(val, defaultVal) {
|
2020-09-22 22:28:28 +08:00
|
|
|
if (val === null) {
|
|
|
|
val = "";
|
|
|
|
}
|
|
|
|
if (defaultVal === null) {
|
|
|
|
defaultVal = "";
|
|
|
|
}
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2024-03-13 06:52:46 +08:00
|
|
|
return !deepEqual(val, defaultVal);
|
2019-04-26 18:16:21 +08:00
|
|
|
},
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2020-02-19 17:01:21 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("valid_values")
|
2019-04-26 18:16:21 +08:00
|
|
|
validValues(validValues) {
|
2022-07-19 10:27:10 +08:00
|
|
|
const values = [];
|
|
|
|
const translateNames = this.translate_names;
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2021-06-18 20:02:21 +08:00
|
|
|
(validValues || []).forEach((v) => {
|
2018-03-05 08:04:23 +08:00
|
|
|
if (v.name && v.name.length > 0 && translateNames) {
|
2022-07-19 10:27:10 +08:00
|
|
|
values.addObject({ name: I18n.t(v.name), value: v.value });
|
2018-03-05 08:04:23 +08:00
|
|
|
} else {
|
2022-07-19 10:27:10 +08:00
|
|
|
values.addObject(v);
|
2018-03-05 08:04:23 +08:00
|
|
|
}
|
|
|
|
});
|
2022-07-19 10:27:10 +08:00
|
|
|
return values;
|
2019-04-26 18:16:21 +08:00
|
|
|
},
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("valid_values")
|
2019-04-26 18:16:21 +08:00
|
|
|
allowsNone(validValues) {
|
2022-07-19 10:27:40 +08:00
|
|
|
if (validValues?.includes("")) {
|
2018-03-05 08:04:23 +08:00
|
|
|
return "admin.settings.none";
|
2018-11-23 00:38:58 +08:00
|
|
|
}
|
2019-04-26 18:16:21 +08:00
|
|
|
},
|
2022-03-08 19:18:43 +08:00
|
|
|
|
|
|
|
anyValue: readOnly("allow_any"),
|
2018-03-05 08:04:23 +08:00
|
|
|
});
|