From a1c4aa6865af9f2dd91ae490c1aef4c2f5e71223 Mon Sep 17 00:00:00 2001 From: Waseem Daher Date: Wed, 14 Nov 2012 14:52:53 -0500 Subject: [PATCH] Modularize search.js. (imported from commit 72f00f832846124d833071fdd46b026c99189512) --- templates/zephyr/home.html | 8 ++++---- tools/jslint/check-all.js | 3 +-- zephyr/static/js/hotkey.js | 2 +- zephyr/static/js/narrow.js | 6 +++--- zephyr/static/js/search.js | 32 ++++++++++++++++++++------------ 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/templates/zephyr/home.html b/templates/zephyr/home.html index 3c069d2b93..a53f0e9474 100644 --- a/templates/zephyr/home.html +++ b/templates/zephyr/home.html @@ -5,10 +5,10 @@ diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index 26facec1e2..3cfcb1c14f 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -27,8 +27,7 @@ var globals = + ' reload' // search.js - + ' search_button_handler something_is_highlighted update_highlight_on_narrow' - + ' initiate_search' + + ' search' // setup.js + ' loading_spinner templates csrf_token' diff --git a/zephyr/static/js/hotkey.js b/zephyr/static/js/hotkey.js index 05cc713a92..10778577fe 100644 --- a/zephyr/static/js/hotkey.js +++ b/zephyr/static/js/hotkey.js @@ -100,7 +100,7 @@ function process_hotkey(e) { respond_to_message("personal"); return process_hotkey; case 47: // '/': initiate search - initiate_search(); + search.initiate_search(); return process_hotkey; case 63: // '?': Show keyboard shortcuts page $('#keyboard-shortcuts').modal('show'); diff --git a/zephyr/static/js/narrow.js b/zephyr/static/js/narrow.js index d4c555964d..95961003b7 100644 --- a/zephyr/static/js/narrow.js +++ b/zephyr/static/js/narrow.js @@ -42,7 +42,7 @@ function do_narrow(bar, filter_function) { } // Before we clear the table, check if anything was highlighted. - var highlighted = something_is_highlighted(); + var highlighted = search.something_is_highlighted(); // Empty the filtered table right before we fill it again clear_table('zfilt'); @@ -71,7 +71,7 @@ function do_narrow(bar, filter_function) { // If anything was highlighted before, try to rehighlight it. if (highlighted) { - update_highlight_on_narrow(); + search.update_highlight_on_narrow(); } } @@ -171,7 +171,7 @@ exports.show_all_messages = function () { scroll_to_selected(); - update_highlight_on_narrow(); + search.update_highlight_on_narrow(); }; exports.restore_home_state = function() { diff --git a/zephyr/static/js/search.js b/zephyr/static/js/search.js index d1c7f82c79..e186e90ce5 100644 --- a/zephyr/static/js/search.js +++ b/zephyr/static/js/search.js @@ -1,3 +1,7 @@ +var search = (function () { + +var exports = {}; + var cached_term = ""; var cached_matches = []; var cached_index; @@ -91,7 +95,7 @@ function highlight_match(row, search_term) { } } -function search_button_handler(reverse) { +exports.search_button_handler = function (reverse) { var query = $('#search').val().toLowerCase(); var res = search(query, selected_message, reverse); if (!res) { @@ -101,24 +105,24 @@ function search_button_handler(reverse) { select_message(res); highlight_match(res, query); scroll_to_selected(); -} +}; function clear_search_cache() { cached_term = ""; } -function focus_search() { +exports.focus_search = function () { $("#search").width("504px"); $("#search_arrows").addClass("input-append"); $('.search_button').show(); disable_search_arrows_if(false, ["up", "down"]); -} +}; -function initiate_search() { +exports.initiate_search = function () { $('#search').val('').focus(); -} +}; -function clear_search() { +exports.clear_search = function () { $('table tr').removeHighlight(); // Reset the width to that in the stylesheet. If you change it there, change // it here. @@ -127,13 +131,17 @@ function clear_search() { $("#search_up, #search_down").removeAttr("disabled"); $('.search_button').blur().hide(); clear_search_cache(); -} +}; -function something_is_highlighted() { +exports.something_is_highlighted = function () { return $(".highlight").length > 0; -} +}; -function update_highlight_on_narrow() { +exports.update_highlight_on_narrow = function () { highlight_match(selected_message, cached_term); clear_search_cache(); -} +}; + +return exports; + +}());