1 // Copyright 2013 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.
5 // Helper base class for all help pages and overlays, which controls
6 // overlays, focus and scroll. This class is partially based on
7 // OptionsPage, but simpler and contains only overlay- and focus-
8 // handling logic. As in OptionsPage each page can be an overlay itself,
9 // but each page contains its own list of registered overlays which can be
12 // TODO (ygorshenin@): crbug.com/313244.
13 cr
.define('help', function() {
14 function HelpBasePage() {
17 HelpBasePage
.prototype = {
18 __proto__
: HTMLDivElement
.prototype,
21 * name of the page, should be the same as an id of the
22 * corresponding HTMLDivElement.
27 * HTML counterpart of this page.
32 * True if current page is overlay.
37 * HTMLElement that was last focused when this page was the
40 lastFocusedElement
: null,
43 * State of vertical scrollbars when this page was the topmost.
48 * Dictionary of registered overlays.
50 registeredOverlays
: {},
53 * Stores currently focused element.
56 storeLastFocusedElement_: function() {
57 if (this.pageDiv
.contains(document
.activeElement
))
58 this.lastFocusedElement
= document
.activeElement
;
62 * Restores focus to the last focused element on this page.
65 restoreLastFocusedElement_: function() {
66 if (this.lastFocusedElement
)
67 this.lastFocusedElement
.focus();
73 * Shows or hides current page iff it's an overlay.
74 * @param {boolean} visible True if overlay should be displayed.
77 setOverlayVisible_: function(visible
) {
78 assert(this.isOverlay
);
79 this.container
.hidden
= !visible
;
81 this.pageDiv
.classList
.add('showing');
83 this.pageDiv
.classList
.remove('showing');
87 * @return {HTMLDivElement} visible non-overlay page or
88 * null, if there are no visible non-overlay pages.
91 getVisibleNonOverlay_: function() {
92 if (this.isOverlay
|| !this.visible
)
98 * @return {HTMLDivElement} Visible overlay page, or null,
99 * if there are no visible overlay pages.
102 getVisibleOverlay_: function() {
103 for (var name
in this.registeredOverlays
) {
104 var overlay
= this.registeredOverlays
[name
];
112 * Freezes current page, makes it impossible to scroll it.
113 * @param {boolean} freeze True if the page should be frozen.
116 freeze_: function(freeze
) {
117 var scrollLeft
= scrollLeftForDocument(document
);
119 this.lastScrollTop
= scrollTopForDocument(document
);
120 document
.body
.style
.overflow
= 'hidden';
121 window
.scroll(scrollLeft
, 0);
123 document
.body
.style
.overflow
= 'auto';
124 window
.scroll(scrollLeft
, this.lastScrollTop
);
129 * Initializes current page.
130 * @param {string} name Name of the current page.
132 initialize: function(name
) {
134 this.pageDiv
= $(name
);
138 * Called before overlay is displayed.
140 onBeforeShow: function() {
144 * @return {HTMLDivElement} Topmost visible page, or null, if
145 * there are no visible pages.
147 getTopmostVisiblePage: function() {
148 return this.getVisibleOverlay_() || this.getVisibleNonOverlay_();
153 * @param {HelpBasePage} overlay Overlay that should be registered.
155 registerOverlay: function(overlay
) {
156 this.registeredOverlays
[overlay
.name
] = overlay
;
157 overlay
.isOverlay
= true;
161 * Shows or hides current page.
162 * @param {boolean} visible True if current page should be displayed.
164 set visible(visible
) {
165 if (this.visible
== visible
)
169 this.storeLastFocusedElement_();
172 this.setOverlayVisible_(visible
);
174 this.pageDiv
.hidden
= !visible
;
177 this.restoreLastFocusedElement_();
184 * Returns true if current page is visible.
185 * @return {boolean} True if current page is visible.
189 return this.pageDiv
.classList
.contains('showing');
190 return !this.pageDiv
.hidden
;
194 * This method returns overlay container, it should be called only
196 * @return {HTMLDivElement} overlay container.
199 assert(this.isOverlay
);
200 return this.pageDiv
.parentNode
;
204 * Shows registered overlay.
205 * @param {string} name Name of registered overlay to show.
207 showOverlay: function(name
) {
208 var currentPage
= this.getTopmostVisiblePage();
209 currentPage
.storeLastFocusedElement_();
210 currentPage
.freeze_(true);
212 var overlay
= this.registeredOverlays
[name
];
215 overlay
.visible
= true;
219 * Hides currently displayed overlay.
221 closeOverlay: function() {
222 var overlay
= this.getVisibleOverlay_();
225 overlay
.visible
= false;
227 var currentPage
= this.getTopmostVisiblePage();
228 currentPage
.restoreLastFocusedElement_();
229 currentPage
.freeze_(false);
233 * If the page does not contain focused elements, focuses on the
237 if (this.pageDiv
.contains(document
.activeElement
))
239 var elements
= this.pageDiv
.querySelectorAll(
240 'input, list, select, textarea, button');
241 for (var i
= 0; i
< elements
.length
; i
++) {
242 var element
= elements
[i
];
244 if (document
.activeElement
== element
)
252 HelpBasePage
: HelpBasePage