diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js index 79e07b59a2..71e8db7586 100644 --- a/frontend_tests/node_tests/composebox_typeahead.js +++ b/frontend_tests/node_tests/composebox_typeahead.js @@ -828,7 +828,7 @@ run_test('initialize', () => { var event = { keyCode: 13, target: { - id: 'stream', + id: 'stream_message_recipient_stream', }, preventDefault: noop, }; @@ -872,7 +872,7 @@ run_test('initialize', () => { $('#compose-textarea').caret = noop; event.keyCode = 13; - event.target.id = 'subject'; + event.target.id = 'stream_message_recipient_topic'; $('form#send_message_form').keydown(event); event.target.id = 'compose-textarea'; page_params.enter_sends = false; @@ -909,7 +909,7 @@ run_test('initialize', () => { event = { keyCode: 13, target: { - id: 'stream', + id: 'stream_message_recipient_stream', }, preventDefault: noop, }; diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js index bad7c9c240..7c89c5529a 100644 --- a/static/js/composebox_typeahead.js +++ b/static/js/composebox_typeahead.js @@ -115,31 +115,42 @@ function handle_keydown(e) { var code = e.keyCode || e.which; if (code === 13 || code === 9 && !e.shiftKey) { // Enter key or tab key - if (e.target.id === "stream" || e.target.id === "subject" || e.target.id === "private_message_recipient") { + var target_sel; + + if (e.target.id) { + target_sel = '#' + e.target.id; + } + + var on_stream = target_sel === "#stream_message_recipient_stream"; + var on_topic = target_sel === "#stream_message_recipient_topic"; + var on_pm = target_sel === "#private_message_recipient"; + var on_compose = target_sel === '#compose-textarea'; + + if (on_stream || on_topic || on_pm) { // For enter, prevent the form from submitting // For tab, prevent the focus from changing again e.preventDefault(); } // In the compose_textarea box, preventDefault() for tab but not for enter - if (e.target.id === 'compose-textarea' && code !== 13) { + if (on_compose && code !== 13) { e.preventDefault(); } - if (e.target.id === "stream") { - nextFocus = "subject"; - } else if (e.target.id === "subject") { + if (on_stream) { + nextFocus = "#stream_message_recipient_topic"; + } else if (on_topic) { if (code === 13) { e.preventDefault(); } - nextFocus = 'compose-textarea'; - } else if (e.target.id === "private_message_recipient") { - nextFocus = 'compose-textarea'; - } else if (e.target.id === 'compose-textarea') { + nextFocus = '#compose-textarea'; + } else if (on_pm) { + nextFocus = '#compose-textarea'; + } else if (on_compose) { if (code === 13) { nextFocus = false; } else { - nextFocus = "compose-send-button"; + nextFocus = "#compose-send-button"; } } else { nextFocus = false; @@ -159,11 +170,11 @@ 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_util.focus_on(nextFocus); + $(nextFocus).focus(); nextFocus = false; } - if (e.target.id === 'compose-textarea' && code === 13) { + if (on_compose && code === 13) { var has_non_shift_modifier_key = e.ctrlKey || e.metaKey || e.altKey; var has_modifier_key = e.shiftKey || has_non_shift_modifier_key; var this_enter_sends; @@ -230,7 +241,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_util.focus_on(nextFocus); + $(nextFocus).focus(); nextFocus = false; } } diff --git a/static/js/ui_util.js b/static/js/ui_util.js index 69c079750e..84e8e7b00b 100644 --- a/static/js/ui_util.js +++ b/static/js/ui_util.js @@ -9,16 +9,6 @@ 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(); -}; - // http://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser exports.place_caret_at_end = function (el) { el.focus();