2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2022-07-21 06:24:16 +02:00
|
|
|
import render_add_alert_word from "../templates/settings/add_alert_word.hbs";
|
2021-02-28 01:08:54 +01:00
|
|
|
import render_alert_word_settings_item from "../templates/settings/alert_word_settings_item.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:08:54 +01:00
|
|
|
import * as alert_words from "./alert_words";
|
|
|
|
import * as channel from "./channel";
|
2022-07-21 06:24:16 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
|
|
|
import {$t, $t_html} from "./i18n";
|
2022-02-21 18:23:21 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
2022-07-21 06:24:16 +02:00
|
|
|
import * as ui_report from "./ui_report";
|
2022-02-21 18:23:21 +01:00
|
|
|
|
|
|
|
export let loaded = false;
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2022-02-26 01:58:57 +01:00
|
|
|
export function rerender_alert_words_ui() {
|
2022-02-21 18:23:21 +01:00
|
|
|
if (!loaded) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-26 01:58:57 +01:00
|
|
|
|
2020-02-26 23:53:46 +01:00
|
|
|
const words = alert_words.get_word_list();
|
2020-04-15 23:45:34 +02:00
|
|
|
words.sort();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $word_list = $("#alert-words-table");
|
2017-06-07 21:46:11 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($word_list, words, {
|
2022-02-21 18:23:21 +01:00
|
|
|
name: "alert-words-list",
|
|
|
|
modifier(alert_word) {
|
|
|
|
return render_alert_word_settings_item({alert_word});
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $("#alert-word-settings"),
|
|
|
|
$simplebar_container: $("#alert-word-settings .progressive-table-wrapper"),
|
2022-02-21 18:23:21 +01:00
|
|
|
});
|
2021-02-28 01:08:54 +01:00
|
|
|
}
|
2017-06-07 21:46:11 +02:00
|
|
|
|
2017-06-07 21:16:02 +02:00
|
|
|
function update_alert_word_status(status_text, is_error) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $alert_word_status = $("#alert_word_status");
|
2017-06-07 21:16:02 +02:00
|
|
|
if (is_error) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$alert_word_status.removeClass("alert-success").addClass("alert-danger");
|
2017-06-07 21:16:02 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$alert_word_status.removeClass("alert-danger").addClass("alert-success");
|
2017-06-07 21:16:02 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$alert_word_status.find(".alert_word_status_text").text(status_text);
|
|
|
|
$alert_word_status.show();
|
2017-06-07 21:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 06:24:16 +02:00
|
|
|
function add_alert_word(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const alert_word = $("#add-alert-word-name").val().trim();
|
|
|
|
|
|
|
|
if (alert_words.has_alert_word(alert_word)) {
|
|
|
|
ui_report.client_error(
|
|
|
|
$t({defaultMessage: "Alert word already exists!"}),
|
|
|
|
$("#dialog_error"),
|
|
|
|
);
|
|
|
|
dialog_widget.hide_dialog_spinner();
|
2017-06-11 21:30:06 +02:00
|
|
|
return;
|
2013-09-09 17:49:39 +02:00
|
|
|
}
|
2014-03-04 23:37:29 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const words_to_be_added = [alert_word];
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2022-07-21 06:24:16 +02:00
|
|
|
const data = {alert_words: JSON.stringify(words_to_be_added)};
|
|
|
|
dialog_widget.submit_api_request(channel.post, "/json/users/me/alert_words", data);
|
2014-03-04 23:37:29 +01:00
|
|
|
}
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2017-06-07 21:36:43 +02:00
|
|
|
function remove_alert_word(alert_word) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const words_to_be_removed = [alert_word];
|
2017-06-07 21:36:43 +02:00
|
|
|
channel.del({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/alert_words",
|
2017-06-07 21:36:43 +02:00
|
|
|
data: {alert_words: JSON.stringify(words_to_be_removed)},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2021-04-13 06:51:54 +02:00
|
|
|
update_alert_word_status(
|
2022-11-20 22:12:05 +01:00
|
|
|
$t(
|
|
|
|
{defaultMessage: `Alert word "{alert_word}" removed successfully!`},
|
|
|
|
{alert_word},
|
|
|
|
),
|
2021-04-13 06:51:54 +02:00
|
|
|
false,
|
|
|
|
);
|
2017-06-07 21:36:43 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error() {
|
2021-04-13 06:51:54 +02:00
|
|
|
update_alert_word_status($t({defaultMessage: "Error removing alert word!"}), true);
|
2017-06-07 21:36:43 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:24:16 +02:00
|
|
|
export function show_add_alert_word_modal() {
|
|
|
|
const html_body = render_add_alert_word();
|
|
|
|
|
|
|
|
function add_alert_word_post_render() {
|
|
|
|
const $add_user_group_input_element = $("#add-alert-word-name");
|
|
|
|
const $add_user_group_submit_button = $("#add-alert-word .dialog_submit_button");
|
|
|
|
$add_user_group_submit_button.prop("disabled", true);
|
|
|
|
|
|
|
|
$add_user_group_input_element.on("input", () => {
|
|
|
|
$add_user_group_submit_button.prop(
|
|
|
|
"disabled",
|
|
|
|
$add_user_group_input_element.val().trim() === "",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_widget.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Add a new alert word"}),
|
|
|
|
html_body,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Add"}),
|
|
|
|
help_link: "/help/pm-mention-alert-notifications#alert-words",
|
|
|
|
form_id: "add-alert-word-form",
|
|
|
|
id: "add-alert-word",
|
|
|
|
loading_spinner: true,
|
|
|
|
on_click: add_alert_word,
|
|
|
|
on_shown: () => $("#add-alert-word-name").trigger("focus"),
|
|
|
|
post_render: add_alert_word_post_render,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:08:54 +01:00
|
|
|
export function set_up_alert_words() {
|
2014-03-04 23:37:29 +01:00
|
|
|
// The settings page must be rendered before this function gets called.
|
2022-02-21 18:23:21 +01:00
|
|
|
loaded = true;
|
2022-02-26 01:58:57 +01:00
|
|
|
rerender_alert_words_ui();
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2022-07-21 06:24:16 +02:00
|
|
|
$("#open-add-alert-word-modal").on("click", () => {
|
|
|
|
show_add_alert_word_modal();
|
2013-09-09 17:49:39 +02:00
|
|
|
});
|
|
|
|
|
2022-02-21 18:23:21 +01:00
|
|
|
$("#alert-words-table").on("click", ".remove-alert-word", (event) => {
|
2021-02-02 01:35:51 +01:00
|
|
|
const word = $(event.currentTarget).parents("tr").find(".value").text().trim();
|
2017-06-07 21:36:43 +02:00
|
|
|
remove_alert_word(word);
|
2013-09-09 17:49:39 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#alert-word-settings").on("click", ".close-alert-word-status", (event) => {
|
2016-07-14 01:32:25 +02:00
|
|
|
event.preventDefault();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $alert = $(event.currentTarget).parents(".alert");
|
|
|
|
$alert.hide();
|
2016-07-14 01:32:25 +02:00
|
|
|
});
|
2021-02-28 01:08:54 +01:00
|
|
|
}
|
2022-02-21 18:23:21 +01:00
|
|
|
|
|
|
|
export function reset() {
|
|
|
|
loaded = false;
|
|
|
|
}
|