OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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('options', function() { | 5 cr.define('options', function() { |
6 ///////////////////////////////////////////////////////////////////////////// | 6 ///////////////////////////////////////////////////////////////////////////// |
7 // OptionsPage class: | 7 // OptionsPage class: |
8 | 8 |
9 /** | 9 /** |
10 * Base class for options page. | 10 * Base class for options page. |
(...skipping 30 matching lines...) Expand all Loading... | |
41 * @private | 41 * @private |
42 */ | 42 */ |
43 OptionsPage.initialized_ = false; | 43 OptionsPage.initialized_ = false; |
44 | 44 |
45 /** | 45 /** |
46 * Shows a registered page. This handles both top-level pages and sub-pages. | 46 * Shows a registered page. This handles both top-level pages and sub-pages. |
47 * @param {string} pageName Page name. | 47 * @param {string} pageName Page name. |
48 */ | 48 */ |
49 OptionsPage.showPageByName = function(pageName) { | 49 OptionsPage.showPageByName = function(pageName) { |
50 var targetPage = this.registeredPages[pageName]; | 50 var targetPage = this.registeredPages[pageName]; |
51 | |
52 // Determine if the root page is 'sticky', meaning that it | |
53 // shouldn't change when showing a sub-page. This can happen for special | |
54 // pages like Search. | |
55 var rootPage = null; | |
56 for (var name in this.registeredPages) { | |
57 var page = this.registeredPages[name]; | |
58 if (page.visible && !page.parentPage) { | |
59 rootPage = page; | |
60 break; | |
61 } | |
62 } | |
63 var isRootPageLocked = | |
64 (rootPage && rootPage.sticky && targetPage.parentPage); | |
arv (Not doing code reviews)
2010/12/23 20:38:22
useless parentheses
csilv
2010/12/23 21:46:07
Done.
| |
65 | |
51 // Notify pages if they will be hidden. | 66 // Notify pages if they will be hidden. |
52 for (var name in this.registeredPages) { | 67 for (var name in this.registeredPages) { |
53 var page = this.registeredPages[name]; | 68 var page = this.registeredPages[name]; |
69 if (!page.parentPage && isRootPageLocked) | |
70 continue; | |
54 if (page.willHidePage && name != pageName && | 71 if (page.willHidePage && name != pageName && |
55 !page.isAncestorOfPage(targetPage)) | 72 !page.isAncestorOfPage(targetPage)) |
56 page.willHidePage(); | 73 page.willHidePage(); |
57 } | 74 } |
58 | 75 |
59 // Update visibilities to show only the hierarchy of the target page. | 76 // Update visibilities to show only the hierarchy of the target page. |
60 for (var name in this.registeredPages) { | 77 for (var name in this.registeredPages) { |
61 var page = this.registeredPages[name]; | 78 var page = this.registeredPages[name]; |
79 if (!page.parentPage && isRootPageLocked) | |
80 continue; | |
62 page.visible = name == pageName || | 81 page.visible = name == pageName || |
63 (document.documentElement.getAttribute('hide-menu') != 'true' && | 82 (document.documentElement.getAttribute('hide-menu') != 'true' && |
64 page.isAncestorOfPage(targetPage)); | 83 page.isAncestorOfPage(targetPage)); |
65 } | 84 } |
66 | 85 |
67 // Notify pages if they were shown. | 86 // Notify pages if they were shown. |
68 for (var name in this.registeredPages) { | 87 for (var name in this.registeredPages) { |
69 var page = this.registeredPages[name]; | 88 var page = this.registeredPages[name]; |
70 if (name == pageName && page.didShowPage) | 89 if (!page.parentPage && isRootPageLocked) |
90 continue; | |
91 if (page.didShowPage && (name == pageName || | |
92 page.isAncestorOfPage(targetPage))) | |
71 page.didShowPage(); | 93 page.didShowPage(); |
72 } | 94 } |
73 }; | 95 }; |
74 | 96 |
75 /** | 97 /** |
76 * Called on load. Dispatch the URL hash to the given page's handleHash | 98 * Called on load. Dispatch the URL hash to the given page's handleHash |
77 * function. | 99 * function. |
78 * @param {string} pageName The string name of the (registered) options page. | 100 * @param {string} pageName The string name of the (registered) options page. |
79 * @param {string} hash The value of the hash component of the URL. | 101 * @param {string} hash The value of the hash component of the URL. |
80 */ | 102 */ |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 OptionsPage.showPageByName(this.getAttribute('pageName')); | 230 OptionsPage.showPageByName(this.getAttribute('pageName')); |
209 } | 231 } |
210 }; | 232 }; |
211 var navbar = $('navbar'); | 233 var navbar = $('navbar'); |
212 navbar.appendChild(pageNav); | 234 navbar.appendChild(pageNav); |
213 page.tab = pageNav; | 235 page.tab = pageNav; |
214 page.initializePage(); | 236 page.initializePage(); |
215 }; | 237 }; |
216 | 238 |
217 /** | 239 /** |
218 * Registers a new Sub tab page. | 240 * Find an enclosing section for an element if it exists. |
219 * @param {OptionsPage} page Page to register. | 241 * @param {Element} element Element to search. |
242 * @return {OptionPage} The section element, or null. | |
243 * @private | |
220 */ | 244 */ |
221 OptionsPage.registerSubPage = function(subPage, parentPage) { | 245 OptionsPage.findSectionForNode_ = function(node) { |
246 while (node = node.parentNode) { | |
arv (Not doing code reviews)
2010/12/23 20:38:22
node.parentElement since you don't care about non
csilv
2010/12/23 21:46:07
parentElement? I can't find any references to tha
| |
247 if (node.nodeType == document.ELEMENT_NODE && | |
arv (Not doing code reviews)
2010/12/23 20:38:22
Node.Element_NODE
... if you use parentNode you c
csilv
2010/12/23 21:46:07
Done.
| |
248 node.nodeName.toLowerCase() == 'section') | |
arv (Not doing code reviews)
2010/12/23 20:38:22
Use tagName?
I would skip the toLowerCase and co
csilv
2010/12/23 21:46:07
Done.
| |
249 return node; | |
250 } | |
251 return null; | |
252 }; | |
253 | |
254 /** | |
255 * Registers a new Sub-page. | |
256 * @param {OptionsPage} subPage Sub-page to register. | |
257 * @param {OptionsPage} parentPage Associated parent page for this page. | |
258 * @param {Array} associatedControls Array of control elements that lead to | |
259 * this sub-page. The first item is typically a button in a root-level | |
arv (Not doing code reviews)
2010/12/23 20:38:22
indent +2
csilv
2010/12/23 21:46:07
Done.
| |
260 * page. There may be additional buttons for nested sub-pages. | |
261 */ | |
262 OptionsPage.registerSubPage = function(subPage, | |
263 parentPage, | |
264 associatedControls) { | |
arv (Not doing code reviews)
2010/12/23 20:38:22
Can you make this optional?
csilv
2010/12/23 21:46:07
Done.
| |
222 this.registeredPages[subPage.name] = subPage; | 265 this.registeredPages[subPage.name] = subPage; |
223 subPage.parentPage = parentPage; | 266 subPage.parentPage = parentPage; |
267 subPage.associatedControls = associatedControls; | |
268 if (associatedControls && associatedControls.length) { | |
269 subPage.associatedSection = | |
270 this.findSectionForNode_(associatedControls[0]); | |
271 } | |
224 subPage.tab = undefined; | 272 subPage.tab = undefined; |
225 subPage.initializePage(); | 273 subPage.initializePage(); |
226 }; | 274 }; |
227 | 275 |
228 /** | 276 /** |
229 * Registers a new Overlay page. | 277 * Registers a new Overlay page. |
230 * @param {OptionsPage} page Page to register, must be a class derived from | 278 * @param {OptionsPage} page Page to register, must be a class derived from |
231 * OptionsPage. | 279 * @param {Array} associatedControls Array of control elements associated with |
280 * this page. | |
232 */ | 281 */ |
233 OptionsPage.registerOverlay = function(page) { | 282 OptionsPage.registerOverlay = function(page, |
283 associatedControls) { | |
arv (Not doing code reviews)
2010/12/23 20:38:22
here too
csilv
2010/12/23 21:46:07
Done.
| |
234 this.registeredOverlayPages[page.name] = page; | 284 this.registeredOverlayPages[page.name] = page; |
285 page.associatedControls = associatedControls; | |
286 if (associatedControls && associatedControls.length) { | |
287 page.associatedSection = this.findSectionForNode_(associatedControls[0]); | |
288 } | |
235 page.tab = undefined; | 289 page.tab = undefined; |
236 page.isOverlay = true; | 290 page.isOverlay = true; |
237 page.initializePage(); | 291 page.initializePage(); |
238 }; | 292 }; |
239 | 293 |
240 /** | 294 /** |
241 * Callback for window.onpopstate. | 295 * Callback for window.onpopstate. |
242 * @param {Object} data State data pushed into history. | 296 * @param {Object} data State data pushed into history. |
243 */ | 297 */ |
244 OptionsPage.setState = function(data) { | 298 OptionsPage.setState = function(data) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 OptionsPage.prototype = { | 368 OptionsPage.prototype = { |
315 __proto__: cr.EventTarget.prototype, | 369 __proto__: cr.EventTarget.prototype, |
316 | 370 |
317 /** | 371 /** |
318 * The parent page of this option page, or null for top-level pages. | 372 * The parent page of this option page, or null for top-level pages. |
319 * @type {OptionsPage} | 373 * @type {OptionsPage} |
320 */ | 374 */ |
321 parentPage: null, | 375 parentPage: null, |
322 | 376 |
323 /** | 377 /** |
378 * The section on the parent page that is associated with this page. | |
379 * Can be null. | |
380 * @type {Element} | |
381 */ | |
382 associatedSection: null, | |
383 | |
384 /** | |
385 * An array of controls that are associated with this page. The first | |
386 * control should be located on a top-level page. | |
387 * @type {OptionsPage} | |
388 */ | |
389 associatedControls: null, | |
390 | |
391 /** | |
324 * Initializes page content. | 392 * Initializes page content. |
325 */ | 393 */ |
326 initializePage: function() {}, | 394 initializePage: function() {}, |
327 | 395 |
328 /** | 396 /** |
329 * Sets managed banner visibility state. | 397 * Sets managed banner visibility state. |
330 */ | 398 */ |
331 setManagedBannerVisibility: function(visible) { | 399 setManagedBannerVisibility: function(visible) { |
332 this.managed = visible; | 400 this.managed = visible; |
333 if (this.visible) { | 401 if (this.visible) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 var level = 0; | 481 var level = 0; |
414 var parent = this.parentPage; | 482 var parent = this.parentPage; |
415 while (parent) { | 483 while (parent) { |
416 level++; | 484 level++; |
417 parent = parent.parentPage; | 485 parent = parent.parentPage; |
418 } | 486 } |
419 return level; | 487 return level; |
420 }, | 488 }, |
421 | 489 |
422 /** | 490 /** |
491 * Checks whether the page is considered 'sticky', such that it will | |
492 * remain a top-level page even if sub-pages change. | |
493 * @return {boolean} True if this page is sticky. | |
arv (Not doing code reviews)
2010/12/23 20:38:22
I've been using @type for getters and setters sinc
csilv
2010/12/23 21:46:07
Done.
| |
494 */ | |
495 get sticky() { | |
496 return false; | |
497 }, | |
498 | |
499 /** | |
423 * Checks whether this page is an ancestor of the given page in terms of | 500 * Checks whether this page is an ancestor of the given page in terms of |
424 * subpage nesting. | 501 * subpage nesting. |
425 * @param {OptionsPage} page | 502 * @param {OptionsPage} page |
426 * @return {boolean} True if this page is nested under |page| | 503 * @return {boolean} True if this page is nested under |page| |
427 */ | 504 */ |
428 isAncestorOfPage: function(page) { | 505 isAncestorOfPage: function(page) { |
429 var parent = page.parentPage; | 506 var parent = page.parentPage; |
430 while (parent) { | 507 while (parent) { |
431 if (parent == this) | 508 if (parent == this) |
432 return true; | 509 return true; |
(...skipping 13 matching lines...) Expand all Loading... | |
446 OptionsPage.showOverlay(hash); | 523 OptionsPage.showOverlay(hash); |
447 }, | 524 }, |
448 }; | 525 }; |
449 | 526 |
450 // Export | 527 // Export |
451 return { | 528 return { |
452 OptionsPage: OptionsPage | 529 OptionsPage: OptionsPage |
453 }; | 530 }; |
454 | 531 |
455 }); | 532 }); |
OLD | NEW |