2021-09-27 04:43:26 -04:00
|
|
|
import { isNone } from "@ember/utils";
|
|
|
|
import { fmt, propertyNotEqual } from "discourse/lib/computed";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { alias, oneWay } from "@ember/object/computed";
|
2019-10-30 18:05:27 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-30 16:13:48 -04:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2019-11-05 11:37:32 -05:00
|
|
|
import { Promise } from "rsvp";
|
2019-11-08 03:47:51 -05:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2018-03-04 19:04:23 -05:00
|
|
|
import { categoryLinkHTML } from "discourse/helpers/category-link";
|
2021-09-27 04:43:26 -04:00
|
|
|
import discourseComputed, { bind } from "discourse-common/utils/decorators";
|
2020-04-30 16:41:02 -04:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { on } from "@ember/object/evented";
|
2019-11-02 08:54:41 -04:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2020-03-06 17:49:28 -05:00
|
|
|
import { warn } from "@ember/debug";
|
2021-09-27 04:43:26 -04:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { splitString } from "discourse/lib/utilities";
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2018-03-13 15:59:12 -04:00
|
|
|
const CUSTOM_TYPES = [
|
|
|
|
"bool",
|
|
|
|
"enum",
|
|
|
|
"list",
|
|
|
|
"url_list",
|
|
|
|
"host_list",
|
|
|
|
"category_list",
|
|
|
|
"value_list",
|
2018-07-18 06:57:43 -04:00
|
|
|
"category",
|
2018-08-03 16:41:37 -04:00
|
|
|
"uploaded_image_list",
|
2018-10-15 01:03:53 -04:00
|
|
|
"compact_list",
|
2018-11-14 02:03:02 -05:00
|
|
|
"secret_list",
|
2019-04-18 13:44:58 -04:00
|
|
|
"upload",
|
2019-11-01 03:10:13 -04:00
|
|
|
"group_list",
|
2020-03-09 05:07:03 -04:00
|
|
|
"tag_list",
|
2020-06-04 10:44:54 -04:00
|
|
|
"color",
|
|
|
|
"simple_list",
|
2021-04-07 09:32:05 -04:00
|
|
|
"emoji_list",
|
2022-03-08 06:18:43 -05:00
|
|
|
"named_list",
|
2018-03-13 15:59:12 -04:00
|
|
|
];
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2019-06-20 10:57:31 -04:00
|
|
|
const AUTO_REFRESH_ON_SAVE = ["logo", "logo_small", "large_icon"];
|
|
|
|
|
2019-10-30 15:03:08 -04:00
|
|
|
export default Mixin.create({
|
2019-01-11 03:39:21 -05:00
|
|
|
classNameBindings: [":row", ":setting", "overridden", "typeClass"],
|
2019-10-30 16:28:29 -04:00
|
|
|
content: alias("setting"),
|
2018-03-04 19:04:23 -05:00
|
|
|
validationMessage: null,
|
2019-10-30 16:28:29 -04:00
|
|
|
isSecret: oneWay("setting.secret"),
|
2021-09-27 04:43:26 -04:00
|
|
|
setting: null,
|
2022-11-11 04:44:40 -05:00
|
|
|
attributeBindings: ["setting.setting:data-setting"],
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("buffered.value", "setting.value")
|
2018-03-04 19:04:23 -05:00
|
|
|
dirty(bufferVal, settingVal) {
|
2021-09-27 04:43:26 -04:00
|
|
|
if (isNone(bufferVal)) {
|
2020-09-22 10:28:28 -04:00
|
|
|
bufferVal = "";
|
|
|
|
}
|
2021-09-27 04:43:26 -04:00
|
|
|
|
|
|
|
if (isNone(settingVal)) {
|
2020-09-22 10:28:28 -04:00
|
|
|
settingVal = "";
|
|
|
|
}
|
2018-03-04 19:04:23 -05:00
|
|
|
|
|
|
|
return bufferVal.toString() !== settingVal.toString();
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("setting", "buffered.value")
|
2018-03-04 19:04:23 -05:00
|
|
|
preview(setting, value) {
|
|
|
|
// A bit hacky, but allows us to use helpers
|
2021-09-27 04:43:26 -04:00
|
|
|
if (setting.setting === "category_style") {
|
|
|
|
const category = this.site.get("categories.firstObject");
|
2018-03-04 19:04:23 -05:00
|
|
|
if (category) {
|
2021-09-27 04:43:26 -04:00
|
|
|
return categoryLinkHTML(category, { categoryStyle: value });
|
2018-03-04 19:04:23 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 04:43:26 -04:00
|
|
|
|
|
|
|
const preview = setting.preview;
|
2018-03-04 19:04:23 -05:00
|
|
|
if (preview) {
|
2021-09-27 04:43:26 -04:00
|
|
|
const escapedValue = preview.replace(/\{\{value\}\}/g, value);
|
|
|
|
return htmlSafe(`<div class='preview'>${escapedValue}</div>`);
|
2018-03-04 19:04:23 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("componentType")
|
2018-03-04 19:04:23 -05:00
|
|
|
typeClass(componentType) {
|
|
|
|
return componentType.replace(/\_/g, "-");
|
|
|
|
},
|
|
|
|
|
2019-11-28 00:19:01 -05:00
|
|
|
@discourseComputed("setting.setting", "setting.label")
|
|
|
|
settingName(setting, label) {
|
|
|
|
return label || setting.replace(/\_/g, " ");
|
2018-03-04 19:04:23 -05:00
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("type")
|
2018-03-04 19:04:23 -05:00
|
|
|
componentType(type) {
|
2022-07-17 14:48:36 -04:00
|
|
|
return CUSTOM_TYPES.includes(type) ? type : "string";
|
2018-03-04 19:04:23 -05:00
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("setting")
|
2018-08-03 16:41:37 -04:00
|
|
|
type(setting) {
|
|
|
|
if (setting.type === "list" && setting.list_type) {
|
|
|
|
return `${setting.list_type}_list`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return setting.type;
|
|
|
|
},
|
|
|
|
|
2021-09-27 04:43:26 -04:00
|
|
|
componentName: fmt("typeClass", "site-settings/%@"),
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2019-11-28 00:19:01 -05:00
|
|
|
@discourseComputed("setting.anyValue")
|
|
|
|
allowAny(anyValue) {
|
|
|
|
return anyValue !== false;
|
|
|
|
},
|
|
|
|
|
2021-09-27 04:43:26 -04:00
|
|
|
overridden: propertyNotEqual("setting.default", "buffered.value"),
|
2019-01-11 03:39:21 -05:00
|
|
|
|
2019-12-04 01:13:41 -05:00
|
|
|
@discourseComputed("buffered.value")
|
2021-09-27 04:43:26 -04:00
|
|
|
bufferedValues(value) {
|
|
|
|
return splitString(value, "|");
|
|
|
|
},
|
2019-12-04 01:13:41 -05:00
|
|
|
|
|
|
|
@discourseComputed("setting.defaultValues")
|
2021-09-27 04:43:26 -04:00
|
|
|
defaultValues(value) {
|
|
|
|
return splitString(value, "|");
|
|
|
|
},
|
2019-12-04 01:13:41 -05:00
|
|
|
|
|
|
|
@discourseComputed("defaultValues", "bufferedValues")
|
|
|
|
defaultIsAvailable(defaultValues, bufferedValues) {
|
|
|
|
return (
|
2019-12-04 16:25:49 -05:00
|
|
|
defaultValues.length > 0 &&
|
2019-12-04 01:13:41 -05:00
|
|
|
!defaultValues.every((value) => bufferedValues.includes(value))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2021-09-27 04:43:26 -04: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",
|
2022-11-06 06:14:17 -05:00
|
|
|
"default_hide_profile_and_presence",
|
2021-09-27 04:43:26 -04:00
|
|
|
"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",
|
2022-06-19 23:49:33 -04:00
|
|
|
"default_categories_normal",
|
2021-09-27 04:43:26 -04:00
|
|
|
"default_tags_watching",
|
|
|
|
"default_tags_tracking",
|
|
|
|
"default_tags_muted",
|
|
|
|
"default_tags_watching_first_post",
|
|
|
|
"default_text_size",
|
|
|
|
"default_title_count_mode",
|
2022-10-26 18:38:50 -04:00
|
|
|
"default_sidebar_categories",
|
|
|
|
"default_sidebar_tags",
|
2021-09-27 04:43:26 -04:00
|
|
|
];
|
2022-10-26 18:38:50 -04:00
|
|
|
|
2021-09-27 04:43:26 -04:00
|
|
|
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 07:42:47 -04:00
|
|
|
}
|
2021-09-27 04:43:26 -04: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-04 19:04:23 -05:00
|
|
|
);
|
2021-09-27 04:43:26 -04: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-17 19:49:41 -04:00
|
|
|
}),
|
2018-03-04 19:04:23 -05:00
|
|
|
|
2019-10-30 18:05:27 -04:00
|
|
|
_removeBindings: on("willDestroyElement", function () {
|
2021-09-27 04:43:26 -04:00
|
|
|
this.element.removeEventListener("keydown", this._handleKeydown);
|
2019-10-17 19:49:41 -04:00
|
|
|
}),
|
2018-03-04 19:04:23 -05:00
|
|
|
|
|
|
|
_save() {
|
2020-03-06 17:49:28 -05:00
|
|
|
warn("You should define a `_save` method", {
|
2019-01-16 14:53:13 -05:00
|
|
|
id: "discourse.setting-component.missing-save",
|
2018-03-04 19:04:23 -05:00
|
|
|
});
|
2019-11-05 11:37:32 -05:00
|
|
|
return Promise.resolve();
|
2018-03-04 19:04:23 -05:00
|
|
|
},
|
|
|
|
});
|