From d4d4ea7a8d04c04bf18bca355a90d4e4bd713ee0 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 12 Dec 2012 13:36:18 -0500 Subject: [PATCH] Display narrowing operators in the search bar (imported from commit d801128c5b961360939c9e3f3f6f89c8602f987b) --- zephyr/static/js/narrow.js | 24 ++++++++++++++++++++++-- zephyr/static/js/search.js | 10 +++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/zephyr/static/js/narrow.js b/zephyr/static/js/narrow.js index 220bb3319b..9eaa2449e8 100644 --- a/zephyr/static/js/narrow.js +++ b/zephyr/static/js/narrow.js @@ -34,13 +34,31 @@ exports.allow_collapse = function () { return (!filter_function) || allow_collapse; }; -/* Build a filter function from a list of operators. +/* Convert a list of operators to a string. Each operator is a key-value pair like ['subject', 'my amazing subject'] These are not keys in a JavaScript object, because we might need to support multiple operators of the same type. */ +function unparse(operators) { + var parts = []; + $.each(operators, function (index, elem) { + var operator = elem[0]; + if (operator === 'search') { + // Search terms are the catch-all case. + // All tokens that don't start with a known operator and + // a colon are glued together to form a search term. + parts.push(elem[1]); + } else { + // FIXME: URI encoding will look really ugly + parts.push(elem[0] + ':' + encodeURIComponent(elem[1])); + } + }); + return parts.join(' '); +} + +// Build a filter function from a list of operators. function build_filter(operators) { // FIXME: This is probably pretty slow. // We could turn it into something more like a compiler: @@ -155,8 +173,9 @@ exports.activate = function (operators, bar, opts) { search.update_highlight_on_narrow(); } - // Put the narrow operators in the URL fragment + // Put the narrow operators in the URL fragment and search bar hashchange.save_narrow(operators); + $('#search_query').val(unparse(operators)); }; exports.time_travel = function () { @@ -252,6 +271,7 @@ exports.show_all_messages = function () { $("#searchbox").removeClass('narrowed_view'); $("#show_all_messages").attr("disabled", "disabled"); $("#currently_narrowed_to").empty(); + $('#search_query').val(''); // Includes scrolling. select_message_by_id(persistent_message_id, {then_scroll: true}); diff --git a/zephyr/static/js/search.js b/zephyr/static/js/search.js index 8bbdd33785..d5f9914de2 100644 --- a/zephyr/static/js/search.js +++ b/zephyr/static/js/search.js @@ -59,6 +59,7 @@ exports.update_typeahead = function() { }; function narrow_or_search_for_term(item) { + var search_query_box = $("#search_query"); var obj = mapped[item]; if (obj.action === "search") { $("#search_up").focus(); @@ -69,13 +70,16 @@ function narrow_or_search_for_term(item) { // It's sort of annoying that this is not in a position to // blur the search box, because it means that Esc won't // unnarrow, it'll leave the searchbox. - return ""; // Keep the search box empty + + // Narrowing will have already put some operators in the search box, + // so leave the current text in. + return search_query_box.val(); } else if (obj.action === "private_message") { narrow.by_private_message_group(obj.query.full_name, obj.query.email); - return ""; + return search_query_box.val(); } else if (obj.action === "search_narrow") { narrow.by_search_term(obj.query); - return ""; + return search_query_box.val(); } return item; }