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 current top-level 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 topPage = this.getTopmostVisiblePage(); | |
56 var isTopPageLocked = (topPage && topPage.sticky && targetPage.parentPage); | |
57 | |
51 // Notify pages if they will be hidden. | 58 // Notify pages if they will be hidden. |
52 for (var name in this.registeredPages) { | 59 for (var name in this.registeredPages) { |
53 var page = this.registeredPages[name]; | 60 var page = this.registeredPages[name]; |
61 if (!page.parentPage && isTopPageLocked) | |
62 continue; | |
54 if (page.willHidePage && name != pageName && | 63 if (page.willHidePage && name != pageName && |
55 !page.isAncestorOfPage(targetPage)) | 64 !page.isAncestorOfPage(targetPage)) |
56 page.willHidePage(); | 65 page.willHidePage(); |
57 } | 66 } |
58 | 67 |
59 // Update visibilities to show only the hierarchy of the target page. | 68 // Update visibilities to show only the hierarchy of the target page. |
60 for (var name in this.registeredPages) { | 69 for (var name in this.registeredPages) { |
61 var page = this.registeredPages[name]; | 70 var page = this.registeredPages[name]; |
71 if (!page.parentPage && isTopPageLocked) | |
72 continue; | |
62 page.visible = name == pageName || | 73 page.visible = name == pageName || |
63 (document.documentElement.getAttribute('hide-menu') != 'true' && | 74 (document.documentElement.getAttribute('hide-menu') != 'true' && |
64 page.isAncestorOfPage(targetPage)); | 75 page.isAncestorOfPage(targetPage)); |
65 } | 76 } |
66 | 77 |
67 // Notify pages if they were shown. | 78 // Notify pages if they were shown. |
68 for (var name in this.registeredPages) { | 79 for (var name in this.registeredPages) { |
69 var page = this.registeredPages[name]; | 80 var page = this.registeredPages[name]; |
70 if (name == pageName && page.didShowPage) | 81 if (!page.parentPage && isTopPageLocked) |
82 continue; | |
83 if (page.didShowPage && (name == pageName || | |
84 page.isAncestorOfPage(targetPage))) | |
71 page.didShowPage(); | 85 page.didShowPage(); |
72 } | 86 } |
73 }; | 87 }; |
74 | 88 |
75 /** | 89 /** |
76 * Called on load. Dispatch the URL hash to the given page's handleHash | 90 * Called on load. Dispatch the URL hash to the given page's handleHash |
77 * function. | 91 * function. |
78 * @param {string} pageName The string name of the (registered) options page. | 92 * @param {string} pageName The string name of the (registered) options page. |
79 * @param {string} hash The value of the hash component of the URL. | 93 * @param {string} hash The value of the hash component of the URL. |
80 */ | 94 */ |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 OptionsPage.showPageByName(this.getAttribute('pageName')); | 222 OptionsPage.showPageByName(this.getAttribute('pageName')); |
209 } | 223 } |
210 }; | 224 }; |
211 var navbar = $('navbar'); | 225 var navbar = $('navbar'); |
212 navbar.appendChild(pageNav); | 226 navbar.appendChild(pageNav); |
213 page.tab = pageNav; | 227 page.tab = pageNav; |
214 page.initializePage(); | 228 page.initializePage(); |
215 }; | 229 }; |
216 | 230 |
217 /** | 231 /** |
232 * Fine an enclosing section for an element if it exists. | |
233 * @param {Element} element Element to search. | |
234 * @return {OptionPage} The section element, or null. | |
235 * @private | |
236 */ | |
237 OptionsPage.findSectionForNode_ = function(node) { | |
238 while (node = node.parentNode) { | |
239 if (node.nodeType == document.ELEMENT_NODE && | |
240 node.nodeName.toLowerCase() == 'section') | |
241 return node; | |
242 } | |
243 return null; | |
244 }; | |
245 | |
246 /** | |
218 * Registers a new Sub tab page. | 247 * Registers a new Sub tab page. |
219 * @param {OptionsPage} page Page to register. | 248 * @param {OptionsPage} page Page to register. |
James Hawkins
2010/12/23 01:16:29
s/page/subPage/
csilv
2010/12/23 02:59:46
Done.
| |
249 * @param {OptionsPage} parentPage Parent page to register. | |
James Hawkins
2010/12/23 01:16:29
'Parent page to register' ? Seems like 'Associated
csilv
2010/12/23 02:59:46
Done.
| |
250 * @param {Array} associatedControls Array of control elements associated with | |
James Hawkins
2010/12/23 01:16:29
Hmm, from just reading this description, I have no
csilv
2010/12/23 02:59:46
Tweaked the wording, PTAL.
| |
251 * this page. | |
220 */ | 252 */ |
221 OptionsPage.registerSubPage = function(subPage, parentPage) { | 253 OptionsPage.registerSubPage = function(subPage, |
254 parentPage, | |
255 associatedControls) { | |
222 this.registeredPages[subPage.name] = subPage; | 256 this.registeredPages[subPage.name] = subPage; |
223 subPage.parentPage = parentPage; | 257 subPage.parentPage = parentPage; |
258 subPage.associatedControls = associatedControls; | |
259 if (associatedControls && associatedControls.length) { | |
260 subPage.associatedSection = | |
261 this.findSectionForNode_(associatedControls[0]); | |
262 } | |
224 subPage.tab = undefined; | 263 subPage.tab = undefined; |
225 subPage.initializePage(); | 264 subPage.initializePage(); |
226 }; | 265 }; |
227 | 266 |
228 /** | 267 /** |
229 * Registers a new Overlay page. | 268 * Registers a new Overlay page. |
230 * @param {OptionsPage} page Page to register, must be a class derived from | 269 * @param {OptionsPage} page Page to register, must be a class derived from |
231 * OptionsPage. | 270 * @param {Array} associatedControls Array of control elements associated with |
271 * this page. | |
232 */ | 272 */ |
233 OptionsPage.registerOverlay = function(page) { | 273 OptionsPage.registerOverlay = function(page, |
274 associatedControls) { | |
234 this.registeredOverlayPages[page.name] = page; | 275 this.registeredOverlayPages[page.name] = page; |
276 page.associatedControls = associatedControls; | |
277 if (associatedControls && associatedControls.length) { | |
278 page.associatedSection = this.findSectionForNode_(associatedControls[0]); | |
279 } | |
235 page.tab = undefined; | 280 page.tab = undefined; |
236 page.isOverlay = true; | 281 page.isOverlay = true; |
237 page.initializePage(); | 282 page.initializePage(); |
238 }; | 283 }; |
239 | 284 |
240 /** | 285 /** |
241 * Callback for window.onpopstate. | 286 * Callback for window.onpopstate. |
242 * @param {Object} data State data pushed into history. | 287 * @param {Object} data State data pushed into history. |
243 */ | 288 */ |
244 OptionsPage.setState = function(data) { | 289 OptionsPage.setState = function(data) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 OptionsPage.prototype = { | 359 OptionsPage.prototype = { |
315 __proto__: cr.EventTarget.prototype, | 360 __proto__: cr.EventTarget.prototype, |
316 | 361 |
317 /** | 362 /** |
318 * The parent page of this option page, or null for top-level pages. | 363 * The parent page of this option page, or null for top-level pages. |
319 * @type {OptionsPage} | 364 * @type {OptionsPage} |
320 */ | 365 */ |
321 parentPage: null, | 366 parentPage: null, |
322 | 367 |
323 /** | 368 /** |
369 * The section on the parent page that is associated with this page. | |
370 * Can be null. | |
371 * @type {Element} | |
372 */ | |
373 associatedSection: null, | |
374 | |
375 /** | |
376 * An array of controls that are associated with this page. The first | |
377 * control should be located on a top-level page. | |
378 * @type {OptionsPage} | |
379 */ | |
380 associatedControls: null, | |
381 | |
382 /** | |
324 * Initializes page content. | 383 * Initializes page content. |
325 */ | 384 */ |
326 initializePage: function() {}, | 385 initializePage: function() {}, |
327 | 386 |
328 /** | 387 /** |
329 * Sets managed banner visibility state. | 388 * Sets managed banner visibility state. |
330 */ | 389 */ |
331 setManagedBannerVisibility: function(visible) { | 390 setManagedBannerVisibility: function(visible) { |
332 this.managed = visible; | 391 this.managed = visible; |
333 if (this.visible) { | 392 if (this.visible) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 var level = 0; | 472 var level = 0; |
414 var parent = this.parentPage; | 473 var parent = this.parentPage; |
415 while (parent) { | 474 while (parent) { |
416 level++; | 475 level++; |
417 parent = parent.parentPage; | 476 parent = parent.parentPage; |
418 } | 477 } |
419 return level; | 478 return level; |
420 }, | 479 }, |
421 | 480 |
422 /** | 481 /** |
482 * Checks whether the page is considered 'sticky', such that it will | |
483 * remain a top-level page even if sub-pages change. | |
484 * @return {boolean} True if this page is sticky. | |
485 */ | |
486 get sticky() { | |
487 return false; | |
488 }, | |
489 | |
490 /** | |
423 * Checks whether this page is an ancestor of the given page in terms of | 491 * Checks whether this page is an ancestor of the given page in terms of |
424 * subpage nesting. | 492 * subpage nesting. |
425 * @param {OptionsPage} page | 493 * @param {OptionsPage} page |
426 * @return {boolean} True if this page is nested under |page| | 494 * @return {boolean} True if this page is nested under |page| |
427 */ | 495 */ |
428 isAncestorOfPage: function(page) { | 496 isAncestorOfPage: function(page) { |
429 var parent = page.parentPage; | 497 var parent = page.parentPage; |
430 while (parent) { | 498 while (parent) { |
431 if (parent == this) | 499 if (parent == this) |
432 return true; | 500 return true; |
(...skipping 13 matching lines...) Expand all Loading... | |
446 OptionsPage.showOverlay(hash); | 514 OptionsPage.showOverlay(hash); |
447 }, | 515 }, |
448 }; | 516 }; |
449 | 517 |
450 // Export | 518 // Export |
451 return { | 519 return { |
452 OptionsPage: OptionsPage | 520 OptionsPage: OptionsPage |
453 }; | 521 }; |
454 | 522 |
455 }); | 523 }); |
OLD | NEW |