2018-03-26 10:04:27 -04:00
|
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2016-05-23 17:18:43 -07:00
|
|
|
|
(function(window, GrDiffBuilderSideBySide) {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-06-27 09:36:36 -07:00
|
|
|
|
// Prevent redefinition.
|
|
|
|
|
if (window.GrDiffBuilderImage) { return; }
|
|
|
|
|
|
2017-05-16 13:45:17 -07:00
|
|
|
|
const IMAGE_MIME_PATTERN = /^image\/(bmp|gif|jpeg|jpg|png|tiff|webp)$/;
|
2017-04-14 15:45:31 -07:00
|
|
|
|
|
2017-07-31 14:33:16 -07:00
|
|
|
|
function GrDiffBuilderImage(
|
|
|
|
|
diff, comments, prefs, projectName, outputEl, baseImage, revisionImage) {
|
|
|
|
|
GrDiffBuilderSideBySide.call(
|
|
|
|
|
this, diff, comments, prefs, projectName, outputEl, []);
|
2016-05-23 17:18:43 -07:00
|
|
|
|
this._baseImage = baseImage;
|
|
|
|
|
this._revisionImage = revisionImage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype = Object.create(
|
|
|
|
|
GrDiffBuilderSideBySide.prototype);
|
|
|
|
|
GrDiffBuilderImage.prototype.constructor = GrDiffBuilderImage;
|
|
|
|
|
|
2018-05-29 13:51:46 -07:00
|
|
|
|
GrDiffBuilderImage.prototype.renderDiff = function() {
|
2018-07-25 14:01:14 -07:00
|
|
|
|
this._outputEl.classList.add('image-diff');
|
2017-05-16 13:45:17 -07:00
|
|
|
|
const section = this._createElement('tbody', 'image-diff');
|
2016-05-23 17:18:43 -07:00
|
|
|
|
|
|
|
|
|
this._emitImagePair(section);
|
|
|
|
|
this._emitImageLabels(section);
|
|
|
|
|
|
|
|
|
|
this._outputEl.appendChild(section);
|
2018-05-29 13:51:46 -07:00
|
|
|
|
this._outputEl.appendChild(this._createEndpoint());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._createEndpoint = function() {
|
2018-06-06 17:00:11 -07:00
|
|
|
|
const tbody = this._createElement('tbody');
|
|
|
|
|
const tr = this._createElement('tr');
|
|
|
|
|
const td = this._createElement('td');
|
|
|
|
|
|
|
|
|
|
// TODO(kaspern): Support blame for image diffs and remove the hardcoded 4
|
|
|
|
|
// column limit.
|
|
|
|
|
td.setAttribute('colspan', '4');
|
2018-05-29 13:51:46 -07:00
|
|
|
|
const endpoint = this._createElement('gr-endpoint-decorator');
|
2018-06-11 12:13:14 -07:00
|
|
|
|
const endpointDomApi = Polymer.dom(endpoint);
|
|
|
|
|
endpointDomApi.setAttribute('name', 'image-diff');
|
|
|
|
|
endpointDomApi.appendChild(
|
2018-05-29 13:51:46 -07:00
|
|
|
|
this._createEndpointParam('baseImage', this._baseImage));
|
2018-06-11 12:13:14 -07:00
|
|
|
|
endpointDomApi.appendChild(
|
2018-05-29 13:51:46 -07:00
|
|
|
|
this._createEndpointParam('revisionImage', this._revisionImage));
|
2018-06-06 17:00:11 -07:00
|
|
|
|
td.appendChild(endpoint);
|
|
|
|
|
tr.appendChild(td);
|
|
|
|
|
tbody.appendChild(tr);
|
|
|
|
|
return tbody;
|
2018-05-29 13:51:46 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._createEndpointParam = function(name, value) {
|
|
|
|
|
const endpointParam = this._createElement('gr-endpoint-param');
|
|
|
|
|
endpointParam.setAttribute('name', name);
|
|
|
|
|
endpointParam.value = value;
|
|
|
|
|
return endpointParam;
|
2016-05-23 17:18:43 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._emitImagePair = function(section) {
|
2017-05-16 13:45:17 -07:00
|
|
|
|
const tr = this._createElement('tr');
|
2016-05-23 17:18:43 -07:00
|
|
|
|
|
2018-07-25 14:01:14 -07:00
|
|
|
|
tr.appendChild(this._createElement('td', 'left lineNum blank'));
|
2017-03-29 17:29:17 -07:00
|
|
|
|
tr.appendChild(this._createImageCell(this._baseImage, 'left', section));
|
2016-05-23 17:18:43 -07:00
|
|
|
|
|
2018-07-25 14:01:14 -07:00
|
|
|
|
tr.appendChild(this._createElement('td', 'right lineNum blank'));
|
2017-03-29 17:29:17 -07:00
|
|
|
|
tr.appendChild(this._createImageCell(
|
2017-04-12 15:13:09 -07:00
|
|
|
|
this._revisionImage, 'right', section));
|
2016-05-23 17:18:43 -07:00
|
|
|
|
|
|
|
|
|
section.appendChild(tr);
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 13:45:17 -07:00
|
|
|
|
GrDiffBuilderImage.prototype._createImageCell = function(image, className,
|
|
|
|
|
section) {
|
|
|
|
|
const td = this._createElement('td', className);
|
2017-04-14 15:45:31 -07:00
|
|
|
|
if (image && IMAGE_MIME_PATTERN.test(image.type)) {
|
2017-05-16 13:45:17 -07:00
|
|
|
|
const imageEl = this._createElement('img');
|
2017-03-29 17:29:17 -07:00
|
|
|
|
imageEl.onload = function() {
|
|
|
|
|
image._height = imageEl.naturalHeight;
|
|
|
|
|
image._width = imageEl.naturalWidth;
|
|
|
|
|
this._updateImageLabel(section, className, image);
|
|
|
|
|
}.bind(this);
|
2016-05-23 17:18:43 -07:00
|
|
|
|
imageEl.src = 'data:' + image.type + ';base64, ' + image.body;
|
2017-05-16 13:45:17 -07:00
|
|
|
|
imageEl.addEventListener('error', () => {
|
2016-05-23 17:18:43 -07:00
|
|
|
|
imageEl.remove();
|
|
|
|
|
td.textContent = '[Image failed to load]';
|
|
|
|
|
});
|
|
|
|
|
td.appendChild(imageEl);
|
2017-03-29 17:29:17 -07:00
|
|
|
|
}
|
2017-04-12 15:13:09 -07:00
|
|
|
|
return td;
|
2017-03-29 17:29:17 -07:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 13:45:17 -07:00
|
|
|
|
GrDiffBuilderImage.prototype._updateImageLabel = function(section, className,
|
|
|
|
|
image) {
|
|
|
|
|
const label = Polymer.dom(section)
|
2017-03-29 17:29:17 -07:00
|
|
|
|
.querySelector('.' + className + ' span.label');
|
|
|
|
|
this._setLabelText(label, image);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._setLabelText = function(label, image) {
|
|
|
|
|
label.textContent = this._getImageLabel(image);
|
2016-05-23 17:18:43 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._emitImageLabels = function(section) {
|
2017-05-16 13:45:17 -07:00
|
|
|
|
const tr = this._createElement('tr');
|
2016-05-23 17:18:43 -07:00
|
|
|
|
|
2017-05-16 13:45:17 -07:00
|
|
|
|
let addNamesInLabel = false;
|
2017-03-29 17:29:17 -07:00
|
|
|
|
|
2017-04-12 15:13:09 -07:00
|
|
|
|
if (this._baseImage && this._revisionImage &&
|
|
|
|
|
this._baseImage._name !== this._revisionImage._name) {
|
2017-03-29 17:29:17 -07:00
|
|
|
|
addNamesInLabel = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 14:01:14 -07:00
|
|
|
|
tr.appendChild(this._createElement('td', 'left lineNum blank'));
|
2017-05-16 13:45:17 -07:00
|
|
|
|
let td = this._createElement('td', 'left');
|
|
|
|
|
let label = this._createElement('label');
|
|
|
|
|
let nameSpan;
|
|
|
|
|
let labelSpan = this._createElement('span', 'label');
|
2017-03-29 17:29:17 -07:00
|
|
|
|
|
|
|
|
|
if (addNamesInLabel) {
|
|
|
|
|
nameSpan = this._createElement('span', 'name');
|
|
|
|
|
nameSpan.textContent = this._baseImage._name;
|
|
|
|
|
label.appendChild(nameSpan);
|
|
|
|
|
label.appendChild(this._createElement('br'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._setLabelText(labelSpan, this._baseImage, addNamesInLabel);
|
|
|
|
|
|
|
|
|
|
label.appendChild(labelSpan);
|
2016-05-23 17:18:43 -07:00
|
|
|
|
td.appendChild(label);
|
|
|
|
|
tr.appendChild(td);
|
|
|
|
|
|
2018-07-25 14:01:14 -07:00
|
|
|
|
tr.appendChild(this._createElement('td', 'right lineNum blank'));
|
2016-05-23 17:18:43 -07:00
|
|
|
|
td = this._createElement('td', 'right');
|
|
|
|
|
label = this._createElement('label');
|
2017-03-29 17:29:17 -07:00
|
|
|
|
labelSpan = this._createElement('span', 'label');
|
|
|
|
|
|
|
|
|
|
if (addNamesInLabel) {
|
|
|
|
|
nameSpan = this._createElement('span', 'name');
|
|
|
|
|
nameSpan.textContent = this._revisionImage._name;
|
|
|
|
|
label.appendChild(nameSpan);
|
|
|
|
|
label.appendChild(this._createElement('br'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._setLabelText(labelSpan, this._revisionImage, addNamesInLabel);
|
|
|
|
|
|
|
|
|
|
label.appendChild(labelSpan);
|
2016-05-23 17:18:43 -07:00
|
|
|
|
td.appendChild(label);
|
|
|
|
|
tr.appendChild(td);
|
|
|
|
|
|
|
|
|
|
section.appendChild(tr);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._getImageLabel = function(image) {
|
|
|
|
|
if (image) {
|
2017-05-16 13:45:17 -07:00
|
|
|
|
const type = image.type || image._expectedType;
|
2016-05-23 17:18:43 -07:00
|
|
|
|
if (image._width && image._height) {
|
2018-01-02 13:23:38 -05:00
|
|
|
|
return image._width + '×' + image._height + ' ' + type;
|
2016-05-23 17:18:43 -07:00
|
|
|
|
} else {
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 'No image';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.GrDiffBuilderImage = GrDiffBuilderImage;
|
|
|
|
|
})(window, GrDiffBuilderSideBySide);
|