From e64c7a9efed60f49bf591fe09b27e86d3bf1b77c Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 27 Feb 2021 16:15:23 -0800 Subject: [PATCH] js: Convert static/js/upload_widget.js to ES6 module. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - frontend_tests/node_tests/settings_emoji.js | 9 +++-- static/js/avatar.js | 1 + static/js/bundles/app.js | 1 - static/js/global.d.ts | 1 - static/js/realm_icon.js | 1 + static/js/realm_logo.js | 1 + static/js/settings_emoji.js | 1 + static/js/upload_widget.js | 37 ++++++++++++--------- 9 files changed, 32 insertions(+), 21 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 5d1da5a907..ac56476db7 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -184,7 +184,6 @@ "ui_init": false, "unread": false, "unread_ops": false, - "upload_widget": false, "widgetize": false, "zxcvbn": false } diff --git a/frontend_tests/node_tests/settings_emoji.js b/frontend_tests/node_tests/settings_emoji.js index 103350c236..57380cbff7 100644 --- a/frontend_tests/node_tests/settings_emoji.js +++ b/frontend_tests/node_tests/settings_emoji.js @@ -2,11 +2,15 @@ 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 $ = require("../zjsunit/zjquery"); -const upload_widget = set_global("upload_widget", {}); +const upload_widget = {__esModule: true}; +rewiremock("../../static/js/upload_widget").with(upload_widget); +rewiremock.enable(); const settings_emoji = zrequire("settings_emoji"); run_test("build_emoji_upload_widget", () => { @@ -28,3 +32,4 @@ run_test("build_emoji_upload_widget", () => { settings_emoji.build_emoji_upload_widget(); assert(build_widget_stub); }); +rewiremock.disable(); diff --git a/static/js/avatar.js b/static/js/avatar.js index 41ceee6903..a39e22e64c 100644 --- a/static/js/avatar.js +++ b/static/js/avatar.js @@ -1,6 +1,7 @@ "use strict"; const channel = require("./channel"); +const upload_widget = require("./upload_widget"); exports.build_bot_create_widget = function () { // We have to do strange gyrations with the file input to clear it, diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 97a85ad080..8a201188ad 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -34,7 +34,6 @@ import "../message_events"; import "../server_events"; import "../zulip"; import "../templates"; -import "../upload_widget"; import "../avatar"; import "../realm_icon"; import "../realm_logo"; diff --git a/static/js/global.d.ts b/static/js/global.d.ts index 669a982191..fb9e7553c6 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -54,7 +54,6 @@ declare let typing_events: any; declare let ui: any; declare let unread: any; declare let unread_ops: any; -declare let upload_widget: any; declare let widgetize: any; interface JQuery { diff --git a/static/js/realm_icon.js b/static/js/realm_icon.js index be0bd691f9..dc43ba23e1 100644 --- a/static/js/realm_icon.js +++ b/static/js/realm_icon.js @@ -1,6 +1,7 @@ "use strict"; const channel = require("./channel"); +const upload_widget = require("./upload_widget"); exports.build_realm_icon_widget = function (upload_function) { const get_file_input = function () { diff --git a/static/js/realm_logo.js b/static/js/realm_logo.js index ff78955a0e..b6f2eda6f1 100644 --- a/static/js/realm_logo.js +++ b/static/js/realm_logo.js @@ -2,6 +2,7 @@ const channel = require("./channel"); const settings_config = require("./settings_config"); +const upload_widget = require("./upload_widget"); exports.build_realm_logo_widget = function (upload_function, is_night) { let logo_section_id = "#realm-day-logo-upload-widget"; diff --git a/static/js/settings_emoji.js b/static/js/settings_emoji.js index 15387bbdf6..ffc43712b9 100644 --- a/static/js/settings_emoji.js +++ b/static/js/settings_emoji.js @@ -9,6 +9,7 @@ const ListWidget = require("./list_widget"); const loading = require("./loading"); const people = require("./people"); const ui_report = require("./ui_report"); +const upload_widget = require("./upload_widget"); const meta = { loaded: false, diff --git a/static/js/upload_widget.js b/static/js/upload_widget.js index cc97df2239..5c2b511ae1 100644 --- a/static/js/upload_widget.js +++ b/static/js/upload_widget.js @@ -1,5 +1,3 @@ -"use strict"; - const default_max_file_size = 5; const supported_types = ["image/jpeg", "image/png", "image/gif", "image/tiff"]; @@ -12,12 +10,17 @@ function is_image_format(file) { return supported_types.includes(type); } -exports.build_widget = function ( - get_file_input, // function returns a jQuery file input object - file_name_field, // jQuery object to show file name - input_error, // jQuery object for error text - clear_button, // jQuery button to clear last upload choice - upload_button, // jQuery button to open file dialog +export function build_widget( + // function returns a jQuery file input object + get_file_input, + // jQuery object to show file name + file_name_field, + // jQuery object for error text + input_error, + // jQuery button to clear last upload choice + clear_button, + // jQuery button to open file dialog + upload_button, preview_text = null, preview_image = null, max_file_upload_size, @@ -110,11 +113,15 @@ exports.build_widget = function ( // so you can release handlers. close, }; -}; -exports.build_direct_upload_widget = function ( - get_file_input, // function returns a jQuery file input object - input_error, // jQuery object for error text - upload_button, // jQuery button to open file dialog +} + +export function build_direct_upload_widget( + // function returns a jQuery file input object + get_file_input, + // jQuery object for error text + input_error, + // jQuery button to open file dialog + upload_button, upload_function, max_file_upload_size, ) { @@ -177,6 +184,4 @@ exports.build_direct_upload_widget = function ( get_file_input().trigger("click"); e.preventDefault(); }); -}; - -window.upload_widget = exports; +}