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) | |
Dan Beam
2016/03/25 03:21:59
shouldn't this be queuedRouteChange_?
michaelpg
2016/03/29 22:04:03
Done.
| |
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 if (!this._queuedRouteChange) | |
87 this._queuedRouteChange = {newRoute: newRoute, oldRoute: oldRoute}; | |
88 else | |
89 this._queuedRouteChange.newRoute = newRoute; | |
Dan Beam
2016/03/25 03:21:59
nit:
this.queuedRouteChange_ = this.queuedRouteCh
michaelpg
2016/03/29 22:04:03
Done.
| |
90 return; | |
91 } | |
92 | |
54 // route.section is only non-empty when the user is within a subpage. | 93 // 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 | 94 // When the user is not in a subpage, but on the Basic page, route.section |
56 // is an empty string. | 95 // is an empty string. |
57 var newRouteIsSubpage = newRoute && newRoute.section == this.section; | 96 var newRouteIsSubpage = newRoute && newRoute.section == this.section; |
58 var oldRouteIsSubpage = oldRoute && oldRoute.section == this.section; | 97 var oldRouteIsSubpage = oldRoute && oldRoute.section == this.section; |
59 | 98 |
99 if (newRouteIsSubpage) | |
100 this.ensureSubpageInstance_(); | |
101 | |
60 if (!newRouteIsSubpage || !oldRouteIsSubpage || | 102 if (!newRouteIsSubpage || !oldRouteIsSubpage || |
61 newRoute.subpage.length == oldRoute.subpage.length) { | 103 newRoute.subpage.length == oldRoute.subpage.length) { |
62 // If two routes are at the same level, or if either the new or old route | 104 // 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. | 105 // is not a subpage, fade in and out. |
64 this.$.animatedPages.exitAnimation = 'fade-out-animation'; | 106 this.$.animatedPages.exitAnimation = 'fade-out-animation'; |
65 this.$.animatedPages.entryAnimation = 'fade-in-animation'; | 107 this.$.animatedPages.entryAnimation = 'fade-in-animation'; |
66 } else { | 108 } else { |
67 // For transitioning between subpages at different levels, slide. | 109 // For transitioning between subpages at different levels, slide. |
68 if (newRoute.subpage.length > oldRoute.subpage.length) { | 110 if (newRoute.subpage.length > oldRoute.subpage.length) { |
69 this.$.animatedPages.exitAnimation = 'slide-left-animation'; | 111 this.$.animatedPages.exitAnimation = 'slide-left-animation'; |
70 this.$.animatedPages.entryAnimation = 'slide-from-right-animation'; | 112 this.$.animatedPages.entryAnimation = 'slide-from-right-animation'; |
71 } else { | 113 } else { |
72 this.$.animatedPages.exitAnimation = 'slide-right-animation'; | 114 this.$.animatedPages.exitAnimation = 'slide-right-animation'; |
73 this.$.animatedPages.entryAnimation = 'slide-from-left-animation'; | 115 this.$.animatedPages.entryAnimation = 'slide-from-left-animation'; |
74 } | 116 } |
75 } | 117 } |
76 | 118 |
77 this.$.animatedPages.selected = | 119 this.$.animatedPages.selected = |
78 newRouteIsSubpage ? newRoute.subpage.slice(-1)[0] : 'main'; | 120 newRouteIsSubpage ? newRoute.subpage.slice(-1)[0] : 'main'; |
79 }, | 121 }, |
80 | 122 |
81 /** | 123 /** |
124 * Ensures that the template enclosing the subpage is stamped. | |
125 * @private | |
126 */ | |
127 ensureSubpageInstance_: function() { | |
128 var id = this.currentRoute.subpage.slice(-1)[0]; | |
129 var template = Polymer.dom(this).querySelector( | |
130 'template[name="' + id + '"]'); | |
131 | |
132 // Set the subpage's id for use by neon-animated-pages. | |
133 var subpage = template._content.querySelector('settings-subpage'); | |
134 if (!subpage.id) | |
135 subpage.id = id; | |
136 | |
137 // Render synchronously so neon-animated-pages can select the subpage. | |
138 template.if = true; | |
139 template.render(); | |
Dan Beam
2016/03/25 03:21:59
why is this better than using the standard declara
michaelpg
2016/03/29 22:04:03
for declarative binding, we'd have to define ensur
| |
140 }, | |
141 | |
142 /** | |
82 * Buttons in this pageset should use this method to transition to subpages. | 143 * Buttons in this pageset should use this method to transition to subpages. |
83 */ | 144 */ |
84 setSubpageChain: function(subpage) { | 145 setSubpageChain: function(subpage) { |
85 this.currentRoute = { | 146 this.currentRoute = { |
86 page: this.currentRoute.page, | 147 page: this.currentRoute.page, |
87 section: subpage.length > 0 ? this.section : '', | 148 section: subpage.length > 0 ? this.section : '', |
88 subpage: subpage, | 149 subpage: subpage, |
89 }; | 150 }; |
90 }, | 151 }, |
91 }); | 152 }); |
OLD | NEW |