discourse/app/assets/javascripts/admin/addon/mixins/setting-object.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
1.8 KiB
JavaScript
Raw Normal View History

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"),
});