From 9eb3bdaf6c133790d1e697714f5ae929e6213e10 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 15 May 2018 20:03:14 +0000 Subject: [PATCH] page load: Make initializations more explicit. We now initialize most modules in ui_init.js, which isn't the perfect place to do it, but at least now we have a mostly consolidated entry point. All the new foo.initialize() methods introduced in this module run the same order relative to each other as before this commit. (I did some console logging with a hacked version of the program to get the order right.) They happen a bit later than before, though. A couple modules still have the `$(function() {` idiom for miscellaneous reasons: archive - is a different bundle common - used elsewhere list_render - non-standard code style scroll_bar - no exports setup - probably special? socket - $(function () is nested! transmit - coupled to socket translations - i18n is a bigger problem ui_init - this bootstraps everything --- .eslintrc.json | 2 ++ frontend_tests/node_tests/stream_list.js | 2 ++ static/js/click_handlers.js | 4 ++-- static/js/condense.js | 4 ++-- static/js/copy_and_paste.js | 4 ++-- static/js/echo.js | 4 ++-- static/js/invite.js | 4 ++-- static/js/lightbox.js | 4 ++-- static/js/message_viewport.js | 4 ++-- static/js/muting_ui.js | 4 ++-- static/js/overlays.js | 4 ++-- static/js/stream_color.js | 4 ++-- static/js/stream_edit.js | 4 ++-- static/js/subs.js | 4 ++-- static/js/tab_bar.js | 4 ++-- static/js/timerender.js | 4 ++-- static/js/ui_init.js | 25 +++++++++++++++++++++++- 17 files changed, 56 insertions(+), 29 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index ed9531ebe5..d5572edd34 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -25,6 +25,8 @@ "page_params": false, "attachments_ui": false, "csrf_token": false, + "copy_and_paste": false, + "click_handlers": false, "typeahead_helper": false, "pygments_data": false, "popovers": false, diff --git a/frontend_tests/node_tests/stream_list.js b/frontend_tests/node_tests/stream_list.js index 42ffca4287..f778933502 100644 --- a/frontend_tests/node_tests/stream_list.js +++ b/frontend_tests/node_tests/stream_list.js @@ -18,6 +18,8 @@ zrequire('scroll_util'); zrequire('list_cursor'); zrequire('stream_list'); +stream_color.initialize(); + var noop = function () {}; var return_false = function () { return false; }; var return_true = function () { return true; }; diff --git a/static/js/click_handlers.js b/static/js/click_handlers.js index c5d41cd4b0..0b45ca3ab8 100644 --- a/static/js/click_handlers.js +++ b/static/js/click_handlers.js @@ -5,7 +5,7 @@ var exports = {}; // You won't find every click handler here, but it's a good place to start! -$(function () { +exports.initialize = function () { // MOUSE MOVING VS DRAGGING FOR SELECTION DATA TRACKING @@ -752,7 +752,7 @@ $(function () { $(".settings-section" + sel + ", .settings-wrapper" + sel).addClass("show"); }); -}); +}; return exports; diff --git a/static/js/condense.js b/static/js/condense.js index 3209bc3ca9..85451a26ad 100644 --- a/static/js/condense.js +++ b/static/js/condense.js @@ -184,7 +184,7 @@ exports.condense_and_collapse = function (elems) { }); }; -$(function () { +exports.initialize = function () { $("#home").on("click", ".message_expander", function () { // Expanding a message can mean either uncollapsing or // uncondensing it. @@ -208,7 +208,7 @@ $(function () { current_msg_list.get(rows.id(row)).condensed = true; condense_row(row); }); -}); +}; return exports; }()); diff --git a/static/js/copy_and_paste.js b/static/js/copy_and_paste.js index 19a54487e7..a62fd9fb68 100644 --- a/static/js/copy_and_paste.js +++ b/static/js/copy_and_paste.js @@ -234,10 +234,10 @@ exports.paste_handler = function (event) { } }; -$(function () { +exports.initialize = function () { $(document).on('copy', copy_handler); $("#compose-textarea").bind('paste', exports.paste_handler); -}); +}; return exports; }()); diff --git a/static/js/echo.js b/static/js/echo.js index 4df5b0af85..32faeec731 100644 --- a/static/js/echo.js +++ b/static/js/echo.js @@ -279,7 +279,7 @@ function abort_message(message) { }); } -$(function () { +exports.initialize = function () { function on_failed_action(action, callback) { $("#main_div").on("click", "." + action + "-failed-message", function (e) { e.stopPropagation(); @@ -299,7 +299,7 @@ $(function () { on_failed_action('remove', abort_message); on_failed_action('refresh', resend_message); -}); +}; return exports; diff --git a/static/js/invite.js b/static/js/invite.js index 47c7b78343..6b482a0313 100644 --- a/static/js/invite.js +++ b/static/js/invite.js @@ -123,7 +123,7 @@ exports.launch = function () { }); }; -$(function () { +exports.initialize = function () { $(document).on('click', '.invite_check_all_button', function (e) { $('#streams_to_add :checkbox').prop('checked', true); e.preventDefault(); @@ -133,7 +133,7 @@ $(function () { $('#streams_to_add :checkbox').prop('checked', false); e.preventDefault(); }); -}); +}; return exports; diff --git a/static/js/lightbox.js b/static/js/lightbox.js index 232d2b14dc..4a41f705e4 100644 --- a/static/js/lightbox.js +++ b/static/js/lightbox.js @@ -209,7 +209,7 @@ exports.next = function () { }; // this is a block of events that are required for the lightbox to work. -$(function () { +exports.initialize = function () { $("#main_div").on("click", ".message_inline_image a", function (e) { var $img = $(this).find("img"); @@ -292,7 +292,7 @@ $(function () { overlays.close_overlay("lightbox"); } }); -}); +}; return exports; }()); diff --git a/static/js/message_viewport.js b/static/js/message_viewport.js index d7afb7c1d3..d14f4124da 100644 --- a/static/js/message_viewport.js +++ b/static/js/message_viewport.js @@ -391,7 +391,7 @@ exports.keep_pointer_in_view = function () { current_msg_list.select_id(rows.id(next_row), {from_scroll: true}); }; -$(function () { +exports.initialize = function () { jwindow = $(window); exports.message_pane = $(".app"); // This handler must be placed before all resize handlers in our application @@ -405,7 +405,7 @@ $(function () { $(document).on('compose_started compose_canceled compose_finished', function () { bottom_of_feed.reset(); }); -}); +}; return exports; }()); diff --git a/static/js/muting_ui.js b/static/js/muting_ui.js index 1650ea5025..603441e636 100644 --- a/static/js/muting_ui.js +++ b/static/js/muting_ui.js @@ -184,9 +184,9 @@ exports.toggle_mute = function (msg) { } }; -$(function () { +exports.initialize = function () { exports.update_muted_topics(page_params.muted_topics); -}); +}; return exports; }()); diff --git a/static/js/overlays.js b/static/js/overlays.js index e877dcd2eb..9a40554d3d 100644 --- a/static/js/overlays.js +++ b/static/js/overlays.js @@ -189,7 +189,7 @@ exports.open_settings = function () { }); }; -$(function () { +exports.initialize = function () { $("body").on("click", ".overlay, .overlay .exit", function (e) { var $target = $(e.target); @@ -209,7 +209,7 @@ $(function () { e.preventDefault(); e.stopPropagation(); }); -}); +}; return exports; diff --git a/static/js/stream_color.js b/static/js/stream_color.js index 422e4a8167..2baad5d45d 100644 --- a/static/js/stream_color.js +++ b/static/js/stream_color.js @@ -137,7 +137,7 @@ exports.sidebar_popover_colorpicker_options_full = { }; var lightness_threshold; -$(function () { +exports.initialize = function () { // sRGB color component for dark label text. // 0x33 to match the color #333333 set by Bootstrap. var label_color = 0x33; @@ -146,7 +146,7 @@ $(function () { // Compute midpoint lightness between that and white (100). lightness_threshold = (lightness + 100) / 2; -}); +}; // From a background color (in format "#fff" or "#ffffff") // pick a CSS class (or empty string) to determine the diff --git a/static/js/stream_edit.js b/static/js/stream_edit.js index 89a2fec09e..d481da18ca 100644 --- a/static/js/stream_edit.js +++ b/static/js/stream_edit.js @@ -471,7 +471,7 @@ exports.delete_stream = function (stream_id, alert_element, stream_row) { }); }; -$(function () { +exports.initialize = function () { $("#zfilt").on("click", ".stream_sub_unsub_button", function (e) { e.preventDefault(); e.stopPropagation(); @@ -658,7 +658,7 @@ $(function () { subs.rerender_subscriptions_settings(sub); }); -}); +}; return exports; diff --git a/static/js/subs.js b/static/js/subs.js index 3231eb44bb..428f159069 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -763,7 +763,7 @@ exports.sub_or_unsub = function (sub) { }; -$(function () { +exports.initialize = function () { stream_data.initialize_from_page_params(); stream_list.create_initial_sidebar_rows(); @@ -850,7 +850,7 @@ $(function () { sub_arrow.removeClass('icon-vector-chevron-up'); sub_arrow.addClass('icon-vector-chevron-down'); }); -}); +}; function focus_on_narrowed_stream() { var stream_name = narrow_state.stream(); diff --git a/static/js/tab_bar.js b/static/js/tab_bar.js index 494de8c311..af40746c2e 100644 --- a/static/js/tab_bar.js +++ b/static/js/tab_bar.js @@ -165,7 +165,7 @@ function build_tab_bar() { tab_bar.removeClass('notdisplayed'); } -$(function () { +exports.initialize = function () { $(document).on('narrow_activated.zulip', function () { build_tab_bar(); }); @@ -174,7 +174,7 @@ $(function () { }); build_tab_bar(); -}); +}; return exports; diff --git a/static/js/timerender.js b/static/js/timerender.js index 2b9418ca39..dd6c2f938c 100644 --- a/static/js/timerender.js +++ b/static/js/timerender.js @@ -105,9 +105,9 @@ var update_list = []; // The time at the beginning of the next day, when the timestamps are updated. // Represented as an XDate with hour, minute, second, millisecond 0. var next_update; -$(function () { +exports.initialize = function () { next_update = set_to_start_of_day(new XDate()).addDays(1); -}); +}; // time_above is an optional argument, to support dates that look like: // --- ▲ Yesterday ▲ ------ ▼ Today ▼ --- diff --git a/static/js/ui_init.js b/static/js/ui_init.js index 6b8ad67939..a019576419 100644 --- a/static/js/ui_init.js +++ b/static/js/ui_init.js @@ -48,7 +48,13 @@ function message_hover(message_row) { } } -$(function () { +function initialize_kitchen_sink_stuff() { + // TODO: + // This function is a historical dumping ground + // for lots of miscellaneous setup. Almost all of + // the code here can probably be moved to more + // specific-purpose modules like message_viewport.js. + var throttled_mousewheelhandler = $.throttle(50, function (e, delta) { // Most of the mouse wheel's work will be handled by the // scroll handler, but when we're at the top or bottom of the @@ -251,8 +257,25 @@ $(function () { if (feature_flags.full_width) { ui.switchToFullWidth(); } +} +$(function () { // initialize other stuff + muting_ui.initialize(); + message_viewport.initialize(); + initialize_kitchen_sink_stuff(); + echo.initialize(); + stream_color.initialize(); + stream_edit.initialize(); + subs.initialize(); + condense.initialize(); + lightbox.initialize(); + click_handlers.initialize(); + copy_and_paste.initialize(); + overlays.initialize(); + invite.initialize(); + timerender.initialize(); + tab_bar.initialize(); server_events.initialize(); people.initialize(); compose_pm_pill.initialize();