OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 * Scrollable thumbnail ribbon at the bottom of the Gallery in the Slide mode. | 6 * Scrollable thumbnail ribbon at the bottom of the Gallery in the Slide mode. |
7 * | 7 * |
8 * @param {Document} document Document. | 8 * @param {Document} document Document. |
9 * @param {MetadataCache} metadataCache MetadataCache instance. | 9 * @param {MetadataCache} metadataCache MetadataCache instance. |
10 * @param {function(number)} selectFunc Selection function. | 10 * @param {cr.ui.ArrayDataModel} dataModel Data model. |
11 * @param {cr.ui.ListSelectionModel} selectionModel Selection model. | |
11 * @return {Element} Ribbon element. | 12 * @return {Element} Ribbon element. |
12 * @constructor | 13 * @constructor |
13 */ | 14 */ |
14 function Ribbon(document, metadataCache, selectFunc) { | 15 function Ribbon(document, metadataCache, dataModel, selectionModel) { |
15 var self = document.createElement('div'); | 16 var self = document.createElement('div'); |
16 Ribbon.decorate(self, metadataCache, selectFunc); | 17 Ribbon.decorate(self, metadataCache, dataModel, selectionModel); |
17 return self; | 18 return self; |
18 } | 19 } |
19 | 20 |
20 /** | 21 /** |
21 * Inherit from HTMLDivElement. | 22 * Inherit from HTMLDivElement. |
22 */ | 23 */ |
23 Ribbon.prototype.__proto__ = HTMLDivElement.prototype; | 24 Ribbon.prototype.__proto__ = HTMLDivElement.prototype; |
24 | 25 |
25 /** | 26 /** |
26 * Decorate a Ribbon instance. | 27 * Decorate a Ribbon instance. |
27 * | 28 * |
28 * @param {Ribbon} self Self pointer. | 29 * @param {Ribbon} self Self pointer. |
29 * @param {MetadataCache} metadataCache MetadataCache instance. | 30 * @param {MetadataCache} metadataCache MetadataCache instance. |
30 * @param {function(number)} selectFunc Selection function. | 31 * @param {cr.ui.ArrayDataModel} dataModel Data model. |
32 * @param {cr.ui.ListSelectionModel} selectionModel Selection model. | |
31 */ | 33 */ |
32 Ribbon.decorate = function(self, metadataCache, selectFunc) { | 34 Ribbon.decorate = function(self, metadataCache, dataModel, selectionModel) { |
33 self.__proto__ = Ribbon.prototype; | 35 self.__proto__ = Ribbon.prototype; |
34 self.metadataCache_ = metadataCache; | 36 self.metadataCache_ = metadataCache; |
35 self.selectFunc_ = selectFunc; | 37 self.dataModel_ = dataModel; |
38 self.selectionModel_ = selectionModel; | |
36 | 39 |
37 self.className = 'ribbon'; | 40 self.className = 'ribbon'; |
38 | 41 |
39 self.firstVisibleIndex_ = 0; | |
40 self.lastVisibleIndex_ = -1; // Zero thumbnails | |
41 | |
42 self.renderCache_ = {}; | 42 self.renderCache_ = {}; |
43 }; | 43 }; |
44 | 44 |
45 /** | 45 /** |
46 * Max number of thumbnails in the ribbon. | 46 * Max number of thumbnails in the ribbon. |
47 * @type {Number} | 47 * @type {Number} |
48 */ | 48 */ |
49 Ribbon.ITEMS_COUNT = 5; | 49 Ribbon.ITEMS_COUNT = 5; |
50 | 50 |
51 /** | 51 /** |
52 * Update the ribbon. | 52 * Enable the ribbon. |
53 * | |
54 * @param {Array.<Gallery.Item>} items Array of items. | |
55 * @param {number} selectedIndex Selected index. | |
56 */ | 53 */ |
57 Ribbon.prototype.update = function(items, selectedIndex) { | 54 Ribbon.prototype.enable = function() { |
58 // Never show a single thumbnail. | 55 this.firstVisibleIndex_ = 0; |
59 if (items.length == 1) | 56 this.lastVisibleIndex_ = -1; // Zero thumbnails |
57 | |
58 this.onSpliceBound_ = this.onSplice_.bind(this); | |
59 this.dataModel_.addEventListener('splice', this.onSpliceBound_); | |
60 | |
61 this.onSelectionBound_ = this.onSelection_.bind(this); | |
62 this.selectionModel_.addEventListener('change', this.onSelectionBound_); | |
63 | |
64 this.onSelection_(); | |
65 }; | |
66 | |
67 /** | |
68 * Disable ribbon. | |
69 */ | |
70 Ribbon.prototype.disable = function() { | |
71 this.dataModel_.removeEventListener('splice', this.onSpliceBound_); | |
72 this.selectionModel_.removeEventListener('change', this.onSelectionBound_); | |
73 | |
74 this.removeVanishing_(); | |
75 this.textContent = ''; | |
76 }; | |
77 | |
78 /** | |
79 * Data model splice handler. | |
80 * @param {Event} event Event. | |
81 * @private | |
82 */ | |
83 Ribbon.prototype.onSplice_ = function(event) { | |
84 if (event.removed.length != 1) | |
60 return; | 85 return; |
dgozman
2012/08/27 13:23:42
console.error too?
Vladislav Kaznacheev
2012/08/27 14:49:08
Done.
| |
61 | 86 |
87 var removed = this.renderCache_[event.removed[0].getUrl()]; | |
88 if (!removed || !removed.parentNode || !removed.hasAttribute('selected')) { | |
89 console.error('Can only remove the selected item'); | |
90 return; | |
91 } | |
92 | |
93 var persistentNodes = this.querySelectorAll(':not([vanishing])'); | |
dgozman
2012/08/27 13:23:42
You need only immediate childs, don't you?
Vladislav Kaznacheev
2012/08/27 14:49:08
Yes of course! Fixed.
On 2012/08/27 13:23:42, dgoz
| |
94 if (this.lastVisibleIndex_ < this.dataModel_.length) { // Not at the end. | |
95 var lastNode = persistentNodes[persistentNodes.length - 1]; | |
96 if (lastNode.nextSibling) { | |
97 // Pull back a vanishing node from the right. | |
98 lastNode.nextSibling.removeAttribute('vanishing'); | |
99 } else { | |
100 // Push a new item at the right end. | |
101 this.appendChild(this.renderThumbnail_(this.lastVisibleIndex_)); | |
102 } | |
103 } else { | |
104 // No items to the right, move the window to the left. | |
105 this.lastVisibleIndex_--; | |
106 if (this.firstVisibleIndex_) { | |
107 this.firstVisibleIndex_--; | |
108 var firstNode = persistentNodes[0]; | |
109 if (firstNode.previousSibling) { | |
110 // Pull back a vanishing node from the left. | |
111 firstNode.previousSibling.removeAttribute('vanishing'); | |
112 } else { | |
113 // Push a new item at the left end. | |
114 var newThumbnail = this.renderThumbnail_(this.firstVisibleIndex_); | |
115 newThumbnail.style.marginLeft = -(this.clientHeight - 2) + 'px'; | |
116 this.insertBefore(newThumbnail, this.firstChild); | |
117 setTimeout(function() { | |
118 newThumbnail.style.marginLeft = '0'; | |
119 }, 0); | |
120 } | |
121 } | |
122 } | |
123 | |
124 removed.removeAttribute('selected'); | |
125 removed.setAttribute('vanishing', 'smooth'); | |
126 this.scheduleRemove_(); | |
127 }; | |
128 | |
129 /** | |
130 * Selection change handler. | |
131 * @private | |
132 */ | |
133 Ribbon.prototype.onSelection_ = function() { | |
134 var indexes = this.selectionModel_.selectedIndexes; | |
135 if (indexes.length == 0) | |
136 return; // Ignore temporary empty selection. | |
137 var selectedIndex = indexes[0]; | |
138 | |
139 var length = this.dataModel_.length; | |
140 | |
62 // TODO(dgozman): use margin instead of 2 here. | 141 // TODO(dgozman): use margin instead of 2 here. |
63 var itemWidth = this.clientHeight - 2; | 142 var itemWidth = this.clientHeight - 2; |
64 var fullItems = Ribbon.ITEMS_COUNT; | 143 var fullItems = Ribbon.ITEMS_COUNT; |
65 fullItems = Math.min(fullItems, items.length); | 144 fullItems = Math.min(fullItems, length); |
66 var right = Math.floor((fullItems - 1) / 2); | 145 var right = Math.floor((fullItems - 1) / 2); |
67 | 146 |
68 var fullWidth = fullItems * itemWidth; | 147 var fullWidth = fullItems * itemWidth; |
69 this.style.width = fullWidth + 'px'; | 148 this.style.width = fullWidth + 'px'; |
70 | 149 |
71 var lastIndex = selectedIndex + right; | 150 var lastIndex = selectedIndex + right; |
72 lastIndex = Math.max(lastIndex, fullItems - 1); | 151 lastIndex = Math.max(lastIndex, fullItems - 1); |
73 lastIndex = Math.min(lastIndex, items.length - 1); | 152 lastIndex = Math.min(lastIndex, length - 1); |
74 var firstIndex = lastIndex - fullItems + 1; | 153 var firstIndex = lastIndex - fullItems + 1; |
75 | 154 |
76 if (this.firstVisibleIndex_ != firstIndex || | 155 if (this.firstVisibleIndex_ != firstIndex || |
77 this.lastVisibleIndex_ != lastIndex) { | 156 this.lastVisibleIndex_ != lastIndex) { |
78 | 157 |
79 if (this.lastVisibleIndex_ == -1) { | 158 if (this.lastVisibleIndex_ == -1) { |
80 this.firstVisibleIndex_ = firstIndex; | 159 this.firstVisibleIndex_ = firstIndex; |
81 this.lastVisibleIndex_ = lastIndex; | 160 this.lastVisibleIndex_ = lastIndex; |
82 } | 161 } |
83 | 162 |
163 this.removeVanishing_(); | |
dgozman
2012/08/27 13:23:42
If you click right arrow with 100ms interval, this
Vladislav Kaznacheev
2012/08/27 14:49:08
The next line will remove all children from the ri
| |
164 | |
84 this.textContent = ''; | 165 this.textContent = ''; |
85 var startIndex = Math.min(firstIndex, this.firstVisibleIndex_); | 166 var startIndex = Math.min(firstIndex, this.firstVisibleIndex_); |
86 var toRemove = []; | |
87 // All the items except the first one treated equally. | 167 // All the items except the first one treated equally. |
88 for (var index = startIndex + 1; | 168 for (var index = startIndex + 1; |
89 index <= Math.max(lastIndex, this.lastVisibleIndex_); | 169 index <= Math.max(lastIndex, this.lastVisibleIndex_); |
90 ++index) { | 170 ++index) { |
91 var box = this.renderThumbnail_(index, items); | 171 // Only add items that are in either old or the new viewport. |
172 if (this.lastVisibleIndex_ < index && index < firstIndex || | |
173 lastIndex < index && index < this.firstVisibleIndex_) | |
174 continue; | |
175 var box = this.renderThumbnail_(index); | |
92 box.style.marginLeft = '0'; | 176 box.style.marginLeft = '0'; |
93 this.appendChild(box); | 177 this.appendChild(box); |
94 if (index < firstIndex || index > lastIndex) { | 178 if (index < firstIndex || index > lastIndex) { |
95 toRemove.push(box); | 179 // If the node is not in the new viewport we only need it while |
180 // the animation is playing out. | |
181 box.setAttribute('vanishing', 'slide'); | |
96 } | 182 } |
97 } | 183 } |
98 | 184 |
99 var margin = itemWidth * Math.abs(firstIndex - this.firstVisibleIndex_); | 185 var slideCount = this.childNodes.length + 1 - Ribbon.ITEMS_COUNT; |
100 var startBox = this.renderThumbnail_(startIndex, items); | 186 var margin = itemWidth * slideCount; |
187 var startBox = this.renderThumbnail_(startIndex); | |
101 if (startIndex == firstIndex) { | 188 if (startIndex == firstIndex) { |
102 // Sliding to the right. | 189 // Sliding to the right. |
103 startBox.style.marginLeft = -margin + 'px'; | 190 startBox.style.marginLeft = -margin + 'px'; |
104 if (this.firstChild) | 191 if (this.firstChild) |
105 this.insertBefore(startBox, this.firstChild); | 192 this.insertBefore(startBox, this.firstChild); |
106 else | 193 else |
107 this.appendChild(startBox); | 194 this.appendChild(startBox); |
108 setTimeout(function() { | 195 setTimeout(function() { |
109 startBox.style.marginLeft = '0'; | 196 startBox.style.marginLeft = '0'; |
110 }, 0); | 197 }, 0); |
111 } else { | 198 } else { |
112 // Sliding to the left. Start item will become invisible and should be | 199 // Sliding to the left. Start item will become invisible and should be |
113 // removed afterwards. | 200 // removed afterwards. |
114 toRemove.push(startBox); | 201 startBox.setAttribute('vanishing', 'slide'); |
115 startBox.style.marginLeft = '0'; | 202 startBox.style.marginLeft = '0'; |
116 if (this.firstChild) | 203 if (this.firstChild) |
117 this.insertBefore(startBox, this.firstChild); | 204 this.insertBefore(startBox, this.firstChild); |
118 else | 205 else |
119 this.appendChild(startBox); | 206 this.appendChild(startBox); |
120 setTimeout(function() { | 207 setTimeout(function() { |
121 startBox.style.marginLeft = -margin + 'px'; | 208 startBox.style.marginLeft = -margin + 'px'; |
122 }, 0); | 209 }, 0); |
123 } | 210 } |
124 | 211 |
125 ImageUtil.setClass(this, 'fade-left', | 212 ImageUtil.setClass(this, 'fade-left', |
126 firstIndex > 0 && selectedIndex != firstIndex); | 213 firstIndex > 0 && selectedIndex != firstIndex); |
127 | 214 |
128 ImageUtil.setClass(this, 'fade-right', | 215 ImageUtil.setClass(this, 'fade-right', |
129 lastIndex < items.length - 1 && selectedIndex != lastIndex); | 216 lastIndex < length - 1 && selectedIndex != lastIndex); |
130 | 217 |
131 this.firstVisibleIndex_ = firstIndex; | 218 this.firstVisibleIndex_ = firstIndex; |
132 this.lastVisibleIndex_ = lastIndex; | 219 this.lastVisibleIndex_ = lastIndex; |
133 | 220 |
134 if (this.removeTimeout_) | 221 this.scheduleRemove_(); |
135 clearTimeout(this.removeTimeout_); | |
136 | |
137 this.removeTimeout_ = setTimeout(function() { | |
138 this.removeTimeout_ = null; | |
139 for (var i = 0; i < toRemove.length; i++) { | |
140 var box = toRemove[i]; | |
141 if (box.parentNode == this) | |
142 this.removeChild(box); | |
143 } | |
144 }.bind(this), 200); | |
145 } | 222 } |
146 | 223 |
147 var oldSelected = this.querySelector('[selected]'); | 224 var oldSelected = this.querySelector('[selected]'); |
148 if (oldSelected) oldSelected.removeAttribute('selected'); | 225 if (oldSelected) oldSelected.removeAttribute('selected'); |
149 | 226 |
150 var newSelected = this.getThumbnail_(selectedIndex); | 227 var newSelected = |
228 this.renderCache_[this.dataModel_.item(selectedIndex).getUrl()]; | |
151 if (newSelected) newSelected.setAttribute('selected', true); | 229 if (newSelected) newSelected.setAttribute('selected', true); |
152 }; | 230 }; |
153 | 231 |
154 /** | 232 /** |
233 * Schedule the removal of thumbnails marked as vanishing. | |
234 * @private | |
235 */ | |
236 Ribbon.prototype.scheduleRemove_ = function() { | |
237 if (this.removeTimeout_) | |
238 clearTimeout(this.removeTimeout_); | |
239 | |
240 this.removeTimeout_ = setTimeout(function() { | |
241 this.removeTimeout_ = null; | |
242 this.removeVanishing_(); | |
243 }.bind(this), 200); | |
244 }; | |
245 | |
246 /** | |
247 * Remove all thumbnails marked as vanishing. | |
248 * @private | |
249 */ | |
250 Ribbon.prototype.removeVanishing_ = function() { | |
251 if (this.removeTimeout_) { | |
252 clearTimeout(this.removeTimeout_); | |
253 this.removeTimeout_ = 0; | |
254 } | |
255 var vanishingNodes = this.querySelectorAll('[vanishing]'); | |
256 for (var i = 0; i != vanishingNodes.length; i++) { | |
257 vanishingNodes[i].removeAttribute('vanishing'); | |
258 this.removeChild(vanishingNodes[i]); | |
259 } | |
260 }; | |
261 | |
262 /** | |
155 * Create a DOM element for a thumbnail. | 263 * Create a DOM element for a thumbnail. |
156 * | 264 * |
157 * @param {number} index Item index. | 265 * @param {number} index Item index. |
158 * @param {Array.<Gallery.Item>} items Items array. | |
159 * @return {Element} Newly created element. | 266 * @return {Element} Newly created element. |
160 * @private | 267 * @private |
161 */ | 268 */ |
162 Ribbon.prototype.renderThumbnail_ = function(index, items) { | 269 Ribbon.prototype.renderThumbnail_ = function(index) { |
163 var url = items[index].getUrl(); | 270 var item = this.dataModel_.item(index); |
271 var url = item.getUrl(); | |
164 | 272 |
165 var cached = this.renderCache_[url]; | 273 var cached = this.renderCache_[url]; |
166 if (cached) | 274 if (cached) |
167 return cached; | 275 return cached; |
168 | 276 |
169 var thumbnail = this.ownerDocument.createElement('div'); | 277 var thumbnail = this.ownerDocument.createElement('div'); |
170 thumbnail.className = 'ribbon-image'; | 278 thumbnail.className = 'ribbon-image'; |
171 thumbnail.addEventListener('click', this.selectFunc_.bind(null, index)); | 279 thumbnail.addEventListener('click', function() { |
172 thumbnail.setAttribute('index', index); | 280 var index = this.dataModel_.slice().indexOf(item); |
281 this.selectionModel_.unselectAll(); | |
282 this.selectionModel_.setIndexSelected(index, true); | |
283 }.bind(this)); | |
173 | 284 |
174 util.createChild(thumbnail, 'image-wrapper'); | 285 util.createChild(thumbnail, 'image-wrapper'); |
175 | 286 |
176 this.metadataCache_.get(url, Gallery.METADATA_TYPE, | 287 this.metadataCache_.get(url, Gallery.METADATA_TYPE, |
177 this.setThumbnailImage_.bind(this, thumbnail, url)); | 288 this.setThumbnailImage_.bind(this, thumbnail, url)); |
178 | 289 |
290 // TODO: Implement LRU eviction. | |
291 // Never evict the thumbnails that are currently in the DOM because we rely | |
292 // on this cache to find them by URL. | |
179 this.renderCache_[url] = thumbnail; | 293 this.renderCache_[url] = thumbnail; |
180 return thumbnail; | 294 return thumbnail; |
181 }; | 295 }; |
182 | 296 |
183 /** | 297 /** |
184 * Set the thumbnail image. | 298 * Set the thumbnail image. |
185 * | 299 * |
186 * @param {Element} thumbnail Thumbnail element | 300 * @param {Element} thumbnail Thumbnail element |
187 * @param {string} url Image url. | 301 * @param {string} url Image url. |
188 * @param {Object} metadata Metadata. | 302 * @param {Object} metadata Metadata. |
189 * @private | 303 * @private |
190 */ | 304 */ |
191 Ribbon.prototype.setThumbnailImage_ = function(thumbnail, url, metadata) { | 305 Ribbon.prototype.setThumbnailImage_ = function(thumbnail, url, metadata) { |
192 new ThumbnailLoader(url, metadata).load( | 306 new ThumbnailLoader(url, metadata).load( |
193 thumbnail.querySelector('.image-wrapper'), true /* fill */); | 307 thumbnail.querySelector('.image-wrapper'), true /* fill */); |
194 }; | 308 }; |
195 | 309 |
196 /** | 310 /** |
197 * Update the thumbnail image. | 311 * Update the thumbnail image. |
198 * | 312 * |
199 * @param {number} index Item index. | |
200 * @param {string} url Image url. | 313 * @param {string} url Image url. |
201 * @param {Object} metadata Metadata. | 314 * @param {Object} metadata Metadata. |
202 */ | 315 */ |
203 Ribbon.prototype.updateThumbnail = function(index, url, metadata) { | 316 Ribbon.prototype.updateThumbnail = function(url, metadata) { |
204 var thumbnail = this.getThumbnail_(index) || this.renderCache_[url]; | 317 var thumbnail = this.renderCache_[url]; |
205 if (thumbnail) | 318 if (thumbnail) |
206 this.setThumbnailImage_(thumbnail, url, metadata); | 319 this.setThumbnailImage_(thumbnail, url, metadata); |
207 }; | 320 }; |
208 | 321 |
209 /** | 322 /** |
210 * @param {number} index Thumbnail index. | |
211 * @return {Element} Thumbnail element or null if not rendered. | |
212 * @private | |
213 */ | |
214 Ribbon.prototype.getThumbnail_ = function(index) { | |
215 return this.querySelector('[index="' + index + '"]'); | |
216 }; | |
217 | |
218 /** | |
219 * Update the thumbnail element cache. | 323 * Update the thumbnail element cache. |
220 * | 324 * |
221 * @param {string} oldUrl Old url. | 325 * @param {string} oldUrl Old url. |
222 * @param {string} newUrl New url. | 326 * @param {string} newUrl New url. |
223 */ | 327 */ |
224 Ribbon.prototype.remapCache = function(oldUrl, newUrl) { | 328 Ribbon.prototype.remapCache = function(oldUrl, newUrl) { |
225 if (oldUrl != newUrl && (oldUrl in this.renderCache_)) { | 329 if (oldUrl != newUrl && (oldUrl in this.renderCache_)) { |
226 this.renderCache_[newUrl] = this.renderCache_[oldUrl]; | 330 this.renderCache_[newUrl] = this.renderCache_[oldUrl]; |
227 delete this.renderCache_[oldUrl]; | 331 delete this.renderCache_[oldUrl]; |
228 } | 332 } |
229 }; | 333 }; |
OLD | NEW |