| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 function Gallery(container, closeCallback) { | 5 function Gallery(container, closeCallback) { |
| 6 this.container_ = container; | 6 this.container_ = container; |
| 7 this.document_ = container.ownerDocument; | 7 this.document_ = container.ownerDocument; |
| 8 this.editing_ = false; | 8 this.editing_ = false; |
| 9 this.closeCallback_ = closeCallback; | 9 this.closeCallback_ = closeCallback; |
| 10 this.onFadeTimeoutBound_ = this.onFadeTimeout_.bind(this); | 10 this.onFadeTimeoutBound_ = this.onFadeTimeout_.bind(this); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 this.toolbar_ = doc.createElement('div'); | 43 this.toolbar_ = doc.createElement('div'); |
| 44 this.toolbar_.className = 'toolbar'; | 44 this.toolbar_.className = 'toolbar'; |
| 45 this.container_.appendChild(this.toolbar_); | 45 this.container_.appendChild(this.toolbar_); |
| 46 | 46 |
| 47 this.ribbon_ = new Ribbon(this.toolbar_, | 47 this.ribbon_ = new Ribbon(this.toolbar_, |
| 48 this.onImageSelected_.bind(this)); | 48 this.onImageSelected_.bind(this)); |
| 49 | 49 |
| 50 this.editBar_ = doc.createElement('div'); | 50 this.editBar_ = doc.createElement('div'); |
| 51 this.editBar_.className = 'edit-bar'; | 51 this.editBar_.className = 'edit-bar'; |
| 52 this.editBar_.setAttribute('hidden', 'hidden'); |
| 52 this.toolbar_.appendChild(this.editBar_); | 53 this.toolbar_.appendChild(this.editBar_); |
| 53 | 54 |
| 54 this.editButton_ = doc.createElement('div'); | 55 this.editButton_ = doc.createElement('div'); |
| 55 this.editButton_.className = 'button'; | 56 this.editButton_.className = 'button edit'; |
| 56 this.editButton_.textContent = Gallery.EDIT_LABEL; | 57 this.editButton_.textContent = Gallery.EDIT_LABEL; |
| 57 this.editButton_.addEventListener('click', this.onEdit_.bind(this)); | 58 this.editButton_.addEventListener('click', this.onEdit_.bind(this)); |
| 58 this.toolbar_.appendChild(this.editButton_); | 59 this.toolbar_.appendChild(this.editButton_); |
| 59 | 60 |
| 60 this.shareButton_ = doc.createElement('div'); | 61 this.shareButton_ = doc.createElement('div'); |
| 61 this.shareButton_.className = 'button'; | 62 this.shareButton_.className = 'button share'; |
| 62 this.shareButton_.textContent = Gallery.SHARE_LABEL; | 63 this.shareButton_.textContent = Gallery.SHARE_LABEL; |
| 63 this.shareButton_.addEventListener('click', this.onShare_.bind(this)); | 64 this.shareButton_.addEventListener('click', this.onShare_.bind(this)); |
| 64 this.toolbar_.appendChild(this.shareButton_); | 65 this.toolbar_.appendChild(this.shareButton_); |
| 65 | 66 |
| 66 this.editor_ = new ImageEditor(this.imageContainer_, this.editBar_, | 67 this.editor_ = new ImageEditor(this.imageContainer_, this.editBar_, |
| 67 this.onSave_.bind(this), null /*closeCallback*/); | 68 this.onSave_.bind(this), null /*closeCallback*/); |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 Gallery.prototype.load = function(parentDirEntry, entries) { | 71 Gallery.prototype.load = function(parentDirEntry, entries) { |
| 71 this.editBar_.setAttribute('hidden', 'hidden'); | 72 this.editBar_.setAttribute('hidden', 'hidden'); |
| 73 // Firstchild is the toolbar with buttons, which should be hidden at start. |
| 74 this.editBar_.firstChild.setAttribute('hidden', 'hidden'); |
| 72 this.editButton_.removeAttribute('pressed'); | 75 this.editButton_.removeAttribute('pressed'); |
| 73 this.shareButton_.removeAttribute('pressed'); | 76 this.shareButton_.removeAttribute('pressed'); |
| 77 this.toolbar_.removeAttribute('hidden'); |
| 78 this.editing_ = false; |
| 74 | 79 |
| 75 this.ribbon_.setEntries(entries); | 80 this.ribbon_.setEntries(entries); |
| 76 this.parentDirEntry_ = parentDirEntry; | 81 this.parentDirEntry_ = parentDirEntry; |
| 77 | 82 |
| 78 if (entries.length == 0) | 83 if (entries.length == 0) |
| 79 return; | 84 return; |
| 80 | 85 |
| 81 this.ribbon_.select(0); | 86 this.ribbon_.select(0); |
| 82 this.editor_.resizeFrame(); | 87 this.editor_.resizeFrame(); |
| 83 | 88 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 }, onError); | 120 }, onError); |
| 116 }, onError); | 121 }, onError); |
| 117 }; | 122 }; |
| 118 | 123 |
| 119 Gallery.prototype.onImageSelected_ = function(entry) { | 124 Gallery.prototype.onImageSelected_ = function(entry) { |
| 120 this.entry_ = entry; | 125 this.entry_ = entry; |
| 121 this.editor_.load(entry.toURL()); | 126 this.editor_.load(entry.toURL()); |
| 122 }; | 127 }; |
| 123 | 128 |
| 124 Gallery.prototype.onEdit_ = function(event) { | 129 Gallery.prototype.onEdit_ = function(event) { |
| 125 // TODO(dgozman): nice animation. | 130 var self = this; |
| 126 if (this.editing_) { | 131 if (this.editing_) { |
| 127 this.editBar_.setAttribute('hidden', 'hidden'); | 132 this.editBar_.setAttribute('hidden', 'hidden'); |
| 128 this.editButton_.removeAttribute('pressed'); | 133 this.editButton_.removeAttribute('pressed'); |
| 129 this.editing_ = false; | 134 this.editing_ = false; |
| 130 this.initiateFading_(); | 135 this.initiateFading_(); |
| 136 window.setTimeout(function() { |
| 137 // Hide the toolbar, so it will not overlap with buttons. |
| 138 self.editBar_.firstChild.setAttribute('hidden', 'hidden'); |
| 139 self.ribbon_.redraw(); |
| 140 }, 500); |
| 131 } else { | 141 } else { |
| 132 this.editBar_.removeAttribute('hidden'); | 142 this.cancelFading_(); |
| 133 this.editButton_.setAttribute('pressed', 'pressed'); | 143 // Show the toolbar. |
| 134 this.editing_ = true; | 144 this.editBar_.firstChild.removeAttribute('hidden'); |
| 145 // Use setTimeout, so computed style will be recomputed. |
| 146 window.setTimeout(function() { |
| 147 self.editBar_.removeAttribute('hidden'); |
| 148 self.editButton_.setAttribute('pressed', 'pressed'); |
| 149 self.editing_ = true; |
| 150 self.ribbon_.redraw(); |
| 151 }, 0); |
| 135 } | 152 } |
| 136 this.ribbon_.redraw(); | |
| 137 }; | 153 }; |
| 138 | 154 |
| 139 Gallery.prototype.onShare_ = function(event) { | 155 Gallery.prototype.onShare_ = function(event) { |
| 140 // TODO(dgozman): implement. | 156 // TODO(dgozman): implement. |
| 141 }; | 157 }; |
| 142 | 158 |
| 143 Gallery.prototype.onKeyDown_ = function(event) { | 159 Gallery.prototype.onKeyDown_ = function(event) { |
| 144 if (this.editing_) | 160 if (this.editing_) |
| 145 return; | 161 return; |
| 146 switch (event.keyIdentifier) { | 162 switch (event.keyIdentifier) { |
| 147 case 'Home': | 163 case 'Home': |
| 148 this.ribbon_.scrollToFirst(); | 164 this.ribbon_.scrollToFirst(); |
| 149 break; | 165 break; |
| 150 case 'Left': | 166 case 'Left': |
| 151 this.ribbon_.scrollLeft(); | 167 this.ribbon_.scrollLeft(); |
| 152 break; | 168 break; |
| 153 case 'Right': | 169 case 'Right': |
| 154 this.ribbon_.scrollRight(); | 170 this.ribbon_.scrollRight(); |
| 155 break; | 171 break; |
| 156 case 'End': | 172 case 'End': |
| 157 this.ribbon_.scrollToLast(); | 173 this.ribbon_.scrollToLast(); |
| 158 break; | 174 break; |
| 159 } | 175 } |
| 160 }; | 176 }; |
| 161 | 177 |
| 162 Gallery.prototype.onMouseMove_ = function(e) { | 178 Gallery.prototype.onMouseMove_ = function(e) { |
| 163 if (this.fadeTimeoutId_) { | 179 this.cancelFading_(); |
| 164 window.clearTimeout(this.fadeTimeoutId_); | |
| 165 } | |
| 166 this.toolbar_.removeAttribute('hidden'); | 180 this.toolbar_.removeAttribute('hidden'); |
| 167 this.initiateFading_(); | 181 this.initiateFading_(); |
| 168 }; | 182 }; |
| 169 | 183 |
| 170 Gallery.prototype.onFadeTimeout_ = function() { | 184 Gallery.prototype.onFadeTimeout_ = function() { |
| 171 this.fadeTimeoutId_ = null; | 185 this.fadeTimeoutId_ = null; |
| 172 this.toolbar_.setAttribute('hidden', 'hidden'); | 186 this.toolbar_.setAttribute('hidden', 'hidden'); |
| 173 }; | 187 }; |
| 174 | 188 |
| 175 Gallery.prototype.initiateFading_ = function() { | 189 Gallery.prototype.initiateFading_ = function() { |
| 176 if (this.editing_) { | 190 if (this.editing_ || this.fadeTimeoutId_) { |
| 177 return; | 191 return; |
| 178 } | 192 } |
| 179 this.fadeTimeoutId_ = window.setTimeout( | 193 this.fadeTimeoutId_ = window.setTimeout( |
| 180 this.onFadeTimeoutBound_, Gallery.FADE_TIMEOUT); | 194 this.onFadeTimeoutBound_, Gallery.FADE_TIMEOUT); |
| 181 }; | 195 }; |
| 182 | 196 |
| 197 Gallery.prototype.cancelFading_ = function() { |
| 198 if (this.fadeTimeoutId_) { |
| 199 window.clearTimeout(this.fadeTimeoutId_); |
| 200 this.fadeTimeoutId_ = null; |
| 201 } |
| 202 }; |
| 203 |
| 183 function Ribbon(parentNode, onSelect) { | 204 function Ribbon(parentNode, onSelect) { |
| 184 this.container_ = parentNode; | 205 this.container_ = parentNode; |
| 185 this.document_ = parentNode.ownerDocument; | 206 this.document_ = parentNode.ownerDocument; |
| 186 | 207 |
| 208 this.left_ = this.document_.createElement('div'); |
| 209 this.left_.className = 'ribbon-left'; |
| 210 this.left_.addEventListener('click', this.scrollLeft.bind(this)); |
| 211 this.container_.appendChild(this.left_); |
| 212 |
| 187 this.bar_ = this.document_.createElement('div'); | 213 this.bar_ = this.document_.createElement('div'); |
| 188 this.bar_.className = 'ribbon'; | 214 this.bar_.className = 'ribbon'; |
| 189 this.container_.appendChild(this.bar_); | 215 this.container_.appendChild(this.bar_); |
| 190 | 216 |
| 191 this.left_ = this.document_.createElement('div'); | |
| 192 this.left_.className = 'ribbon-left'; | |
| 193 this.left_.addEventListener('click', this.scrollLeft.bind(this)); | |
| 194 | |
| 195 this.right_ = this.document_.createElement('div'); | 217 this.right_ = this.document_.createElement('div'); |
| 196 this.right_.className = 'ribbon-right'; | 218 this.right_.className = 'ribbon-right'; |
| 197 this.right_.addEventListener('click', this.scrollRight.bind(this)); | 219 this.right_.addEventListener('click', this.scrollRight.bind(this)); |
| 220 this.container_.appendChild(this.right_); |
| 198 | 221 |
| 199 this.spacer_ = this.document_.createElement('div'); | 222 this.spacer_ = this.document_.createElement('div'); |
| 200 this.spacer_.className = 'ribbon-spacer'; | 223 this.spacer_.className = 'ribbon-spacer'; |
| 201 | 224 |
| 202 this.onSelect_ = onSelect; | 225 this.onSelect_ = onSelect; |
| 203 this.entries_ = []; | 226 this.entries_ = []; |
| 204 this.images_ = []; | 227 this.images_ = []; |
| 205 this.selectedIndex_ = -1; | 228 this.selectedIndex_ = -1; |
| 206 this.firstIndex_ = 0; | 229 this.firstIndex_ = 0; |
| 207 } | 230 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 this.selectedIndex_ = index; | 268 this.selectedIndex_ = index; |
| 246 this.images_[this.selectedIndex_].setAttribute('selected', 'selected'); | 269 this.images_[this.selectedIndex_].setAttribute('selected', 'selected'); |
| 247 if (this.onSelect_) | 270 if (this.onSelect_) |
| 248 this.onSelect_(this.entries_[index]); | 271 this.onSelect_(this.entries_[index]); |
| 249 }; | 272 }; |
| 250 | 273 |
| 251 Ribbon.prototype.redraw = function() { | 274 Ribbon.prototype.redraw = function() { |
| 252 this.bar_.textContent = ''; | 275 this.bar_.textContent = ''; |
| 253 | 276 |
| 254 // TODO(dgozman): get rid of these constants. | 277 // TODO(dgozman): get rid of these constants. |
| 255 var itemWidth = 80; | 278 var itemWidth = 49; |
| 256 var width = this.bar_.clientWidth - 40; | 279 var width = this.bar_.clientWidth - 40; |
| 257 | 280 |
| 258 var fit = Math.round(Math.floor(width / itemWidth)); | 281 var fit = Math.round(Math.floor(width / itemWidth)); |
| 259 var lastIndex = Math.min(this.entries_.length, this.firstIndex_ + fit); | 282 var lastIndex = Math.min(this.entries_.length, this.firstIndex_ + fit); |
| 260 this.firstIndex_ = Math.max(0, lastIndex - fit); | 283 this.firstIndex_ = Math.max(0, lastIndex - fit); |
| 261 this.bar_.appendChild(this.left_); | |
| 262 for (var index = this.firstIndex_; index < lastIndex; ++index) { | 284 for (var index = this.firstIndex_; index < lastIndex; ++index) { |
| 263 this.bar_.appendChild(this.images_[index]); | 285 this.bar_.appendChild(this.images_[index]); |
| 264 } | 286 } |
| 265 this.bar_.appendChild(this.spacer_); | 287 this.bar_.appendChild(this.spacer_); |
| 266 this.bar_.appendChild(this.right_); | |
| 267 }; | 288 }; |
| 268 | 289 |
| 269 Ribbon.prototype.scrollLeft = function() { | 290 Ribbon.prototype.scrollLeft = function() { |
| 270 if (this.firstIndex_ > 0) { | 291 if (this.firstIndex_ > 0) { |
| 271 this.firstIndex_--; | 292 this.firstIndex_--; |
| 272 this.redraw(); | 293 this.redraw(); |
| 273 } | 294 } |
| 274 }; | 295 }; |
| 275 | 296 |
| 276 Ribbon.prototype.scrollRight = function() { | 297 Ribbon.prototype.scrollRight = function() { |
| 277 if (this.firstIndex_ < this.entries_.length - 1) { | 298 if (this.firstIndex_ < this.entries_.length - 1) { |
| 278 this.firstIndex_++; | 299 this.firstIndex_++; |
| 279 this.redraw(); | 300 this.redraw(); |
| 280 } | 301 } |
| 281 }; | 302 }; |
| 282 | 303 |
| 283 Ribbon.prototype.scrollToFirst = function() { | 304 Ribbon.prototype.scrollToFirst = function() { |
| 284 if (this.firstIndex_ > 0) { | 305 if (this.firstIndex_ > 0) { |
| 285 this.firstIndex_ = 0; | 306 this.firstIndex_ = 0; |
| 286 this.redraw(); | 307 this.redraw(); |
| 287 } | 308 } |
| 288 }; | 309 }; |
| 289 | 310 |
| 290 Ribbon.prototype.scrollToLast = function() { | 311 Ribbon.prototype.scrollToLast = function() { |
| 291 if (this.firstIndex_ < this.entries_.length - 1) { | 312 if (this.firstIndex_ < this.entries_.length - 1) { |
| 292 this.firstIndex_ = this.entries_.length - 1; | 313 this.firstIndex_ = this.entries_.length - 1; |
| 293 this.redraw(); | 314 this.redraw(); |
| 294 } | 315 } |
| 295 }; | 316 }; |
| OLD | NEW |