OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * 'settings-animated-pages' is a container for a page and animated subpages. | 7 * 'settings-animated-pages' is a container for a page and animated subpages. |
8 * It provides a set of common behaviors and animations. | 8 * It provides a set of common behaviors and animations. |
9 * | 9 * |
10 * Example: | 10 * Example: |
(...skipping 23 matching lines...) Expand all Loading... |
34 * | 34 * |
35 * The section name must match the name specified in settings_router.js. | 35 * The section name must match the name specified in settings_router.js. |
36 */ | 36 */ |
37 section: { | 37 section: { |
38 type: String, | 38 type: String, |
39 }, | 39 }, |
40 }, | 40 }, |
41 | 41 |
42 /** @override */ | 42 /** @override */ |
43 created: function() { | 43 created: function() { |
| 44 // Observe the light DOM so we know when it's ready. |
| 45 this.lightDomObserver_ = Polymer.dom(this).observeNodes( |
| 46 this.lightDomChanged_.bind(this)); |
| 47 |
44 this.addEventListener('subpage-back', function() { | 48 this.addEventListener('subpage-back', function() { |
45 assert(this.currentRoute.section == this.section); | 49 assert(this.currentRoute.section == this.section); |
46 assert(this.currentRoute.subpage.length >= 1); | 50 assert(this.currentRoute.subpage.length >= 1); |
47 | 51 |
48 this.setSubpageChain(this.currentRoute.subpage.slice(0, -1)); | 52 this.setSubpageChain(this.currentRoute.subpage.slice(0, -1)); |
49 }.bind(this)); | 53 }.bind(this)); |
50 }, | 54 }, |
51 | 55 |
| 56 /** |
| 57 * Called initially once the effective children are ready. |
| 58 * @private |
| 59 */ |
| 60 lightDomChanged_: function() { |
| 61 if (this.lightDomReady_) |
| 62 return; |
| 63 |
| 64 this.lightDomReady_ = true; |
| 65 Polymer.dom(this).unobserveNodes(this.lightDomObserver_); |
| 66 this.runQueuedRouteChange_(); |
| 67 }, |
| 68 |
| 69 /** |
| 70 * Calls currentRouteChanged_ with the deferred route change info. |
| 71 * @private |
| 72 */ |
| 73 runQueuedRouteChange_: function() { |
| 74 if (!this.queuedRouteChange_) |
| 75 return; |
| 76 this.async(this.currentRouteChanged_.bind( |
| 77 this, |
| 78 this.queuedRouteChange_.newRoute, |
| 79 this.queuedRouteChange_.oldRoute)); |
| 80 }, |
| 81 |
52 /** @private */ | 82 /** @private */ |
53 currentRouteChanged_: function(newRoute, oldRoute) { | 83 currentRouteChanged_: function(newRoute, oldRoute) { |
| 84 // Don't manipulate the light DOM until it's ready. |
| 85 if (!this.lightDomReady_) { |
| 86 this.queuedRouteChange_ = this.queuedRouteChange_ || {oldRoute: oldRoute}; |
| 87 this.queuedRouteChange_.newRoute = newRoute; |
| 88 return; |
| 89 } |
| 90 |
54 // route.section is only non-empty when the user is within a subpage. | 91 // route.section is only non-empty when the user is within a subpage. |
55 // When the user is not in a subpage, but on the Basic page, route.section | 92 // When the user is not in a subpage, but on the Basic page, route.section |
56 // is an empty string. | 93 // is an empty string. |
57 var newRouteIsSubpage = newRoute && newRoute.section == this.section; | 94 var newRouteIsSubpage = newRoute && newRoute.section == this.section; |
58 var oldRouteIsSubpage = oldRoute && oldRoute.section == this.section; | 95 var oldRouteIsSubpage = oldRoute && oldRoute.section == this.section; |
59 | 96 |
| 97 if (newRouteIsSubpage) |
| 98 this.ensureSubpageInstance_(); |
| 99 |
60 if (!newRouteIsSubpage || !oldRouteIsSubpage || | 100 if (!newRouteIsSubpage || !oldRouteIsSubpage || |
61 newRoute.subpage.length == oldRoute.subpage.length) { | 101 newRoute.subpage.length == oldRoute.subpage.length) { |
62 // If two routes are at the same level, or if either the new or old route | 102 // If two routes are at the same level, or if either the new or old route |
63 // is not a subpage, fade in and out. | 103 // is not a subpage, fade in and out. |
64 this.$.animatedPages.exitAnimation = 'fade-out-animation'; | 104 this.$.animatedPages.exitAnimation = 'fade-out-animation'; |
65 this.$.animatedPages.entryAnimation = 'fade-in-animation'; | 105 this.$.animatedPages.entryAnimation = 'fade-in-animation'; |
66 } else { | 106 } else { |
67 // For transitioning between subpages at different levels, slide. | 107 // For transitioning between subpages at different levels, slide. |
68 if (newRoute.subpage.length > oldRoute.subpage.length) { | 108 if (newRoute.subpage.length > oldRoute.subpage.length) { |
69 this.$.animatedPages.exitAnimation = 'slide-left-animation'; | 109 this.$.animatedPages.exitAnimation = 'slide-left-animation'; |
70 this.$.animatedPages.entryAnimation = 'slide-from-right-animation'; | 110 this.$.animatedPages.entryAnimation = 'slide-from-right-animation'; |
71 } else { | 111 } else { |
72 this.$.animatedPages.exitAnimation = 'slide-right-animation'; | 112 this.$.animatedPages.exitAnimation = 'slide-right-animation'; |
73 this.$.animatedPages.entryAnimation = 'slide-from-left-animation'; | 113 this.$.animatedPages.entryAnimation = 'slide-from-left-animation'; |
74 } | 114 } |
75 } | 115 } |
76 | 116 |
77 this.$.animatedPages.selected = | 117 this.$.animatedPages.selected = |
78 newRouteIsSubpage ? newRoute.subpage.slice(-1)[0] : 'main'; | 118 newRouteIsSubpage ? newRoute.subpage.slice(-1)[0] : 'main'; |
79 }, | 119 }, |
80 | 120 |
81 /** | 121 /** |
| 122 * Ensures that the template enclosing the subpage is stamped. |
| 123 * @private |
| 124 */ |
| 125 ensureSubpageInstance_: function() { |
| 126 var id = this.currentRoute.subpage.slice(-1)[0]; |
| 127 var template = Polymer.dom(this).querySelector( |
| 128 'template[name="' + id + '"]'); |
| 129 |
| 130 // Do nothing if the subpage is already stamped. |
| 131 if (template.if) |
| 132 return; |
| 133 |
| 134 // Set the subpage's id for use by neon-animated-pages. |
| 135 var subpage = /** @type {{_content: DocumentFragment}} */(template)._content |
| 136 .querySelector('settings-subpage'); |
| 137 if (!subpage.id) |
| 138 subpage.id = id; |
| 139 |
| 140 // Render synchronously so neon-animated-pages can select the subpage. |
| 141 template.if = true; |
| 142 template.render(); |
| 143 }, |
| 144 |
| 145 /** |
82 * Buttons in this pageset should use this method to transition to subpages. | 146 * Buttons in this pageset should use this method to transition to subpages. |
83 */ | 147 */ |
84 setSubpageChain: function(subpage) { | 148 setSubpageChain: function(subpage) { |
85 this.currentRoute = { | 149 this.currentRoute = { |
86 page: this.currentRoute.page, | 150 page: this.currentRoute.page, |
87 section: subpage.length > 0 ? this.section : '', | 151 section: subpage.length > 0 ? this.section : '', |
88 subpage: subpage, | 152 subpage: subpage, |
89 }; | 153 }; |
90 }, | 154 }, |
91 }); | 155 }); |
OLD | NEW |