mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 10:13:40 +08:00
d4479eab73
Ember's legacy mixin system does not support native-class syntax, so we have to use the non-decorator syntaxes for `action()` and `computed()`. Eventually, we will need to refactor things to remove these mixins... but today is not that day.
75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
import { computed } from "@ember/object";
|
|
import { readOnly } from "@ember/object/computed";
|
|
import Mixin from "@ember/object/mixin";
|
|
import { isPresent } from "@ember/utils";
|
|
import { deepEqual } from "discourse-common/lib/object";
|
|
import I18n from "discourse-i18n";
|
|
|
|
export default Mixin.create({
|
|
overridden: computed("value", "default", function () {
|
|
let val = this.value;
|
|
let defaultVal = this.default;
|
|
|
|
if (val === null) {
|
|
val = "";
|
|
}
|
|
if (defaultVal === null) {
|
|
defaultVal = "";
|
|
}
|
|
|
|
return !deepEqual(val, defaultVal);
|
|
}),
|
|
|
|
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;
|
|
}
|
|
}),
|
|
|
|
validValues: computed("valid_values", function () {
|
|
const validValues = this.valid_values;
|
|
|
|
const values = [];
|
|
const translateNames = this.translate_names;
|
|
|
|
(validValues || []).forEach((v) => {
|
|
if (v.name && v.name.length > 0 && translateNames) {
|
|
values.addObject({ name: I18n.t(v.name), value: v.value });
|
|
} else {
|
|
values.addObject(v);
|
|
}
|
|
});
|
|
return values;
|
|
}),
|
|
|
|
allowsNone: computed("valid_values", function () {
|
|
if (this.valid_values?.includes("")) {
|
|
return "admin.settings.none";
|
|
}
|
|
}),
|
|
|
|
anyValue: readOnly("allow_any"),
|
|
});
|