Get rid of global GrDiffBuilderSideBySide

* Replace the global GrDiffBuilderSideBySide variable with named
  imports.
* Update gr-app-global-var-init.js

Change-Id: I11d2480a2709d0037c255cf885b2375c2e7e1dbf
This commit is contained in:
Dmitrii Filippov
2020-03-23 20:50:29 +01:00
parent eca71692d2
commit ad32f07dcd
5 changed files with 88 additions and 92 deletions

View File

@@ -179,7 +179,6 @@ module.exports = {
"GrCountStringFormatter": "readonly",
"GrDiffBuilderBinary": "readonly",
"GrDiffBuilderImage": "readonly",
"GrDiffBuilderSideBySide": "readonly",
"GrDiffBuilderUnified": "readonly",
"GrDomHook": "readonly",
"GrDomHooksManager": "readonly",

View File

@@ -32,6 +32,7 @@ import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-diff-builder-element_html.js';
import {GrAnnotation} from '../gr-diff-highlight/gr-annotation.js';
import {GrDiffBuilder} from './gr-diff-builder.js';
import {GrDiffBuilderSideBySide} from './gr-diff-builder-side-by-side.js';
const DiffViewMode = {
SIDE_BY_SIDE: 'SIDE_BY_SIDE',

View File

@@ -14,7 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function(window, GrDiffBuilderSideBySide) {
import {GrDiffBuilderSideBySide} from './gr-diff-builder-side-by-side.js';
(function(window) {
'use strict';
// Prevent redefinition.
@@ -180,4 +183,4 @@
};
window.GrDiffBuilderImage = GrDiffBuilderImage;
})(window, GrDiffBuilderSideBySide);
})(window);

View File

