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-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-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() {
|
|
|
|
const rendered_set_status_overlay = render_set_status_overlay();
|
|
|
|
$(".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 user_id = people.my_current_user_id();
|
|
|
|
const old_status_text = user_status.get_status_text(user_id);
|
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-02-10 17:03:40 +01:00
|
|
|
const new_status_text = input_field().val().trim();
|
2019-01-26 21:14:33 +01:00
|
|
|
|
|
|
|
if (old_status_text === new_status_text) {
|
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,
|
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-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
|
|
|
|
|
|
|
if (old_status_text === new_status_text) {
|
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() {
|
|
|
|
if (input_field().val() !== "") {
|
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-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();
|
|
|
|
update_button();
|
2019-01-26 21:14:33 +01:00
|
|
|
});
|
2021-02-10 17:03:40 +01:00
|
|
|
}
|