| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('bookmarks', function() { |
| 6 /** |
| 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 |
| 9 * first dialog was opened. |
| 10 * @constructor |
| 11 */ |
| 12 function DialogFocusManager() { |
| 13 /** @private {HTMLElement} */ |
| 14 this.previousFocusElement_ = null; |
| 15 |
| 16 /** @private {boolean} */ |
| 17 this.previousMouseFocus_ = false; |
| 18 |
| 19 /** @private {Set<HTMLDialogElement>} */ |
| 20 this.dialogs_ = new Set(); |
| 21 } |
| 22 |
| 23 DialogFocusManager.prototype = { |
| 24 /** |
| 25 * @param {HTMLDialogElement} dialog |
| 26 * @param {function()=} showFn |
| 27 */ |
| 28 showDialog: function(dialog, showFn) { |
| 29 if (!showFn) { |
| 30 showFn = function() { |
| 31 dialog.showModal(); |
| 32 }; |
| 33 } |
| 34 |
| 35 // Update the focus if there are no open dialogs or if this is the only |
| 36 // dialog and it's getting reshown. |
| 37 if (!this.dialogs_.size || |
| 38 (this.dialogs_.has(dialog) && this.dialogs_.size == 1)) { |
| 39 this.updatePreviousFocus_(); |
| 40 } |
| 41 |
| 42 if (!this.dialogs_.has(dialog)) { |
| 43 dialog.addEventListener('close', this.getCloseListener_(dialog)); |
| 44 this.dialogs_.add(dialog); |
| 45 } |
| 46 |
| 47 showFn(); |
| 48 }, |
| 49 |
| 50 /** @private */ |
| 51 updatePreviousFocus_: function() { |
| 52 this.previousFocusElement_ = this.getFocusedElement_(); |
| 53 this.previousMouseFocus_ = bookmarks.MouseFocusBehavior.isMouseFocused( |
| 54 this.previousFocusElement_); |
| 55 }, |
| 56 |
| 57 /** |
| 58 * @return {HTMLElement} |
| 59 * @private |
| 60 */ |
| 61 getFocusedElement_: function() { |
| 62 var focus = document.activeElement; |
| 63 while (focus.root && focus.root.activeElement) |
| 64 focus = focus.root.activeElement; |
| 65 |
| 66 return focus; |
| 67 }, |
| 68 |
| 69 /** |
| 70 * @param {HTMLDialogElement} dialog |
| 71 * @return {function(Event)} |
| 72 * @private |
| 73 */ |
| 74 getCloseListener_: function(dialog) { |
| 75 var closeListener = function(e) { |
| 76 // If the dialog is open, then it got reshown immediately and we |
| 77 // shouldn't clear it until it is closed again. |
| 78 if (dialog.open) |
| 79 return; |
| 80 |
| 81 assert(this.dialogs_.delete(dialog)); |
| 82 // Focus the originally focused element if there are no more dialogs. |
| 83 if (!this.dialogs_.size) { |
| 84 this.previousFocusElement_.focus(); |
| 85 if (this.previousMouseFocus_) { |
| 86 bookmarks.MouseFocusBehavior.addMouseFocusClass( |
| 87 this.previousFocusElement_); |
| 88 } |
| 89 } |
| 90 dialog.removeEventListener('close', closeListener); |
| 91 }.bind(this); |
| 92 |
| 93 return closeListener; |
| 94 }, |
| 95 }; |
| 96 |
| 97 cr.addSingletonGetter(DialogFocusManager); |
| 98 |
| 99 return { |
| 100 DialogFocusManager: DialogFocusManager, |
| 101 }; |
| 102 }); |
| OLD | NEW |