| 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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 if (!this.dialogs_.has(dialog)) { | 39 if (!this.dialogs_.has(dialog)) { |
| 40 dialog.addEventListener('close', this.getCloseListener_(dialog)); | 40 dialog.addEventListener('close', this.getCloseListener_(dialog)); |
| 41 this.dialogs_.add(dialog); | 41 this.dialogs_.add(dialog); |
| 42 } | 42 } |
| 43 | 43 |
| 44 showFn(); | 44 showFn(); |
| 45 }, | 45 }, |
| 46 | 46 |
| 47 /** |
| 48 * Clears the stored focus element, so that focus does not restore when all |
| 49 * dialogs are closed. |
| 50 */ |
| 51 clearFocus: function() { |
| 52 this.previousFocusElement_ = null; |
| 53 }, |
| 54 |
| 47 /** @private */ | 55 /** @private */ |
| 48 updatePreviousFocus_: function() { | 56 updatePreviousFocus_: function() { |
| 49 this.previousFocusElement_ = this.getFocusedElement_(); | 57 this.previousFocusElement_ = this.getFocusedElement_(); |
| 50 }, | 58 }, |
| 51 | 59 |
| 52 /** | 60 /** |
| 53 * @return {HTMLElement} | 61 * @return {HTMLElement} |
| 54 * @private | 62 * @private |
| 55 */ | 63 */ |
| 56 getFocusedElement_: function() { | 64 getFocusedElement_: function() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 */ | 76 */ |
| 69 getCloseListener_: function(dialog) { | 77 getCloseListener_: function(dialog) { |
| 70 var closeListener = function(e) { | 78 var closeListener = function(e) { |
| 71 // If the dialog is open, then it got reshown immediately and we | 79 // If the dialog is open, then it got reshown immediately and we |
| 72 // shouldn't clear it until it is closed again. | 80 // shouldn't clear it until it is closed again. |
| 73 if (dialog.open) | 81 if (dialog.open) |
| 74 return; | 82 return; |
| 75 | 83 |
| 76 assert(this.dialogs_.delete(dialog)); | 84 assert(this.dialogs_.delete(dialog)); |
| 77 // Focus the originally focused element if there are no more dialogs. | 85 // Focus the originally focused element if there are no more dialogs. |
| 78 if (!this.dialogs_.size) | 86 if (!this.dialogs_.size && this.previousFocusElement_) |
| 79 this.previousFocusElement_.focus(); | 87 this.previousFocusElement_.focus(); |
| 80 | 88 |
| 81 dialog.removeEventListener('close', closeListener); | 89 dialog.removeEventListener('close', closeListener); |
| 82 }.bind(this); | 90 }.bind(this); |
| 83 | 91 |
| 84 return closeListener; | 92 return closeListener; |
| 85 }, | 93 }, |
| 86 }; | 94 }; |
| 87 | 95 |
| 88 cr.addSingletonGetter(DialogFocusManager); | 96 cr.addSingletonGetter(DialogFocusManager); |
| 89 | 97 |
| 90 return { | 98 return { |
| 91 DialogFocusManager: DialogFocusManager, | 99 DialogFocusManager: DialogFocusManager, |
| 92 }; | 100 }; |
| 93 }); | 101 }); |
| OLD | NEW |