2014-03-04 23:37:29 +01:00
|
|
|
var alert_words_ui = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
function update_word_alerts() {
|
|
|
|
var words = _.map($('.alert-word-item'), function (e) {
|
|
|
|
return $(e).data('word');
|
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({
|
|
|
|
url: '/json/set_alert_words',
|
|
|
|
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) {
|
|
|
|
if (word === '') {
|
|
|
|
return;
|
2013-09-09 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
var final_li = templates.render('alert_word_settings_item', {'word': word, editing: false});
|
|
|
|
|
|
|
|
var li = $(event.target).parent();
|
|
|
|
li.replaceWith(final_li);
|
2013-09-09 17:49:39 +02:00
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
var new_word = templates.render('alert_word_settings_item', {'word': '', editing: true});
|
|
|
|
var word_list = $('#word-alerts');
|
|
|
|
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
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
update_word_alerts();
|
|
|
|
}
|
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
|
|
|
|
2014-03-04 23:37:29 +01:00
|
|
|
var word_list = $('#word-alerts');
|
|
|
|
_.each(alert_words.words, function (word) {
|
|
|
|
var li = templates.render('alert_word_settings_item', {'word': word});
|
|
|
|
word_list.append(li);
|
|
|
|
});
|
|
|
|
var new_word = templates.render('alert_word_settings_item', {'word': '', editing: true});
|
|
|
|
word_list.append(new_word);
|
2013-09-09 17:49:39 +02:00
|
|
|
|
|
|
|
$('#word-alerts').on('click', '.add-alert-word', function (event) {
|
|
|
|
var word = $(event.target).siblings('input').val();
|
2013-11-07 00:42:14 +01:00
|
|
|
add_alert_word(word, event);
|
2013-09-09 17:49:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#word-alerts').on('click', '.remove-alert-word', function (event) {
|
2013-10-21 01:04:25 +02:00
|
|
|
var li = $(event.currentTarget).parent();
|
2013-09-09 17:49:39 +02:00
|
|
|
li.remove();
|
|
|
|
|
|
|
|
update_word_alerts();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#word-alerts').on('keypress', '.edit-alert-word', function (event) {
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
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;
|
|
|
|
}());
|