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