|
84 | 84 | contribute: { |
85 | 85 |
|
86 | 86 | applySelection: function(item, selected) { |
| 87 | + var index = this.collective.indexOfItem(item); |
| 88 | + // See if the corresponding dot has already been created. |
| 89 | + // If not, the correct dot will be selected when it gets created. |
87 | 90 | var dots = this.dots; |
88 | | - if (dots && dots.length > 0) { |
89 | | - var index = this.collective.indexOfItem(item); |
| 91 | + if (dots && dots.length > index) { |
90 | 92 | var dot = this.dots[index]; |
91 | 93 | if (dot) { |
92 | 94 | dot.classList.toggle('selected', selected); |
93 | 95 | } |
94 | | - } else { |
95 | | - // HACK: Dots haven't been created yet, because binding hasn't updated. |
96 | | - // Wait a tick and try again. |
97 | | - var callee = arguments.callee; |
98 | | - setTimeout(function() { |
99 | | - callee.call(this, item, selected); |
100 | | - }.bind(this)); |
101 | 96 | } |
102 | 97 | }, |
103 | 98 |
|
104 | | - // TODO: Move the requirement for a selction to basic-item-selection. This |
105 | | - // should ideally pick the nearest item to the previously-selected item. |
106 | | - // collectiveChanged: function() { |
107 | | - // if (this.collective.selectedItem === null && this.collective.items != null && this.collective.selectFirst) { |
108 | | - // this.collective.selectFirst(); |
109 | | - // } |
110 | | - // }, |
| 99 | + collectiveChanged: function() { |
| 100 | + // Apply any selection made before assimilation. |
| 101 | + if (this._prematureSelectedIndex |
| 102 | + && 'selectedIndex' in this.collective |
| 103 | + && this.collective.selectedIndex === -1) { |
| 104 | + this.collective.selectedIndex = this._prematureSelectedIndex; |
| 105 | + this._prematureSelectedIndex = null; |
| 106 | + } |
| 107 | + // TODO: Move the requirement for a selction to basic-item-selection. This |
| 108 | + // should ideally pick the nearest item to the previously-selected item. |
| 109 | + // if (this.collective.selectedItem === null && this.collective.items != null && this.collective.selectFirst) { |
| 110 | + // this.collective.selectFirst(); |
| 111 | + // } |
| 112 | + }, |
111 | 113 |
|
112 | 114 | // TODO: Seems like this ultimately belongs in Collective. |
113 | 115 | itemsChanged: function() { |
|
139 | 141 |
|
140 | 142 | is: 'basic-page-dots', |
141 | 143 |
|
| 144 | + properties: { |
| 145 | + |
| 146 | + selectedIndex: { |
| 147 | + type: Number |
| 148 | + }, |
| 149 | + |
| 150 | + selectedItem: { |
| 151 | + type: Object |
| 152 | + } |
| 153 | + |
| 154 | + }, |
| 155 | + |
142 | 156 | ready: function() { |
143 | 157 | this.$.dots.addEventListener('click', function(event) { |
144 | 158 | this.dotClick(event); |
|
153 | 167 | * @type Number |
154 | 168 | */ |
155 | 169 | get selectedIndex() { |
156 | | - return this.collective.selectedIndex; |
| 170 | + // See note at basic-item-selection's selectedIndex getter. |
| 171 | + if (this._readied) { |
| 172 | + return this.collective.selectedIndex; |
| 173 | + } |
157 | 174 | }, |
158 | 175 | set selectedIndex(index) { |
159 | | - this.collective.selectedIndex = index; |
| 176 | + if ('selectedIndex' in this.collective) { |
| 177 | + this.collective.selectedIndex = index; |
| 178 | + } else { |
| 179 | + // Selection is being made before the collective supports it. |
| 180 | + this._prematureSelectedIndex = index; |
| 181 | + } |
160 | 182 | }, |
161 | 183 |
|
162 | 184 | /** |
|
173 | 195 | }, |
174 | 196 |
|
175 | 197 | _createDots: function(count) { |
| 198 | + var selectedIndex = this.selectedIndex; |
176 | 199 | var dotContainer = this.$.dots; |
177 | | - var existingDots = dotContainer.children; |
178 | | - var existingDotCount = existingDots.length; |
179 | | - if (count == existingDots.length) { |
| 200 | + var existingDotCount = dotContainer.children.length; |
| 201 | + if (count === existingDotCount) { |
180 | 202 | return; |
181 | | - } else if (existingDots.length > count) { |
| 203 | + } else if (existingDotCount > count) { |
182 | 204 | // Remove extra dots. |
183 | | - for (var i = count; i < existingDotCount; i++) { |
184 | | - var existingDot = existingDots[i]; |
185 | | - Polymer.dom(dotContainer).removeChild(existingDot); |
| 205 | + while (dotContainer.children.length > count) { |
| 206 | + Polymer.dom(dotContainer).removeChild(dotContainer.children[0]); |
186 | 207 | } |
187 | 208 | } else { |
188 | 209 | // Create needed dots. |
189 | | - // var dotTemplate = this.$.dotTemplate; |
190 | 210 | for (var i = existingDotCount; i < count; i++) { |
191 | | - // var newDot = document.importNode(dotTemplate.content, true); |
192 | 211 | var newDot = document.createElement('div'); |
193 | 212 | newDot.classList.add('dot'); |
194 | 213 | newDot.classList.add('style-scope'); |
195 | 214 | newDot.classList.add('basic-page-dots'); |
| 215 | + newDot.classList.toggle('selected', i === selectedIndex); |
196 | 216 | dotContainer.appendChild(newDot); |
197 | 217 | } |
198 | 218 | } |
199 | | - } |
| 219 | + }, |
| 220 | + |
| 221 | + // Tracks selection before the collective supports it. |
| 222 | + _prematureSelectedIndex: null |
200 | 223 |
|
201 | 224 | }); |
202 | 225 | </script> |
0 commit comments