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 * @param {string} imageUrl Image URL | 6 * @param {string} url File URL. |
7 * @param {Object} opt_metadata Metadata object | 7 * @param {Object} opt_metadata Metadata object. |
| 8 * @param {string} opt_mediaType Media type. |
8 * @constructor | 9 * @constructor |
9 */ | 10 */ |
10 function ThumbnailLoader(imageUrl, opt_metadata) { | 11 function ThumbnailLoader(url, opt_metadata, opt_mediaType) { |
11 var genericIconUrl = FileType.getPreviewArt(FileType.getMediaType(imageUrl)); | 12 this.mediaType_ = opt_mediaType || FileType.getMediaType(url); |
12 if (opt_metadata && opt_metadata.gdata) { | 13 |
| 14 if (!opt_metadata) { |
| 15 this.thumbnailUrl_ = url; // Use the URL directly. |
| 16 return; |
| 17 } |
| 18 |
| 19 if (opt_metadata.gdata) { |
13 var apps = opt_metadata.gdata.driveApps; | 20 var apps = opt_metadata.gdata.driveApps; |
14 if (apps.length > 0 && apps[0].docIcon) { | 21 if (apps.length > 0 && apps[0].docIcon) { |
15 genericIconUrl = apps[0].docIcon; | 22 this.fallbackUrl_ = apps[0].docIcon; |
16 } | 23 } |
17 } | 24 } |
18 | 25 |
19 if (!opt_metadata) { | 26 if (opt_metadata.thumbnail && opt_metadata.thumbnail.url) { |
20 this.thumbnailUrl_ = imageUrl; | |
21 } else if (opt_metadata.thumbnail && opt_metadata.thumbnail.url) { | |
22 this.thumbnailUrl_ = opt_metadata.thumbnail.url; | 27 this.thumbnailUrl_ = opt_metadata.thumbnail.url; |
23 this.fallbackUrl_ = genericIconUrl; | |
24 this.transform_ = opt_metadata.thumbnail.transform; | 28 this.transform_ = opt_metadata.thumbnail.transform; |
25 } else if (FileType.isImage(imageUrl) && | 29 } else if (FileType.isImage(url) && |
26 ThumbnailLoader.canUseImageUrl_(opt_metadata)) { | 30 ThumbnailLoader.canUseImageUrl_(opt_metadata)) { |
27 this.thumbnailUrl_ = imageUrl; | 31 this.thumbnailUrl_ = url; |
28 this.fallbackUrl_ = genericIconUrl; | |
29 this.transform_ = opt_metadata.media && opt_metadata.media.imageTransform; | 32 this.transform_ = opt_metadata.media && opt_metadata.media.imageTransform; |
30 } else { | 33 } else if (this.fallbackUrl_) { |
31 this.thumbnailUrl_ = genericIconUrl; | 34 // Use fallback as the primary thumbnail. |
32 } | 35 this.thumbnailUrl_ = this.fallbackUrl_; |
| 36 this.fallbackUrl_ = null; |
| 37 } // else the generic thumbnail based on the media type will be used. |
33 } | 38 } |
34 | 39 |
35 /** | 40 /** |
36 * Files with more pixels won't have thumbnails. | 41 * Files with more pixels won't have thumbnails. |
37 */ | 42 */ |
38 ThumbnailLoader.MAX_PIXEL_COUNT = 1 << 21; // 2 MPix | 43 ThumbnailLoader.MAX_PIXEL_COUNT = 1 << 21; // 2 MPix |
39 | 44 |
40 /** | 45 /** |
41 * Files of bigger size won't have thumbnails. | 46 * Files of bigger size won't have thumbnails. |
42 */ | 47 */ |
(...skipping 19 matching lines...) Expand all Loading... |
62 /** | 67 /** |
63 * | 68 * |
64 * @param {HTMLElement} box Container element. | 69 * @param {HTMLElement} box Container element. |
65 * @param {boolean} fill True if fill, false if fit. | 70 * @param {boolean} fill True if fill, false if fit. |
66 * @param {function(HTMLImageElement, object} opt_onSuccess Success callback, | 71 * @param {function(HTMLImageElement, object} opt_onSuccess Success callback, |
67 * accepts the image and the transform. | 72 * accepts the image and the transform. |
68 * @param {function} opt_onError Error callback. | 73 * @param {function} opt_onError Error callback. |
69 */ | 74 */ |
70 ThumbnailLoader.prototype.load = function( | 75 ThumbnailLoader.prototype.load = function( |
71 box, fill, opt_onSuccess, opt_onError) { | 76 box, fill, opt_onSuccess, opt_onError) { |
| 77 if (!this.thumbnailUrl_) { |
| 78 // Relevant CSS rules are in file_types.css. |
| 79 box.setAttribute('generic-thumbnail', this.mediaType_); |
| 80 return; |
| 81 } |
| 82 |
72 var img = box.ownerDocument.createElement('img'); | 83 var img = box.ownerDocument.createElement('img'); |
73 img.onload = function() { | 84 img.onload = function() { |
74 util.applyTransform(box, this.transform_); | 85 util.applyTransform(box, this.transform_); |
75 ThumbnailLoader.centerImage_(box, img, fill, | 86 ThumbnailLoader.centerImage_(box, img, fill, |
76 this.transform_ && (this.transform_.rotate90 % 2 == 1)); | 87 this.transform_ && (this.transform_.rotate90 % 2 == 1)); |
77 if (opt_onSuccess) | 88 if (opt_onSuccess) |
78 opt_onSuccess(img, this.transform_); | 89 opt_onSuccess(img, this.transform_); |
79 box.textContent = ''; | 90 box.textContent = ''; |
80 box.appendChild(img); | 91 box.appendChild(img); |
81 }.bind(this); | 92 }.bind(this); |
82 img.onerror = function() { | 93 img.onerror = function() { |
83 if (opt_onError) | 94 if (opt_onError) |
84 opt_onError(); | 95 opt_onError(); |
85 if (this.fallbackUrl_) | 96 if (this.fallbackUrl_) |
86 new ThumbnailLoader(this.fallbackUrl_).load(box, fill, opt_onSuccess); | 97 new ThumbnailLoader(this.fallbackUrl_, null, this.mediaType_). |
| 98 load(box, fill, opt_onSuccess); |
87 }.bind(this); | 99 }.bind(this); |
88 | 100 |
89 if (img.src == this.thumbnailUrl_) { | 101 if (img.src == this.thumbnailUrl_) { |
90 console.warn('Thumnbnail already loaded: ' + this.thumbnailUrl_); | 102 console.warn('Thumnbnail already loaded: ' + this.thumbnailUrl_); |
91 return; | 103 return; |
92 } | 104 } |
93 | 105 |
94 img.src = this.thumbnailUrl_; | 106 img.src = this.thumbnailUrl_; |
95 }; | 107 }; |
96 | 108 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 158 |
147 function percent(fraction) { | 159 function percent(fraction) { |
148 return (fraction * 100).toFixed(2) + '%'; | 160 return (fraction * 100).toFixed(2) + '%'; |
149 } | 161 } |
150 | 162 |
151 img.style.width = percent(fractionX); | 163 img.style.width = percent(fractionX); |
152 img.style.height = percent(fractionY); | 164 img.style.height = percent(fractionY); |
153 img.style.left = percent((1 - fractionX) / 2); | 165 img.style.left = percent((1 - fractionX) / 2); |
154 img.style.top = percent((1 - fractionY) / 2); | 166 img.style.top = percent((1 - fractionY) / 2); |
155 }; | 167 }; |
OLD | NEW |