2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-05-16 13:13:59 +02:00
|
|
|
const settings_config = require("./settings_config");
|
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
exports.build_realm_logo_widget = function (upload_function, is_night) {
|
2020-07-15 01:29:15 +02:00
|
|
|
let logo_section_id = "#realm-day-logo-upload-widget";
|
2020-05-12 19:27:01 +02:00
|
|
|
let logo_source = page_params.realm_logo_source;
|
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
if (is_night) {
|
2020-07-15 01:29:15 +02:00
|
|
|
logo_section_id = "#realm-night-logo-upload-widget";
|
2020-05-12 19:27:01 +02:00
|
|
|
logo_source = page_params.realm_night_logo_source;
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2019-03-11 16:48:59 +01:00
|
|
|
|
2020-07-15 20:58:34 +02:00
|
|
|
const delete_button_elem = $(logo_section_id + " .image-delete-button");
|
2020-06-17 07:43:53 +02:00
|
|
|
const file_input_elem = $(logo_section_id + " .image_file_input");
|
|
|
|
const file_input_error_elem = $(logo_section_id + " .image_file_input_error");
|
|
|
|
const upload_button_elem = $(logo_section_id + " .image_upload_button");
|
2019-03-11 16:48:59 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const get_file_input = function () {
|
2019-10-25 09:15:16 +02:00
|
|
|
return file_input_elem.expectOne();
|
|
|
|
};
|
2019-02-27 15:45:26 +01:00
|
|
|
|
2020-03-28 20:28:14 +01:00
|
|
|
if (!page_params.is_admin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (logo_source === "D") {
|
2019-10-25 09:15:16 +02:00
|
|
|
delete_button_elem.hide();
|
|
|
|
} else {
|
|
|
|
delete_button_elem.show();
|
|
|
|
}
|
2019-02-27 15:45:26 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {night: JSON.stringify(is_night)};
|
2020-07-15 01:29:15 +02:00
|
|
|
delete_button_elem.on("click", (e) => {
|
2019-10-25 09:15:16 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
channel.del({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/realm/logo",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2018-08-16 01:26:55 +02:00
|
|
|
});
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
2018-08-16 01:26:55 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
return upload_widget.build_direct_upload_widget(
|
|
|
|
get_file_input,
|
|
|
|
file_input_error_elem.expectOne(),
|
|
|
|
upload_button_elem.expectOne(),
|
|
|
|
upload_function,
|
2020-07-02 02:16:03 +02:00
|
|
|
page_params.max_logo_file_size,
|
2019-10-25 09:15:16 +02:00
|
|
|
);
|
|
|
|
};
|
2019-01-27 08:25:10 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
function change_logo_delete_button(logo_source, logo_delete_button, file_input) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (logo_source === "U") {
|
2019-10-25 09:15:16 +02:00
|
|
|
logo_delete_button.show();
|
|
|
|
} else {
|
|
|
|
logo_delete_button.hide();
|
|
|
|
// Need to clear input because of a small edge case
|
|
|
|
// where you try to upload the same image you just deleted.
|
2020-07-15 01:29:15 +02:00
|
|
|
file_input.val("");
|
2019-08-20 18:52:33 +02:00
|
|
|
}
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2019-08-20 18:52:33 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
exports.rerender = function () {
|
2020-06-17 07:43:53 +02:00
|
|
|
const file_input = $("#realm-day-logo-upload-widget .image_file_input");
|
|
|
|
const night_file_input = $("#realm-night-logo-upload-widget .realm-logo-file-input");
|
|
|
|
$("#realm-day-logo-upload-widget .image-block").attr("src", page_params.realm_logo_url);
|
2019-02-21 02:55:48 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (page_params.realm_night_logo_source === "D" && page_params.realm_logo_source !== "D") {
|
2019-10-25 09:15:16 +02:00
|
|
|
// If no night mode logo is uploaded but a day mode one
|
|
|
|
// is, use the day mode one; this handles the common case
|
|
|
|
// of transparent background logos that look good on both
|
|
|
|
// night and day themes. See also similar code in admin.js.
|
2019-02-21 02:55:48 +01:00
|
|
|
|
2020-06-17 07:43:53 +02:00
|
|
|
$("#realm-night-logo-upload-widget .image-block").attr("src", page_params.realm_logo_url);
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#realm-night-logo-upload-widget .image-block").attr(
|
|
|
|
"src",
|
|
|
|
page_params.realm_night_logo_url,
|
|
|
|
);
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2019-02-21 02:55:48 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
(page_params.color_scheme === settings_config.color_scheme_values.night.code &&
|
|
|
|
page_params.realm_night_logo_source !== "D") ||
|
|
|
|
(page_params.color_scheme === settings_config.color_scheme_values.automatic.code &&
|
|
|
|
page_params.realm_night_logo_source !== "D" &&
|
|
|
|
window.matchMedia &&
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches)
|
|
|
|
) {
|
2019-10-25 09:15:16 +02:00
|
|
|
$("#realm-logo").attr("src", page_params.realm_night_logo_url);
|
|
|
|
} else {
|
|
|
|
$("#realm-logo").attr("src", page_params.realm_logo_url);
|
|
|
|
}
|
2019-01-27 08:25:10 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
change_logo_delete_button(
|
|
|
|
page_params.realm_logo_source,
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-day-logo-upload-widget .image-delete-button"),
|
2020-07-15 00:34:28 +02:00
|
|
|
file_input,
|
|
|
|
);
|
|
|
|
change_logo_delete_button(
|
|
|
|
page_params.realm_night_logo_source,
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-night-logo-upload-widget .image-delete-button"),
|
2020-07-15 00:34:28 +02:00
|
|
|
night_file_input,
|
|
|
|
);
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2018-08-16 01:26:55 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.realm_logo = exports;
|