Extract js/ui_util.js.

This commit is contained in:
Steve Howell 2017-03-18 13:35:35 -07:00 committed by Tim Abbott
parent 099f401b9b
commit d026344b37
16 changed files with 59 additions and 44 deletions

View File

@ -27,6 +27,7 @@
"ui": false, "ui": false,
"ui_state": false, "ui_state": false,
"ui_report": false, "ui_report": false,
"ui_util": false,
"lightbox": false, "lightbox": false,
"stream_color": false, "stream_color": false,
"people": false, "people": false,

View File

@ -102,7 +102,7 @@ $(function () {
$('body').on('click', '.notification', function () { $('body').on('click', '.notification', function () {
var payload = $(this).data("narrow"); var payload = $(this).data("narrow");
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
narrow.activate(payload.raw_operators, payload.opts_notif); narrow.activate(payload.raw_operators, payload.opts_notif);
}); });
@ -153,7 +153,7 @@ $(function () {
}); });
$("body").on("click", "a", function () { $("body").on("click", "a", function () {
if (document.activeElement === this) { 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 // Capture both the left-sidebar Home click and the tab breadcrumb Home
$(document).on('click', ".home-link[data-name='home']", function (e) { $(document).on('click', ".home-link[data-name='home']", function (e) {
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
narrow.deactivate(); narrow.deactivate();
// We need to maybe scroll to the selected message // We need to maybe scroll to the selected message
// once we have the proper viewport set up // once we have the proper viewport set up
@ -273,7 +273,7 @@ $(function () {
$(".brand").on('click', function (e) { $(".brand").on('click', function (e) {
if (ui_state.home_tab_obscured()) { if (ui_state.home_tab_obscured()) {
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
} else { } else {
narrow.restore_home_state(); narrow.restore_home_state();
} }

View File

@ -262,7 +262,7 @@ exports.start = function (msg_type, opts) {
compose.message_content(opts.content); compose.message_content(opts.content);
} }
ui.change_tab_to("#home"); ui_util.change_tab_to("#home");
is_composing_message = msg_type; is_composing_message = msg_type;

View File

@ -122,7 +122,7 @@ function handle_keydown(e) {
// takes the typeaheads a little time to open after the user finishes typing, which // 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. // can lead to the focus moving without the autocomplete having a chance to happen.
if (nextFocus) { if (nextFocus) {
ui.focus_on(nextFocus); ui_util.focus_on(nextFocus);
nextFocus = false; nextFocus = false;
} }
@ -149,7 +149,7 @@ function handle_keyup(e) {
var code = e.keyCode || e.which; var code = e.keyCode || e.which;
if (code === 13 || (code === 9 && !e.shiftKey)) { // Enter key or tab key if (code === 13 || (code === 9 && !e.shiftKey)) { // Enter key or tab key
if (nextFocus) { if (nextFocus) {
ui.focus_on(nextFocus); ui_util.focus_on(nextFocus);
nextFocus = false; nextFocus = false;
} }
} }
@ -480,7 +480,7 @@ exports.initialize = function () {
previous_recipients += ", "; previous_recipients += ", ";
} }
if (event && event.type === 'click') { if (event && event.type === 'click') {
ui.focus_on('private_message_recipient'); ui_util.focus_on('private_message_recipient');
} }
return previous_recipients + item.email + ", "; return previous_recipients + item.email + ", ";
}, },

View File

@ -91,7 +91,7 @@ exports.parse_narrow = function (hash) {
}; };
function activate_home_tab() { function activate_home_tab() {
ui.change_tab_to("#home"); ui_util.change_tab_to("#home");
narrow.deactivate(); narrow.deactivate();
floating_recipient_bar.update(); floating_recipient_bar.update();
} }
@ -120,7 +120,7 @@ function do_hashchange(from_reload) {
var hash = window.location.hash.split("/"); var hash = window.location.hash.split("/");
switch (hash[0]) { switch (hash[0]) {
case "#narrow": case "#narrow":
ui.change_tab_to("#home"); ui_util.change_tab_to("#home");
var operators = exports.parse_narrow(hash); var operators = exports.parse_narrow(hash);
if (operators === undefined) { if (operators === undefined) {
// If the narrow URL didn't parse, clear // If the narrow URL didn't parse, clear
@ -146,16 +146,16 @@ function do_hashchange(from_reload) {
activate_home_tab(); activate_home_tab();
break; break;
case "#streams": case "#streams":
ui.change_tab_to("#streams"); ui_util.change_tab_to("#streams");
break; break;
case "#drafts": case "#drafts":
ui.change_tab_to("#drafts"); ui_util.change_tab_to("#drafts");
break; break;
case "#administration": case "#administration":
ui.change_tab_to("#administration"); ui_util.change_tab_to("#administration");
break; break;
case "#settings": case "#settings":
ui.change_tab_to("#settings"); ui_util.change_tab_to("#settings");
break; break;
} }
return false; return false;
@ -295,7 +295,7 @@ exports.close_modals = function () {
exports.exit_modal = function (callback) { exports.exit_modal = function (callback) {
if (should_ignore(window.location.hash)) { if (should_ignore(window.location.hash)) {
ui.blur_active_element(); ui_util.blur_active_element();
ignore.flag = true; ignore.flag = true;
window.location.hash = ignore.prev || "#"; window.location.hash = ignore.prev || "#";
if (typeof callback === "function") { if (typeof callback === "function") {

View File

@ -374,9 +374,9 @@ exports.edit_last_sent_message = function () {
message_edit.start(msg_row, function () { message_edit.start(msg_row, function () {
var editability_type = message_edit.get_editability(msg, 5); var editability_type = message_edit.get_editability(msg, 5);
if (editability_type === message_edit.editability_types.TOPIC_ONLY) { if (editability_type === message_edit.editability_types.TOPIC_ONLY) {
ui.focus_on('message_edit_topic'); ui_util.focus_on('message_edit_topic');
} else { } else {
ui.focus_on('message_edit_content'); ui_util.focus_on('message_edit_content');
} }
}); });
} }

View File

@ -275,7 +275,7 @@ exports.activate = function (raw_operators, opts) {
$("#zfilt").addClass("focused_table"); $("#zfilt").addClass("focused_table");
$("#zhome").removeClass("focused_table"); $("#zhome").removeClass("focused_table");
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
message_list.narrowed = msg_list; message_list.narrowed = msg_list;
current_msg_list = message_list.narrowed; current_msg_list = message_list.narrowed;

View File

@ -64,7 +64,7 @@ exports.fast_forward_pointer = function () {
unread_ops.mark_all_as_read(function () { unread_ops.mark_all_as_read(function () {
pointer.furthest_read = data.max_message_id; pointer.furthest_read = data.max_message_id;
unconditionally_send_pointer_update().then(function () { unconditionally_send_pointer_update().then(function () {
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
reload.initiate({immediate: true, reload.initiate({immediate: true,
save_pointer: false, save_pointer: false,
save_narrow: false, save_narrow: false,

View File

@ -4,7 +4,7 @@ var exports = {};
function narrow_or_search_for_term(search_string) { function narrow_or_search_for_term(search_string) {
var search_query_box = $("#search_query"); var search_query_box = $("#search_query");
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
var operators = Filter.parse(search_string); var operators = Filter.parse(search_string);
narrow.activate(operators, {trigger: 'search'}); narrow.activate(operators, {trigger: 'search'});

View File

@ -474,7 +474,7 @@ $(function () {
return; return;
} }
if (ui_state.home_tab_obscured()) { 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'); var stream = $(e.target).parents('li').attr('data-name');
popovers.hide_all(); popovers.hide_all();
@ -535,7 +535,7 @@ function maybe_select_stream(e) {
if (topStream !== undefined) { if (topStream !== undefined) {
// undefined if there are no results // undefined if there are no results
if (ui_state.home_tab_obscured()) { if (ui_state.home_tab_obscured()) {
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
} }
exports.clear_and_hide_search(); exports.clear_and_hide_search();
narrow.by('stream', topStream, {select_first_unread: true, trigger: 'sidebar enter key'}); narrow.by('stream', topStream, {select_first_unread: true, trigger: 'sidebar enter key'});

View File

@ -1394,7 +1394,7 @@ function focus_on_narrowed_stream() {
exports.show_and_focus_on_narrow = function () { exports.show_and_focus_on_narrow = function () {
$(document).one('subs_page_loaded.zulip', focus_on_narrowed_stream); $(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. // *Synchronously* check if a stream exists.

View File

@ -211,7 +211,7 @@ exports.set_click_handlers = function (callbacks) {
// In a more componentized world, we would delegate some // In a more componentized world, we would delegate some
// of this stuff back up to our parents. // of this stuff back up to our parents.
if (ui_state.home_tab_obscured()) { 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'); var stream = $(e.target).parents('ul').attr('data-stream');

View File

@ -505,7 +505,7 @@ function welcome() {
exports.start = function () { exports.start = function () {
if (ui_state.home_tab_obscured()) { if (ui_state.home_tab_obscured()) {
ui.change_tab_to('#home'); ui_util.change_tab_to('#home');
} }
narrow.deactivate(); narrow.deactivate();

View File

@ -19,25 +19,6 @@ exports.home_tab_obscured = function () {
return false; 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() { function amount_to_paginate() {
// Some day we might have separate versions of this function // Some day we might have separate versions of this function
// for Page Up vs. Page Down, but for now it's the same // for Page Up vs. Page Down, but for now it's the same

32
static/js/ui_util.js Normal file
View File

@ -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;
}

View File

@ -852,6 +852,7 @@ JS_SPECS = {
'js/floating_recipient_bar.js', 'js/floating_recipient_bar.js',
'js/lightbox.js', 'js/lightbox.js',
'js/ui.js', 'js/ui.js',
'js/ui_util.js',
'js/pointer.js', 'js/pointer.js',
'js/click_handlers.js', 'js/click_handlers.js',
'js/scroll_bar.js', 'js/scroll_bar.js',