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