blob: 4abc3c31009ce7095a38167a5d3c04c0dc54021a [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5BrowserComponents.ImagePreview = class {
6 /**
7 * @param {!SDK.Target} target
8 * @param {string} originalImageURL
9 * @param {boolean} showDimensions
10 * @param {!Object=} precomputedFeatures
11 * @return {!Promise<?Element>}
12 */
13 static build(target, originalImageURL, showDimensions, precomputedFeatures) {
14 const resourceTreeModel = target.model(SDK.ResourceTreeModel);
15 if (!resourceTreeModel)
16 return Promise.resolve(/** @type {?Element} */ (null));
17 let resource = resourceTreeModel.resourceForURL(originalImageURL);
18 let imageURL = originalImageURL;
19 if (!isImageResource(resource) && precomputedFeatures && precomputedFeatures.currentSrc) {
20 imageURL = precomputedFeatures.currentSrc;
21 resource = resourceTreeModel.resourceForURL(imageURL);
22 }
23 if (!isImageResource(resource))
24 return Promise.resolve(/** @type {?Element} */ (null));
25
26 let fulfill;
27 const promise = new Promise(x => fulfill = x);
28 const imageElement = createElement('img');
29 imageElement.addEventListener('load', buildContent, false);
30 imageElement.addEventListener('error', () => fulfill(null), false);
31 resource.populateImageSource(imageElement);
32 return promise;
33
34 /**
35 * @param {?SDK.Resource} resource
36 * @return {boolean}
37 */
38 function isImageResource(resource) {
39 return !!resource && resource.resourceType() === Common.resourceTypes.Image;
40 }
41
42 function buildContent() {
43 const container = createElement('table');
44 UI.appendStyle(container, 'browser_components/imagePreview.css');
45 container.className = 'image-preview-container';
46 const naturalWidth = precomputedFeatures ? precomputedFeatures.naturalWidth : imageElement.naturalWidth;
47 const naturalHeight = precomputedFeatures ? precomputedFeatures.naturalHeight : imageElement.naturalHeight;
48 const offsetWidth = precomputedFeatures ? precomputedFeatures.offsetWidth : naturalWidth;
49 const offsetHeight = precomputedFeatures ? precomputedFeatures.offsetHeight : naturalHeight;
50 let description;
51 if (showDimensions) {
52 if (offsetHeight === naturalHeight && offsetWidth === naturalWidth) {
53 description = Common.UIString('%d \xd7 %d pixels', offsetWidth, offsetHeight);
54 } else {
55 description = Common.UIString(
56 '%d \xd7 %d pixels (Natural: %d \xd7 %d pixels)', offsetWidth, offsetHeight, naturalWidth, naturalHeight);
57 }
58 }
59
60 container.createChild('tr').createChild('td', 'image-container').appendChild(imageElement);
61 if (description)
62 container.createChild('tr').createChild('td').createChild('span', 'description').textContent = description;
63 if (imageURL !== originalImageURL) {
64 container.createChild('tr').createChild('td').createChild('span', 'description').textContent =
65 String.sprintf('currentSrc: %s', imageURL.trimMiddle(100));
66 }
67 fulfill(container);
68 }
69 }
70};