diff --git a/static/js/alert_words_ui.js b/static/js/alert_words_ui.js index a823525c18..a259dd1b9b 100644 --- a/static/js/alert_words_ui.js +++ b/static/js/alert_words_ui.js @@ -1,4 +1,45 @@ -$(function () { +var alert_words_ui = (function () { + +var exports = {}; + +function update_word_alerts() { + var words = _.map($('.alert-word-item'), function (e) { + return $(e).data('word'); + }); + words = _.filter(words, function (word) { + return word !== ""; + }); + + channel.post({ + url: '/json/set_alert_words', + idempotent: true, + data: {alert_words: JSON.stringify(words)}}); +} + +function add_alert_word(word, event) { + if (word === '') { + return; + } + + var final_li = templates.render('alert_word_settings_item', {'word': word, editing: false}); + + var li = $(event.target).parent(); + li.replaceWith(final_li); + + var new_word = templates.render('alert_word_settings_item', {'word': '', editing: true}); + var word_list = $('#word-alerts'); + word_list.append(new_word); + + if (word_list.find('input').length > 0) { + word_list.find('input').focus(); + } + + update_word_alerts(); +} + +exports.set_up_alert_words = function () { + // The settings page must be rendered before this function gets called. + var word_list = $('#word-alerts'); _.each(alert_words.words, function (word) { var li = templates.render('alert_word_settings_item', {'word': word}); @@ -7,40 +48,6 @@ $(function () { var new_word = templates.render('alert_word_settings_item', {'word': '', editing: true}); word_list.append(new_word); - function update_word_alerts() { - var words = _.map($('.alert-word-item'), function (e) { - return $(e).data('word'); - }); - words = _.filter(words, function (word) { - return word !== ""; - }); - - channel.post({ - url: '/json/set_alert_words', - idempotent: true, - data: {alert_words: JSON.stringify(words)}}); - } - - function add_alert_word(word, event) { - if (word === '') { - return; - } - - var final_li = templates.render('alert_word_settings_item', {'word': word, editing: false}); - - var li = $(event.target).parent(); - li.replaceWith(final_li); - - var new_word = templates.render('alert_word_settings_item', {'word': '', editing: true}); - word_list.append(new_word); - - if (word_list.find('input').length > 0) { - word_list.find('input').focus(); - } - - update_word_alerts(); - } - $('#word-alerts').on('click', '.add-alert-word', function (event) { var word = $(event.target).siblings('input').val(); add_alert_word(word, event); @@ -63,5 +70,7 @@ $(function () { add_alert_word(word, event); } }); -}); +}; +return exports; +}()); diff --git a/static/js/settings.js b/static/js/settings.js index ac7cf3f9cd..2676df1138 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -30,6 +30,8 @@ exports.setup_page = function () { $("#notify-settings-status").hide(); $("#ui-settings-status").hide(); + alert_words_ui.set_up_alert_words(); + $("#api_key_value").text(""); $("#get_api_key_box").hide(); $("#show_api_key_box").hide(); diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index fc6781caa2..9bd2a500a8 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -25,7 +25,7 @@ var globals = + ' compose compose_fade rows hotkeys narrow reload notifications_bar search subs' + ' composebox_typeahead server_events typeahead_helper notifications hashchange' + ' invite ui util activity timerender MessageList MessageListView blueslip unread stream_list' - + ' message_edit tab_bar emoji popovers navigate people settings message_store' + + ' message_edit tab_bar emoji popovers navigate people settings alert_words_ui message_store' + ' avatar feature_flags search_suggestion referral stream_color Dict' + ' Filter summary admin stream_data muting WinChan muting_ui Socket channel' + ' message_flags'