2016-05-23 17:18:43 -07:00
|
|
|
// 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.
|
|
|
|
(function(window, GrDiffBuilderSideBySide) {
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-27 09:36:36 -07:00
|
|
|
// Prevent redefinition.
|
|
|
|
if (window.GrDiffBuilderImage) { return; }
|
|
|
|
|
2016-05-23 17:18:43 -07:00
|
|
|
function GrDiffBuilderImage(diff, comments, prefs, outputEl, baseImage,
|
|
|
|
revisionImage) {
|
2016-07-14 12:31:09 -07:00
|
|
|
GrDiffBuilderSideBySide.call(this, diff, comments, prefs, 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;
|
|
|
|
|
2016-06-27 12:19:21 -07:00
|
|
|
GrDiffBuilderImage.prototype.renderDiffImages = function() {
|
2016-05-23 17:18:43 -07:00
|
|
|
var section = this._createElement('tbody', 'image-diff');
|
|
|
|
|
|
|
|
this._emitImagePair(section);
|
|
|
|
this._emitImageLabels(section);
|
|
|
|
|
|
|
|
this._outputEl.appendChild(section);
|
|
|
|
};
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._emitImagePair = function(section) {
|
|
|
|
var tr = this._createElement('tr');
|
|
|
|
|
|
|
|
tr.appendChild(this._createElement('td'));
|
2017-03-29 17:29:17 -07:00
|
|
|
tr.appendChild(this._createImageCell(this._baseImage, 'left', section));
|
2016-05-23 17:18:43 -07:00
|
|
|
|
|
|
|
tr.appendChild(this._createElement('td'));
|
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-03-29 17:29:17 -07:00
|
|
|
GrDiffBuilderImage.prototype._createImageCell =
|
|
|
|
function(image, className, section) {
|
2016-05-23 17:18:43 -07:00
|
|
|
var td = this._createElement('td', className);
|
|
|
|
if (image) {
|
|
|
|
var 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-04-12 15:13:09 -07:00
|
|
|
imageEl.addEventListener('error', function() {
|
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
|
|
|
};
|
|
|
|
|
|
|
|
GrDiffBuilderImage.prototype._updateImageLabel =
|
|
|
|
function(section, className, image) {
|
|
|
|
var label = Polymer.dom(section)
|
|
|
|
.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) {
|
|
|
|
var tr = this._createElement('tr');
|
|
|
|
|
2017-03-29 17:29:17 -07:00
|
|
|
var addNamesInLabel = false;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:18:43 -07:00
|
|
|
tr.appendChild(this._createElement('td'));
|
|
|
|
var td = this._createElement('td', 'left');
|
|
|
|
var label = this._createElement('label');
|
2017-03-29 17:29:17 -07:00
|
|
|
var nameSpan;
|
|
|
|
var labelSpan = this._createElement('span', 'label');
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
tr.appendChild(this._createElement('td'));
|
|
|
|
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) {
|
|
|
|
var type = image.type || image._expectedType;
|
|
|
|
if (image._width && image._height) {
|
|
|
|
return image._width + '⨉' + image._height + ' ' + type;
|
|
|
|
} else {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'No image';
|
|
|
|
};
|
|
|
|
|
|
|
|
window.GrDiffBuilderImage = GrDiffBuilderImage;
|
|
|
|
})(window, GrDiffBuilderSideBySide);
|