2014-03-04 23:37:29 +01:00
|
|
|
var alert_words_ui = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-06-07 21:46:11 +02:00
|
|
|
exports.render_alert_words_ui = function () {
|
|
|
|
var alert_words = page_params.alert_words;
|
|
|
|
var word_list = $('#alert_words_list');
|
|
|
|
|
|
|
|
word_list.find('.alert-word-item').remove();
|
|
|
|
_.each(alert_words, function (alert_word) {
|
|
|
|
var rendered_alert_word = templates.render('alert_word_settings_item',
|
|
|
|
{word: alert_word, editing: false});
|
|
|
|
word_list.append(rendered_alert_word);
|
|
|
|
});
|
|
|
|
var new_alert_word_form = templates.render('alert_word_settings_item',
|
|
|
|
{word: '', editing: true});
|
|
|
|
word_list.append(new_alert_word_form);
|
|
|
|
|
|
|
|
// Focus new alert word name text box.
|
|
|
|
$('#create_alert_word_name').focus();
|
|
|
|
};
|
|
|
|
|
2017-06-07 21:16:02 +02:00
|
|
|
function update_alert_word_status(status_text, is_error) {
|
|
|
|
var alert_word_status = $('#alert_word_status');
|
|
|
|
if (is_error) {
|
|
|
|
alert_word_status.removeClass('alert-success').addClass('alert-danger');
|
|
|
|
} else {
|
|
|
|
alert_word_status.removeClass('alert-danger').addClass('alert-success');
|
|
|
|
}
|
|
|
|
alert_word_status.find('.alert_word_status_text').text(status_text);
|
|
|
|
alert_word_status.show();
|
|
|
|
}
|
|
|
|
|
2016-06-24 01:53:49 +02:00
|
|
|
function update_alert_words() {
|
2014-03-04 23:37:29 +01:00
|
|
|
var words = _.map($('.alert-word-item'), function (e) {
|
2016-06-24 01:53:49 +02:00
|
|
|
return $(e).data('word').toString();
|
2013-09-09 17:49:39 +02:00
|
|
|
});
|
2014-03-04 23:37:29 +01:00
|
|
|
words = _.filter(words, function (word) {
|
|
|
|
return word !== "";
|
|
|
|
});
|
|
|
|
channel.post({
|
2016-04-01 21:14:10 +02:00
|
|
|
url: '/json/users/me/alert_words',
|
2014-03-04 23:37:29 +01:00
|
|
|
idempotent: true,
|
|
|
|
data: {alert_words: JSON.stringify(words)}});
|
|
|
|
}
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
function add_alert_word(word, event) {
|
2017-01-21 18:30:33 +01:00
|
|
|
if ($.trim(word) === '') {
|
2017-06-07 21:16:02 +02:00
|
|
|
update_alert_word_status(i18n.t("Alert words can't be empty!"), true);
|
2014-03-04 23:37:29 +01:00
|
|
|
return;
|
2013-09-09 17:49:39 +02:00
|
|
|
}
|
2016-12-03 03:08:47 +01:00
|
|
|
var final_li = templates.render('alert_word_settings_item', {word: word, editing: false});
|
2014-03-04 23:37:29 +01:00
|
|
|
|
2016-06-24 01:53:49 +02:00
|
|
|
var li = $(event.target).parents('li');
|
2014-03-04 23:37:29 +01:00
|
|
|
li.replaceWith(final_li);
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2016-12-03 03:08:47 +01:00
|
|
|
var new_word = templates.render('alert_word_settings_item', {word: '', editing: true});
|
2016-06-24 01:53:49 +02:00
|
|
|
var word_list = $('#alert_words_list');
|
2014-03-04 23:37:29 +01:00
|
|
|
word_list.append(new_word);
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
if (word_list.find('input').length > 0) {
|
|
|
|
word_list.find('input').focus();
|
|
|
|
}
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2016-06-24 01:53:49 +02:00
|
|
|
update_alert_words();
|
2014-03-04 23:37:29 +01:00
|
|
|
}
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
exports.set_up_alert_words = function () {
|
|
|
|
// The settings page must be rendered before this function gets called.
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2017-06-07 21:46:11 +02:00
|
|
|
exports.render_alert_words_ui();
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2016-09-19 22:30:44 +02:00
|
|
|
$('#alert_words_list').on('click', '#create_alert_word_button', function (event) {
|
2016-06-24 01:53:49 +02:00
|
|
|
var word = $('#create_alert_word_name').val();
|
2013-11-07 00:42:14 +01:00
|
|
|
add_alert_word(word, event);
|
2013-09-09 17:49:39 +02:00
|
|
|
});
|
|
|
|
|
2016-06-24 01:53:49 +02:00
|
|
|
$('#alert_words_list').on('click', '.remove-alert-word', function (event) {
|
|
|
|
var li = $(event.currentTarget).parents('li');
|
2013-09-09 17:49:39 +02:00
|
|
|
li.remove();
|
|
|
|
|
2016-06-24 01:53:49 +02:00
|
|
|
update_alert_words();
|
2013-09-09 17:49:39 +02:00
|
|
|
});
|
|
|
|
|
2016-06-24 01:53:49 +02:00
|
|
|
$('#alert_words_list').on('keypress', '#create_alert_word_name', function (event) {
|
2013-09-09 17:49:39 +02:00
|
|
|
var key = event.which;
|
2013-11-06 15:12:28 +01:00
|
|
|
// Handle enter (13) as "add".
|
|
|
|
if (key === 13) {
|
2013-09-09 17:49:39 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var word = $(event.target).val();
|
2013-11-08 16:31:31 +01:00
|
|
|
add_alert_word(word, event);
|
2013-09-09 17:49:39 +02:00
|
|
|
}
|
|
|
|
});
|
2016-07-14 01:32:25 +02:00
|
|
|
|
2017-06-07 21:16:02 +02:00
|
|
|
$('#alert-word-settings').on('click', '.close-alert-word-status', function (event) {
|
2016-07-14 01:32:25 +02:00
|
|
|
event.preventDefault();
|
|
|
|
var alert = $(event.currentTarget).parents('.alert');
|
|
|
|
alert.hide();
|
|
|
|
});
|
2014-03-04 23:37:29 +01:00
|
|
|
};
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = alert_words_ui;
|
|
|
|
}
|