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 this.editBar_.firstChild.setAttribute('hidden', 'hidden'); | |
Vladislav Kaznacheev
2011/09/14 10:50:39
Comment what the firstChild really is?
dgozman
2011/09/14 11:31:31
Done.
| |
72 this.editButton_.removeAttribute('pressed'); | 74 this.editButton_.removeAttribute('pressed'); |
73 this.shareButton_.removeAttribute('pressed'); | 75 this.shareButton_.removeAttribute('pressed'); |
76 this.toolbar_.removeAttribute('hidden'); | |
77 this.editing_ = false; | |
74 | 78 |
75 this.ribbon_.setEntries(entries); | 79 this.ribbon_.setEntries(entries); |
76 this.parentDirEntry_ = parentDirEntry; | 80 this.parentDirEntry_ = parentDirEntry; |
77 | 81 |
78 if (entries.length == 0) | 82 if (entries.length == 0) |
79 return; | 83 return; |
80 | 84 |
81 this.ribbon_.select(0); | 85 this.ribbon_.select(0); |
82 this.editor_.resizeFrame(); | 86 this.editor_.resizeFrame(); |
83 | 87 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 }, onError); | 119 }, onError); |
116 }, onError); | 120 }, onError); |
117 }; | 121 }; |
118 | 122 |
119 Gallery.prototype.onImageSelected_ = function(entry) { | 123 Gallery.prototype.onImageSelected_ = function(entry) { |
120 this.entry_ = entry; | 124 this.entry_ = entry; |
121 this.editor_.load(entry.toURL()); | 125 this.editor_.load(entry.toURL()); |
122 }; | 126 }; |
123 | 127 |
124 Gallery.prototype.onEdit_ = function(event) { | 128 Gallery.prototype.onEdit_ = function(event) { |
125 // TODO(dgozman): nice animation. | 129 var self = this; |
126 if (this.editing_) { | 130 if (this.editing_) { |
127 this.editBar_.setAttribute('hidden', 'hidden'); | 131 this.editBar_.setAttribute('hidden', 'hidden'); |
128 this.editButton_.removeAttribute('pressed'); | 132 this.editButton_.removeAttribute('pressed'); |
129 this.editing_ = false; | 133 this.editing_ = false; |
130 this.initiateFading_(); | 134 this.initiateFading_(); |
135 window.setTimeout(function() { | |
136 self.editBar_.firstChild.setAttribute('hidden', 'hidden'); | |
137 self.ribbon_.redraw(); | |
138 }, 500); | |
131 } else { | 139 } else { |
132 this.editBar_.removeAttribute('hidden'); | 140 this.cancelFading_(); |
133 this.editButton_.setAttribute('pressed', 'pressed'); | 141 this.editBar_.firstChild.removeAttribute('hidden'); |
134 this.editing_ = true; | 142 window.setTimeout(function() { |
143 self.editBar_.removeAttribute('hidden'); | |
144 self.editButton_.setAttribute('pressed', 'pressed'); | |
145 self.editing_ = true; | |
146 self.ribbon_.redraw(); | |
147 }, 0); | |
135 } | 148 } |
136 this.ribbon_.redraw(); | |
137 }; | 149 }; |
138 | 150 |
139 Gallery.prototype.onShare_ = function(event) { | 151 Gallery.prototype.onShare_ = function(event) { |
140 // TODO(dgozman): implement. | 152 // TODO(dgozman): implement. |
141 }; | 153 }; |
142 | 154 |
143 Gallery.prototype.onKeyDown_ = function(event) { | 155 Gallery.prototype.onKeyDown_ = function(event) { |
144 if (this.editing_) | 156 if (this.editing_) |
145 return; | 157 return; |
146 switch (event.keyIdentifier) { | 158 switch (event.keyIdentifier) { |
147 case 'Home': | 159 case 'Home': |
148 this.ribbon_.scrollToFirst(); | 160 this.ribbon_.scrollToFirst(); |
149 break; | 161 break; |
150 case 'Left': | 162 case 'Left': |
151 this.ribbon_.scrollLeft(); | 163 this.ribbon_.scrollLeft(); |
152 break; | 164 break; |
153 case 'Right': | 165 case 'Right': |
154 this.ribbon_.scrollRight(); | 166 this.ribbon_.scrollRight(); |
155 break; | 167 break; |
156 case 'End': | 168 case 'End': |
157 this.ribbon_.scrollToLast(); | 169 this.ribbon_.scrollToLast(); |
158 break; | 170 break; |
159 } | 171 } |
160 }; | 172 }; |
161 | 173 |
162 Gallery.prototype.onMouseMove_ = function(e) { | 174 Gallery.prototype.onMouseMove_ = function(e) { |
163 if (this.fadeTimeoutId_) { | 175 this.cancelFading_(); |
164 window.clearTimeout(this.fadeTimeoutId_); | |
165 } | |
166 this.toolbar_.removeAttribute('hidden'); | 176 this.toolbar_.removeAttribute('hidden'); |
167 this.initiateFading_(); | 177 this.initiateFading_(); |
168 }; | 178 }; |
169 | 179 |
170 Gallery.prototype.onFadeTimeout_ = function() { | 180 Gallery.prototype.onFadeTimeout_ = function() { |
171 this.fadeTimeoutId_ = null; | 181 this.fadeTimeoutId_ = null; |
172 this.toolbar_.setAttribute('hidden', 'hidden'); | 182 this.toolbar_.setAttribute('hidden', 'hidden'); |
173 }; | 183 }; |
174 | 184 |
175 Gallery.prototype.initiateFading_ = function() { | 185 Gallery.prototype.initiateFading_ = function() { |
176 if (this.editing_) { | 186 if (this.editing_ || this.fadeTimeoutId_) { |
177 return; | 187 return; |
178 } | 188 } |
179 this.fadeTimeoutId_ = window.setTimeout( | 189 this.fadeTimeoutId_ = window.setTimeout( |
180 this.onFadeTimeoutBound_, Gallery.FADE_TIMEOUT); | 190 this.onFadeTimeoutBound_, Gallery.FADE_TIMEOUT); |
181 }; | 191 }; |
182 | 192 |
193 Gallery.prototype.cancelFading_ = function() { | |
194 if (this.fadeTimeoutId_) { | |
195 window.clearTimeout(this.fadeTimeoutId_); | |
196 this.fadeTimeoutId_ = null; | |
197 } | |
198 }; | |
199 | |
183 function Ribbon(parentNode, onSelect) { | 200 function Ribbon(parentNode, onSelect) { |
184 this.container_ = parentNode; | 201 this.container_ = parentNode; |
185 this.document_ = parentNode.ownerDocument; | 202 this.document_ = parentNode.ownerDocument; |
186 | 203 |
204 this.left_ = this.document_.createElement('div'); | |
205 this.left_.className = 'ribbon-left'; | |
206 this.left_.addEventListener('click', this.scrollLeft.bind(this)); | |
207 this.container_.appendChild(this.left_); | |
208 | |
187 this.bar_ = this.document_.createElement('div'); | 209 this.bar_ = this.document_.createElement('div'); |
188 this.bar_.className = 'ribbon'; | 210 this.bar_.className = 'ribbon'; |
189 this.container_.appendChild(this.bar_); | 211 this.container_.appendChild(this.bar_); |
190 | 212 |
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'); | 213 this.right_ = this.document_.createElement('div'); |
196 this.right_.className = 'ribbon-right'; | 214 this.right_.className = 'ribbon-right'; |
197 this.right_.addEventListener('click', this.scrollRight.bind(this)); | 215 this.right_.addEventListener('click', this.scrollRight.bind(this)); |
216 this.container_.appendChild(this.right_); | |
198 | 217 |
199 this.spacer_ = this.document_.createElement('div'); | 218 this.spacer_ = this.document_.createElement('div'); |
200 this.spacer_.className = 'ribbon-spacer'; | 219 this.spacer_.className = 'ribbon-spacer'; |
201 | 220 |
202 this.onSelect_ = onSelect; | 221 this.onSelect_ = onSelect; |
203 this.entries_ = []; | 222 this.entries_ = []; |
204 this.images_ = []; | 223 this.images_ = []; |
205 this.selectedIndex_ = -1; | 224 this.selectedIndex_ = -1; |
206 this.firstIndex_ = 0; | 225 this.firstIndex_ = 0; |
207 } | 226 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 this.selectedIndex_ = index; | 264 this.selectedIndex_ = index; |
246 this.images_[this.selectedIndex_].setAttribute('selected', 'selected'); | 265 this.images_[this.selectedIndex_].setAttribute('selected', 'selected'); |
247 if (this.onSelect_) | 266 if (this.onSelect_) |
248 this.onSelect_(this.entries_[index]); | 267 this.onSelect_(this.entries_[index]); |
249 }; | 268 }; |
250 | 269 |
251 Ribbon.prototype.redraw = function() { | 270 Ribbon.prototype.redraw = function() { |
252 this.bar_.textContent = ''; | 271 this.bar_.textContent = ''; |
253 | 272 |
254 // TODO(dgozman): get rid of these constants. | 273 // TODO(dgozman): get rid of these constants. |
255 var itemWidth = 80; | 274 var itemWidth = 49; |
256 var width = this.bar_.clientWidth - 40; | 275 var width = this.bar_.clientWidth - 40; |
257 | 276 |
258 var fit = Math.round(Math.floor(width / itemWidth)); | 277 var fit = Math.round(Math.floor(width / itemWidth)); |
259 var lastIndex = Math.min(this.entries_.length, this.firstIndex_ + fit); | 278 var lastIndex = Math.min(this.entries_.length, this.firstIndex_ + fit); |
260 this.firstIndex_ = Math.max(0, lastIndex - fit); | 279 this.firstIndex_ = Math.max(0, lastIndex - fit); |
261 this.bar_.appendChild(this.left_); | |
262 for (var index = this.firstIndex_; index < lastIndex; ++index) { | 280 for (var index = this.firstIndex_; index < lastIndex; ++index) { |
263 this.bar_.appendChild(this.images_[index]); | 281 this.bar_.appendChild(this.images_[index]); |
264 } | 282 } |
265 this.bar_.appendChild(this.spacer_); | 283 this.bar_.appendChild(this.spacer_); |
266 this.bar_.appendChild(this.right_); | |
267 }; | 284 }; |
268 | 285 |
269 Ribbon.prototype.scrollLeft = function() { | 286 Ribbon.prototype.scrollLeft = function() { |
270 if (this.firstIndex_ > 0) { | 287 if (this.firstIndex_ > 0) { |
271 this.firstIndex_--; | 288 this.firstIndex_--; |
272 this.redraw(); | 289 this.redraw(); |
273 } | 290 } |
274 }; | 291 }; |
275 | 292 |
276 Ribbon.prototype.scrollRight = function() { | 293 Ribbon.prototype.scrollRight = function() { |
277 if (this.firstIndex_ < this.entries_.length - 1) { | 294 if (this.firstIndex_ < this.entries_.length - 1) { |
278 this.firstIndex_++; | 295 this.firstIndex_++; |
279 this.redraw(); | 296 this.redraw(); |
280 } | 297 } |
281 }; | 298 }; |
282 | 299 |
283 Ribbon.prototype.scrollToFirst = function() { | 300 Ribbon.prototype.scrollToFirst = function() { |
284 if (this.firstIndex_ > 0) { | 301 if (this.firstIndex_ > 0) { |
285 this.firstIndex_ = 0; | 302 this.firstIndex_ = 0; |
286 this.redraw(); | 303 this.redraw(); |
287 } | 304 } |
288 }; | 305 }; |
289 | 306 |
290 Ribbon.prototype.scrollToLast = function() { | 307 Ribbon.prototype.scrollToLast = function() { |
291 if (this.firstIndex_ < this.entries_.length - 1) { | 308 if (this.firstIndex_ < this.entries_.length - 1) { |
292 this.firstIndex_ = this.entries_.length - 1; | 309 this.firstIndex_ = this.entries_.length - 1; |
293 this.redraw(); | 310 this.redraw(); |
294 } | 311 } |
295 }; | 312 }; |
OLD | NEW |