minor: Specify types of pills while initializing pill typeahead.

In options that we pass to pill_typeahead.set_up we
specify if we want typeahead to support stream or
user_group pills, and use users as source by default.

Using users for source by default, can have unnecessary
suggestions in typeaheads where only user_groups or streams
are needed.

So to solve that, we specify if we want users pill in the input.
This is then utilized in further commits, to clean up hacky code
that deals with intializing source for typeahead.
This commit is contained in:
m-e-l-u-h-a-n 2021-05-25 13:41:21 +05:30 committed by Tim Abbott
parent 637f648828
commit 0080daa5a3
5 changed files with 91 additions and 4 deletions

View File

@ -0,0 +1,77 @@
"use strict";
const {strict: assert} = require("assert");
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const $ = require("../zjsunit/zjquery");
const input_pill = zrequire("input_pill");
const pill_typeahead = zrequire("pill_typeahead");
const noop = function () {};
run_test("set_up", () => {
// Test set_up without any type
let input_pill_typeahead_called = false;
const fake_input = $.create(".input");
fake_input.typeahead = (config) => {
assert.equal(config.items, 5);
assert(config.fixed);
assert(config.dropup);
assert(config.stopAdvance);
// Working of functions that are part of config
// is tested separately based on the widgets that
// try to use it. Here we just check if config
// passed to the typeahead is in the right format.
assert.equal(typeof config.source, "function");
assert.equal(typeof config.highlighter, "function");
assert.equal(typeof config.matcher, "function");
assert.equal(typeof config.sorter, "function");
assert.equal(typeof config.updater, "function");
// input_pill_typeahead_called is set true if
// no exception occurs in pill_typeahead.set_up.
input_pill_typeahead_called = true;
};
const container = $.create(".pill-container");
container.find = () => fake_input;
const pill_widget = input_pill.create({
container,
create_item_from_text: noop,
get_text_from_item: noop,
});
// call set_up with only user type in opts.
pill_typeahead.set_up(fake_input, pill_widget, {user: true});
assert(input_pill_typeahead_called);
// call set_up with only stream type in opts.
input_pill_typeahead_called = false;
pill_typeahead.set_up(fake_input, pill_widget, {stream: true});
assert(input_pill_typeahead_called);
// call set_up with only user_group type in opts.
input_pill_typeahead_called = false;
pill_typeahead.set_up(fake_input, pill_widget, {user_group: true});
assert(input_pill_typeahead_called);
// call set_up with combination two types in opts.
input_pill_typeahead_called = false;
pill_typeahead.set_up(fake_input, pill_widget, {user_group: true, stream: true});
assert(input_pill_typeahead_called);
// call set_up with all three types in opts.
input_pill_typeahead_called = false;
pill_typeahead.set_up(fake_input, pill_widget, {user_group: true, stream: true, user: true});
assert(input_pill_typeahead_called);
// call set_up without specifying type in opts.
input_pill_typeahead_called = false;
blueslip.expect("error", "Unspecified possible item types");
pill_typeahead.set_up(fake_input, pill_widget, {});
assert(!input_pill_typeahead_called);
});

View File

@ -1,3 +1,4 @@
import * as blueslip from "./blueslip";
import * as composebox_typeahead from "./composebox_typeahead";
import * as people from "./people";
import * as stream_pill from "./stream_pill";
@ -21,6 +22,10 @@ function group_matcher(query, item) {
}
export function set_up(input, pills, opts) {
if (!opts.user && !opts.user_group && !opts.stream) {
blueslip.error("Unspecified possible item types");
return;
}
let source = opts.source;
if (!opts.source) {
source = () => user_pill.typeahead_source(pills);

View File

@ -255,13 +255,13 @@ export function initialize_custom_user_type_fields(
if (is_editable) {
const input = pill_container.children(".input");
if (set_handler_on_update) {
const opts = {update_func: update_custom_user_field};
const opts = {update_func: update_custom_user_field, user: true};
pill_typeahead.set_up(input, pills, opts);
pills.onPillRemove(() => {
update_custom_user_field();
});
} else {
pill_typeahead.set_up(input, pills, {});
pill_typeahead.set_up(input, pills, {user: true});
}
}
user_pills.set(field.id, pills);

View File

@ -275,7 +275,7 @@ export function populate_user_groups() {
const input = pill_container.children(".input");
if (can_edit(data.id)) {
const opts = {update_func: update_cancel_button};
const opts = {update_func: update_cancel_button, user: true};
pill_typeahead.set_up(input, pills, opts);
}

View File

@ -418,7 +418,12 @@ function show_subscription_settings(sub) {
simplebar_container: $(".subscriber_list_container"),
});
const opts = {source: get_users_for_subscriber_typeahead, stream: true, user_group: true};
const opts = {
source: get_users_for_subscriber_typeahead,
stream: true,
user_group: true,
user: true,
};
pill_typeahead.set_up(sub_settings.find(".input"), pill_widget, opts);
}