2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-22 12:09:09 +02:00
|
|
|
// Exported for unit testing
|
|
|
|
exports.is_using_input_method = false;
|
2018-06-09 11:10:56 +02:00
|
|
|
|
2020-05-10 03:58:57 +02:00
|
|
|
exports.narrow_or_search_for_term = function (search_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_query_box = $("#search_query");
|
2018-06-22 12:09:09 +02:00
|
|
|
if (exports.is_using_input_method) {
|
|
|
|
// Neither narrow nor search when using input tools as
|
|
|
|
// `updater` is also triggered when 'enter' is triggered
|
|
|
|
// while using input tool
|
2018-06-09 11:10:56 +02:00
|
|
|
return search_query_box.val();
|
|
|
|
}
|
2020-07-04 16:25:41 +02:00
|
|
|
ui_util.change_tab_to("#message_feed_container");
|
2018-07-14 16:10:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators;
|
2018-07-14 16:10:00 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
2020-05-10 03:58:57 +02:00
|
|
|
// We have to take care to append the new pill before calling this
|
|
|
|
// function, so that the base_query includes the suggestion selected
|
|
|
|
// along with query corresponding to the existing pills.
|
2019-11-02 00:06:25 +01:00
|
|
|
const base_query = search_pill.get_search_string_for_current_filter(
|
2020-07-15 00:34:28 +02:00
|
|
|
search_pill_widget.widget,
|
|
|
|
);
|
2020-05-10 03:58:57 +02:00
|
|
|
operators = Filter.parse(base_query);
|
2018-07-14 16:10:00 +02:00
|
|
|
} else {
|
|
|
|
operators = Filter.parse(search_string);
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
narrow.activate(operators, {trigger: "search"});
|
2012-12-17 19:47:09 +01:00
|
|
|
|
2013-07-16 04:55:46 +02:00
|
|
|
// 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.
|
2013-02-28 22:10:22 +01:00
|
|
|
|
2013-07-16 04:55:46 +02:00
|
|
|
// Narrowing will have already put some operators in the search box,
|
|
|
|
// so leave the current text in.
|
2020-05-10 03:58:57 +02:00
|
|
|
if (!page_params.search_pills_enabled) {
|
2020-07-20 21:24:26 +02:00
|
|
|
search_query_box.trigger("blur");
|
2020-05-10 03:58:57 +02:00
|
|
|
}
|
2013-07-16 04:55:46 +02:00
|
|
|
return search_query_box.val();
|
2020-05-10 03:58:57 +02:00
|
|
|
};
|
2012-11-14 22:12:21 +01:00
|
|
|
|
2013-01-02 19:58:55 +01:00
|
|
|
function update_buttons_with_focus(focused) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const search_query_box = $("#search_query");
|
2012-12-18 23:38:55 +01:00
|
|
|
|
|
|
|
// Show buttons iff the search input is focused, or has non-empty contents,
|
|
|
|
// or we are narrowed.
|
2020-07-15 00:34:28 +02:00
|
|
|
if (focused || search_query_box.val() || narrow_state.active()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".search_button").prop("disabled", false);
|
2012-12-18 23:38:55 +01:00
|
|
|
}
|
2013-01-02 19:58:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_button_visibility = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
update_buttons_with_focus($("#search_query").is(":focus"));
|
2012-12-18 23:38:55 +01:00
|
|
|
};
|
|
|
|
|
2012-11-14 22:12:21 +01:00
|
|
|
exports.initialize = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
const search_query_box = $("#search_query");
|
|
|
|
const searchbox_form = $("#searchbox_form");
|
|
|
|
const searchbox = $("#searchbox");
|
2013-07-30 19:41:23 +02:00
|
|
|
|
|
|
|
// Data storage for the typeahead.
|
|
|
|
// This maps a search string to an object with a "description" field.
|
|
|
|
// (It's a bit of legacy that we have an object with only one important
|
|
|
|
// field. There's also a "search_string" field on each element that actually
|
|
|
|
// just represents the key of the hash, so it's redundant.)
|
2020-02-12 06:58:20 +01:00
|
|
|
let search_map = new Map();
|
2013-07-30 19:41:23 +02:00
|
|
|
|
2018-06-22 11:59:47 +02:00
|
|
|
search_query_box.typeahead({
|
2020-07-20 22:18:43 +02:00
|
|
|
source(query) {
|
2020-07-15 01:29:15 +02:00
|
|
|
let base_query = "";
|
2018-07-14 13:01:21 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
2020-06-01 15:00:42 +02:00
|
|
|
base_query = search_pill.get_search_string_for_current_filter(
|
2020-07-15 00:34:28 +02:00
|
|
|
search_pill_widget.widget,
|
|
|
|
);
|
2018-07-14 13:01:21 +02:00
|
|
|
}
|
2020-06-01 15:00:42 +02:00
|
|
|
const suggestions = search_suggestion.get_suggestions(base_query, query);
|
2020-02-12 06:58:20 +01:00
|
|
|
// Update our global search_map hash
|
|
|
|
search_map = suggestions.lookup_table;
|
2013-07-30 19:41:23 +02:00
|
|
|
return suggestions.strings;
|
|
|
|
},
|
2013-08-21 17:37:54 +02:00
|
|
|
fixed: true,
|
2019-12-25 16:58:11 +01:00
|
|
|
items: search_suggestion.max_num_of_search_results,
|
2018-12-04 22:55:55 +01:00
|
|
|
helpOnEmptyStrings: true,
|
2013-07-23 03:05:06 +02:00
|
|
|
naturalSearch: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
highlighter(item) {
|
2020-02-12 06:58:20 +01:00
|
|
|
const obj = search_map.get(item);
|
2013-07-16 01:03:13 +02:00
|
|
|
return obj.description;
|
2012-11-18 19:19:52 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
matcher() {
|
2013-07-15 22:43:25 +02:00
|
|
|
return true;
|
2012-11-15 03:44:50 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
updater(search_string) {
|
2018-07-14 16:10:00 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
2020-07-15 00:34:28 +02:00
|
|
|
search_pill.append_search_string(search_string, search_pill_widget.widget);
|
2020-07-14 21:26:28 +02:00
|
|
|
return search_query_box.val();
|
2018-07-14 16:10:00 +02:00
|
|
|
}
|
2020-05-10 03:58:57 +02:00
|
|
|
return exports.narrow_or_search_for_term(search_string);
|
2018-07-14 16:10:00 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
sorter(items) {
|
2013-07-15 22:57:52 +02:00
|
|
|
return items;
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2018-07-14 16:10:00 +02:00
|
|
|
stopAdvance: page_params.search_pills_enabled,
|
2018-07-28 07:33:24 +02:00
|
|
|
advanceKeyCodes: [8],
|
2020-05-06 21:44:50 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
on_move() {
|
2020-06-09 12:59:12 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
|
|
|
ui_util.place_caret_at_end(search_query_box[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2020-05-06 21:44:50 +02:00
|
|
|
// Use our custom typeahead `on_escape` hook to exit
|
|
|
|
// the search bar as soon as the user hits Esc.
|
2020-07-08 23:44:01 +02:00
|
|
|
on_escape: message_view_header.exit_search,
|
2012-11-14 22:12:21 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
searchbox_form.on("compositionend", () => {
|
2018-06-09 11:10:56 +02:00
|
|
|
// Set `is_using_input_method` to true if enter is pressed to exit
|
|
|
|
// the input tool popover and get the text in the search bar. Then
|
|
|
|
// we suppress searching triggered by this enter key by checking
|
|
|
|
// `is_using_input_method` before searching.
|
|
|
|
// More details in the commit message that added this line.
|
2018-06-22 12:09:09 +02:00
|
|
|
exports.is_using_input_method = true;
|
2018-06-09 11:10:56 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
searchbox_form
|
2020-07-20 21:26:58 +02:00
|
|
|
.on("keydown", (e) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.update_button_visibility();
|
|
|
|
const code = e.which;
|
|
|
|
if (code === 13 && search_query_box.is(":focus")) {
|
|
|
|
// Don't submit the form so that the typeahead can instead
|
|
|
|
// handle our Enter keypress. Any searching that needs
|
|
|
|
// to be done will be handled in the keyup.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
2020-07-20 21:26:58 +02:00
|
|
|
.on("keyup", (e) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
if (exports.is_using_input_method) {
|
|
|
|
exports.is_using_input_method = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const code = e.which;
|
|
|
|
if (code === 13 && search_query_box.is(":focus")) {
|
|
|
|
// We just pressed enter and the box had focus, which
|
|
|
|
// means we didn't use the typeahead at all. In that
|
|
|
|
// case, we should act as though we're searching by
|
|
|
|
// operators. (The reason the other actions don't call
|
|
|
|
// this codepath is that they first all blur the box to
|
|
|
|
// indicate that they've done what they need to do)
|
|
|
|
|
|
|
|
// Pill is already added during keydown event of input pills.
|
|
|
|
exports.narrow_or_search_for_term(search_query_box.val());
|
2020-07-20 21:24:26 +02:00
|
|
|
search_query_box.trigger("blur");
|
2020-07-15 00:34:28 +02:00
|
|
|
update_buttons_with_focus(false);
|
|
|
|
}
|
|
|
|
});
|
2013-02-27 20:29:25 +01:00
|
|
|
|
|
|
|
// Some of these functions don't actually need to be exported,
|
|
|
|
// but the code was moved here from elsewhere, and it would be
|
|
|
|
// more work to re-order everything and make them private.
|
2013-02-27 21:00:06 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
search_query_box.on("focus", exports.focus_search);
|
|
|
|
search_query_box.on("blur", (e) => {
|
2013-02-27 21:00:06 +01:00
|
|
|
// The search query box is a visual cue as to
|
|
|
|
// whether search or narrowing is active. If
|
2013-07-23 03:41:21 +02:00
|
|
|
// the user blurs the search box, then we should
|
2018-06-08 21:14:55 +02:00
|
|
|
// update the search string to reflect the current
|
2013-07-23 03:41:21 +02:00
|
|
|
// narrow (or lack of narrow).
|
2013-02-27 21:00:06 +01:00
|
|
|
//
|
|
|
|
// But we can't do this right away, because
|
|
|
|
// selecting something in the typeahead menu causes
|
2013-07-23 03:41:21 +02:00
|
|
|
// the box to lose focus a moment before.
|
2013-02-27 21:00:06 +01:00
|
|
|
//
|
2020-07-14 21:26:28 +02:00
|
|
|
// The workaround is to check 100ms later -- long
|
2013-02-27 21:00:06 +01:00
|
|
|
// enough for the search to have gone through, but
|
|
|
|
// short enough that the user won't notice (though
|
|
|
|
// really it would be OK if they did).
|
|
|
|
|
2020-06-18 17:17:11 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const pill_id = $(e.relatedTarget).closest(".pill").data("id");
|
2020-06-18 17:17:11 +02:00
|
|
|
const search_pill = search_pill_widget.widget.getByID(pill_id);
|
|
|
|
if (search_pill) {
|
|
|
|
// The searchbox loses focus while the search
|
|
|
|
// pill element gains focus.
|
|
|
|
// We do not consider the searchbox to actually
|
|
|
|
// lose focus when a pill inside it gets selected
|
|
|
|
// or deleted by a click.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2013-02-27 21:00:06 +01:00
|
|
|
exports.update_button_visibility();
|
2020-07-14 21:26:28 +02:00
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (page_params.search_pills_enabled) {
|
|
|
|
// Uses jquery instead of pure css as the `:focus` event occurs on `#search_query`,
|
|
|
|
// while we want to add box-shadow to `#searchbox`. This could have been done
|
|
|
|
// with `:focus-within` CSS selector, but it is not supported in IE or Opera.
|
2020-07-15 01:29:15 +02:00
|
|
|
searchbox.on("focusout", () => {
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.close_search_bar_and_open_narrow_description();
|
2018-07-23 00:25:21 +02:00
|
|
|
searchbox.css({"box-shadow": "unset"});
|
2020-07-14 21:26:28 +02:00
|
|
|
});
|
|
|
|
}
|
2012-11-14 22:12:21 +01:00
|
|
|
};
|
|
|
|
|
2012-12-18 22:42:13 +01:00
|
|
|
exports.focus_search = function () {
|
2013-01-02 19:58:55 +01:00
|
|
|
// The search bar is not focused yet, but will be.
|
|
|
|
update_buttons_with_focus(true);
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-11-01 19:20:51 +01:00
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
exports.initiate_search = function () {
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.open_search_bar_and_close_narrow_description();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#searchbox").css({"box-shadow": "inset 0px 0px 0px 2px hsl(204, 20%, 74%)"});
|
2020-07-22 04:50:11 +02:00
|
|
|
$("#search_query").typeahead("lookup").trigger("select");
|
2018-07-23 17:14:53 +02:00
|
|
|
if (page_params.search_pills_enabled) {
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#search_query").trigger("focus");
|
2020-07-15 01:29:15 +02:00
|
|
|
ui_util.place_caret_at_end($("#search_query")[0]);
|
2018-07-23 17:14:53 +02:00
|
|
|
}
|
2012-11-14 20:52:53 +01:00
|
|
|
};
|
2012-11-01 17:14:33 +01:00
|
|
|
|
Simplify narrow/search interactions.
Before this change, if you hit ESC, then hotkey
code would call search.clear_search, which would
call narrow.deactivate(), which would then use
`$('#search_query')` to clear a value, but then
let search.clear_search blur the input and
disable the exit button. It was all confusing.
Things are a bit more organized now.
Now the code works like this:
hotkey.process_escape_key
Just call narrow.deactivate.
$('#search_exit').on('click', ...):
Just call narrow.deactivate.
narrow.deactivate:
Just call search.clear_search_form
search.clear_search_form:
Just do simple jquery stuff. Don't
change the entire user's narrow, not
even indirectly!
There's still a two-way interaction between
the narrow.js module and the search.js module,
but in each direction it's a one-liner.
The guiding principle here is that we only
want one top-level API, which is narrow.deactivate,
and that does the whole "kitchen sink" of
clearing searches, closing popovers, switching
in views, etc. And then all the functions it
calls out to tend to have much smaller jobs to
do.
This commit can mostly be considered a refactoring, but the
order of operations changes slightly. Basically, as
soon as you hit ESC or click on the search "X", we
clear the search widget. Most users won't notice
any difference, because we don't have to hit the
server to populate the home view. And it's arguably
an improvement to give more immediate feedback.
2018-09-10 19:36:58 +02:00
|
|
|
exports.clear_search_form = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#search_query").val("");
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#search_query").trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".search_button").prop("disabled", true);
|
2012-12-07 20:18:01 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.search = exports;
|