2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:16:32 +01:00
|
|
|
import * as channel from "./channel";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-07-08 13:14:17 +02:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 01:16:32 +01:00
|
|
|
import * as upload_widget from "./upload_widget";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:16:32 +01:00
|
|
|
export function build_realm_logo_widget(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
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $delete_button_elem = $(logo_section_id + " .image-delete-button");
|
|
|
|
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 () {
|
2022-01-25 11:36:19 +01:00
|
|
|
return $file_input_elem.expectOne();
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2019-02-27 15:45:26 +01:00
|
|
|
|
2020-03-28 20:28:14 +01:00
|
|
|
if (!page_params.is_admin) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-03-28 20:28:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (logo_source === "D") {
|
2022-01-25 11:36:19 +01:00
|
|
|
$delete_button_elem.hide();
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$delete_button_elem.show();
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2019-02-27 15:45:26 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {night: JSON.stringify(is_night)};
|
2022-01-25 11:36:19 +01: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,
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_input_error_elem.expectOne(),
|
|
|
|
$upload_button_elem.expectOne(),
|
2019-10-25 09:15:16 +02:00
|
|
|
upload_function,
|
2021-05-25 18:49:36 +02:00
|
|
|
page_params.max_logo_file_size_mib,
|
2019-10-25 09:15:16 +02:00
|
|
|
);
|
2021-02-28 01:16:32 +01:00
|
|
|
}
|
2019-01-27 08:25:10 +01:00
|
|
|
|
2022-01-25 11:36:19 +01: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") {
|
2022-01-25 11:36:19 +01:00
|
|
|
$logo_delete_button.show();
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$logo_delete_button.hide();
|
2019-10-25 09:15:16 +02:00
|
|
|
// Need to clear input because of a small edge case
|
|
|
|
// where you try to upload the same image you just deleted.
|
2022-01-25 11:36:19 +01: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
|
|
|
|
2021-07-19 13:49:40 +02:00
|
|
|
export function render() {
|
2022-01-25 11:36:19 +01: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");
|
2020-06-17 07:43:53 +02:00
|
|
|
$("#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") {
|
2021-11-26 10:29:05 +01:00
|
|
|
// If no dark theme logo is uploaded but a light theme one
|
|
|
|
// is, use the light theme one; this handles the common case
|
2019-10-25 09:15:16 +02:00
|
|
|
// of transparent background logos that look good on both
|
2021-11-26 10:31:52 +01:00
|
|
|
// dark and light 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
|
|
|
|
2021-07-08 13:14:17 +02:00
|
|
|
if (settings_data.using_dark_theme() && page_params.realm_night_logo_source !== "D") {
|
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"),
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_input,
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
|
|
|
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"),
|
2022-01-25 11:36:19 +01:00
|
|
|
$night_file_input,
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 01:16:32 +01:00
|
|
|
}
|