zulip/web/src/user_status_ui.ts

218 lines
7.3 KiB
TypeScript
Raw Normal View History

import $ from "jquery";
import render_set_status_overlay from "../templates/set_status_overlay.hbs";
import render_status_emoji_selector from "../templates/status_emoji_selector.hbs";
import * as dialog_widget from "./dialog_widget";
import * as emoji from "./emoji";
import type {EmojiRenderingDetails} from "./emoji";
import {$t, $t_html} from "./i18n";
import * as keydown_util from "./keydown_util";
import * as people from "./people";
import * as user_status from "./user_status";
import type {UserStatusEmojiInfo} from "./user_status";
let selected_emoji_info: Partial<UserStatusEmojiInfo> = {};
let default_status_messages_and_emoji_info: {status_text: string; emoji: EmojiRenderingDetails}[];
export function set_selected_emoji_info(emoji_info: Partial<UserStatusEmojiInfo>): void {
selected_emoji_info = {...emoji_info};
rebuild_status_emoji_selector_ui(selected_emoji_info);
}
export function input_field(): JQuery {
2023-03-15 19:45:26 +01:00
return $("#set-user-status-modal input.user-status");
}
export function submit_button(): JQuery {
return $("#set-user-status-modal .dialog_submit_button");
}
export function open_user_status_modal(): void {
const user_id = people.my_current_user_id();
const selected_emoji_info = user_status.get_status_emoji(user_id) ?? {};
const rendered_set_status_overlay = render_set_status_overlay({
default_status_messages_and_emoji_info,
selected_emoji_info,
});
dialog_widget.launch({
html_heading: $t_html({defaultMessage: "Set status"}),
html_body: rendered_set_status_overlay,
html_submit_button: $t_html({defaultMessage: "Save"}),
id: "set-user-status-modal",
on_click: submit_new_status,
post_render: user_status_post_render,
on_shown() {
input_field().trigger("focus");
},
});
}
export function submit_new_status(): void {
const user_id = people.my_current_user_id();
let old_status_text = user_status.get_status_text(user_id) ?? "";
old_status_text = old_status_text.trim();
const old_emoji_info = user_status.get_status_emoji(user_id);
const new_status_text = input_field().val()?.toString().trim() ?? "";
if (
old_status_text === new_status_text &&
!emoji_status_fields_changed(selected_emoji_info, old_emoji_info)
) {
2023-10-10 14:32:20 +02:00
dialog_widget.close();
return;
}
user_status.server_update_status({
status_text: new_status_text,
emoji_name: selected_emoji_info.emoji_name ?? "",
emoji_code: selected_emoji_info.emoji_code ?? "",
reaction_type: selected_emoji_info.reaction_type ?? "",
success() {
2023-10-10 14:32:20 +02:00
dialog_widget.close();
},
});
}
export function update_button(): void {
const user_id = people.my_current_user_id();
let old_status_text = user_status.get_status_text(user_id) ?? "";
old_status_text = old_status_text.trim();
const old_emoji_info = user_status.get_status_emoji(user_id);
const new_status_text = input_field().val()?.toString().trim() ?? "";
const $button = submit_button();
if (
old_status_text === new_status_text &&
!emoji_status_fields_changed(selected_emoji_info, old_emoji_info)
) {
$button.prop("disabled", true);
} else {
$button.prop("disabled", false);
}
}
export function toggle_clear_message_button(): void {
if (input_field().val() !== "" || selected_emoji_info.emoji_name) {
$("#clear_status_message_button").prop("disabled", false);
} else {
$("#clear_status_message_button").prop("disabled", true);
}
}
export function clear_message(): void {
const $field = input_field();
$field.val("");
$("#clear_status_message_button").prop("disabled", true);
}
export function user_status_picker_open(): boolean {
return $("#set-user-status-modal").length !== 0;
}
function emoji_status_fields_changed(
selected_emoji_info: Partial<UserStatusEmojiInfo>,
old_emoji_info?: UserStatusEmojiInfo,
): boolean {
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;
}
function rebuild_status_emoji_selector_ui(selected_emoji_info: Partial<UserStatusEmojiInfo>): void {
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});
$("#set-user-status-modal .status-emoji-wrapper").html(rendered_status_emoji_selector);
}
function user_status_post_render(): void {
const user_id = people.my_current_user_id();
const old_status_text = user_status.get_status_text(user_id) ?? "";
const old_emoji_info = user_status.get_status_emoji(user_id) ?? {};
set_selected_emoji_info(old_emoji_info);
const $field = input_field();
$field.val(old_status_text);
toggle_clear_message_button();
const $button = submit_button();
$button.prop("disabled", true);
$("#set-user-status-modal .user-status-value").on("click", (event) => {
event.stopPropagation();
const user_status_value = $(event.currentTarget).text().trim();
2023-03-15 19:45:26 +01:00
$("input.user-status").val(user_status_value);
const emoji_info =
default_status_messages_and_emoji_info.find(
(status) => status.status_text === user_status_value,
)?.emoji ?? {};
set_selected_emoji_info(emoji_info);
toggle_clear_message_button();
update_button();
});
input_field().on("keypress", (event) => {
if (keydown_util.is_enter_event(event)) {
event.preventDefault();
submit_new_status();
}
});
input_field().on("keyup", () => {
update_button();
toggle_clear_message_button();
});
$("#clear_status_message_button").on("click", () => {
clear_message();
2024-02-18 07:31:25 +01:00
set_selected_emoji_info({});
update_button();
});
}
export function initialize(): void {
default_status_messages_and_emoji_info = [
{
status_text: $t({defaultMessage: "Busy"}),
emoji: emoji.get_emoji_details_by_name("working_on_it"),
},
{
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"),
},
{
status_text: $t({defaultMessage: "At the office"}),
emoji: emoji.get_emoji_details_by_name("office"),
},
];
}