From c66d61675202f123126e699ca40a1d17cf4f83a3 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 27 Feb 2021 16:19:16 -0800 Subject: [PATCH] js: Convert static/js/settings_bots.js to ES6 module. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - frontend_tests/node_tests/bot_data.js | 10 ++- frontend_tests/node_tests/dispatch.js | 3 +- static/js/admin.js | 1 + static/js/bot_data.js | 1 + static/js/bundles/app.js | 1 - static/js/global.d.ts | 1 - static/js/server_events_dispatch.js | 1 + static/js/settings.js | 1 + static/js/settings_account.js | 1 + static/js/settings_bots.js | 125 ++++++++++++-------------- static/js/settings_sections.js | 1 + static/js/settings_users.js | 1 + 13 files changed, 76 insertions(+), 72 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b1ee7b647a..7e185ef891 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -156,7 +156,6 @@ "recent_topics": false, "search_pill_widget": false, "settings": false, - "settings_bots": false, "settings_emoji": false, "settings_exports": false, "settings_linkifiers": false, diff --git a/frontend_tests/node_tests/bot_data.js b/frontend_tests/node_tests/bot_data.js index 93d4708e16..3aad29f122 100644 --- a/frontend_tests/node_tests/bot_data.js +++ b/frontend_tests/node_tests/bot_data.js @@ -2,14 +2,19 @@ const {strict: assert} = require("assert"); -const {set_global, zrequire} = require("../zjsunit/namespace"); +const rewiremock = require("rewiremock/node"); + +const {zrequire} = require("../zjsunit/namespace"); const {run_test} = require("../zjsunit/test"); const _settings_bots = { + __esModule: true, render_bots: () => {}, }; -set_global("settings_bots", _settings_bots); +rewiremock("../../static/js/settings_bots").with(_settings_bots); + +rewiremock.enable(); const bot_data = zrequire("bot_data"); const people = zrequire("people"); @@ -171,3 +176,4 @@ run_test("test_basics", () => { assert.equal(bots[1].email, "bot2@zulip.com"); })(); }); +rewiremock.disable(); diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 16d437cdb1..b105c21b30 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -57,7 +57,8 @@ const scroll_bar = {__esModule: true}; rewiremock("../../static/js/scroll_bar").with(scroll_bar); const settings_account = {__esModule: true}; rewiremock("../../static/js/settings_account").with(settings_account); -const settings_bots = set_global("settings_bots", {}); +const settings_bots = {__esModule: true}; +rewiremock("../../static/js/settings_bots").with(settings_bots); const settings_display = {__esModule: true}; rewiremock("../../static/js/settings_display").with(settings_display); const settings_emoji = set_global("settings_emoji", {}); diff --git a/static/js/admin.js b/static/js/admin.js index c427a36ef2..375008c44b 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -4,6 +4,7 @@ const render_admin_tab = require("../templates/admin_tab.hbs"); const render_settings_organization_settings_tip = require("../templates/settings/organization_settings_tip.hbs"); const overlays = require("./overlays"); +const settings_bots = require("./settings_bots"); const settings_config = require("./settings_config"); const settings_data = require("./settings_data"); const settings_panel_menu = require("./settings_panel_menu"); diff --git a/static/js/bot_data.js b/static/js/bot_data.js index 1d75325ab7..08d60112d0 100644 --- a/static/js/bot_data.js +++ b/static/js/bot_data.js @@ -3,6 +3,7 @@ const _ = require("lodash"); const people = require("./people"); +const settings_bots = require("./settings_bots"); const bots = new Map(); diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 4314932d3f..1d27d2614f 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -35,7 +35,6 @@ import "../server_events"; import "../zulip"; import "../templates"; import "../dropdown_list_widget"; -import "../settings_bots"; import "../settings_sections"; import "../settings_emoji"; import "../settings_exports"; diff --git a/static/js/global.d.ts b/static/js/global.d.ts index cb18d464a3..de1bf38995 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -28,7 +28,6 @@ declare let reactions: any; declare let recent_topics: any; declare let search_pill_widget: any; declare let settings: any; -declare let settings_bots: any; declare let settings_emoji: any; declare let settings_exports: any; declare let settings_invites: any; diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index c04e6c2d62..b0b4630627 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -22,6 +22,7 @@ import * as realm_logo from "./realm_logo"; import * as reload from "./reload"; import * as scroll_bar from "./scroll_bar"; import * as settings_account from "./settings_account"; +import * as settings_bots from "./settings_bots"; import * as settings_config from "./settings_config"; import * as settings_display from "./settings_display"; import * as settings_notifications from "./settings_notifications"; diff --git a/static/js/settings.js b/static/js/settings.js index 288de55601..525c786eed 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -6,6 +6,7 @@ const render_settings_tab = require("../templates/settings_tab.hbs"); const overlays = require("./overlays"); const people = require("./people"); const settings_account = require("./settings_account"); +const settings_bots = require("./settings_bots"); const settings_config = require("./settings_config"); const settings_notifications = require("./settings_notifications"); const settings_panel_menu = require("./settings_panel_menu"); diff --git a/static/js/settings_account.js b/static/js/settings_account.js index 94a983e82f..1880f1aace 100644 --- a/static/js/settings_account.js +++ b/static/js/settings_account.js @@ -11,6 +11,7 @@ import * as overlays from "./overlays"; import * as people from "./people"; import * as pill_typeahead from "./pill_typeahead"; import * as popovers from "./popovers"; +import * as settings_bots from "./settings_bots"; import * as settings_ui from "./settings_ui"; import * as setup from "./setup"; import * as ui_report from "./ui_report"; diff --git a/static/js/settings_bots.js b/static/js/settings_bots.js index 26615f1aee..36737438d5 100644 --- a/static/js/settings_bots.js +++ b/static/js/settings_bots.js @@ -1,23 +1,21 @@ -"use strict"; +import ClipboardJS from "clipboard"; -const ClipboardJS = require("clipboard"); +import render_bot_avatar_row from "../templates/bot_avatar_row.hbs"; +import render_edit_bot from "../templates/edit_bot.hbs"; +import render_settings_edit_embedded_bot_service from "../templates/settings/edit_embedded_bot_service.hbs"; +import render_settings_edit_outgoing_webhook_service from "../templates/settings/edit_outgoing_webhook_service.hbs"; -const render_bot_avatar_row = require("../templates/bot_avatar_row.hbs"); -const render_edit_bot = require("../templates/edit_bot.hbs"); -const render_settings_edit_embedded_bot_service = require("../templates/settings/edit_embedded_bot_service.hbs"); -const render_settings_edit_outgoing_webhook_service = require("../templates/settings/edit_outgoing_webhook_service.hbs"); +import * as avatar from "./avatar"; +import * as channel from "./channel"; +import * as loading from "./loading"; +import * as overlays from "./overlays"; +import * as people from "./people"; +import * as typeahead_helper from "./typeahead_helper"; -const avatar = require("./avatar"); -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 () { +export function hide_errors() { $("#bot_table_error").hide(); $(".bot_error").hide(); -}; +} const focus_tab = { add_a_new_bot_tab() { @@ -26,7 +24,7 @@ const focus_tab = { $("#add-a-new-bot-form").show(); $("#active_bots_list").hide(); $("#inactive_bots_list").hide(); - exports.hide_errors(); + hide_errors(); }, active_bots_tab() { $("#bots_lists_navbar .active").removeClass("active"); @@ -34,7 +32,7 @@ const focus_tab = { $("#add-a-new-bot-form").hide(); $("#active_bots_list").show(); $("#inactive_bots_list").hide(); - exports.hide_errors(); + hide_errors(); }, inactive_bots_tab() { $("#bots_lists_navbar .active").removeClass("active"); @@ -42,23 +40,23 @@ const focus_tab = { $("#add-a-new-bot-form").hide(); $("#active_bots_list").hide(); $("#inactive_bots_list").show(); - exports.hide_errors(); + hide_errors(); }, }; -exports.get_bot_info_div = function (bot_id) { +export function get_bot_info_div(bot_id) { const sel = `.bot_info[data-user-id="${CSS.escape(bot_id)}"]`; return $(sel).expectOne(); -}; +} -exports.bot_error = function (bot_id, xhr) { - const bot_info = exports.get_bot_info_div(bot_id); +export function bot_error(bot_id, xhr) { + const bot_info = get_bot_info_div(bot_id); const bot_error_div = bot_info.find(".bot_error"); bot_error_div.text(JSON.parse(xhr.responseText).msg); bot_error_div.show(); const bot_box = bot_info.closest(".bot-information-box"); bot_box.scrollTop(bot_box[0].scrollHeight - bot_box[0].clientHeight); -}; +} function add_bot_row(info) { const row = $(render_bot_avatar_row(info)); @@ -77,11 +75,11 @@ function is_local_part(value, element) { ); } -exports.type_id_to_string = function (type_id) { +export function type_id_to_string(type_id) { return page_params.bot_types.find((bot_type) => bot_type.type_id === type_id).name; -}; +} -exports.render_bots = function () { +export function render_bots() { $("#active_bots_list").empty(); $("#inactive_bots_list").empty(); @@ -93,7 +91,7 @@ exports.render_bots = function () { name: elem.full_name, email: elem.email, user_id: elem.user_id, - type: exports.type_id_to_string(elem.bot_type), + type: type_id_to_string(elem.bot_type), avatar_url: elem.avatar_url, api_key: elem.api_key, is_active: elem.is_active, @@ -102,7 +100,7 @@ exports.render_bots = function () { user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active; } - if (exports.can_create_new_bots() && !user_owns_an_active_bot) { + if (can_create_new_bots() && !user_owns_an_active_bot) { focus_tab.add_a_new_bot_tab(); return; } @@ -120,19 +118,19 @@ exports.render_bots = function () { $("#active_bots_list").hide(); $("#inactive_bots_list").show(); } -}; +} -exports.generate_zuliprc_uri = function (bot_id) { +export function generate_zuliprc_uri(bot_id) { const bot = bot_data.get(bot_id); - const data = exports.generate_zuliprc_content(bot); - return exports.encode_zuliprc_as_uri(data); -}; + const data = generate_zuliprc_content(bot); + return encode_zuliprc_as_uri(data); +} -exports.encode_zuliprc_as_uri = function (zuliprc) { +export function encode_zuliprc_as_uri(zuliprc) { return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc); -}; +} -exports.generate_zuliprc_content = function (bot) { +export function generate_zuliprc_content(bot) { let token; // For outgoing webhooks, include the token in the zuliprc. // It's needed for authenticating to the Botserver. @@ -151,9 +149,9 @@ exports.generate_zuliprc_content = function (bot) { // Some tools would not work in files without a trailing new line. "\n" ); -}; +} -exports.generate_botserverrc_content = function (email, api_key, token) { +export function generate_botserverrc_content(email, api_key, token) { return ( "[]" + "\nemail=" + @@ -166,9 +164,9 @@ exports.generate_botserverrc_content = function (email, api_key, token) { token + "\n" ); -}; +} -exports.bot_creation_policy_values = { +export const bot_creation_policy_values = { admins_only: { code: 3, description: i18n.t("Admins"), @@ -183,7 +181,7 @@ exports.bot_creation_policy_values = { }, }; -exports.can_create_new_bots = function () { +export function can_create_new_bots() { if (page_params.is_admin) { return true; } @@ -192,14 +190,11 @@ exports.can_create_new_bots = function () { return false; } - return ( - page_params.realm_bot_creation_policy !== - exports.bot_creation_policy_values.admins_only.code - ); -}; + return page_params.realm_bot_creation_policy !== bot_creation_policy_values.admins_only.code; +} -exports.update_bot_settings_tip = function () { - const permission_type = exports.bot_creation_policy_values; +export function update_bot_settings_tip() { + const permission_type = bot_creation_policy_values; const current_permission = page_params.realm_bot_creation_policy; let tip_text; if (current_permission === permission_type.admins_only.code) { @@ -210,13 +205,13 @@ exports.update_bot_settings_tip = function () { tip_text = i18n.t("Anyone in this organization can add bots"); } $(".bot-settings-tip").text(tip_text); -}; +} -exports.update_bot_permissions_ui = function () { - exports.update_bot_settings_tip(); - exports.hide_errors(); +export function update_bot_permissions_ui() { + update_bot_settings_tip(); + hide_errors(); $("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy); - if (!exports.can_create_new_bots()) { + if (!can_create_new_bots()) { $("#create_bot_form").hide(); $(".add-a-new-bot-tab").hide(); focus_tab.active_bots_tab(); @@ -224,9 +219,9 @@ exports.update_bot_permissions_ui = function () { $("#create_bot_form").show(); $(".add-a-new-bot-tab").show(); } -}; +} -exports.set_up = function () { +export function set_up() { $("#payload_url_inputbox").hide(); $("#create_payload_url").val(""); $("#service_name_list").hide(); @@ -243,7 +238,7 @@ exports.set_up = function () { for (const bot of bot_data.get_all_bots_for_current_user()) { if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) { const bot_token = bot_data.get_services(bot.user_id)[0].token; - content += exports.generate_botserverrc_content(bot.email, bot.api_key, bot_token); + content += generate_botserverrc_content(bot.email, bot.api_key, bot_token); } } @@ -253,7 +248,7 @@ exports.set_up = function () { ); }); - exports.render_bots(); + render_bots(); $.validator.addMethod( "bot_local_part", @@ -273,7 +268,7 @@ exports.set_up = function () { $("#create_bot_form").validate({ errorClass: "text-error", success() { - exports.hide_errors(); + hide_errors(); }, submitHandler() { const bot_type = $("#create_bot_type :selected").val(); @@ -316,7 +311,7 @@ exports.set_up = function () { processData: false, contentType: false, success() { - exports.hide_errors(); + hide_errors(); $("#create_bot_name").val(""); $("#create_bot_short_name").val(""); $("#create_payload_url").val(""); @@ -382,7 +377,7 @@ exports.set_up = function () { }); }, error(xhr) { - exports.bot_error(bot_id, xhr); + bot_error(bot_id, xhr); }, }); }); @@ -393,7 +388,7 @@ exports.set_up = function () { channel.post({ url: "/json/users/" + encodeURIComponent(user_id) + "/reactivate", error(xhr) { - exports.bot_error(user_id, xhr); + bot_error(user_id, xhr); }, }); }); @@ -543,7 +538,7 @@ exports.set_up = function () { $("#active_bots_list").on("click", "a.download_bot_zuliprc", function () { const bot_info = $(this).closest(".bot-information-box").find(".bot_info"); const bot_id = Number.parseInt(bot_info.attr("data-user-id"), 10); - $(this).attr("href", exports.generate_zuliprc_uri(bot_id)); + $(this).attr("href", generate_zuliprc_uri(bot_id)); }); new ClipboardJS("#copy_zuliprc", { @@ -551,7 +546,7 @@ exports.set_up = function () { const bot_info = $(trigger).closest(".bot-information-box").find(".bot_info"); const bot_id = Number.parseInt(bot_info.attr("data-user-id"), 10); const bot = bot_data.get(bot_id); - const data = exports.generate_zuliprc_content(bot); + const data = generate_zuliprc_content(bot); return data; }, }); @@ -573,6 +568,4 @@ exports.set_up = function () { e.stopPropagation(); focus_tab.inactive_bots_tab(); }); -}; - -window.settings_bots = exports; +} diff --git a/static/js/settings_sections.js b/static/js/settings_sections.js index 60b408d9d3..3825dc3cb6 100644 --- a/static/js/settings_sections.js +++ b/static/js/settings_sections.js @@ -3,6 +3,7 @@ const alert_words_ui = require("./alert_words_ui"); const attachments_ui = require("./attachments_ui"); const settings_account = require("./settings_account"); +const settings_bots = require("./settings_bots"); const settings_display = require("./settings_display"); const settings_muting = require("./settings_muting"); const settings_notifications = require("./settings_notifications"); diff --git a/static/js/settings_users.js b/static/js/settings_users.js index 9b9ba3755b..3ab8a15d48 100644 --- a/static/js/settings_users.js +++ b/static/js/settings_users.js @@ -11,6 +11,7 @@ const overlays = require("./overlays"); const people = require("./people"); const presence = require("./presence"); const settings_account = require("./settings_account"); +const settings_bots = require("./settings_bots"); const settings_config = require("./settings_config"); const settings_data = require("./settings_data"); const settings_panel_menu = require("./settings_panel_menu");