2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-06-28 10:02:45 +02:00
|
|
|
import * as emoji from "../shared/js/emoji";
|
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-02-28 01:03:46 +01:00
|
|
|
import * as overlays from "./overlays";
|
2021-02-10 17:03:40 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as user_status from "./user_status";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-06-28 10:02:45 +02:00
|
|
|
let selected_emoji_info = {};
|
|
|
|
|
|
|
|
export function set_selected_emoji_info(emoji_info) {
|
|
|
|
selected_emoji_info = {...emoji_info};
|
|
|
|
rebuild_status_emoji_selector_ui(selected_emoji_info);
|
|
|
|
}
|
2021-02-10 17:03:40 +01:00
|
|
|
export function input_field() {
|
2021-06-24 18:46:59 +02: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
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
export function submit_button() {
|
2021-06-24 18:46:59 +02:00
|
|
|
return $("#set_user_status_modal .set_user_status");
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-06-24 18:46:59 +02:00
|
|
|
export function open_user_status_modal() {
|
2021-06-28 10:02:45 +02:00
|
|
|
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,
|
|
|
|
});
|
2021-06-24 18:46:59 +02:00
|
|
|
$(".app").append(rendered_set_status_overlay);
|
|
|
|
|
|
|
|
overlays.open_modal("#set_user_status_modal", {autoremove: true});
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const old_status_text = user_status.get_status_text(user_id);
|
2021-06-28 10:02:45 +02:00
|
|
|
const old_emoji_info = user_status.get_status_emoji(user_id) || {};
|
|
|
|
set_selected_emoji_info(old_emoji_info);
|
2021-02-10 17:03:40 +01:00
|
|
|
const field = input_field();
|
2019-01-26 21:14:33 +01:00
|
|
|
field.val(old_status_text);
|
2020-07-20 21:24:26 +02:00
|
|
|
field.trigger("select");
|
|
|
|
field.trigger("focus");
|
2021-02-10 17:03:40 +01:00
|
|
|
toggle_clear_message_button();
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
const button = submit_button();
|
2020-07-22 02:59:06 +02:00
|
|
|
button.prop("disabled", true);
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-06-24 18:46:59 +02:00
|
|
|
export function close_user_status_modal() {
|
|
|
|
overlays.close_modal("#set_user_status_modal");
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
export function submit_new_status() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = people.my_current_user_id();
|
2020-07-15 01:29:15 +02: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();
|
2021-06-28 10:02:45 +02:00
|
|
|
const old_emoji_info = user_status.get_status_emoji(user_id) || {};
|
2021-02-10 17:03:40 +01:00
|
|
|
const new_status_text = input_field().val().trim();
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-06-28 10:02:45 +02:00
|
|
|
if (
|
|
|
|
old_status_text === new_status_text &&
|
|
|
|
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
|
|
|
|
) {
|
2021-06-24 18:46:59 +02:00
|
|
|
close_user_status_modal();
|
2019-01-26 21:14:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
user_status.server_update({
|
|
|
|
status_text: new_status_text,
|
2021-06-28 10:02:45 +02: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() {
|
2021-06-24 18:46:59 +02:00
|
|
|
close_user_status_modal();
|
2019-01-26 21:14:33 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-01-26 21:14:33 +01:00
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
export function update_button() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = people.my_current_user_id();
|
2020-07-15 01:29:15 +02: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();
|
2021-06-28 10:02:45 +02:00
|
|
|
const old_emoji_info = user_status.get_status_emoji(user_id) || {};
|
2021-02-10 17:03:40 +01:00
|
|
|
const new_status_text = input_field().val().trim();
|
|
|
|
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 &&
|
|
|
|
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
|
|
|
|
) {
|
2020-07-22 02:59:06 +02:00
|
|
|
button.prop("disabled", true);
|
2019-01-26 21:14:33 +01:00
|
|
|
} else {
|
2020-07-22 02:59:06 +02: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
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
export function toggle_clear_message_button() {
|
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
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
export function clear_message() {
|
|
|
|
const field = input_field();
|
2020-07-15 01:29:15 +02:00
|
|
|
field.val("");
|
|
|
|
$("#clear_status_message_button").prop("disabled", true);
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|
2019-04-28 22:32:38 +02:00
|
|
|
|
2021-06-27 21:24:11 +02:00
|
|
|
export function user_status_picker_open() {
|
|
|
|
return $("#set_user_status_modal").length !== 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:02:45 +02:00
|
|
|
function rebuild_status_emoji_selector_ui(selected_emoji_info) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:03:40 +01:00
|
|
|
export function initialize() {
|
2021-06-14 09:18:22 +02:00
|
|
|
$("body").on("click", ".user-status-value", (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
const user_status_value = $(event.currentTarget).text();
|
|
|
|
$("input.user_status").val(user_status_value);
|
|
|
|
toggle_clear_message_button();
|
|
|
|
update_button();
|
|
|
|
});
|
|
|
|
|
2021-06-24 18:46:59 +02:00
|
|
|
$("body").on("click", "#set_user_status_modal .set_user_status", () => {
|
2021-02-10 17:03:40 +01:00
|
|
|
submit_new_status();
|
2019-01-26 21:14:33 +01:00
|
|
|
});
|
|
|
|
|
2021-06-24 18:46:59 +02:00
|
|
|
$("body").on("keypress", "#set_user_status_modal .user_status", (event) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event.key === "Enter") {
|
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-06-24 18:46:59 +02:00
|
|
|
$("body").on("keyup", "#set_user_status_modal input.user_status", () => {
|
2021-02-10 17:03:40 +01:00
|
|
|
update_button();
|
|
|
|
toggle_clear_message_button();
|
2019-04-28 22:32:38 +02:00
|
|
|
});
|
|
|
|
|
2021-06-14 09:18:22 +02:00
|
|
|
$("body").on("click", "#clear_status_message_button", () => {
|
2021-02-10 17:03:40 +01:00
|
|
|
clear_message();
|
2021-06-28 10:02:45 +02: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
|
|
|
}
|