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