2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:16:09 +01:00
|
|
|
import * as channel from "./channel";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:16:09 +01:00
|
|
|
import * as upload_widget from "./upload_widget";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:16:09 +01:00
|
|
|
export function build_realm_icon_widget(upload_function) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const get_file_input = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $("#realm-icon-upload-widget .image_file_input").expectOne();
|
2019-10-25 09:15:16 +02:00
|
|
|
};
|
2017-02-21 03:41:20 +01:00
|
|
|
|
2020-03-28 20:33:49 +01:00
|
|
|
if (!page_params.is_admin) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-03-28 20:33:49 +01:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (page_params.realm_icon_source === "G") {
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-icon-upload-widget .image-delete-button").hide();
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-icon-upload-widget .image-delete-button").show();
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-icon-upload-widget .image-delete-button").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/icon",
|
2017-02-21 03:41:20 +01:00
|
|
|
});
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
2017-02-21 03:41:20 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
return upload_widget.build_direct_upload_widget(
|
|
|
|
get_file_input,
|
2020-06-17 05:58:39 +02:00
|
|
|
$("#realm-icon-upload-widget .image_file_input_error").expectOne(),
|
|
|
|
$("#realm-icon-upload-widget .image_upload_button").expectOne(),
|
2019-10-25 09:15:16 +02:00
|
|
|
upload_function,
|
2021-05-22 09:02:47 +02:00
|
|
|
page_params.max_icon_file_size_mib,
|
2019-10-25 09:15:16 +02:00
|
|
|
);
|
2021-02-28 01:16:09 +01:00
|
|
|
}
|
2017-02-21 03:41:20 +01:00
|
|
|
|
2021-02-28 01:16:09 +01:00
|
|
|
export function rerender() {
|
2020-06-17 05:58:39 +02:00
|
|
|
$("#realm-icon-upload-widget .image-block").attr("src", page_params.realm_icon_url);
|
2020-07-15 01:29:15 +02:00
|
|
|
if (page_params.realm_icon_source === "U") {
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-icon-upload-widget .image-delete-button").show();
|
2019-10-25 09:15:16 +02:00
|
|
|
} else {
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#realm-icon-upload-widget .image-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.
|
2020-06-17 05:58:39 +02:00
|
|
|
const file_input = $("#realm-icon-upload-widget .image_file_input");
|
2020-07-15 01:29:15 +02:00
|
|
|
file_input.val("");
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
2021-02-28 01:16:09 +01:00
|
|
|
}
|