discourse/app/assets/javascripts/admin/mixins/setting-component.js.es6

148 lines
3.6 KiB
Plaintext
Raw Normal View History

import { alias, oneWay } from "@ember/object/computed";
2018-06-15 11:03:24 -04:00
import computed from "ember-addons/ember-computed-decorators";
import { categoryLinkHTML } from "discourse/helpers/category-link";
import { on } from "@ember/object/evented";
import Mixin from "@ember/object/mixin";
const CUSTOM_TYPES = [
2018-06-15 11:03:24 -04:00
"bool",
"enum",
"list",
"url_list",
"host_list",
"category_list",
"value_list",
2018-07-18 06:57:43 -04:00
"category",
"uploaded_image_list",
"compact_list",
2018-11-14 02:03:02 -05:00
"secret_list",
"upload",
"group_list",
"tag_list"
];
const AUTO_REFRESH_ON_SAVE = ["logo", "logo_small", "large_icon"];
export default Mixin.create({
classNameBindings: [":row", ":setting", "overridden", "typeClass"],
content: alias("setting"),
validationMessage: null,
isSecret: oneWay("setting.secret"),
@computed("buffered.value", "setting.value")
dirty(bufferVal, settingVal) {
2018-06-15 11:03:24 -04:00
if (bufferVal === null || bufferVal === undefined) bufferVal = "";
if (settingVal === null || settingVal === undefined) settingVal = "";
return bufferVal.toString() !== settingVal.toString();
},
@computed("setting", "buffered.value")
preview(setting, value) {
// A bit hacky, but allows us to use helpers
2018-06-15 11:03:24 -04:00
if (setting.get("setting") === "category_style") {
let category = this.site.get("categories.firstObject");
if (category) {
return categoryLinkHTML(category, {
categoryStyle: value
});
}
}
2018-06-15 11:03:24 -04:00
let preview = setting.get("preview");
if (preview) {
2018-06-15 11:03:24 -04:00
return new Handlebars.SafeString(
"<div class='preview'>" +
preview.replace(/\{\{value\}\}/g, value) +
"</div>"
);
}
},
2018-06-15 11:03:24 -04:00
@computed("componentType")
typeClass(componentType) {
2018-06-15 11:03:24 -04:00
return componentType.replace(/\_/g, "-");
},
@computed("setting.setting")
settingName(setting) {
2018-06-15 11:03:24 -04:00
return setting.replace(/\_/g, " ");
},
@computed("type")
componentType(type) {
2018-06-15 11:03:24 -04:00
return CUSTOM_TYPES.indexOf(type) !== -1 ? type : "string";
},
@computed("setting")
type(setting) {
if (setting.type === "list" && setting.list_type) {
return `${setting.list_type}_list`;
}
return setting.type;
},
@computed("typeClass")
componentName(typeClass) {
return "site-settings/" + typeClass;
},
@computed("setting.default", "buffered.value")
overridden(settingDefault, bufferedValue) {
return settingDefault !== bufferedValue;
},
_watchEnterKey: on("didInsertElement", function() {
$(this.element).on("keydown.setting-enter", ".input-setting-string", e => {
2018-06-15 11:03:24 -04:00
if (e.keyCode === 13) {
// enter key
this.send("save");
}
});
}),
_removeBindings: on("willDestroyElement", function() {
$(this.element).off("keydown.setting-enter");
}),
_save() {
2019-01-11 11:54:23 -05:00
Ember.warn("You should define a `_save` method", {
id: "discourse.setting-component.missing-save"
2018-06-15 11:03:24 -04:00
});
return Ember.RSVP.resolve();
},
actions: {
save() {
this._save()
.then(() => {
this.set("validationMessage", null);
this.commitBuffer();
if (AUTO_REFRESH_ON_SAVE.includes(this.setting.setting)) {
this.afterSave();
}
})
.catch(e => {
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
this.set("validationMessage", e.jqXHR.responseJSON.errors[0]);
} else {
this.set("validationMessage", I18n.t("generic_error"));
}
});
},
cancel() {
this.rollbackBuffer();
},
resetDefault() {
2018-06-15 11:03:24 -04:00
this.set("buffered.value", this.get("setting.default"));
},
toggleSecret() {
2018-06-15 11:03:24 -04:00
this.toggleProperty("isSecret");
}
}
});