Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
bootstrap_typeahead: Change matcher to accept query & return a function.
Pratik Chandac8eea3aa9f1f
diff --git a/web/src/bootstrap_typeahead.ts b/web/src/bootstrap_typeahead.tsindex 7492c483197..0ffd3e3bbe3 100644--- a/web/src/bootstrap_typeahead.ts+++ b/web/src/bootstrap_typeahead.ts@@ -220,7 +220,7 @@ export type TypeaheadInputElement =export class Typeahead<ItemType extends string | object> {input_element: TypeaheadInputElement;items: number;- matcher: (item: ItemType, query: string) => boolean;+ matcher: (query: string) => (item: ItemType) => boolean;sorter: (items: ItemType[], query: string) => ItemType[];item_html: (query: string) => (item: ItemType) => string | undefined;updater: (@@ -282,7 +282,7 @@ export class Typeahead<ItemType extends string | object> {assert(!this.input_element.$element.is("[contenteditable]"));}this.items = options.items ?? MAX_ITEMS;- this.matcher = options.matcher ?? ((item, query) => this.defaultMatcher(item, query));+ this.matcher = options.matcher ?? ((query) => (item) => this.defaultMatcher(item, query));this.sorter = options.sorter;this.item_html = options.item_html;this.updater = options.updater ?? ((items) => this.defaultUpdater(items));@@ -564,7 +564,7 @@ export class Typeahead<ItemType extends string | object> {}process(items: ItemType[]): this {- const matching_items = $.grep(items, (item) => this.matcher(item, this.query));+ const matching_items = $.grep(items, this.matcher(this.query));const final_items = this.sorter(matching_items, this.query);@@ -964,7 +964,7 @@ type TypeaheadOptions<ItemType> = {footer_html?: (matching_items: ItemType[]) => string | false;helpOnEmptyStrings?: boolean;hideOnEmptyAfterBackspace?: boolean;- matcher?: (item: ItemType, query: string) => boolean;+ matcher?: (query: string) => (item: ItemType) => boolean;on_escape?: () => void;clear_typeahead_tooltip?: () => void;openInputFieldOnKeyUp?: () => void;diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.tsindex baed003d931..779d4c8ff4f 100644--- a/web/src/composebox_typeahead.ts+++ b/web/src/composebox_typeahead.ts@@ -1573,9 +1573,8 @@ export function initialize_topic_edit_typeahead(});};},- matcher(item: string, query: string): boolean {- const matcher = get_topic_matcher(query);- return matcher(item);+ matcher(query: string): (item: string) => boolean {+ return get_topic_matcher(query);},sorter(items: string[], query: string): string[] {const stream_id = stream_data.get_stream_id(stream_name);@@ -1660,8 +1659,8 @@ export function initialize_compose_typeahead($element: JQuery<HTMLTextAreaElemen// inside the typeahead library.source: get_candidates,item_html: content_item_html,- matcher() {- return true;+ matcher(_query: string) {+ return () => true;},sorter(items) {return items;@@ -1764,12 +1763,14 @@ export function initialize({return typeahead_helper.render_person_or_user_group(item);};},- matcher(item: UserPillData | string, query: string): boolean {- if (typeof item === "string") {- const matcher = get_topic_matcher(query);- return matcher(item);- }- return true;+ matcher(query: string): (item: UserPillData | string) => boolean {+ const topic_matcher = get_topic_matcher(query);+ return (item: UserPillData | string): boolean => {+ if (typeof item === "string") {+ return topic_matcher(item);+ }+ return true;+ };},sorter(items: (UserPillData | string)[], query: string): (UserPillData | string)[] {const topic_items: string[] = [];@@ -1855,8 +1856,8 @@ export function initialize({return (item: UserGroupPillData | UserPillData) =>typeahead_helper.render_person_or_user_group(item);},- matcher(): boolean {- return true;+ matcher(_query: string) {+ return () => true;},sorter(items: (UserGroupPillData | UserPillData)[]): (UserGroupPillData | UserPillData)[] {return items;diff --git a/web/src/pill_typeahead.ts b/web/src/pill_typeahead.tsindex 6f6c3adffe4..bf588a588bd 100644--- a/web/src/pill_typeahead.ts+++ b/web/src/pill_typeahead.ts@@ -50,10 +50,10 @@ export function set_up_user(item_html(_query: string): (item: UserPillData) => string {return (item: UserPillData) => typeahead_helper.render_person(item);},- matcher(item: UserPillData, query: string): boolean {+ matcher(query: string): (item: UserPillData) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");- return person_matcher(query, item);+ return (item: UserPillData) => person_matcher(query, item);},sorter(matches: UserPillData[], query: string): UserPillData[] {const users = matches.filter((match) => people.is_known_user_id(match.user.user_id));@@ -99,14 +99,14 @@ export function set_up_stream(item_html(_query: string): (item: StreamPillData) => string {return (item: StreamPillData) => typeahead_helper.render_stream(item);},- matcher(item: StreamPillData, query: string): boolean {+ matcher(query: string): (item: StreamPillData) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");query = query.trim();if (query.startsWith("#")) {query = query.slice(1);}- return item.name.toLowerCase().includes(query);+ return (item: StreamPillData) => item.name.toLowerCase().includes(query);},sorter(matches: StreamPillData[], query: string): StreamPillData[] {const stream_matches: StreamPillData[] = [];@@ -150,10 +150,11 @@ export function set_up_user_group(item_html(_query: string): (item: UserGroupPillData) => string {return (item: UserGroupPillData) => typeahead_helper.render_user_group(item);},- matcher(item: UserGroupPillData, query: string): boolean {+ matcher(query: string): (item: UserGroupPillData) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");- return group_matcher(query, item);++ return (item: UserGroupPillData) => group_matcher(query, item);},sorter(matches: UserGroupPillData[], query: string): UserGroupPillData[] {return typeahead_helper.sort_user_groups(matches, query);@@ -204,19 +205,21 @@ export function set_up_group_setting_typeahead(return typeahead_helper.render_person(item);};},- matcher(item: GroupSettingTypeaheadItem, query: string): boolean {+ matcher(query: string): (item: GroupSettingTypeaheadItem) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");- let matches = false;- if (item.type === "user_group") {- matches = matches || group_matcher(query, item);- }+ return (item: GroupSettingTypeaheadItem): boolean => {+ let matches = false;+ if (item.type === "user_group") {+ matches = matches || group_matcher(query, item);+ }- if (item.type === "user") {- matches = matches || person_matcher(query, item);- }- return matches;+ if (item.type === "user") {+ matches = matches || person_matcher(query, item);+ }+ return matches;+ };},sorter(matches: GroupSettingTypeaheadItem[], query: string): GroupSettingTypeaheadItem[] {const users: UserPillData[] = [];@@ -350,32 +353,34 @@ export function set_up_combined(return typeahead_helper.render_person(item);};},- matcher(item: TypeaheadItem, query: string): boolean {+ matcher(query: string): (item: TypeaheadItem) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");- if (include_streams(query) && item.type === "stream") {- query = query.trim().slice(1);- return item.name.toLowerCase().includes(query);- }+ return (item: TypeaheadItem): boolean => {+ if (include_streams(query) && item.type === "stream") {+ const stream_query = query.trim().slice(1);+ return item.name.toLowerCase().includes(stream_query);+ }- if (include_user_groups && query.startsWith("@")) {- if (item.type === "user_group") {- const normalized_query = query.slice(1);- return group_matcher(normalized_query, item);+ if (include_user_groups && query.startsWith("@")) {+ if (item.type === "user_group") {+ const normalized_query = query.slice(1);+ return group_matcher(normalized_query, item);+ }+ return false;}- return false;- }- let matches = false;- if (include_user_groups && item.type === "user_group") {- matches = group_matcher(query, item);- }+ let matches = false;+ if (include_user_groups && item.type === "user_group") {+ matches = group_matcher(query, item);+ }- if (include_users && item.type === "user") {- matches = matches || person_matcher(query, item);- }- return matches;+ if (include_users && item.type === "user") {+ matches = matches || person_matcher(query, item);+ }+ return matches;+ };},sorter(matches: TypeaheadItem[], query: string): TypeaheadItem[] {if (include_streams(query)) {diff --git a/web/src/search.ts b/web/src/search.tsindex b28bd81054f..ec324685144 100644--- a/web/src/search.ts+++ b/web/src/search.ts@@ -233,8 +233,8 @@ export function initialize(opts: {on_narrow_search: OnNarrowSearch}): void {const text_terms = Filter.parse(search_bar_text);return text_terms.at(-1)?.operator === "search";},- matcher(): boolean {- return true;+ matcher(_query: string) {+ return () => true;},updater(search_string: string): string {if (search_string) {diff --git a/web/src/settings_playgrounds.ts b/web/src/settings_playgrounds.tsindex 4de160fee0c..be7026efd30 100644--- a/web/src/settings_playgrounds.ts+++ b/web/src/settings_playgrounds.ts@@ -168,9 +168,9 @@ function build_page(): void {item_html(_query: string): (item: string) => string {return (item: string) => render_typeahead_item({primary: language_labels.get(item)});},- matcher(item: string, query: string): boolean {+ matcher(query: string): (item: string) => boolean {const q = query.trim().toLowerCase();- return item.toLowerCase().startsWith(q);+ return (item: string) => item.toLowerCase().startsWith(q);},sorter(items: string[], query: string): string[] {return bootstrap_typeahead.defaultSorter(items, query);diff --git a/web/src/topic_filter_pill.ts b/web/src/topic_filter_pill.tsindex 88e311a4185..c025695a01e 100644--- a/web/src/topic_filter_pill.ts+++ b/web/src/topic_filter_pill.ts@@ -16,7 +16,7 @@ export type TopicFilterPillWidget = InputPillContainer<TopicFilterPill>;type TopicFilterTypeaheadOptions = {item_html: (query: string) => (item: TopicFilterPill) => string | undefined;- matcher: (item: TopicFilterPill, query: string) => boolean;+ matcher: (query: string) => (item: TopicFilterPill) => boolean;sorter: (items: TopicFilterPill[], query: string) => TopicFilterPill[];stopAdvance: boolean;dropup: boolean;@@ -51,14 +51,13 @@ export function get_typeahead_base_options(): TopicFilterTypeaheadOptions {item_html(_query: string) {return (item: TopicFilterPill) => typeahead_helper.render_topic_state(item.label);},- matcher(item: TopicFilterPill, query: string) {+ matcher(query: string) {// This basically only matches if `is:` is in the query.- return (+ return (item: TopicFilterPill) =>query.includes(":") &&(item.syntax.toLowerCase().startsWith(query.toLowerCase()) ||(item.syntax.startsWith("-") &&- item.syntax.slice(1).toLowerCase().startsWith(query.toLowerCase())))- );+ item.syntax.slice(1).toLowerCase().startsWith(query.toLowerCase())));},sorter(items: TopicFilterPill[], _query: string) {// This sort order places "Unresolved topics" firstdiff --git a/web/tests/pill_typeahead.test.cjs b/web/tests/pill_typeahead.test.cjsindex 3eaa961d3ad..48130cfc778 100644--- a/web/tests/pill_typeahead.test.cjs+++ b/web/tests/pill_typeahead.test.cjs@@ -201,9 +201,9 @@ run_test("set_up_user", ({mock_template, override, override_rewire}) => {(function test_matcher() {let result;- result = config.matcher(me_item, person_query);+ result = config.matcher(person_query)(me_item);assert.ok(result);- result = config.matcher(jill_item, person_query);+ result = config.matcher(person_query)(jill_item);assert.ok(!result);})();@@ -293,9 +293,9 @@ run_test("set_up_stream", ({mock_template, override, override_rewire}) => {(function test_matcher() {let result;- result = config.matcher(denmark_item, stream_query);+ result = config.matcher(stream_query)(denmark_item);assert.ok(result);- result = config.matcher(sweden_item, stream_query);+ result = config.matcher(stream_query)(sweden_item);assert.ok(!result);})();@@ -385,9 +385,9 @@ run_test("set_up_user_group", ({mock_template, override, override_rewire}) => {(function test_matcher() {let result;- result = config.matcher(testers_item, group_query);+ result = config.matcher(group_query)(testers_item);assert.ok(result);- result = config.matcher(admins_item, group_query);+ result = config.matcher(group_query)(admins_item);assert.ok(!result);})();@@ -507,9 +507,9 @@ run_test("set_up_combined", ({mock_template, override, override_rewire}) => {(function test_matcher() {let result;if (opts.stream) {- result = config.matcher(denmark_item, stream_query);+ result = config.matcher(stream_query)(denmark_item);assert.ok(result);- result = config.matcher(sweden_item, stream_query);+ result = config.matcher(stream_query)(sweden_item);assert.ok(!result);}if (opts.user_group && opts.user) {@@ -518,36 +518,36 @@ run_test("set_up_combined", ({mock_template, override, override_rewire}) => {or group is returned. */// group query, with correct item.- result = config.matcher(testers_item, group_query);+ result = config.matcher(group_query)(testers_item);assert.ok(result);// group query, with wrong item.- result = config.matcher(admins_item, group_query);+ result = config.matcher(group_query)(admins_item);assert.ok(!result);// person query with correct item.- result = config.matcher(me_item, person_query);+ result = config.matcher(person_query)(me_item);assert.ok(result);// person query with wrong item.- result = config.matcher(jill_item, person_query);+ result = config.matcher(person_query)(jill_item);assert.ok(!result);// @-prefixed query matches groups only.- result = config.matcher(testers_item, "@" + group_query);+ result = config.matcher("@" + group_query)(testers_item);assert.ok(result);- result = config.matcher(admins_item, "@admins");+ result = config.matcher("@admins")(admins_item);assert.ok(result);// @-prefixed query must NOT match users.- result = config.matcher(me_item, "@" + person_query);+ result = config.matcher("@" + person_query)(me_item);assert.ok(!result);}if (opts.user_group && !opts.user) {- result = config.matcher(testers_item, group_query);+ result = config.matcher(group_query)(testers_item);assert.ok(result);- result = config.matcher(admins_item, group_query);+ result = config.matcher(group_query)(admins_item);assert.ok(!result);}if (opts.user && !opts.user_group) {- result = config.matcher(me_item, person_query);+ result = config.matcher(person_query)(me_item);assert.ok(result);- result = config.matcher(jill_item, person_query);+ result = config.matcher(person_query)(jill_item);assert.ok(!result);}})();@@ -801,16 +801,16 @@ run_test("set_up_group_setting_typeahead", ({mock_template, override, override_r(function test_matcher() {let result;// group query, with correct item.- result = config.matcher(testers_item, group_query);+ result = config.matcher(group_query)(testers_item);assert.ok(result);// group query, with wrong item.- result = config.matcher(admins_item, group_query);+ result = config.matcher(group_query)(admins_item);assert.ok(!result);// person query with correct item.- result = config.matcher(me_item, person_query);+ result = config.matcher(person_query)(me_item);assert.ok(result);// person query with wrong item.- result = config.matcher(jill_item, person_query);+ result = config.matcher(person_query)(jill_item);assert.ok(!result);})();diff --git a/web/tests/search.test.cjs b/web/tests/search.test.cjsindex c53b29d1f1d..ac5115cbb6c 100644--- a/web/tests/search.test.cjs+++ b/web/tests/search.test.cjs@@ -82,7 +82,7 @@ run_test("initialize", ({override, override_rewire, mock_template}) => {assert.equal(input_element.$element[0], $search_query_box[0]);assert.equal(opts.items, 999);assert.equal(opts.helpOnEmptyStrings, true);- assert.equal(opts.matcher(), true);+ assert.equal(opts.matcher("")(), true);return {lookup() {