| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('bookmarks', function() { | 5 cr.define('bookmarks', function() { |
| 6 /** | 6 /** |
| 7 * Manages focus restoration for modal dialogs. After the final dialog in a | 7 * Manages focus restoration for modal dialogs. After the final dialog in a |
| 8 * stack is closed, restores focus to the element which was focused when the | 8 * stack is closed, restores focus to the element which was focused when the |
| 9 * first dialog was opened. | 9 * first dialog was opened. |
| 10 * @constructor | 10 * @constructor |
| 11 */ | 11 */ |
| 12 function DialogFocusManager() { | 12 function DialogFocusManager() { |
| 13 /** @private {HTMLElement} */ | 13 /** @private {HTMLElement} */ |
| 14 this.previousFocusElement_ = null; | 14 this.previousFocusElement_ = null; |
| 15 | 15 |
| 16 /** @private {boolean} */ | |
| 17 this.previousMouseFocus_ = false; | |
| 18 | |
| 19 /** @private {Set<HTMLDialogElement>} */ | 16 /** @private {Set<HTMLDialogElement>} */ |
| 20 this.dialogs_ = new Set(); | 17 this.dialogs_ = new Set(); |
| 21 } | 18 } |
| 22 | 19 |
| 23 DialogFocusManager.prototype = { | 20 DialogFocusManager.prototype = { |
| 24 /** | 21 /** |
| 25 * @param {HTMLDialogElement} dialog | 22 * @param {HTMLDialogElement} dialog |
| 26 * @param {function()=} showFn | 23 * @param {function()=} showFn |
| 27 */ | 24 */ |
| 28 showDialog: function(dialog, showFn) { | 25 showDialog: function(dialog, showFn) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 43 dialog.addEventListener('close', this.getCloseListener_(dialog)); | 40 dialog.addEventListener('close', this.getCloseListener_(dialog)); |
| 44 this.dialogs_.add(dialog); | 41 this.dialogs_.add(dialog); |
| 45 } | 42 } |
| 46 | 43 |
| 47 showFn(); | 44 showFn(); |
| 48 }, | 45 }, |
| 49 | 46 |
| 50 /** @private */ | 47 /** @private */ |
| 51 updatePreviousFocus_: function() { | 48 updatePreviousFocus_: function() { |
| 52 this.previousFocusElement_ = this.getFocusedElement_(); | 49 this.previousFocusElement_ = this.getFocusedElement_(); |
| 53 this.previousMouseFocus_ = bookmarks.MouseFocusBehavior.isMouseFocused( | |
| 54 this.previousFocusElement_); | |
| 55 }, | 50 }, |
| 56 | 51 |
| 57 /** | 52 /** |
| 58 * @return {HTMLElement} | 53 * @return {HTMLElement} |
| 59 * @private | 54 * @private |
| 60 */ | 55 */ |
| 61 getFocusedElement_: function() { | 56 getFocusedElement_: function() { |
| 62 var focus = document.activeElement; | 57 var focus = document.activeElement; |
| 63 while (focus.root && focus.root.activeElement) | 58 while (focus.root && focus.root.activeElement) |
| 64 focus = focus.root.activeElement; | 59 focus = focus.root.activeElement; |
| 65 | 60 |
| 66 return focus; | 61 return focus; |
| 67 }, | 62 }, |
| 68 | 63 |
| 69 /** | 64 /** |
| 70 * @param {HTMLDialogElement} dialog | 65 * @param {HTMLDialogElement} dialog |
| 71 * @return {function(Event)} | 66 * @return {function(Event)} |
| 72 * @private | 67 * @private |
| 73 */ | 68 */ |
| 74 getCloseListener_: function(dialog) { | 69 getCloseListener_: function(dialog) { |
| 75 var closeListener = function(e) { | 70 var closeListener = function(e) { |
| 76 // If the dialog is open, then it got reshown immediately and we | 71 // If the dialog is open, then it got reshown immediately and we |
| 77 // shouldn't clear it until it is closed again. | 72 // shouldn't clear it until it is closed again. |
| 78 if (dialog.open) | 73 if (dialog.open) |
| 79 return; | 74 return; |
| 80 | 75 |
| 81 assert(this.dialogs_.delete(dialog)); | 76 assert(this.dialogs_.delete(dialog)); |
| 82 // Focus the originally focused element if there are no more dialogs. | 77 // Focus the originally focused element if there are no more dialogs. |
| 83 if (!this.dialogs_.size) { | 78 if (!this.dialogs_.size) |
| 84 this.previousFocusElement_.focus(); | 79 this.previousFocusElement_.focus(); |
| 85 if (this.previousMouseFocus_) { | 80 |
| 86 bookmarks.MouseFocusBehavior.addMouseFocusClass( | |
| 87 this.previousFocusElement_); | |
| 88 } | |
| 89 } | |
| 90 dialog.removeEventListener('close', closeListener); | 81 dialog.removeEventListener('close', closeListener); |
| 91 }.bind(this); | 82 }.bind(this); |
| 92 | 83 |
| 93 return closeListener; | 84 return closeListener; |
| 94 }, | 85 }, |
| 95 }; | 86 }; |
| 96 | 87 |
| 97 cr.addSingletonGetter(DialogFocusManager); | 88 cr.addSingletonGetter(DialogFocusManager); |
| 98 | 89 |
| 99 return { | 90 return { |
| 100 DialogFocusManager: DialogFocusManager, | 91 DialogFocusManager: DialogFocusManager, |
| 101 }; | 92 }; |
| 102 }); | 93 }); |
| OLD | NEW |