@@ -17,106 +17,97 @@
import {GrDiffBuilder} from './gr-diff-builder.js';
(function(window) {
'use strict';
/** @constructor */
export function GrDiffBuilderSideBySide(diff, prefs, outputEl, layers) {
GrDiffBuilder.call(this, diff, prefs, outputEl, layers);
}
GrDiffBuilderSideBySide.prototype = Object.create(GrDiffBuilder.prototype);
GrDiffBuilderSideBySide.prototype.constructor = GrDiffBuilderSideBySide;
// Prevent redefinition.
if (window.GrDiffBuilderSideBySide) { return; }
/** @constructor */
function GrDiffBuilderSideBySide(diff, prefs, outputEl, layers) {
GrDiffBuilder.call(this, diff, prefs, outputEl, layers);
GrDiffBuilderSideBySide.prototype.buildSectionElement = function(group) {
const sectionEl = this._createElement('tbody', 'section');
sectionEl.classList.add(group.type);
if (this._isTotal(group)) {
sectionEl.classList.add('total');
}
GrDiffBuilderSideBySide.prototype = Object.create(GrDiffBuilder.prototype);
GrDiffBuilderSideBySide.prototype.constructor = GrDiffBuilderSideBySide;
if (group.dueToRebase) {
sectionEl.classList.add('dueToRebase');
}
if (group.ignoredWhitespaceOnly) {
sectionEl.classList.add('ignoredWhitespaceOnly');
}
const pairs = group.getSideBySidePairs();
for (let i = 0; i < pairs.length; i++) {
sectionEl.appendChild(this._createRow(sectionEl, pairs[i].left,
pairs[i].right));
}
return sectionEl;
};
GrDiffBuilderSideBySide.prototype.buildSectionElement = function(group) {
const sectionEl = this._createElement('tbody', 'section');
sectionEl.classList.add(group.type);
if (this._isTotal(group)) {
sectionEl.classList.add('total');
}
if (group.dueToRebase) {
sectionEl.classList.add('dueToRebase');
}
if (group.ignoredWhitespaceOnly) {
sectionEl.classList.add('ignoredWhitespaceOnly');
}
const pairs = group.getSideBySidePairs();
for (let i = 0; i < pairs.length; i++) {
sectionEl.appendChild(this._createRow(sectionEl, pairs[i].left,
pairs[i].right));
}
return sectionEl;
};
GrDiffBuilderSideBySide.prototype.addColumns = function(outputEl, fontSize) {
const width = fontSize * 4;
const colgroup = document.createElement('colgroup');
GrDiffBuilderSideBySide.prototype.addColumns = function(outputEl, fontSize) {
const width = fontSize * 4;
const colgroup = document.createElement('colgroup');
// Add the blame column.
let col = this._createElement('col', 'blame');
colgroup.appendChild(col);
// Add the blame column.
let col = this._createElement('col', 'blame');
colgroup.appendChild(col);
// Add left-side line number.
col = document.createElement('col');
col.setAttribute('width', width);
colgroup.appendChild(col);
// Add left-side line number.
col = document.createElement('col');
col.setAttribute('width', width);
colgroup.appendChild(col);
// Add left-side content.
colgroup.appendChild(document.createElement('col'));
// Add left-side content.
colgroup.appendChild(document.createElement('col'));
// Add right-side line number.
col = document.createElement('col');
col.setAttribute('width', width);
colgroup.appendChild(col);
// Add right-side line number.
col = document.createElement('col');
col.setAttribute('width', width);
colgroup.appendChild(col);
// Add right-side content.
colgroup.appendChild(document.createElement('col'));
// Add right-side content.
colgroup.appendChild(document.createElement('col'));
outputEl.appendChild(colgroup);
};
outputEl.appendChild(colgroup);
};
GrDiffBuilderSideBySide.prototype._createRow = function(section, leftLine,
rightLine) {
const row = this._createElement('tr');
row.classList.add('diff-row', 'side-by-side');
row.setAttribute('left-type', leftLine.type);
row.setAttribute('right-type', rightLine.type);
row.tabIndex = -1;
GrDiffBuilderSideBySide.prototype._createRow = function(section, leftLine,
rightLine) {
const row = this._createElement('tr');
row.classList.add('diff-row', 'side-by-side');
row.setAttribute('left-type', leftLine.type);
row.setAttribute('right-type', rightLine.type);
row.tabIndex = -1;
row.appendChild(this._createBlameCell(leftLine));
row.appendChild(this._createBlameCell(leftLine));
this._appendPair(section, row, leftLine, leftLine.beforeNumber,
GrDiffBuilder.Side.LEFT);
this._appendPair(section, row, rightLine, rightLine.afterNumber,
GrDiffBuilder.Side.RIGHT);
return row;
};
this._appendPair(section, row, leftLine, leftLine.beforeNumber,
GrDiffBuilder.Side.LEFT);
this._appendPair(section, row, rightLine, rightLine.afterNumber,
GrDiffBuilder.Side.RIGHT);
return row;
};
GrDiffBuilderSideBySide.prototype._appendPair = function(section, row, line,
lineNumber, side) {
const lineNumberEl = this._createLineEl(line, lineNumber, line.type, side);
row.appendChild(lineNumberEl);
const action = this._createContextControl(section, line);
if (action) {
row.appendChild(action);
} else {
const textEl = this._createTextEl(lineNumberEl, line, side);
row.appendChild(textEl);
}
};
GrDiffBuilderSideBySide.prototype._appendPair = function(section, row, line,
lineNumber, side) {
const lineNumberEl = this._createLineEl(line, lineNumber, line.type, side);
row.appendChild(lineNumberEl);
const action = this._createContextControl(section, line);
if (action) {
row.appendChild(action);
} else {
const textEl = this._createTextEl(lineNumberEl, line, side);
row.appendChild(textEl);
}
};
GrDiffBuilderSideBySide.prototype._getNextContentOnSide = function(
content, side) {
let tr = content.parentElement.parentElement;
while (tr = tr.nextSibling) {
content = tr.querySelector(
'td.content .contentText[data-side="' + side + '"]');
if (content) { return content; }
}
return null;
};
window.GrDiffBuilderSideBySide = GrDiffBuilderSideBySide;
})(window);
GrDiffBuilderSideBySide.prototype._getNextContentOnSide = function(
content, side) {
let tr = content.parentElement.parentElement;
while (tr = tr.nextSibling) {
content = tr.querySelector(
'td.content .contentText[data-side="' + side + '"]');
if (content) { return content; }
}
return null;
};

View File

@@ -28,6 +28,7 @@ import {GrAttributeHelper} from './plugins/gr-attribute-helper/gr-attribute-help
import {GrDiffLine} from './diff/gr-diff/gr-diff-line.js';
import {GrDiffGroup} from './diff/gr-diff/gr-diff-group.js';
import {GrDiffBuilder} from './diff/gr-diff-builder/gr-diff-builder.js';
import {GrDiffBuilderSideBySide} from './diff/gr-diff-builder/gr-diff-builder-side-by-side.js';
export function initGlobalVariables() {
window.GrDisplayNameUtils = GrDisplayNameUtils;
@@ -36,4 +37,5 @@ export function initGlobalVariables() {
window.GrDiffLine = GrDiffLine;
window.GrDiffGroup = GrDiffGroup;
window.GrDiffBuilder = GrDiffBuilder;
window.GrDiffBuilderSideBySide = GrDiffBuilderSideBySide;
}