diff --git a/ide/static/ide/js/fuzzyprompt.js b/ide/static/ide/js/fuzzyprompt.js index 6b2cf063..d16f835d 100644 --- a/ide/static/ide/js/fuzzyprompt.js +++ b/ide/static/ide/js/fuzzyprompt.js @@ -8,9 +8,10 @@ CloudPebble.FuzzyPrompt = (function() { var selected_id = null; var default_item; var selection_was_made; + var focus_pane; var opened = false; var prompt_kind = null; - + var focus_pane_selector = '#main-pane' // While manual is false, always highlight the first item var manual = false; @@ -184,6 +185,8 @@ CloudPebble.FuzzyPrompt = (function() { selection_was_made = false; opened = true; prompt_kind = kind; + focus_pane = $(focus_pane_selector).get()[0]; + // Build up the list of files to search through var id = 0; _.each(sources, function(source) { @@ -253,6 +256,11 @@ CloudPebble.FuzzyPrompt = (function() { if (opened) { opened = false; prompt.modal('hide'); + // If we switched page, never refocus + if (focus_pane != $(focus_pane_selector).get()[0]) { + refocus = false; + } + // Otherwise, if we want to refocus, do so if (refocus) { setTimeout(function () { $(previously_active).focus(); diff --git a/ide/static/ide/js/sidebar.js b/ide/static/ide/js/sidebar.js index aa9432ab..dd963959 100644 --- a/ide/static/ide/js/sidebar.js +++ b/ide/static/ide/js/sidebar.js @@ -17,6 +17,7 @@ CloudPebble.Sidebar = (function() { if (list_entry) { list_entry.removeClass('active'); } + suspended_panes[pane_id] = pane; pane.detach(); // Create a new empty one. @@ -38,7 +39,10 @@ CloudPebble.Sidebar = (function() { }; var refocus_pane = function(pane) { - pane.find('*[autofocus]').first().focus(); + setTimeout(function() { + var previous_focus = pane.data('previous-focus'); + (previous_focus || pane.find('*[autofocus]').first().focus()).focus(); + }, 50); }; var restore_suspended_pane = function(id) { @@ -57,7 +61,7 @@ CloudPebble.Sidebar = (function() { pane.data('pane-restore-function')(); } - refocus_pane(pane); + refocus_pane($('#main-pane')); return true; } @@ -66,7 +70,7 @@ CloudPebble.Sidebar = (function() { var set_main_pane = function(pane, options) { $('#main-pane').append(pane).data('pane-id', options.id); - refocus_pane(pane); + refocus_pane($('#main-pane')); if (options.onRestore) { $('#main-pane').data('pane-restore-function', options.onRestore); } @@ -176,6 +180,10 @@ CloudPebble.Sidebar = (function() { $('#sidebar-pane-github > a').click(CloudPebble.GitHub.Show); $('#sidebar-pane-timeline > a').click(CloudPebble.Timeline.show); create_initial_sections(CloudPebble.ProjectInfo.type); + + $('#pane-parent').on('focusin', '#main-pane *', _.debounce(function(e) { + $('#main-pane').data('previous-focus', $(e.target)); + }, 1)); }, SetPopover: function(pane_id, title, content) { $('#sidebar-pane-' + pane_id).find('a').popover('destroy').popover({