Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
js: Convert static/js/typeahead_helper.js to ES6 module.
Anders Kaseorg940861fd7e7e
diff --git a/.eslintrc.json b/.eslintrc.jsonindex 0e18022eead..de59e69a258 100644--- a/.eslintrc.json+++ b/.eslintrc.json@@ -201,7 +201,6 @@"subs": false,"message_view_header": false,"timerender": false,- "typeahead_helper": false,"typing_events": false,"ui": false,"ui_init": false,diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.jsindex 5a7954ce002..13697fe4551 100644--- a/frontend_tests/node_tests/composebox_typeahead.js+++ b/frontend_tests/node_tests/composebox_typeahead.js@@ -1365,25 +1365,25 @@ run_test("content_highlighter", () => {let fake_this = {completing: "emoji"};const emoji = {emoji_name: "person shrugging", emoji_url: "¯\\_(ツ)_/¯"};let th_render_typeahead_item_called = false;- typeahead_helper.render_emoji = (item) => {+ typeahead_helper.__Rewire__("render_emoji", (item) => {assert.deepEqual(item, emoji);th_render_typeahead_item_called = true;- };+ });ct.content_highlighter.call(fake_this, emoji);fake_this = {completing: "mention"};let th_render_person_called = false;- typeahead_helper.render_person = (person) => {+ typeahead_helper.__Rewire__("render_person", (person) => {assert.deepEqual(person, othello);th_render_person_called = true;- };+ });ct.content_highlighter.call(fake_this, othello);let th_render_user_group_called = false;- typeahead_helper.render_user_group = (user_group) => {+ typeahead_helper.__Rewire__("render_user_group", (user_group) => {assert.deepEqual(user_group, backend);th_render_user_group_called = true;- };+ });ct.content_highlighter.call(fake_this, backend);// We don't have any fancy rendering for slash commands yet.@@ -1392,28 +1392,28 @@ run_test("content_highlighter", () => {const me_slash = {text: "/me is excited (Display action text)",};- typeahead_helper.render_typeahead_item = (item) => {+ typeahead_helper.__Rewire__("render_typeahead_item", (item) => {assert.deepEqual(item, {primary: "/me is excited (Display action text)",});th_render_slash_command_called = true;- };+ });ct.content_highlighter.call(fake_this, me_slash);fake_this = {completing: "stream"};let th_render_stream_called = false;- typeahead_helper.render_stream = (stream) => {+ typeahead_helper.__Rewire__("render_stream", (stream) => {assert.deepEqual(stream, denmark_stream);th_render_stream_called = true;- };+ });ct.content_highlighter.call(fake_this, denmark_stream);fake_this = {completing: "syntax"};th_render_typeahead_item_called = false;- typeahead_helper.render_typeahead_item = (item) => {+ typeahead_helper.__Rewire__("render_typeahead_item", (item) => {assert.deepEqual(item, {primary: "py"});th_render_typeahead_item_called = true;- };+ });ct.content_highlighter.call(fake_this, "py");fake_this = {completing: "something-else"};diff --git a/frontend_tests/node_tests/search_suggestion.js b/frontend_tests/node_tests/search_suggestion.jsindex 0e5496d7ef8..c25513a6ff7 100644--- a/frontend_tests/node_tests/search_suggestion.js+++ b/frontend_tests/node_tests/search_suggestion.js@@ -21,7 +21,6 @@ page_params.realm_email_address_visibility =const huddle_data = zrequire("huddle_data");-zrequire("typeahead_helper");const narrow_state = zrequire("narrow_state");const stream_data = zrequire("stream_data");const stream_topic_history = zrequire("stream_topic_history");diff --git a/frontend_tests/node_tests/search_suggestion_legacy.js b/frontend_tests/node_tests/search_suggestion_legacy.jsindex 8e4db165ae2..e2833ceb069 100644--- a/frontend_tests/node_tests/search_suggestion_legacy.js+++ b/frontend_tests/node_tests/search_suggestion_legacy.js@@ -19,7 +19,6 @@ page_params.realm_email_address_visibility =const huddle_data = zrequire("huddle_data");-zrequire("typeahead_helper");const narrow_state = zrequire("narrow_state");const stream_data = zrequire("stream_data");const stream_topic_history = zrequire("stream_topic_history");diff --git a/frontend_tests/node_tests/settings_user_groups.js b/frontend_tests/node_tests/settings_user_groups.jsindex d2a05bed7ab..3c7b74d3385 100644--- a/frontend_tests/node_tests/settings_user_groups.js+++ b/frontend_tests/node_tests/settings_user_groups.js@@ -22,7 +22,8 @@ let create_item_handler;const channel = {__esModule: true};rewiremock("../../static/js/channel").with(channel);-const typeahead_helper = set_global("typeahead_helper", {});+const typeahead_helper = {__esModule: true};+rewiremock("../../static/js/typeahead_helper").with(typeahead_helper);const user_groups = {__esModule: true,get_user_group_from_id: noop,diff --git a/frontend_tests/node_tests/stream_edit.js b/frontend_tests/node_tests/stream_edit.jsindex 2a87fea88df..329a8bfc238 100644--- a/frontend_tests/node_tests/stream_edit.js+++ b/frontend_tests/node_tests/stream_edit.js@@ -28,7 +28,8 @@ set_global("settings_notifications", {rewiremock("../../static/js/stream_color").with({set_colorpicker_color: noop,});-const typeahead_helper = set_global("typeahead_helper", {});+const typeahead_helper = {__esModule: true};+rewiremock("../../static/js/typeahead_helper").with(typeahead_helper);const ui = set_global("ui", {get_scroll_element: noop,});diff --git a/static/js/bundles/app.js b/static/js/bundles/app.jsindex aad3adf49d1..9fbdfe2c8fc 100644--- a/static/js/bundles/app.js+++ b/static/js/bundles/app.js@@ -27,7 +27,6 @@ import "../reload";import "../compose_actions";import "../subs";import "../ui";-import "../typeahead_helper";import "../search_suggestion";import "../search";import "../composebox_typeahead";diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.jsindex 55c228c6ba1..e14be873675 100644--- a/static/js/composebox_typeahead.js+++ b/static/js/composebox_typeahead.js@@ -18,6 +18,7 @@ const rows = require("./rows");const settings_data = require("./settings_data");const stream_data = require("./stream_data");const stream_topic_history = require("./stream_topic_history");+const typeahead_helper = require("./typeahead_helper");const user_groups = require("./user_groups");const user_pill = require("./user_pill");diff --git a/static/js/global.d.ts b/static/js/global.d.tsindex bd3620a3ceb..0f86c110cf6 100644--- a/static/js/global.d.ts+++ b/static/js/global.d.ts@@ -73,7 +73,6 @@ declare let stream_list: any;declare let subs: any;declare let message_view_header: any;declare let timerender: any;-declare let typeahead_helper: any;declare let typing_events: any;declare let ui: any;declare let unread: any;diff --git a/static/js/pill_typeahead.js b/static/js/pill_typeahead.jsindex 30018f7303e..4e36bafd753 100644--- a/static/js/pill_typeahead.js+++ b/static/js/pill_typeahead.js@@ -1,6 +1,7 @@import * as people from "./people";import * as settings_data from "./settings_data";import * as stream_pill from "./stream_pill";+import * as typeahead_helper from "./typeahead_helper";import * as user_pill from "./user_pill";export function set_up(input, pills, opts) {diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.jsindex 410def28e40..24a1dfcc373 100644--- a/static/js/search_suggestion.js+++ b/static/js/search_suggestion.js@@ -10,6 +10,7 @@ const people = require("./people");const settings_data = require("./settings_data");const stream_data = require("./stream_data");const stream_topic_history = require("./stream_topic_history");+const typeahead_helper = require("./typeahead_helper");exports.max_num_of_search_results = 12;diff --git a/static/js/settings_bots.js b/static/js/settings_bots.jsindex 1b6ac2dd536..82f7c493eed 100644--- a/static/js/settings_bots.js+++ b/static/js/settings_bots.js@@ -11,6 +11,7 @@ const channel = require("./channel");const loading = require("./loading");const overlays = require("./overlays");const people = require("./people");+const typeahead_helper = require("./typeahead_helper");exports.hide_errors = function () {$("#bot_table_error").hide();diff --git a/static/js/settings_streams.js b/static/js/settings_streams.jsindex 939646e09b8..6378e5e3619 100644--- a/static/js/settings_streams.js+++ b/static/js/settings_streams.js@@ -6,6 +6,7 @@ const channel = require("./channel");const ListWidget = require("./list_widget");const loading = require("./loading");const stream_data = require("./stream_data");+const typeahead_helper = require("./typeahead_helper");const ui_report = require("./ui_report");const meta = {diff --git a/static/js/subs.js b/static/js/subs.jsindex 832270aeb8a..f82bd726d15 100644--- a/static/js/subs.js+++ b/static/js/subs.js@@ -21,6 +21,7 @@ const stream_data = require("./stream_data");const stream_edit = require("./stream_edit");const stream_muting = require("./stream_muting");const stream_ui_updates = require("./stream_ui_updates");+const typeahead_helper = require("./typeahead_helper");const ui_report = require("./ui_report");const util = require("./util");diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.jsindex 77b6bc44e60..b215c4fdc44 100644--- a/static/js/typeahead_helper.js+++ b/static/js/typeahead_helper.js@@ -1,35 +1,33 @@-"use strict";--const Handlebars = require("handlebars/runtime");-const _ = require("lodash");--const pygments_data = require("../generated/pygments_data.json");-const emoji = require("../shared/js/emoji");-const typeahead = require("../shared/js/typeahead");-const render_typeahead_list_item = require("../templates/typeahead_list_item.hbs");--const people = require("./people");-const pm_conversations = require("./pm_conversations");-const recent_senders = require("./recent_senders");-const settings_data = require("./settings_data");-const stream_data = require("./stream_data");-const user_groups = require("./user_groups");-const util = require("./util");+import Handlebars from "handlebars/runtime";+import _ from "lodash";++import pygments_data from "../generated/pygments_data.json";+import * as emoji from "../shared/js/emoji";+import * as typeahead from "../shared/js/typeahead";+import render_typeahead_list_item from "../templates/typeahead_list_item.hbs";++import * as people from "./people";+import * as pm_conversations from "./pm_conversations";+import * as recent_senders from "./recent_senders";+import * as settings_data from "./settings_data";+import * as stream_data from "./stream_data";+import * as user_groups from "./user_groups";+import * as util from "./util";// Returns an array of private message recipients, removing empty elements.// For example, "a,,b, " => ["a", "b"]-exports.get_cleaned_pm_recipients = function (query_string) {+export function get_cleaned_pm_recipients(query_string) {let recipients = util.extract_pm_recipients(query_string);recipients = recipients.filter((elem) => elem.match(/\S/));return recipients;-};+}-exports.build_highlight_regex = function (query) {+export function build_highlight_regex(query) {const regex = new RegExp("(" + _.escapeRegExp(query) + ")", "ig");return regex;-};+}-exports.highlight_with_escaping_and_regex = function (regex, item) {+export function highlight_with_escaping_and_regex(regex, item) {// We need to assemble this manually (as opposed to doing 'join') because we need to// (1) escape all the pieces and (2) the regex is case-insensitive, and we need// to know the case of the content we're replacing (you can't just use a bolded@@ -47,13 +45,13 @@ exports.highlight_with_escaping_and_regex = function (regex, item) {}return result;-};+}-exports.make_query_highlighter = function (query) {+export function make_query_highlighter(query) {let i;query = query.toLowerCase();- const regex = exports.build_highlight_regex(query);+ const regex = build_highlight_regex(query);return function (phrase) {let result = "";@@ -62,23 +60,23 @@ exports.make_query_highlighter = function (query) {if (i > 0) {result += " ";}- result += exports.highlight_with_escaping_and_regex(regex, parts[i]);+ result += highlight_with_escaping_and_regex(regex, parts[i]);}return result;};-};+}-exports.render_typeahead_item = function (args) {+export function render_typeahead_item(args) {args.has_image = args.img_src !== undefined;args.has_secondary = args.secondary !== undefined;return render_typeahead_list_item(args);-};+}const rendered = {persons: new Map(), streams: new Map(), user_groups: new Map()};-exports.render_person = function (person) {+export function render_person(person) {if (person.special_item_text) {- return exports.render_typeahead_item({+ return render_typeahead_item({primary: person.special_item_text,is_person: true,});@@ -94,20 +92,20 @@ exports.render_person = function (person) {is_person: true,};typeahead_arguments.secondary = settings_data.email_for_user_settings(person);- html = exports.render_typeahead_item(typeahead_arguments);+ html = render_typeahead_item(typeahead_arguments);rendered.persons.set(person.user_id, html);}return html;-};+}-exports.clear_rendered_person = function (user_id) {+export function clear_rendered_person(user_id) {rendered.persons.delete(user_id);-};+}-exports.render_user_group = function (user_group) {+export function render_user_group(user_group) {let html = rendered.user_groups.get(user_group.id);if (html === undefined) {- html = exports.render_typeahead_item({+ html = render_typeahead_item({primary: user_group.name,secondary: user_group.description,is_user_group: true,@@ -116,23 +114,23 @@ exports.render_user_group = function (user_group) {}return html;-};+}-exports.render_person_or_user_group = function (item) {+export function render_person_or_user_group(item) {if (user_groups.is_user_group(item)) {- return exports.render_user_group(item);+ return render_user_group(item);}- return exports.render_person(item);-};+ return render_person(item);+}-exports.clear_rendered_stream = function (stream_id) {+export function clear_rendered_stream(stream_id) {if (rendered.streams.has(stream_id)) {rendered.streams.delete(stream_id);}-};+}-exports.render_stream = function (stream) {+export function render_stream(stream) {let desc = stream.description;const short_desc = desc.slice(0, 35);@@ -142,7 +140,7 @@ exports.render_stream = function (stream) {let html = rendered.streams.get(stream.stream_id);if (html === undefined) {- html = exports.render_typeahead_item({+ html = render_typeahead_item({primary: stream.name,secondary: desc,is_unsubscribed: !stream.subscribed,@@ -151,9 +149,9 @@ exports.render_stream = function (stream) {}return html;-};+}-exports.render_emoji = function (item) {+export function render_emoji(item) {const args = {is_emoji: true,primary: item.emoji_name.split("_").join(" "),@@ -163,15 +161,15 @@ exports.render_emoji = function (item) {} else {args.emoji_code = item.emoji_code;}- return exports.render_typeahead_item(args);-};+ return render_typeahead_item(args);+}-exports.sorter = function (query, objs, get_item) {+export function sorter(query, objs, get_item) {const results = typeahead.triage(query, objs, get_item);return results.matches.concat(results.rest);-};+}-exports.compare_by_pms = function (user_a, user_b) {+export function compare_by_pms(user_a, user_b) {const count_a = people.get_recipient_count(user_a);const count_b = people.get_recipient_count(user_b);@@ -195,9 +193,9 @@ exports.compare_by_pms = function (user_a, user_b) {return 0;}return 1;-};+}-exports.compare_people_for_relevance = function (+export function compare_people_for_relevance(person_a,person_b,tertiary_compare,@@ -241,9 +239,9 @@ exports.compare_people_for_relevance = function (}return tertiary_compare(person_a, person_b);-};+}-exports.sort_people_for_relevance = function (objs, current_stream_name, current_topic) {+export function sort_people_for_relevance(objs, current_stream_name, current_topic) {// If sorting for recipientbox typeahead or compose state is private, then current_stream = ""let current_stream = false;if (current_stream_name) {@@ -251,13 +249,13 @@ exports.sort_people_for_relevance = function (objs, current_stream_name, current}if (!current_stream) {objs.sort((person_a, person_b) =>- exports.compare_people_for_relevance(person_a, person_b, exports.compare_by_pms),+ compare_people_for_relevance(person_a, person_b, compare_by_pms),);} else {const stream_id = current_stream.stream_id;objs.sort((person_a, person_b) =>- exports.compare_people_for_relevance(+ compare_people_for_relevance(person_a,person_b,(user_a, user_b) =>@@ -268,21 +266,21 @@ exports.sort_people_for_relevance = function (objs, current_stream_name, current}return objs;-};+}-exports.compare_by_popularity = function (lang_a, lang_b) {+export function compare_by_popularity(lang_a, lang_b) {const diff = pygments_data.langs[lang_b].priority - pygments_data.langs[lang_a].priority;if (diff !== 0) {return diff;}return util.strcmp(lang_a, lang_b);-};+}-exports.sort_languages = function (matches, query) {+export function sort_languages(matches, query) {const results = typeahead.triage(query, matches);// Languages that start with the query- results.matches = results.matches.sort(exports.compare_by_popularity);+ results.matches = results.matches.sort(compare_by_popularity);// Push exact matches to top.const match_index = results.matches.indexOf(query);@@ -292,11 +290,11 @@ exports.sort_languages = function (matches, query) {}// Languages that have the query somewhere in their name- results.rest = results.rest.sort(exports.compare_by_popularity);+ results.rest = results.rest.sort(compare_by_popularity);return results.matches.concat(results.rest);-};+}-exports.sort_recipients = function (+export function sort_recipients(users,query,current_stream,@@ -313,7 +311,7 @@ exports.sort_recipients = function (}function sort_relevance(items) {- return exports.sort_people_for_relevance(items, current_stream, current_topic);+ return sort_people_for_relevance(items, current_stream, current_topic);}const users_name_results = typeahead.triage(query, users, (p) => p.full_name);@@ -347,7 +345,7 @@ exports.sort_recipients = function (}return items.slice(0, max_num_items);-};+}function slash_command_comparator(slash_command_a, slash_command_b) {if (slash_command_a.name < slash_command_b.name) {@@ -358,7 +356,8 @@ function slash_command_comparator(slash_command_a, slash_command_b) {/* istanbul ignore next */return 0;}-exports.sort_slash_commands = function (matches, query) {++export function sort_slash_commands(matches, query) {// We will likely want to in the future make this sort the// just-`/` commands by something approximating usefulness.const results = typeahead.triage(query, matches, (x) => x.name);@@ -366,7 +365,7 @@ exports.sort_slash_commands = function (matches, query) {results.matches = results.matches.sort(slash_command_comparator);results.rest = results.rest.sort(slash_command_comparator);return results.matches.concat(results.rest);-};+}// Gives stream a score from 0 to 3 based on its activityfunction activity_score(sub) {@@ -388,7 +387,7 @@ function activity_score(sub) {// Sort streams by ranking them by activity. If activity is equal,// as defined bv activity_score, decide based on our weekly traffic// stats.-exports.compare_by_activity = function (stream_a, stream_b) {+export function compare_by_activity(stream_a, stream_b) {let diff = activity_score(stream_b) - activity_score(stream_a);if (diff !== 0) {return diff;@@ -398,28 +397,26 @@ exports.compare_by_activity = function (stream_a, stream_b) {return diff;}return util.strcmp(stream_a.name, stream_b.name);-};+}-exports.sort_streams = function (matches, query) {+export function sort_streams(matches, query) {const name_results = typeahead.triage(query, matches, (x) => x.name);const desc_results = typeahead.triage(query, name_results.rest, (x) => x.description);// Streams that start with the query.- name_results.matches = name_results.matches.sort(exports.compare_by_activity);+ name_results.matches = name_results.matches.sort(compare_by_activity);// Streams with descriptions that start with the query.- desc_results.matches = desc_results.matches.sort(exports.compare_by_activity);+ desc_results.matches = desc_results.matches.sort(compare_by_activity);// Streams with names and descriptions that don't start with the query.- desc_results.rest = desc_results.rest.sort(exports.compare_by_activity);+ desc_results.rest = desc_results.rest.sort(compare_by_activity);return name_results.matches.concat(desc_results.matches.concat(desc_results.rest));-};+}-exports.sort_recipientbox_typeahead = function (query, matches, current_stream) {+export function sort_recipientbox_typeahead(query, matches, current_stream) {// input_text may be one or more pm recipients- const cleaned = exports.get_cleaned_pm_recipients(query);+ const cleaned = get_cleaned_pm_recipients(query);query = cleaned[cleaned.length - 1];- return exports.sort_recipients(matches, query, current_stream);-};--window.typeahead_helper = exports;+ return sort_recipients(matches, query, current_stream);+}