2018-07-14 12:46:02 +02:00
|
|
|
set_global('page_params', {
|
|
|
|
search_pills_enabled: true,
|
|
|
|
});
|
2018-06-22 12:19:40 +02:00
|
|
|
zrequire('search');
|
2018-07-14 16:10:00 +02:00
|
|
|
zrequire('search_pill');
|
|
|
|
zrequire('Filter', 'js/filter');
|
2020-02-03 17:01:11 +01:00
|
|
|
zrequire('tab_bar');
|
2018-06-22 12:19:40 +02:00
|
|
|
|
|
|
|
const noop = () => {};
|
|
|
|
const return_true = () => true;
|
|
|
|
const return_false = () => false;
|
|
|
|
|
|
|
|
set_global('$', global.make_zjquery());
|
2020-05-15 14:44:30 +02:00
|
|
|
set_global('narrow_state', {filter: return_false});
|
2018-06-22 12:19:40 +02:00
|
|
|
set_global('search_suggestion', {});
|
|
|
|
set_global('ui_util', {
|
|
|
|
change_tab_to: noop,
|
2020-06-09 12:59:12 +02:00
|
|
|
place_caret_at_end: noop,
|
2018-06-22 12:19:40 +02:00
|
|
|
});
|
|
|
|
set_global('narrow', {});
|
2020-06-18 17:17:11 +02:00
|
|
|
set_global('search_pill_widget', {
|
|
|
|
widget: {
|
|
|
|
getByID: return_true,
|
|
|
|
},
|
|
|
|
});
|
2018-07-14 16:10:00 +02:00
|
|
|
|
|
|
|
search_pill.append_search_string = noop;
|
2020-05-10 03:58:57 +02:00
|
|
|
search_pill.get_search_string_for_current_filter = noop;
|
2018-06-22 12:19:40 +02:00
|
|
|
|
|
|
|
global.patch_builtin('setTimeout', func => func());
|
|
|
|
|
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
|
|
|
run_test('clear_search_form', () => {
|
|
|
|
$('#search_query').val('noise');
|
|
|
|
$('#search_query').focus();
|
|
|
|
$('.search_button').prop('disabled', false);
|
|
|
|
|
|
|
|
search.clear_search_form();
|
|
|
|
|
|
|
|
assert.equal($('#search_query').is_focused(), false);
|
|
|
|
assert.equal($('#search_query').val(), '');
|
|
|
|
assert.equal($('.search_button').prop('disabled'), true);
|
|
|
|
});
|
|
|
|
|
2018-06-22 12:19:40 +02:00
|
|
|
run_test('update_button_visibility', () => {
|
|
|
|
const search_query = $('#search_query');
|
|
|
|
const search_button = $('.search_button');
|
|
|
|
|
2020-05-31 16:47:55 +02:00
|
|
|
search_query.is = return_false;
|
|
|
|
search_query.val('');
|
|
|
|
narrow_state.active = return_false;
|
|
|
|
search_button.prop('disabled', true);
|
|
|
|
search.update_button_visibility();
|
|
|
|
assert(search_button.prop('disabled'));
|
|
|
|
|
2018-06-22 12:19:40 +02:00
|
|
|
search_query.is = return_true;
|
|
|
|
search_query.val('');
|
|
|
|
narrow_state.active = return_false;
|
|
|
|
search_button.prop('disabled', true);
|
|
|
|
search.update_button_visibility();
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
|
|
|
|
search_query.is = return_false;
|
|
|
|
search_query.val('Test search term');
|
|
|
|
narrow_state.active = return_false;
|
|
|
|
search_button.prop('disabled', true);
|
|
|
|
search.update_button_visibility();
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
|
|
|
|
search_query.is = return_false;
|
|
|
|
search_query.val('');
|
|
|
|
narrow_state.active = return_true;
|
|
|
|
search_button.prop('disabled', true);
|
|
|
|
search.update_button_visibility();
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
});
|
|
|
|
|
2020-05-31 16:47:55 +02:00
|
|
|
run_test('initialize', () => {
|
2018-06-22 12:19:40 +02:00
|
|
|
const search_query_box = $('#search_query');
|
|
|
|
const searchbox_form = $('#searchbox_form');
|
|
|
|
const search_button = $('.search_button');
|
|
|
|
|
|
|
|
searchbox_form.on = (event, func) => {
|
|
|
|
assert.equal(event, 'compositionend');
|
|
|
|
search.is_using_input_method = false;
|
|
|
|
func();
|
|
|
|
assert(search.is_using_input_method);
|
|
|
|
};
|
|
|
|
|
2018-07-02 22:30:18 +02:00
|
|
|
search_pill.get_search_string_for_current_filter = function () {
|
|
|
|
return 'is:starred';
|
|
|
|
};
|
|
|
|
|
2019-12-25 16:58:11 +01:00
|
|
|
search_suggestion.max_num_of_search_results = 99;
|
2018-06-22 12:19:40 +02:00
|
|
|
search_query_box.typeahead = (opts) => {
|
2019-12-25 16:58:11 +01:00
|
|
|
|
2018-06-22 12:19:40 +02:00
|
|
|
assert.equal(opts.fixed, true);
|
2019-12-25 16:58:11 +01:00
|
|
|
assert.equal(opts.items, 99);
|
2018-06-22 12:19:40 +02:00
|
|
|
assert.equal(opts.naturalSearch, true);
|
2018-12-04 22:55:55 +01:00
|
|
|
assert.equal(opts.helpOnEmptyStrings, true);
|
2018-06-22 12:19:40 +02:00
|
|
|
assert.equal(opts.matcher(), true);
|
2020-06-09 12:59:12 +02:00
|
|
|
assert.equal(opts.on_move(), true);
|
2018-06-22 12:19:40 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const search_suggestions = {
|
2020-02-12 06:58:20 +01:00
|
|
|
lookup_table: new Map([
|
|
|
|
['stream:Verona', {
|
2018-06-22 12:19:40 +02:00
|
|
|
description: 'Stream <strong>Ver</strong>ona',
|
|
|
|
search_string: 'stream:Verona',
|
2020-02-12 06:58:20 +01:00
|
|
|
}],
|
|
|
|
['ver', {
|
2018-06-22 12:19:40 +02:00
|
|
|
description: 'Search for ver',
|
|
|
|
search_string: 'ver',
|
2020-02-12 06:58:20 +01:00
|
|
|
}],
|
|
|
|
]),
|
2018-06-22 12:19:40 +02:00
|
|
|
strings: ['ver', 'stream:Verona'],
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Test source */
|
|
|
|
search_suggestion.get_suggestions = () => search_suggestions;
|
|
|
|
const expected_source_value = search_suggestions.strings;
|
|
|
|
const source = opts.source('ver');
|
|
|
|
assert.equal(source, expected_source_value);
|
|
|
|
|
|
|
|
/* Test highlighter */
|
|
|
|
let expected_value = 'Search for ver';
|
|
|
|
assert.equal(opts.highlighter(source[0]), expected_value);
|
|
|
|
|
|
|
|
expected_value = 'Stream <strong>Ver</strong>ona';
|
|
|
|
assert.equal(opts.highlighter(source[1]), expected_value);
|
|
|
|
|
|
|
|
/* Test sorter */
|
|
|
|
assert.equal(opts.sorter(search_suggestions.strings), search_suggestions.strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let operators;
|
|
|
|
let is_blurred;
|
2018-07-14 16:10:00 +02:00
|
|
|
let is_append_search_string_called;
|
2018-06-22 12:19:40 +02:00
|
|
|
search_query_box.blur = () => {
|
|
|
|
is_blurred = true;
|
|
|
|
};
|
2018-07-14 16:10:00 +02:00
|
|
|
search_pill.append_search_string = () => {
|
|
|
|
is_append_search_string_called = true;
|
|
|
|
};
|
2018-06-22 12:19:40 +02:00
|
|
|
/* Test updater */
|
|
|
|
const _setup = (search_box_val) => {
|
|
|
|
is_blurred = false;
|
2018-07-14 16:10:00 +02:00
|
|
|
is_append_search_string_called = false;
|
2018-06-22 12:19:40 +02:00
|
|
|
search_query_box.val(search_box_val);
|
2020-05-31 16:47:55 +02:00
|
|
|
Filter.parse = (search_string) => {
|
|
|
|
assert.equal(search_string, search_box_val);
|
|
|
|
return operators;
|
|
|
|
};
|
2018-06-22 12:19:40 +02:00
|
|
|
narrow.activate = (raw_operators, options) => {
|
|
|
|
assert.deepEqual(raw_operators, operators);
|
|
|
|
assert.deepEqual(options, {trigger: 'search'});
|
|
|
|
};
|
2018-07-14 16:10:00 +02:00
|
|
|
search_pill.get_search_string_for_current_filter = () => {
|
2020-05-10 03:58:57 +02:00
|
|
|
return search_box_val;
|
2018-07-14 16:10:00 +02:00
|
|
|
};
|
2018-06-22 12:19:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
operators = [{
|
|
|
|
negated: false,
|
|
|
|
operator: 'search',
|
|
|
|
operand: 'ver',
|
|
|
|
}];
|
|
|
|
_setup('ver');
|
2020-05-31 16:47:55 +02:00
|
|
|
|
|
|
|
assert.equal(opts.updater('ver'), 'ver');
|
2020-05-10 03:58:57 +02:00
|
|
|
assert(!is_blurred);
|
2018-07-14 16:10:00 +02:00
|
|
|
assert(is_append_search_string_called);
|
2018-06-22 12:19:40 +02:00
|
|
|
|
|
|
|
operators = [{
|
|
|
|
negated: false,
|
|
|
|
operator: 'stream',
|
|
|
|
operand: 'Verona',
|
|
|
|
}];
|
|
|
|
_setup('stream:Verona');
|
2020-05-31 16:47:55 +02:00
|
|
|
|
|
|
|
assert.equal(opts.updater('stream:Verona'), 'stream:Verona');
|
2020-05-10 03:58:57 +02:00
|
|
|
assert(!is_blurred);
|
2018-07-14 16:10:00 +02:00
|
|
|
assert(is_append_search_string_called);
|
2018-06-22 12:19:40 +02:00
|
|
|
|
|
|
|
search.is_using_input_method = true;
|
|
|
|
_setup('stream:Verona');
|
2020-06-18 15:47:17 +02:00
|
|
|
search_query_box.is = return_true;
|
2020-05-31 16:47:55 +02:00
|
|
|
|
|
|
|
assert.equal(opts.updater('stream:Verona'), 'stream:Verona');
|
2018-06-22 12:19:40 +02:00
|
|
|
assert(!is_blurred);
|
2018-07-14 16:10:00 +02:00
|
|
|
assert(is_append_search_string_called);
|
2018-06-22 12:19:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
searchbox_form.keydown = (func) => {
|
|
|
|
const ev = {
|
|
|
|
which: 15,
|
|
|
|
};
|
|
|
|
search_query_box.is = return_false;
|
|
|
|
assert.equal(func(ev), undefined);
|
|
|
|
|
|
|
|
ev.which = 13;
|
|
|
|
assert.equal(func(ev), undefined);
|
|
|
|
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_true;
|
|
|
|
assert.equal(func(ev), false);
|
|
|
|
|
|
|
|
return search_query_box;
|
|
|
|
};
|
|
|
|
|
|
|
|
search_query_box.keyup = (func) => {
|
|
|
|
const ev = {};
|
|
|
|
let operators;
|
|
|
|
let is_blurred;
|
|
|
|
narrow_state.active = return_false;
|
|
|
|
search_query_box.blur = () => {
|
|
|
|
is_blurred = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const _setup = (search_box_val) => {
|
|
|
|
is_blurred = false;
|
|
|
|
search_button.prop('disabled', false);
|
|
|
|
search_query_box.val(search_box_val);
|
2020-05-31 16:47:55 +02:00
|
|
|
Filter.parse = (search_string) => {
|
|
|
|
assert.equal(search_string, search_box_val);
|
|
|
|
return operators;
|
|
|
|
};
|
2018-06-22 12:19:40 +02:00
|
|
|
narrow.activate = (raw_operators, options) => {
|
|
|
|
assert.deepEqual(raw_operators, operators);
|
|
|
|
assert.deepEqual(options, {trigger: 'search'});
|
|
|
|
};
|
2018-07-14 16:10:00 +02:00
|
|
|
search_pill.get_search_string_for_current_filter = () => {
|
2020-05-10 03:58:57 +02:00
|
|
|
return search_box_val;
|
2018-07-14 16:10:00 +02:00
|
|
|
};
|
2018-06-22 12:19:40 +02:00
|
|
|
};
|
|
|
|
|
2020-05-31 16:47:55 +02:00
|
|
|
operators = [{
|
|
|
|
negated: false,
|
|
|
|
operator: 'search',
|
|
|
|
operand: '',
|
|
|
|
}];
|
2018-06-22 12:19:40 +02:00
|
|
|
_setup('');
|
|
|
|
|
|
|
|
ev.which = 15;
|
|
|
|
search_query_box.is = return_false;
|
|
|
|
func(ev);
|
|
|
|
|
|
|
|
assert(!is_blurred);
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_false;
|
|
|
|
func(ev);
|
|
|
|
|
|
|
|
assert(!is_blurred);
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_true;
|
|
|
|
func(ev);
|
|
|
|
assert(is_blurred);
|
|
|
|
|
|
|
|
_setup('ver');
|
|
|
|
search.is_using_input_method = true;
|
|
|
|
func(ev);
|
|
|
|
// No change on enter keyup event when using input tool
|
|
|
|
assert(!is_blurred);
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
|
|
|
|
_setup('ver');
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_true;
|
|
|
|
func(ev);
|
|
|
|
assert(is_blurred);
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
};
|
|
|
|
|
2020-06-18 17:17:11 +02:00
|
|
|
const search_pill_stub = $.create('.pill');
|
|
|
|
search_pill_stub.closest = () => {
|
|
|
|
return { data: noop };
|
|
|
|
};
|
|
|
|
const stub_event = {
|
|
|
|
relatedTarget: search_pill_stub,
|
|
|
|
};
|
2018-06-22 12:19:40 +02:00
|
|
|
search_query_box.on = (event, callback) => {
|
|
|
|
if (event === 'focus') {
|
|
|
|
search_button.prop('disabled', true);
|
|
|
|
callback();
|
|
|
|
assert(!search_button.prop('disabled'));
|
|
|
|
} else if (event === 'blur') {
|
|
|
|
search_query_box.val("test string");
|
|
|
|
narrow_state.search_string = () => 'ver';
|
2020-06-18 17:17:11 +02:00
|
|
|
callback(stub_event);
|
2020-02-03 17:01:11 +01:00
|
|
|
assert.equal(search_query_box.val(), 'test string');
|
2018-06-22 12:19:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
search.initialize();
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('initiate_search', () => {
|
2020-05-31 15:53:39 +02:00
|
|
|
// open typeahead and select text when navbar is open
|
|
|
|
// this implicitly expects the code to used the chained
|
|
|
|
// function calls, which is something to keep in mind if
|
|
|
|
// this test ever fails unexpectedly.
|
|
|
|
let typeahead_forced_open = false;
|
|
|
|
let is_searchbox_text_selected = false;
|
2018-07-23 17:14:53 +02:00
|
|
|
let is_searchbox_focused = false;
|
2020-05-31 15:53:39 +02:00
|
|
|
$('#search_query').focus = () => {
|
2018-07-23 17:14:53 +02:00
|
|
|
is_searchbox_focused = true;
|
2018-06-22 12:19:40 +02:00
|
|
|
};
|
2020-05-31 15:53:39 +02:00
|
|
|
$('#search_query').select = noop;
|
|
|
|
$('#search_query').typeahead = (lookup) => {
|
|
|
|
if (lookup === "lookup") {
|
|
|
|
typeahead_forced_open = true;
|
|
|
|
return {
|
|
|
|
select: () => {
|
|
|
|
is_searchbox_text_selected = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-22 12:19:40 +02:00
|
|
|
search.initiate_search();
|
2020-05-31 15:53:39 +02:00
|
|
|
assert(typeahead_forced_open);
|
|
|
|
assert(is_searchbox_text_selected);
|
2018-07-23 17:14:53 +02:00
|
|
|
assert(is_searchbox_focused);
|
2018-06-22 12:19:40 +02:00
|
|
|
});
|