OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * PreviewPanel UI class. | 8 * PreviewPanel UI class. |
9 * @param {HTMLElement} element DOM Element of preview panel. | 9 * @param {HTMLElement} element DOM Element of preview panel. |
10 * @param {PreviewPanel.VisibilityType} visibilityType Initial value of the | 10 * @param {PreviewPanel.VisibilityType} visibilityType Initial value of the |
11 * visibility type. | 11 * visibility type. |
12 * @param {string} currentPath Initial value of the current path. | 12 * @param {string} currentPath Initial value of the current path. |
13 * @param {MetadataCache} metadataCache Metadata cache. | |
13 * @constructor | 14 * @constructor |
14 * @extends {cr.EventTarget} | 15 * @extends {cr.EventTarget} |
15 */ | 16 */ |
16 var PreviewPanel = function(element, visibilityType, currentPath) { | 17 var PreviewPanel = function(element, |
18 visibilityType, | |
19 currentPath, | |
20 metadataCache) { | |
17 /** | 21 /** |
18 * The cached height of preview panel. | 22 * The cached height of preview panel. |
19 * @type {number} | 23 * @type {number} |
20 * @private | 24 * @private |
21 */ | 25 */ |
22 this.height_ = 0; | 26 this.height_ = 0; |
23 | 27 |
24 /** | 28 /** |
25 * Visiblity type of the preview panel. | 29 * Visiblity type of the preview panel. |
26 * @type {PreviewPanel.VisiblityType} | 30 * @type {PreviewPanel.VisiblityType} |
27 * @private | 31 * @private |
28 */ | 32 */ |
29 this.visibilityType_ = visibilityType; | 33 this.visibilityType_ = visibilityType; |
30 | 34 |
31 /** | 35 /** |
32 * Current path to be desplayed. | 36 * Current path to be desplayed. |
33 * @type {string} | 37 * @type {string} |
34 * @private | 38 * @private |
35 */ | 39 */ |
36 this.currentPath_ = currentPath || '/'; | 40 this.currentPath_ = currentPath || '/'; |
37 | 41 |
38 /** | 42 /** |
39 * Dom element of the preview panel. | 43 * Dom element of the preview panel. |
40 * @type {HTMLElement} | 44 * @type {HTMLElement} |
41 * @private | 45 * @private |
42 */ | 46 */ |
43 this.element_ = element; | 47 this.element_ = element; |
44 | 48 |
45 /** | 49 /** |
46 * @type {HTMLElement} | 50 * @type {PreviewPanel.Thumbnails} |
47 * @private | |
48 */ | 51 */ |
49 this.thumbnailElement_ = element.querySelector('.preview-thumbnails'); | 52 this.thumbnails = new PreviewPanel.Thumbnails( |
53 element.querySelector('.preview-thumbnails'), metadataCache); | |
50 | 54 |
51 /** | 55 /** |
52 * @type {HTMLElement} | 56 * @type {HTMLElement} |
53 * @private | 57 * @private |
54 */ | 58 */ |
55 this.summaryElement_ = element.querySelector('.preview-summary'); | 59 this.summaryElement_ = element.querySelector('.preview-summary'); |
56 | 60 |
57 /** | 61 /** |
58 * @type {PreviewPanel.CalculatingSizeLabel} | 62 * @type {PreviewPanel.CalculatingSizeLabel} |
59 * @private | 63 * @private |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 | 169 |
166 /** | 170 /** |
167 * Apply the selection and update the view of the preview panel. | 171 * Apply the selection and update the view of the preview panel. |
168 * @param {FileSelection} selection Selection to be applied. | 172 * @param {FileSelection} selection Selection to be applied. |
169 */ | 173 */ |
170 PreviewPanel.prototype.setSelection = function(selection) { | 174 PreviewPanel.prototype.setSelection = function(selection) { |
171 this.sequence_++; | 175 this.sequence_++; |
172 this.selection_ = selection; | 176 this.selection_ = selection; |
173 this.updateVisibility_(); | 177 this.updateVisibility_(); |
174 this.updatePreviewText_(); | 178 this.updatePreviewText_(); |
179 this.thumbnails.selection = selection; | |
175 }; | 180 }; |
176 | 181 |
177 /** | 182 /** |
178 * Update the visibility of the preview panel. | 183 * Update the visibility of the preview panel. |
179 * @private | 184 * @private |
180 */ | 185 */ |
181 PreviewPanel.prototype.updateVisibility_ = function() { | 186 PreviewPanel.prototype.updateVisibility_ = function() { |
182 // Get the new visibility value. | 187 // Get the new visibility value. |
183 var visibility = this.element_.getAttribute('visibility'); | 188 var visibility = this.element_.getAttribute('visibility'); |
184 var newVisible = null; | 189 var newVisible = null; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 * @private | 327 * @private |
323 */ | 328 */ |
324 PreviewPanel.CalculatingSizeLabel.prototype.onStep_ = function() { | 329 PreviewPanel.CalculatingSizeLabel.prototype.onStep_ = function() { |
325 var text = str('CALCULATING_SIZE'); | 330 var text = str('CALCULATING_SIZE'); |
326 for (var i = 0; i < ~~(this.count_ / 2) % 4; i++) { | 331 for (var i = 0; i < ~~(this.count_ / 2) % 4; i++) { |
327 text += '.'; | 332 text += '.'; |
328 } | 333 } |
329 this.element_.textContent = text; | 334 this.element_.textContent = text; |
330 this.count_++; | 335 this.count_++; |
331 }; | 336 }; |
337 | |
338 /** | |
339 * Thumbnails on the preview panel. | |
340 * @param {HTMLElement} element DOM Element of thumbnail container. | |
341 * @param {MetadataCache} metadataCache MetadataCache. | |
342 */ | |
343 PreviewPanel.Thumbnails = function(element, metadataCache) { | |
344 this.element_ = element; | |
345 this.metadataCache_ = metadataCache; | |
346 this.sequence_ = 0; | |
347 }; | |
348 | |
349 /** | |
350 * Maximium number of thumbnails. | |
351 * @const {number} | |
352 */ | |
353 PreviewPanel.Thumbnails.MAX_THUMBNAIL_COUNT = 4; | |
354 | |
355 /** | |
356 * Edge length of the thumbnail square. | |
357 * @const {number} | |
358 */ | |
359 PreviewPanel.Thumbnails.THUMBNAIL_SIZE = 35; | |
360 | |
361 /** | |
362 * Longer edge length of zoomed thumbnail rectangle. | |
363 * @const {number} | |
364 */ | |
365 PreviewPanel.Thumbnails.ZOOMED_THUMBNAIL_SIZE = 200; | |
366 | |
367 PreviewPanel.Thumbnails.prototype = { | |
368 /** | |
369 * Sets entries to be displayed in the view. | |
370 * @param {Array.<Entry>} value Entries. | |
371 */ | |
372 set selection(value) { | |
373 this.sequence_++; | |
374 this.loadThumbnails_(value); | |
375 } | |
376 }; | |
377 | |
378 /** | |
379 * Loads thumbnail images. | |
380 * @param {FileSelection} selection Selection containing entries that are | |
381 * sources of images. | |
382 * @private | |
383 */ | |
384 PreviewPanel.Thumbnails.prototype.loadThumbnails_ = function(selection) { | |
385 var entries = selection.entries; | |
386 this.element_.classList.remove('has-zoom'); | |
387 this.element_.innerText = ''; | |
388 var clickHandler = selection.tasks && | |
389 selection.tasks.executeDefault.bind(selection.tasks); | |
390 var length = Math.min(entries.length, | |
391 PreviewPanel.Thumbnails.MAX_THUMBNAIL_COUNT); | |
392 for (var i = 0; i < length; i++) { | |
393 // Create a box. | |
394 var box = this.element_.ownerDocument.createElement('div'); | |
395 box.style.zIndex = PreviewPanel.Thumbnails.MAX_THUMBNAIL_COUNT + 1 - i; | |
396 | |
397 // Load the image. | |
398 FileGrid.decorateThumbnailBox(box, | |
399 entries[i], | |
400 this.metadataCache_, | |
401 ThumbnailLoader.FillMode.FILL, | |
402 FileGrid.ThumbnailQuality.LOW, | |
403 i == 0 && length == 1 && | |
404 this.setZoomedImage_.bind(this)); | |
405 | |
406 // Register the click handler. | |
407 if (clickHandler) | |
408 box.addEventListener('click', clickHandler); | |
409 | |
410 // Append | |
411 this.element_.appendChild(box); | |
412 } | |
413 }; | |
414 | |
415 /** | |
416 * Create the zoomed version of image and set it to the DOM element to show the | |
417 * zoomed image. | |
418 * | |
419 * @param {Image} image Image to be source of the zoomed image. | |
420 * @param {transform} transform Transoformation to be applied to the image. | |
421 * @private | |
422 */ | |
423 PreviewPanel.Thumbnails.prototype.setZoomedImage_ = | |
424 function(image, transform) { | |
425 if (!image) | |
426 return; | |
427 var width = image.width; | |
428 var height = image.height; | |
yoshiki
2013/09/10 01:24:19
Shouldn't we check width=0 or height=0?
hirono
2013/09/10 01:44:17
Done.
| |
429 if (width < PreviewPanel.Thumbnails.THUMBNAIL_SIZE * 2 && | |
430 height < PreviewPanel.Thumbnails.THUMBNAIL_SIZE * 2) | |
431 return; | |
432 | |
433 var scale = Math.min(1, | |
434 PreviewPanel.Thumbnails.ZOOMED_THUMBNAIL_SIZE / | |
435 Math.max(width, height)); | |
436 var imageWidth = ~~(width * scale); | |
437 var imageHeight = ~~(height * scale); | |
438 var zoomedImage = this.element_.ownerDocument.createElement('img'); | |
439 | |
440 if (scale < 0.3) { | |
441 // Scaling large images kills animation. Downscale it in advance. | |
442 // Canvas scales images with liner interpolation. Make a larger | |
443 // image (but small enough to not kill animation) and let IMAGE | |
444 // scale it smoothly. | |
445 var INTERMEDIATE_SCALE = 3; | |
446 var canvas = this.fileManager_.document_.createElement('canvas'); | |
yoshiki
2013/09/10 01:24:19
You can't access 'document_' in fileManager, since
hirono
2013/09/10 01:44:17
Done.
| |
447 canvas.width = imageWidth * INTERMEDIATE_SCALE; | |
448 canvas.height = imageHeight * INTERMEDIATE_SCALE; | |
449 var ctx = canvas.getContext('2d'); | |
450 ctx.drawImage(image, 0, 0, canvas.width, canvas.height); | |
451 // Using bigger than default compression reduces image size by | |
452 // several times. Quality degradation compensated by greater resolution. | |
453 zoomedImage.src = canvas.toDataURL('image/jpeg', 0.6); | |
454 } else { | |
455 zoomedImage.src = image.src; | |
456 } | |
457 | |
458 var boxWidth = Math.max(PreviewPanel.Thumbnails.THUMBNAIL_SIZE, imageWidth); | |
459 var boxHeight = Math.max(PreviewPanel.Thumbnails.THUMBNAIL_SIZE, imageHeight); | |
460 if (transform && transform.rotate90 % 2 == 1) { | |
461 var t = boxWidth; | |
462 boxWidth = boxHeight; | |
463 boxHeight = t; | |
464 } | |
465 | |
466 util.applyTransform(zoomedImage, transform); | |
467 | |
468 var zoomedBox = this.element_.ownerDocument.createElement('div'); | |
469 zoomedBox.className = 'popup'; | |
470 zoomedBox.style.width = boxWidth + 'px'; | |
471 zoomedBox.style.height = boxHeight + 'px'; | |
472 zoomedBox.appendChild(zoomedImage); | |
473 | |
474 this.element_.appendChild(zoomedBox); | |
475 this.element_.classList.add('has-zoom'); | |
476 return; | |
477 }; | |
OLD | NEW |