From d026344b377413d714c3beb529e46d8769a8b825 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 18 Mar 2017 13:35:35 -0700 Subject: [PATCH] Extract js/ui_util.js. --- .eslintrc.json | 1 + static/js/click_handlers.js | 8 ++++---- static/js/compose.js | 2 +- static/js/composebox_typeahead.js | 6 +++--- static/js/hashchange.js | 14 +++++++------- static/js/message_edit.js | 4 ++-- static/js/narrow.js | 2 +- static/js/pointer.js | 2 +- static/js/search.js | 2 +- static/js/stream_list.js | 4 ++-- static/js/subs.js | 2 +- static/js/topic_list.js | 2 +- static/js/tutorial.js | 2 +- static/js/ui.js | 19 ------------------ static/js/ui_util.js | 32 +++++++++++++++++++++++++++++++ zproject/settings.py | 1 + 16 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 static/js/ui_util.js diff --git a/.eslintrc.json b/.eslintrc.json index ed13a3f2bf..912bbfa36a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -27,6 +27,7 @@ "ui": false, "ui_state": false, "ui_report": false, + "ui_util": false, "lightbox": false, "stream_color": false, "people": false, diff --git a/static/js/click_handlers.js b/static/js/click_handlers.js index 5ba3f7c92b..a976937619 100644 --- a/static/js/click_handlers.js +++ b/static/js/click_handlers.js @@ -102,7 +102,7 @@ $(function () { $('body').on('click', '.notification', function () { var payload = $(this).data("narrow"); - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); narrow.activate(payload.raw_operators, payload.opts_notif); }); @@ -153,7 +153,7 @@ $(function () { }); $("body").on("click", "a", function () { if (document.activeElement === this) { - ui.blur_active_element(); + ui_util.blur_active_element(); } }); @@ -263,7 +263,7 @@ $(function () { // Capture both the left-sidebar Home click and the tab breadcrumb Home $(document).on('click', ".home-link[data-name='home']", function (e) { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); narrow.deactivate(); // We need to maybe scroll to the selected message // once we have the proper viewport set up @@ -273,7 +273,7 @@ $(function () { $(".brand").on('click', function (e) { if (ui_state.home_tab_obscured()) { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); } else { narrow.restore_home_state(); } diff --git a/static/js/compose.js b/static/js/compose.js index 845ffe5d5a..ad26ab7cd6 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -262,7 +262,7 @@ exports.start = function (msg_type, opts) { compose.message_content(opts.content); } - ui.change_tab_to("#home"); + ui_util.change_tab_to("#home"); is_composing_message = msg_type; diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js index 530bb3c6d0..95149af1f9 100644 --- a/static/js/composebox_typeahead.js +++ b/static/js/composebox_typeahead.js @@ -122,7 +122,7 @@ function handle_keydown(e) { // takes the typeaheads a little time to open after the user finishes typing, which // can lead to the focus moving without the autocomplete having a chance to happen. if (nextFocus) { - ui.focus_on(nextFocus); + ui_util.focus_on(nextFocus); nextFocus = false; } @@ -149,7 +149,7 @@ function handle_keyup(e) { var code = e.keyCode || e.which; if (code === 13 || (code === 9 && !e.shiftKey)) { // Enter key or tab key if (nextFocus) { - ui.focus_on(nextFocus); + ui_util.focus_on(nextFocus); nextFocus = false; } } @@ -480,7 +480,7 @@ exports.initialize = function () { previous_recipients += ", "; } if (event && event.type === 'click') { - ui.focus_on('private_message_recipient'); + ui_util.focus_on('private_message_recipient'); } return previous_recipients + item.email + ", "; }, diff --git a/static/js/hashchange.js b/static/js/hashchange.js index fd9db6055a..6371dc0f5a 100644 --- a/static/js/hashchange.js +++ b/static/js/hashchange.js @@ -91,7 +91,7 @@ exports.parse_narrow = function (hash) { }; function activate_home_tab() { - ui.change_tab_to("#home"); + ui_util.change_tab_to("#home"); narrow.deactivate(); floating_recipient_bar.update(); } @@ -120,7 +120,7 @@ function do_hashchange(from_reload) { var hash = window.location.hash.split("/"); switch (hash[0]) { case "#narrow": - ui.change_tab_to("#home"); + ui_util.change_tab_to("#home"); var operators = exports.parse_narrow(hash); if (operators === undefined) { // If the narrow URL didn't parse, clear @@ -146,16 +146,16 @@ function do_hashchange(from_reload) { activate_home_tab(); break; case "#streams": - ui.change_tab_to("#streams"); + ui_util.change_tab_to("#streams"); break; case "#drafts": - ui.change_tab_to("#drafts"); + ui_util.change_tab_to("#drafts"); break; case "#administration": - ui.change_tab_to("#administration"); + ui_util.change_tab_to("#administration"); break; case "#settings": - ui.change_tab_to("#settings"); + ui_util.change_tab_to("#settings"); break; } return false; @@ -295,7 +295,7 @@ exports.close_modals = function () { exports.exit_modal = function (callback) { if (should_ignore(window.location.hash)) { - ui.blur_active_element(); + ui_util.blur_active_element(); ignore.flag = true; window.location.hash = ignore.prev || "#"; if (typeof callback === "function") { diff --git a/static/js/message_edit.js b/static/js/message_edit.js index d3b71b4064..9869c09e54 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -374,9 +374,9 @@ exports.edit_last_sent_message = function () { message_edit.start(msg_row, function () { var editability_type = message_edit.get_editability(msg, 5); if (editability_type === message_edit.editability_types.TOPIC_ONLY) { - ui.focus_on('message_edit_topic'); + ui_util.focus_on('message_edit_topic'); } else { - ui.focus_on('message_edit_content'); + ui_util.focus_on('message_edit_content'); } }); } diff --git a/static/js/narrow.js b/static/js/narrow.js index 51318039a6..d8f180a712 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -275,7 +275,7 @@ exports.activate = function (raw_operators, opts) { $("#zfilt").addClass("focused_table"); $("#zhome").removeClass("focused_table"); - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); message_list.narrowed = msg_list; current_msg_list = message_list.narrowed; diff --git a/static/js/pointer.js b/static/js/pointer.js index d3c9d79f87..9774b9aaba 100644 --- a/static/js/pointer.js +++ b/static/js/pointer.js @@ -64,7 +64,7 @@ exports.fast_forward_pointer = function () { unread_ops.mark_all_as_read(function () { pointer.furthest_read = data.max_message_id; unconditionally_send_pointer_update().then(function () { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); reload.initiate({immediate: true, save_pointer: false, save_narrow: false, diff --git a/static/js/search.js b/static/js/search.js index 4c3075cc87..54d0e20339 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -4,7 +4,7 @@ var exports = {}; function narrow_or_search_for_term(search_string) { var search_query_box = $("#search_query"); - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); var operators = Filter.parse(search_string); narrow.activate(operators, {trigger: 'search'}); diff --git a/static/js/stream_list.js b/static/js/stream_list.js index 261aa5af71..8e41c77fdb 100644 --- a/static/js/stream_list.js +++ b/static/js/stream_list.js @@ -474,7 +474,7 @@ $(function () { return; } if (ui_state.home_tab_obscured()) { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); } var stream = $(e.target).parents('li').attr('data-name'); popovers.hide_all(); @@ -535,7 +535,7 @@ function maybe_select_stream(e) { if (topStream !== undefined) { // undefined if there are no results if (ui_state.home_tab_obscured()) { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); } exports.clear_and_hide_search(); narrow.by('stream', topStream, {select_first_unread: true, trigger: 'sidebar enter key'}); diff --git a/static/js/subs.js b/static/js/subs.js index ece72dd553..1f4ca43f70 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -1394,7 +1394,7 @@ function focus_on_narrowed_stream() { exports.show_and_focus_on_narrow = function () { $(document).one('subs_page_loaded.zulip', focus_on_narrowed_stream); - ui.change_tab_to("#streams"); + ui_util.change_tab_to("#streams"); }; // *Synchronously* check if a stream exists. diff --git a/static/js/topic_list.js b/static/js/topic_list.js index 5e460281d6..1a70517f42 100644 --- a/static/js/topic_list.js +++ b/static/js/topic_list.js @@ -211,7 +211,7 @@ exports.set_click_handlers = function (callbacks) { // In a more componentized world, we would delegate some // of this stuff back up to our parents. if (ui_state.home_tab_obscured()) { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); } var stream = $(e.target).parents('ul').attr('data-stream'); diff --git a/static/js/tutorial.js b/static/js/tutorial.js index 477f433002..1316433ad7 100644 --- a/static/js/tutorial.js +++ b/static/js/tutorial.js @@ -505,7 +505,7 @@ function welcome() { exports.start = function () { if (ui_state.home_tab_obscured()) { - ui.change_tab_to('#home'); + ui_util.change_tab_to('#home'); } narrow.deactivate(); diff --git a/static/js/ui.js b/static/js/ui.js index 02ea0a9b49..b42831dfba 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -19,25 +19,6 @@ exports.home_tab_obscured = function () { return false; }; -exports.change_tab_to = function (tabname) { - $('#gear-menu a[href="' + tabname + '"]').tab('show'); -}; - -exports.focus_on = function (field_id) { - // Call after autocompleting on a field, to advance the focus to - // the next input field. - - // Bootstrap's typeahead does not expose a callback for when an - // autocomplete selection has been made, so we have to do this - // manually. - $("#" + field_id).focus(); -}; - -exports.blur_active_element = function () { - // this blurs anything that may perhaps be actively focused on. - document.activeElement.blur(); -}; - function amount_to_paginate() { // Some day we might have separate versions of this function // for Page Up vs. Page Down, but for now it's the same diff --git a/static/js/ui_util.js b/static/js/ui_util.js new file mode 100644 index 0000000000..3d5cfdec28 --- /dev/null +++ b/static/js/ui_util.js @@ -0,0 +1,32 @@ +var ui_util = (function () { + +var exports = {}; + +// Add functions to this that have no non-trivial +// dependencies other than jQuery. + +exports.change_tab_to = function (tabname) { + $('#gear-menu a[href="' + tabname + '"]').tab('show'); +}; + +exports.focus_on = function (field_id) { + // Call after autocompleting on a field, to advance the focus to + // the next input field. + + // Bootstrap's typeahead does not expose a callback for when an + // autocomplete selection has been made, so we have to do this + // manually. + $("#" + field_id).focus(); +}; + +exports.blur_active_element = function () { + // this blurs anything that may perhaps be actively focused on. + document.activeElement.blur(); +}; + +return exports; +}()); + +if (typeof module !== 'undefined') { + module.exports = ui_util; +} diff --git a/zproject/settings.py b/zproject/settings.py index c72e4cab6c..9f06d18b50 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -852,6 +852,7 @@ JS_SPECS = { 'js/floating_recipient_bar.js', 'js/lightbox.js', 'js/ui.js', + 'js/ui_util.js', 'js/pointer.js', 'js/click_handlers.js', 'js/scroll_bar.js',