2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2020-03-06 17:49:28 -05:00
|
|
|
import { warn } from "@ember/debug";
|
2019-11-07 16:38:28 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { alias, oneWay } from "@ember/object/computed";
|
2018-03-04 19:04:23 -05:00
|
|
|
import { categoryLinkHTML } from "discourse/helpers/category-link";
|
2019-10-30 18:05:27 -04:00
|
|
|
import { on } from "@ember/object/evented";
|
2019-10-30 16:13:48 -04:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2019-11-02 08:54:41 -04:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
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";
|
2020-04-30 16:41:02 -04:00
|
|
|
import { htmlSafe } from "@ember/template";
|
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",
|
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-12-04 16:25:49 -05:00
|
|
|
function splitPipes(str) {
|
|
|
|
if (typeof str === "string") {
|
|
|
|
return str.split("|").filter(Boolean);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"),
|
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) {
|
2020-09-22 10:28:28 -04:00
|
|
|
if (bufferVal === null || bufferVal === undefined) {
|
|
|
|
bufferVal = "";
|
|
|
|
}
|
|
|
|
if (settingVal === null || settingVal === undefined) {
|
|
|
|
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
|
|
|
|
if (setting.get("setting") === "category_style") {
|
|
|
|
let category = this.site.get("categories.firstObject");
|
|
|
|
if (category) {
|
|
|
|
return categoryLinkHTML(category, {
|
|
|
|
categoryStyle: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let preview = setting.get("preview");
|
|
|
|
if (preview) {
|
2020-04-30 16:41:02 -04:00
|
|
|
return htmlSafe(
|
2018-03-04 19:04:23 -05:00
|
|
|
"<div class='preview'>" +
|
|
|
|
preview.replace(/\{\{value\}\}/g, value) +
|
|
|
|
"</div>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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) {
|
2018-03-13 15:59:12 -04:00
|
|
|
return CUSTOM_TYPES.indexOf(type) !== -1 ? 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;
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("typeClass")
|
2018-03-04 19:04:23 -05:00
|
|
|
componentName(typeClass) {
|
|
|
|
return "site-settings/" + typeClass;
|
|
|
|
},
|
|
|
|
|
2019-11-28 00:19:01 -05:00
|
|
|
@discourseComputed("setting.anyValue")
|
|
|
|
allowAny(anyValue) {
|
|
|
|
return anyValue !== false;
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("setting.default", "buffered.value")
|
2019-01-11 03:39:21 -05:00
|
|
|
overridden(settingDefault, bufferedValue) {
|
|
|
|
return settingDefault !== bufferedValue;
|
|
|
|
},
|
|
|
|
|
2019-12-04 01:13:41 -05:00
|
|
|
@discourseComputed("buffered.value")
|
2019-12-04 16:25:49 -05:00
|
|
|
bufferedValues: splitPipes,
|
2019-12-04 01:13:41 -05:00
|
|
|
|
|
|
|
@discourseComputed("setting.defaultValues")
|
2019-12-04 16:25:49 -05:00
|
|
|
defaultValues: splitPipes,
|
2019-12-04 01:13:41 -05:00
|
|
|
|
|
|
|
@discourseComputed("defaultValues", "bufferedValues")
|
|
|
|
defaultIsAvailable(defaultValues, bufferedValues) {
|
|
|
|
return (
|
|
|
|
defaultValues &&
|
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))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-10-30 18:05:27 -04:00
|
|
|
_watchEnterKey: on("didInsertElement", function () {
|
2019-07-16 06:45:15 -04:00
|
|
|
$(this.element).on(
|
|
|
|
"keydown.setting-enter",
|
|
|
|
".input-setting-string",
|
|
|
|
(e) => {
|
2018-03-04 19:04:23 -05:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
// enter key
|
2019-01-11 03:39:21 -05:00
|
|
|
this.send("save");
|
2020-09-04 07:42:47 -04:00
|
|
|
}
|
2018-03-04 19:04:23 -05:00
|
|
|
}
|
|
|
|
);
|
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 () {
|
2019-07-16 06:45:15 -04:00
|
|
|
$(this.element).off("keydown.setting-enter");
|
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
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2019-11-02 08:54:41 -04:00
|
|
|
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",
|
2020-08-14 09:40:56 -04:00
|
|
|
"default_other_skip_new_user_tips",
|
2019-11-02 08:54:41 -04:00
|
|
|
"default_topics_automatic_unpin",
|
|
|
|
"default_categories_watching",
|
|
|
|
"default_categories_tracking",
|
|
|
|
"default_categories_muted",
|
|
|
|
"default_categories_watching_first_post",
|
2020-08-19 15:05:04 -04:00
|
|
|
"default_categories_regular",
|
2019-11-02 08:54:41 -04:00
|
|
|
"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)) {
|
2019-11-17 13:39:38 -05:00
|
|
|
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: result.user_count,
|
|
|
|
key: key.replace(/_/g, " "),
|
|
|
|
},
|
|
|
|
admin: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
controller.set("onClose", () => {
|
|
|
|
this.updateExistingUsers = controller.updateExistingUsers;
|
|
|
|
this.send("save");
|
|
|
|
});
|
|
|
|
} else {
|
2019-11-08 03:47:51 -05:00
|
|
|
this.send("save");
|
2019-11-17 13:39:38 -05:00
|
|
|
}
|
2019-11-08 03:47:51 -05:00
|
|
|
});
|
2019-11-02 08:54:41 -04:00
|
|
|
} else {
|
|
|
|
this.send("save");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-03-04 19:04:23 -05:00
|
|
|
save() {
|
2019-10-29 13:01:45 -04:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
});
|
2018-03-04 19:04:23 -05:00
|
|
|
},
|
|
|
|
|
2018-06-02 09:57:52 -04:00
|
|
|
cancel() {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
},
|
|
|
|
|
2018-03-04 19:04:23 -05:00
|
|
|
resetDefault() {
|
|
|
|
this.set("buffered.value", this.get("setting.default"));
|
|
|
|
},
|
|
|
|
|
2018-06-02 09:57:52 -04:00
|
|
|
toggleSecret() {
|
|
|
|
this.toggleProperty("isSecret");
|
2019-11-28 00:19:01 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
setDefaultValues() {
|
2019-12-04 01:13:41 -05:00
|
|
|
this.set(
|
|
|
|
"buffered.value",
|
|
|
|
this.bufferedValues.concat(this.defaultValues).uniq().join("|")
|
|
|
|
);
|
2020-02-19 03:04:57 -05:00
|
|
|
return false;
|
2018-03-04 19:04:23 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|