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 var pageDiv = this.pageDiv;
80 this.container.hidden = !visible;
82 pageDiv.classList.add('showing');
84 pageDiv.classList.remove('showing');
88 * @return {HTMLDivElement} visible non-overlay page or
89 * null, if there are no visible non-overlay pages.
92 getVisibleNonOverlay_: function() {
93 if (this.isOverlay || !this.visible)
99 * @return {HTMLDivElement} Visible overlay page, or null,
100 * if there are no visible overlay pages.
103 getVisibleOverlay_: function() {
104 for (var name in this.registeredOverlays) {
105 var overlay = this.registeredOverlays[name];
113 * Freezes current page, makes it impossible to scroll it.
114 * @param {boolean} freeze True if the page should be frozen.
117 freeze_: function(freeze) {
119 this.lastScrollTop = document.documentElement.scrollTop;
120 document.body.style.overflow = 'hidden';
121 window.scroll(document.body.scrollLeft, 0);
123 document.body.style.overflow = 'auto';
124 window.scroll(document.body.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 * @return {HTMLDivElement} Topmost visible page, or null, if
139 * there are no visible pages.
141 getTopmostVisiblePage: function() {
142 return this.getVisibleOverlay_() || this.getVisibleNonOverlay_();
147 * @param {HelpBasePage} overlay Overlay that should be registered.
149 registerOverlay: function(overlay) {
150 this.registeredOverlays[overlay.name] = overlay;
151 overlay.isOverlay = true;
155 * Shows or hides current page.
156 * @param {boolean} visible True if current page should be displayed.
158 set visible(visible) {
159 if (this.visible == visible)
163 this.storeLastFocusedElement_();
166 this.setOverlayVisible_(visible);
168 this.pageDiv.hidden = !visible;
171 this.restoreLastFocusedElement_();
175 * Returns true if current page is visible.
176 * @return {boolean} True if current page is visible.
180 return this.pageDiv.classList.contains('showing');
181 return !this.pageDiv.hidden;
185 * This method returns overlay container, it should be called only
187 * @return {HTMLDivElement} overlay container.
190 assert(this.isOverlay);
191 return this.pageDiv.parentNode;
195 * Shows registered overlay.
196 * @param {string} name Name of registered overlay to show.
198 showOverlay: function(name) {
199 var currentPage = this.getTopmostVisiblePage();
200 currentPage.storeLastFocusedElement_();
201 currentPage.freeze_(true);
203 var overlay = this.registeredOverlays[name];
206 overlay.visible = true;
210 * Hides currently displayed overlay.
212 closeOverlay: function() {
213 var overlay = this.getVisibleOverlay_();
216 overlay.visible = false;
218 var currentPage = this.getTopmostVisiblePage();
219 currentPage.restoreLastFocusedElement_();
220 currentPage.freeze_(false);
224 * If the page does not contain focused elements, focuses on the
228 if (this.pageDiv.contains(document.activeElement))
230 var elements = this.pageDiv.querySelectorAll(
231 'input, list, select, textarea, button');
232 for (var i = 0; i < elements.length; i++) {
233 var element = elements[i];
235 if (document.activeElement == element)
243 HelpBasePage: HelpBasePage