2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-06-21 10:04:43 +02:00
|
|
|
import render_confirm_delete_user_avatar from "../templates/confirm_dialog/confirm_delete_user_avatar.hbs";
|
2021-03-23 08:42:16 +01:00
|
|
|
|
2021-02-28 01:15:45 +01:00
|
|
|
import * as channel from "./channel";
|
2021-01-18 12:31:44 +01:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-04-13 05:24:31 +02:00
|
|
|
import {$t_html} from "./i18n";
|
2021-03-21 16:46:13 +01:00
|
|
|
import * as settings_data from "./settings_data";
|
2024-02-13 02:08:24 +01:00
|
|
|
import {current_user, realm} from "./state_data";
|
2021-02-28 01:15:45 +01:00
|
|
|
import * as upload_widget from "./upload_widget";
|
2023-09-01 10:18:48 +02:00
|
|
|
import type {UploadFunction, UploadWidget} from "./upload_widget";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2023-09-01 10:18:48 +02:00
|
|
|
export function build_bot_create_widget(): UploadWidget {
|
2013-07-29 20:39:38 +02:00
|
|
|
// We have to do strange gyrations with the file input to clear it,
|
|
|
|
// where we replace it wholesale, so we generalize the file input with
|
|
|
|
// a callback function.
|
2023-09-01 10:18:48 +02:00
|
|
|
const get_file_input = function (): JQuery<HTMLInputElement> {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $("#bot_avatar_file_input");
|
2013-07-29 20:39:38 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $file_name_field = $("#bot_avatar_file");
|
|
|
|
const $input_error = $("#bot_avatar_file_input_error");
|
|
|
|
const $clear_button = $("#bot_avatar_clear_button");
|
|
|
|
const $upload_button = $("#bot_avatar_upload_button");
|
2023-01-02 19:19:52 +01:00
|
|
|
const $preview_text = $("#add_bot_preview_text");
|
|
|
|
const $preview_image = $("#add_bot_preview_image");
|
2017-02-21 03:15:22 +01:00
|
|
|
return upload_widget.build_widget(
|
2013-07-29 21:31:21 +02:00
|
|
|
get_file_input,
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_name_field,
|
|
|
|
$input_error,
|
|
|
|
$clear_button,
|
|
|
|
$upload_button,
|
2023-01-02 19:19:52 +01:00
|
|
|
$preview_text,
|
|
|
|
$preview_image,
|
2013-07-29 21:31:21 +02:00
|
|
|
);
|
2021-02-28 01:15:45 +01:00
|
|
|
}
|
2013-07-29 21:31:21 +02:00
|
|
|
|
2023-09-01 10:18:48 +02:00
|
|
|
export function build_bot_edit_widget($target: JQuery): UploadWidget {
|
|
|
|
const get_file_input = function (): JQuery<HTMLInputElement> {
|
|
|
|
return $target.find<HTMLInputElement>(".edit_bot_avatar_file_input");
|
2013-07-29 16:27:18 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $file_name_field = $target.find(".edit_bot_avatar_file");
|
|
|
|
const $input_error = $target.find(".edit_bot_avatar_error");
|
|
|
|
const $clear_button = $target.find(".edit_bot_avatar_clear_button");
|
|
|
|
const $upload_button = $target.find(".edit_bot_avatar_upload_button");
|
2023-01-02 19:19:52 +01:00
|
|
|
const $preview_text = $target.find(".edit_bot_avatar_preview_text");
|
|
|
|
const $preview_image = $target.find(".edit_bot_avatar_preview_image");
|
2013-07-29 16:27:18 +02:00
|
|
|
|
2017-02-21 03:15:22 +01:00
|
|
|
return upload_widget.build_widget(
|
2013-07-29 16:27:18 +02:00
|
|
|
get_file_input,
|
2022-01-25 11:36:19 +01:00
|
|
|
$file_name_field,
|
|
|
|
$input_error,
|
|
|
|
$clear_button,
|
|
|
|
$upload_button,
|
2023-01-02 19:19:52 +01:00
|
|
|
$preview_text,
|
|
|
|
$preview_image,
|
2013-07-29 16:27:18 +02:00
|
|
|
);
|
2021-02-28 01:15:45 +01:00
|
|
|
}
|
2013-07-29 16:27:18 +02:00
|
|
|
|
2023-09-01 10:18:48 +02:00
|
|
|
function display_avatar_delete_complete(): void {
|
2021-04-13 21:32:16 +02:00
|
|
|
$("#user-avatar-upload-widget .upload-spinner-background").css({visibility: "hidden"});
|
|
|
|
$("#user-avatar-upload-widget .image-upload-text").show();
|
|
|
|
$("#user-avatar-source").show();
|
|
|
|
}
|
|
|
|
|
2023-09-01 10:18:48 +02:00
|
|
|
function display_avatar_delete_started(): void {
|
2021-04-13 21:32:16 +02:00
|
|
|
$("#user-avatar-upload-widget .upload-spinner-background").css({visibility: "visible"});
|
|
|
|
$("#user-avatar-upload-widget .image-upload-text").hide();
|
|
|
|
$("#user-avatar-upload-widget .image-delete-button").hide();
|
|
|
|
}
|
|
|
|
|
2023-09-01 10:18:48 +02:00
|
|
|
export function build_user_avatar_widget(upload_function: UploadFunction): void {
|
|
|
|
const get_file_input = function (): JQuery<HTMLInputElement> {
|
2023-09-08 22:13:54 +02:00
|
|
|
return $<HTMLInputElement>("#user-avatar-upload-widget input.image_file_input").expectOne();
|
2013-10-28 15:49:38 +01:00
|
|
|
};
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.avatar_source === "G") {
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#user-avatar-upload-widget .image-delete-button").hide();
|
2018-03-02 18:33:20 +01:00
|
|
|
$("#user-avatar-source").show();
|
|
|
|
} else {
|
|
|
|
$("#user-avatar-source").hide();
|
2016-12-21 18:34:03 +01:00
|
|
|
}
|
2018-03-02 18:33:20 +01:00
|
|
|
|
2023-01-09 15:41:06 +01:00
|
|
|
if (!settings_data.user_can_change_avatar) {
|
2023-12-22 01:00:27 +01:00
|
|
|
return;
|
2023-01-09 15:41:06 +01:00
|
|
|
}
|
|
|
|
|
2021-10-23 18:40:05 +02:00
|
|
|
$("#user-avatar-upload-widget .image-delete-button").on("click", (e) => {
|
2016-12-21 18:34:03 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2023-09-01 10:18:48 +02:00
|
|
|
function delete_user_avatar(): void {
|
2021-04-13 21:32:16 +02:00
|
|
|
display_avatar_delete_started();
|
2023-09-01 10:18:48 +02:00
|
|
|
void channel.del({
|
2021-01-18 12:31:44 +01:00
|
|
|
url: "/json/users/me/avatar",
|
|
|
|
success() {
|
2021-04-13 21:32:16 +02:00
|
|
|
display_avatar_delete_complete();
|
|
|
|
|
2021-01-18 12:31:44 +01:00
|
|
|
// Need to clear input because of a small edge case
|
|
|
|
// where you try to upload the same image you just deleted.
|
|
|
|
get_file_input().val("");
|
|
|
|
// Rest of the work is done via the user_events -> avatar_url event we will get
|
|
|
|
},
|
2021-04-13 21:32:16 +02:00
|
|
|
error() {
|
|
|
|
display_avatar_delete_complete();
|
|
|
|
$("#user-avatar-upload-widget .image-delete-button").show();
|
|
|
|
},
|
2021-01-18 12:31:44 +01:00
|
|
|
});
|
|
|
|
}
|
2023-09-01 10:18:48 +02:00
|
|
|
const html_body = render_confirm_delete_user_avatar({});
|
2021-01-18 12:31:44 +01:00
|
|
|
|
|
|
|
confirm_dialog.launch({
|
2021-04-13 05:24:31 +02:00
|
|
|
html_heading: $t_html({defaultMessage: "Delete profile picture"}),
|
2021-01-18 12:31:44 +01:00
|
|
|
html_body,
|
|
|
|
on_click: delete_user_avatar,
|
2016-12-21 18:34:03 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-22 01:00:27 +01:00
|
|
|
upload_widget.build_direct_upload_widget(
|
2023-01-09 15:41:06 +01:00
|
|
|
get_file_input,
|
|
|
|
$("#user-avatar-upload-widget .image_file_input_error").expectOne(),
|
|
|
|
$("#user-avatar-upload-widget .image_upload_button").expectOne(),
|
|
|
|
upload_function,
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.max_avatar_file_size_mib,
|
2023-01-09 15:41:06 +01:00
|
|
|
);
|
2021-02-28 01:15:45 +01:00
|
|
|
}
|