2021-09-27 16:43:26 +08:00
|
|
|
import { isNone } from "@ember/utils";
|
|
|
|
import { fmt, propertyNotEqual } from "discourse/lib/computed";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { alias, oneWay } from "@ember/object/computed";
|
2019-10-31 06:05:27 +08:00
|
|
|
import I18n from "I18n";
|
2019-10-31 04:13:48 +08:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2019-11-06 00:37:32 +08:00
|
|
|
import { Promise } from "rsvp";
|
2019-11-08 16:47:51 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2018-03-05 08:04:23 +08:00
|
|
|
import { categoryLinkHTML } from "discourse/helpers/category-link";
|
2021-09-27 16:43:26 +08:00
|
|
|
import discourseComputed, { bind } from "discourse-common/utils/decorators";
|
2020-05-01 04:41:02 +08:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { on } from "@ember/object/evented";
|
2019-11-02 20:54:41 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2020-03-07 06:49:28 +08:00
|
|
|
import { warn } from "@ember/debug";
|
2021-09-27 16:43:26 +08:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { splitString } from "discourse/lib/utilities";
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
const CUSTOM_TYPES = [
|
|
|
|
"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",
|
2019-11-01 15:10:13 +08:00
|
|
|
"group_list",
|
2020-03-09 17:07:03 +08:00
|
|
|
"tag_list",
|
2020-06-04 22:44:54 +08:00
|
|
|
"color",
|
|
|
|
"simple_list",
|
2021-04-07 21:32:05 +08:00
|
|
|
"emoji_list",
|
2018-03-14 03:59:12 +08:00
|
|
|
];
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-06-20 22:57:31 +08:00
|
|
|
const AUTO_REFRESH_ON_SAVE = ["logo", "logo_small", "large_icon"];
|
|
|
|
|
2019-10-31 03:03:08 +08:00
|
|
|
export default Mixin.create({
|
2019-01-11 16:39:21 +08:00
|
|
|
classNameBindings: [":row", ":setting", "overridden", "typeClass"],
|
2019-10-31 04:28:29 +08:00
|
|
|
content: alias("setting"),
|
2018-03-05 08:04:23 +08:00
|
|
|
validationMessage: null,
|
2019-10-31 04:28:29 +08:00
|
|
|
isSecret: oneWay("setting.secret"),
|
2021-09-27 16:43:26 +08:00
|
|
|
setting: null,
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("buffered.value", "setting.value")
|
2018-03-05 08:04:23 +08:00
|
|
|
dirty(bufferVal, settingVal) {
|
2021-09-27 16:43:26 +08:00
|
|
|
if (isNone(bufferVal)) {
|
2020-09-22 22:28:28 +08:00
|
|
|
bufferVal = "";
|
|
|
|
}
|
2021-09-27 16:43:26 +08:00
|
|
|
|
|
|
|
if (isNone(settingVal)) {
|
2020-09-22 22:28:28 +08:00
|
|
|
settingVal = "";
|
|
|
|
}
|
2018-03-05 08:04:23 +08:00
|
|
|
|
|
|
|
return bufferVal.toString() !== settingVal.toString();
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("setting", "buffered.value")
|
2018-03-05 08:04:23 +08:00
|
|
|
preview(setting, value) {
|
|
|
|
// A bit hacky, but allows us to use helpers
|
2021-09-27 16:43:26 +08:00
|
|
|
if (setting.setting === "category_style") {
|
|
|
|
const category = this.site.get("categories.firstObject");
|
2018-03-05 08:04:23 +08:00
|
|
|
if (category) {
|
2021-09-27 16:43:26 +08:00
|
|
|
return categoryLinkHTML(category, { categoryStyle: value });
|
2018-03-05 08:04:23 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 16:43:26 +08:00
|
|
|
|
|
|
|
const preview = setting.preview;
|
2018-03-05 08:04:23 +08:00
|
|
|
if (preview) {
|
2021-09-27 16:43:26 +08:00
|
|
|
const escapedValue = preview.replace(/\{\{value\}\}/g, value);
|
|
|
|
return htmlSafe(`<div class='preview'>${escapedValue}</div>`);
|
2018-03-05 08:04:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("componentType")
|
2018-03-05 08:04:23 +08:00
|
|
|
typeClass(componentType) {
|
|
|
|
return componentType.replace(/\_/g, "-");
|
|
|
|
},
|
|
|
|
|
2019-11-28 13:19:01 +08:00
|
|
|
@discourseComputed("setting.setting", "setting.label")
|
|
|
|
settingName(setting, label) {
|
|
|
|
return label || setting.replace(/\_/g, " ");
|
2018-03-05 08:04:23 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("type")
|
2018-03-05 08:04:23 +08:00
|
|
|
componentType(type) {
|
2018-03-14 03:59:12 +08:00
|
|
|
return CUSTOM_TYPES.indexOf(type) !== -1 ? type : "string";
|
2018-03-05 08:04:23 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("setting")
|
2018-08-04 04:41:37 +08:00
|
|
|
type(setting) {
|
|
|
|
if (setting.type === "list" && setting.list_type) {
|
|
|
|
return `${setting.list_type}_list`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return setting.type;
|
|
|
|
},
|
|
|
|
|
2021-09-27 16:43:26 +08:00
|
|
|
componentName: fmt("typeClass", "site-settings/%@"),
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-11-28 13:19:01 +08:00
|
|
|
@discourseComputed("setting.anyValue")
|
|
|
|
allowAny(anyValue) {
|
|
|
|
return anyValue !== false;
|
|
|
|
},
|
|
|
|
|
2021-09-27 16:43:26 +08:00
|
|
|
overridden: propertyNotEqual("setting.default", "buffered.value"),
|
2019-01-11 16:39:21 +08:00
|
|
|
|
2019-12-04 14:13:41 +08:00
|
|
|
@discourseComputed("buffered.value")
|
2021-09-27 16:43:26 +08:00
|
|
|
bufferedValues(value) {
|
|
|
|
return splitString(value, "|");
|
|
|
|
},
|
2019-12-04 14:13:41 +08:00
|
|
|
|
|
|
|
@discourseComputed("setting.defaultValues")
|
2021-09-27 16:43:26 +08:00
|
|
|
defaultValues(value) {
|
|
|
|
return splitString(value, "|");
|
|
|
|
},
|
2019-12-04 14:13:41 +08:00
|
|
|
|
|
|
|
@discourseComputed("defaultValues", "bufferedValues")
|
|
|
|
defaultIsAvailable(defaultValues, bufferedValues) {
|
|
|
|
return (
|
2019-12-05 05:25:49 +08:00
|
|
|
defaultValues.length > 0 &&
|
2019-12-04 14:13:41 +08:00
|
|
|
!defaultValues.every((value) => bufferedValues.includes(value))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2021-09-27 16:43:26 +08:00
|
|
|
@action
|
|
|
|
update() {
|
|
|
|
const defaultUserPreferences = [
|
|
|
|
"default_email_digest_frequency",
|
|
|
|
"default_include_tl0_in_digests",
|
|
|
|
"default_email_level",
|
|
|
|
"default_email_messages_level",
|
|
|
|
"default_email_mailing_list_mode",
|
|
|
|
"default_email_mailing_list_mode_frequency",
|
|
|
|
"default_email_previous_replies",
|
|
|
|
"default_email_in_reply_to",
|
|
|
|
"default_other_new_topic_duration_minutes",
|
|
|
|
"default_other_auto_track_topics_after_msecs",
|
|
|
|
"default_other_notification_level_when_replying",
|
|
|
|
"default_other_external_links_in_new_tab",
|
|
|
|
"default_other_enable_quoting",
|
|
|
|
"default_other_enable_defer",
|
|
|
|
"default_other_dynamic_favicon",
|
|
|
|
"default_other_like_notification_frequency",
|
|
|
|
"default_other_skip_new_user_tips",
|
|
|
|
"default_topics_automatic_unpin",
|
|
|
|
"default_categories_watching",
|
|
|
|
"default_categories_tracking",
|
|
|
|
"default_categories_muted",
|
|
|
|
"default_categories_watching_first_post",
|
|
|
|
"default_categories_regular",
|
|
|
|
"default_tags_watching",
|
|
|
|
"default_tags_tracking",
|
|
|
|
"default_tags_muted",
|
|
|
|
"default_tags_watching_first_post",
|
|
|
|
"default_text_size",
|
|
|
|
"default_title_count_mode",
|
|
|
|
];
|
|
|
|
const key = this.buffered.get("setting");
|
|
|
|
|
|
|
|
if (defaultUserPreferences.includes(key)) {
|
|
|
|
const data = {};
|
|
|
|
data[key] = this.buffered.get("value");
|
|
|
|
|
|
|
|
ajax(`/admin/site_settings/${key}/user_count.json`, {
|
|
|
|
type: "PUT",
|
|
|
|
data,
|
|
|
|
}).then((result) => {
|
|
|
|
const count = result.user_count;
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
const controller = showModal("site-setting-default-categories", {
|
|
|
|
model: { count, key: key.replaceAll("_", " ") },
|
|
|
|
admin: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
controller.set("onClose", () => {
|
|
|
|
this.updateExistingUsers = controller.updateExistingUsers;
|
|
|
|
this.save();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.save();
|
2020-09-04 19:42:47 +08:00
|
|
|
}
|
2021-09-27 16:43:26 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
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?.errors) {
|
|
|
|
this.set("validationMessage", e.jqXHR.responseJSON.errors[0]);
|
|
|
|
} else {
|
|
|
|
this.set("validationMessage", I18n.t("generic_error"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancel() {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
resetDefault() {
|
|
|
|
this.set("buffered.value", this.get("setting.default"));
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleSecret() {
|
|
|
|
this.toggleProperty("isSecret");
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDefaultValues() {
|
|
|
|
this.set(
|
|
|
|
"buffered.value",
|
|
|
|
this.bufferedValues.concat(this.defaultValues).uniq().join("|")
|
2018-03-05 08:04:23 +08:00
|
|
|
);
|
2021-09-27 16:43:26 +08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
@bind
|
|
|
|
_handleKeydown(event) {
|
|
|
|
if (
|
|
|
|
event.key === "Enter" &&
|
|
|
|
event.target.classList.contains("input-setting-string")
|
|
|
|
) {
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_watchEnterKey: on("didInsertElement", function () {
|
|
|
|
this.element.addEventListener("keydown", this._handleKeydown);
|
2019-10-18 07:49:41 +08:00
|
|
|
}),
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-10-31 06:05:27 +08:00
|
|
|
_removeBindings: on("willDestroyElement", function () {
|
2021-09-27 16:43:26 +08:00
|
|
|
this.element.removeEventListener("keydown", this._handleKeydown);
|
2019-10-18 07:49:41 +08:00
|
|
|
}),
|
2018-03-05 08:04:23 +08:00
|
|
|
|
|
|
|
_save() {
|
2020-03-07 06:49:28 +08:00
|
|
|
warn("You should define a `_save` method", {
|
2019-01-17 03:53:13 +08:00
|
|
|
id: "discourse.setting-component.missing-save",
|
2018-03-05 08:04:23 +08:00
|
|
|
});
|
2019-11-06 00:37:32 +08:00
|
|
|
return Promise.resolve();
|
2018-03-05 08:04:23 +08:00
|
|
|
},
|
|
|
|
});
|