2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-06-14 09:18:22 +02:00
|
|
|
import render_set_status_overlay from "../templates/set_status_overlay.hbs";
|
2021-06-28 10:02:45 +02:00
|
|
|
import render_status_emoji_selector from "../templates/status_emoji_selector.hbs";
|
2021-06-14 09:18:22 +02:00
|
|
|
|
2021-07-07 09:16:19 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
2022-03-25 14:47:51 +01:00
|
|
|
import * as emoji from "./emoji";
|
2024-02-28 20:06:16 +01:00
|
|
|
import type {EmojiRenderingDetails} from "./emoji";
|
2021-07-07 09:16:19 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2022-09-28 08:27:24 +02:00
|
|
|
import * as keydown_util from "./keydown_util";
|
2021-02-10 17:03:40 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as user_status from "./user_status";
|
2024-02-28 20:06:16 +01:00
|
|
|
import type {UserStatusEmojiInfo} from "./user_status";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
let selected_emoji_info: Partial<UserStatusEmojiInfo> = {};
|
|
|
|
let default_status_messages_and_emoji_info: {status_text: string; emoji: EmojiRenderingDetails}[];
|
2021-06-28 10:02:45 +02:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function set_selected_emoji_info(emoji_info: Partial<UserStatusEmojiInfo>): void {
|
2021-06-28 10:02:45 +02:00
|
|
|
selected_emoji_info = {...emoji_info};
|
|
|
|
rebuild_status_emoji_selector_ui(selected_emoji_info);
|
|
|
|
}
|
2024-02-28 20:06:16 +01:00
|
|
|
export function input_field(): JQuery {
|
2023-03-15 19:45:26 +01:00
|
|
|
return $("#set-user-status-modal input.user-status");
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function submit_button(): JQuery {
|
2023-03-15 19:39:06 +01:00
|
|
|
return $("#set-user-status-modal .dialog_submit_button");
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function open_user_status_modal(): void {
|
2021-06-28 10:02:45 +02:00
|
|
|
const user_id = people.my_current_user_id();
|
user_status_ui: Replace logical OR with null coalescing operator.
While || targets null, undefined, false, NaN, 0, -0, 0n, "",
and document.all, ??(null coalescing) specifically targets
null and undefined,which we are looking for here.
2024-02-18 11:29:05 +01:00
|
|
|
const selected_emoji_info = user_status.get_status_emoji(user_id) ?? {};
|
2021-06-28 10:02:45 +02:00
|
|
|
const rendered_set_status_overlay = render_set_status_overlay({
|
|
|
|
default_status_messages_and_emoji_info,
|
|
|
|
selected_emoji_info,
|
|
|
|
});
|
2021-06-24 18:46:59 +02:00
|
|
|
|
2021-07-07 09:16:19 +02:00
|
|
|
dialog_widget.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Set status"}),
|
|
|
|
html_body: rendered_set_status_overlay,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Save"}),
|
2023-03-15 19:39:06 +01:00
|
|
|
id: "set-user-status-modal",
|
2021-07-07 09:16:19 +02:00
|
|
|
on_click: submit_new_status,
|
|
|
|
post_render: user_status_post_render,
|
2022-11-17 23:33:43 +01:00
|
|
|
on_shown() {
|
2021-07-07 09:16:19 +02:00
|
|
|
input_field().trigger("focus");
|
|
|
|
},
|
|
|
|
});
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function submit_new_status(): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = people.my_current_user_id();
|
user_status_ui: Replace logical OR with null coalescing operator.
While || targets null, undefined, false, NaN, 0, -0, 0n, "",
and document.all, ??(null coalescing) specifically targets
null and undefined,which we are looking for here.
2024-02-18 11:29:05 +01:00
|
|
|
let old_status_text = user_status.get_status_text(user_id) ?? "";
|
2019-01-26 21:14:33 +01:00
|
|
|
old_status_text = old_status_text.trim();
|
2024-02-28 18:58:39 +01:00
|
|
|
const old_emoji_info = user_status.get_status_emoji(user_id);
|
2024-02-28 20:06:16 +01:00
|
|
|
const new_status_text = input_field().val()?.toString().trim() ?? "";
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-06-28 10:02:45 +02:00
|
|
|
if (
|
|
|
|
old_status_text === new_status_text &&
|
2024-02-28 18:58:39 +01:00
|
|
|
!emoji_status_fields_changed(selected_emoji_info, old_emoji_info)
|
2021-06-28 10:02:45 +02:00
|
|
|
) {
|
2023-10-10 14:32:20 +02:00
|
|
|
dialog_widget.close();
|
2019-01-26 21:14:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:49:36 +02:00
|
|
|
user_status.server_update_status({
|
2019-01-26 21:14:33 +01:00
|
|
|
status_text: new_status_text,
|
user_status_ui: Replace logical OR with null coalescing operator.
While || targets null, undefined, false, NaN, 0, -0, 0n, "",
and document.all, ??(null coalescing) specifically targets
null and undefined,which we are looking for here.
2024-02-18 11:29:05 +01:00
|
|
|
emoji_name: selected_emoji_info.emoji_name ?? "",
|
|
|
|
emoji_code: selected_emoji_info.emoji_code ?? "",
|
|
|
|
reaction_type: selected_emoji_info.reaction_type ?? "",
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2023-10-10 14:32:20 +02:00
|
|
|
dialog_widget.close();
|
2019-01-26 21:14:33 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function update_button(): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = people.my_current_user_id();
|
user_status_ui: Replace logical OR with null coalescing operator.
While || targets null, undefined, false, NaN, 0, -0, 0n, "",
and document.all, ??(null coalescing) specifically targets
null and undefined,which we are looking for here.
2024-02-18 11:29:05 +01:00
|
|
|
let old_status_text = user_status.get_status_text(user_id) ?? "";
|
2019-01-26 21:14:33 +01:00
|
|
|
old_status_text = old_status_text.trim();
|
2024-02-28 18:58:39 +01:00
|
|
|
const old_emoji_info = user_status.get_status_emoji(user_id);
|
2024-02-28 20:06:16 +01:00
|
|
|
const new_status_text = input_field().val()?.toString().trim() ?? "";
|
2022-01-25 11:36:19 +01:00
|
|
|
const $button = submit_button();
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-06-28 10:02:45 +02:00
|
|
|
if (
|
|
|
|
old_status_text === new_status_text &&
|
2024-02-28 18:58:39 +01:00
|
|
|
!emoji_status_fields_changed(selected_emoji_info, old_emoji_info)
|
2021-06-28 10:02:45 +02:00
|
|
|
) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$button.prop("disabled", true);
|
2019-01-26 21:14:33 +01:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$button.prop("disabled", false);
|
2019-01-26 21:14:33 +01:00
|
|
|
}
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function toggle_clear_message_button(): void {
|
2021-06-28 10:02:45 +02:00
|
|
|
if (input_field().val() !== "" || selected_emoji_info.emoji_name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#clear_status_message_button").prop("disabled", false);
|
2019-04-28 22:32:38 +02:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#clear_status_message_button").prop("disabled", true);
|
2019-04-28 22:32:38 +02:00
|
|
|
}
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-04-28 22:32:38 +02:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function clear_message(): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $field = input_field();
|
|
|
|
$field.val("");
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#clear_status_message_button").prop("disabled", true);
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-04-28 22:32:38 +02:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function user_status_picker_open(): boolean {
|
2023-03-15 19:39:06 +01:00
|
|
|
return $("#set-user-status-modal").length !== 0;
|
2021-06-27 21:24:11 +02:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
function emoji_status_fields_changed(
|
|
|
|
selected_emoji_info: Partial<UserStatusEmojiInfo>,
|
|
|
|
old_emoji_info?: UserStatusEmojiInfo,
|
|
|
|
): boolean {
|
2024-02-28 18:58:39 +01:00
|
|
|
if (old_emoji_info === undefined && Object.keys(selected_emoji_info).length === 0) {
|
|
|
|
return false;
|
|
|
|
} else if (
|
|
|
|
old_emoji_info !== undefined &&
|
|
|
|
old_emoji_info.emoji_name === selected_emoji_info.emoji_name &&
|
|
|
|
old_emoji_info.reaction_type === selected_emoji_info.reaction_type &&
|
|
|
|
old_emoji_info.emoji_code === selected_emoji_info.emoji_code
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
function rebuild_status_emoji_selector_ui(selected_emoji_info: Partial<UserStatusEmojiInfo>): void {
|
2021-06-28 10:02:45 +02:00
|
|
|
let selected_emoji = null;
|
|
|
|
if (selected_emoji_info && Object.keys(selected_emoji_info).length) {
|
|
|
|
selected_emoji = selected_emoji_info;
|
|
|
|
}
|
|
|
|
const rendered_status_emoji_selector = render_status_emoji_selector({selected_emoji});
|
2023-03-15 19:47:37 +01:00
|
|
|
$("#set-user-status-modal .status-emoji-wrapper").html(rendered_status_emoji_selector);
|
2021-06-28 10:02:45 +02:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
function user_status_post_render(): void {
|
2021-07-07 09:16:19 +02:00
|
|
|
const user_id = people.my_current_user_id();
|
2024-02-28 20:06:16 +01:00
|
|
|
const old_status_text = user_status.get_status_text(user_id) ?? "";
|
user_status_ui: Replace logical OR with null coalescing operator.
While || targets null, undefined, false, NaN, 0, -0, 0n, "",
and document.all, ??(null coalescing) specifically targets
null and undefined,which we are looking for here.
2024-02-18 11:29:05 +01:00
|
|
|
const old_emoji_info = user_status.get_status_emoji(user_id) ?? {};
|
2021-07-07 09:16:19 +02:00
|
|
|
set_selected_emoji_info(old_emoji_info);
|
2022-01-25 11:36:19 +01:00
|
|
|
const $field = input_field();
|
|
|
|
$field.val(old_status_text);
|
2021-07-07 09:16:19 +02:00
|
|
|
toggle_clear_message_button();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $button = submit_button();
|
|
|
|
$button.prop("disabled", true);
|
2021-07-07 09:16:19 +02:00
|
|
|
|
2023-03-15 19:39:06 +01:00
|
|
|
$("#set-user-status-modal .user-status-value").on("click", (event) => {
|
2021-06-14 09:18:22 +02:00
|
|
|
event.stopPropagation();
|
2021-07-01 17:25:18 +02:00
|
|
|
const user_status_value = $(event.currentTarget).text().trim();
|
2023-03-15 19:45:26 +01:00
|
|
|
$("input.user-status").val(user_status_value);
|
2021-07-01 17:25:18 +02:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
const emoji_info =
|
|
|
|
default_status_messages_and_emoji_info.find(
|
|
|
|
(status) => status.status_text === user_status_value,
|
|
|
|
)?.emoji ?? {};
|
2021-07-01 17:25:18 +02:00
|
|
|
set_selected_emoji_info(emoji_info);
|
2021-06-14 09:18:22 +02:00
|
|
|
toggle_clear_message_button();
|
|
|
|
update_button();
|
|
|
|
});
|
|
|
|
|
2021-07-07 09:16:19 +02:00
|
|
|
input_field().on("keypress", (event) => {
|
2022-09-28 08:27:24 +02:00
|
|
|
if (keydown_util.is_enter_event(event)) {
|
2020-03-05 10:08:27 +01:00
|
|
|
event.preventDefault();
|
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
submit_new_status();
|
2020-03-05 10:08:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-07 09:16:19 +02:00
|
|
|
input_field().on("keyup", () => {
|
2021-02-10 17:03:40 +01:00
|
|
|
update_button();
|
|
|
|
toggle_clear_message_button();
|
2019-04-28 22:32:38 +02:00
|
|
|
});
|
|
|
|
|
2021-07-07 09:16:19 +02:00
|
|
|
$("#clear_status_message_button").on("click", () => {
|
2021-02-10 17:03:40 +01:00
|
|
|
clear_message();
|
2024-02-18 07:31:25 +01:00
|
|
|
set_selected_emoji_info({});
|
2021-02-10 17:03:40 +01:00
|
|
|
update_button();
|
2019-01-26 21:14:33 +01:00
|
|
|
});
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2021-07-07 09:16:19 +02:00
|
|
|
|
2024-02-28 20:06:16 +01:00
|
|
|
export function initialize(): void {
|
2021-07-07 09:16:19 +02:00
|
|
|
default_status_messages_and_emoji_info = [
|
2022-02-18 15:38:34 +01:00
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "Busy"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("working_on_it"),
|
|
|
|
},
|
2021-07-07 09:16:19 +02:00
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "In a meeting"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("calendar"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "Commuting"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("bus"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "Out sick"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("hurt"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "Vacationing"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("palm_tree"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "Working remotely"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("house"),
|
|
|
|
},
|
2022-09-01 18:51:42 +02:00
|
|
|
{
|
|
|
|
status_text: $t({defaultMessage: "At the office"}),
|
|
|
|
emoji: emoji.get_emoji_details_by_name("office"),
|
|
|
|
},
|
2021-07-07 09:16:19 +02:00
|
|
|
];
|
|
|
|
}
|