1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Helper base class for all help pages and overlays, which controls
// overlays, focus and scroll. This class is partially based on
// OptionsPage, but simpler and contains only overlay- and focus-
// handling logic. As in OptionsPage each page can be an overlay itself,
// but each page contains its own list of registered overlays which can be
// displayed over it.
//
// TODO (ygorshenin@): crbug.com/313244.
cr.define('help', function() {
function HelpBasePage() {
}
HelpBasePage.prototype = {
__proto__: HTMLDivElement.prototype,
/**
* name of the page, should be the same as an id of the
* corresponding HTMLDivElement.
*/
name: null,
/**
* HTML counterpart of this page.
*/
pageDiv: null,
/**
* True if current page is overlay.
*/
isOverlay: false,
/**
* HTMLElement that was last focused when this page was the
* topmost.
*/
lastFocusedElement: null,
/**
* State of vertical scrollbars when this page was the topmost.
*/
lastScrollTop: 0,
/**
* Dictionary of registered overlays.
*/
registeredOverlays: {},
/**
* Stores currently focused element.
* @private
*/
storeLastFocusedElement_: function() {
if (this.pageDiv.contains(document.activeElement))
this.lastFocusedElement = document.activeElement;
},
/**
* Restores focus to the last focused element on this page.
* @private
*/
restoreLastFocusedElement_: function() {
if (this.lastFocusedElement)
this.lastFocusedElement.focus();
else
this.focus();
},
/**
* Shows or hides current page iff it's an overlay.
* @param {boolean} visible True if overlay should be displayed.
* @private
*/
setOverlayVisible_: function(visible) {
assert(this.isOverlay);
var pageDiv = this.pageDiv;
this.container.hidden = !visible;
if (visible)
pageDiv.classList.add('showing');
else
pageDiv.classList.remove('showing');
},
/**
* @return {HTMLDivElement} visible non-overlay page or
* null, if there are no visible non-overlay pages.
* @private
*/
getVisibleNonOverlay_: function() {
if (this.isOverlay || !this.visible)
return null;
return this;
},
/**
* @return {HTMLDivElement} Visible overlay page, or null,
* if there are no visible overlay pages.
* @private
*/
getVisibleOverlay_: function() {
for (var name in this.registeredOverlays) {
var overlay = this.registeredOverlays[name];
if (overlay.visible)
return overlay;
}
return null;
},
/**
* Freezes current page, makes it impossible to scroll it.
* @param {boolean} freeze True if the page should be frozen.
* @private
*/
freeze_: function(freeze) {
var scrollLeft = scrollLeftForDocument(document);
if (freeze) {
this.lastScrollTop = scrollTopForDocument(document);
document.body.style.overflow = 'hidden';
window.scroll(scrollLeft, 0);
} else {
document.body.style.overflow = 'auto';
window.scroll(scrollLeft, this.lastScrollTop);
}
},
/**
* Initializes current page.
* @param {string} name Name of the current page.
*/
initialize: function(name) {
this.name = name;
this.pageDiv = $(name);
},
/**
* Called before overlay is displayed.
*/
onBeforeShow: function() {
},
/**
* @return {HTMLDivElement} Topmost visible page, or null, if
* there are no visible pages.
*/
getTopmostVisiblePage: function() {
return this.getVisibleOverlay_() || this.getVisibleNonOverlay_();
},
/**
* Registers overlay.
* @param {HelpBasePage} overlay Overlay that should be registered.
*/
registerOverlay: function(overlay) {
this.registeredOverlays[overlay.name] = overlay;
overlay.isOverlay = true;
},
/**
* Shows or hides current page.
* @param {boolean} visible True if current page should be displayed.
*/
set visible(visible) {
if (this.visible == visible)
return;
if (!visible)
this.storeLastFocusedElement_();
if (this.isOverlay)
this.setOverlayVisible_(visible);
else
this.pageDiv.hidden = !visible;
if (visible)
this.restoreLastFocusedElement_();
if (visible)
this.onBeforeShow();
},
/**
* Returns true if current page is visible.
* @return {boolean} True if current page is visible.
*/
get visible() {
if (this.isOverlay)
return this.pageDiv.classList.contains('showing');
return !this.pageDiv.hidden;
},
/**
* This method returns overlay container, it should be called only
* on overlays.
* @return {HTMLDivElement} overlay container.
*/
get container() {
assert(this.isOverlay);
return this.pageDiv.parentNode;
},
/**
* Shows registered overlay.
* @param {string} name Name of registered overlay to show.
*/
showOverlay: function(name) {
var currentPage = this.getTopmostVisiblePage();
currentPage.storeLastFocusedElement_();
currentPage.freeze_(true);
var overlay = this.registeredOverlays[name];
if (!overlay)
return;
overlay.visible = true;
},
/**
* Hides currently displayed overlay.
*/
closeOverlay: function() {
var overlay = this.getVisibleOverlay_();
if (!overlay)
return;
overlay.visible = false;
var currentPage = this.getTopmostVisiblePage();
currentPage.restoreLastFocusedElement_();
currentPage.freeze_(false);
},
/**
* If the page does not contain focused elements, focuses on the
* first appropriate.
*/
focus: function() {
if (this.pageDiv.contains(document.activeElement))
return;
var elements = this.pageDiv.querySelectorAll(
'input, list, select, textarea, button');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
element.focus();
if (document.activeElement == element)
return;
}
},
};
// Export
return {
HelpBasePage: HelpBasePage
};
});
|