diff --git a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js index fa1aeb2f4d..ecddba2ad2 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js +++ b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js @@ -18,13 +18,13 @@ * Possible CSS classes indicating the state of selection. Dynamically added/ * removed based on where the user clicks within the diff. */ - var SelectionClass = { + const SelectionClass = { COMMENT: 'selected-comment', LEFT: 'selected-left', RIGHT: 'selected-right', }; - var getNewCache = function() { return {left: null, right: null}; }; + const getNewCache = () => { return {left: null, right: null}; }; Polymer({ is: 'gr-diff-selection', @@ -43,11 +43,11 @@ ], listeners: { - 'copy': '_handleCopy', - 'down': '_handleDown', + copy: '_handleCopy', + down: '_handleDown', }, - attached: function() { + attached() { this.classList.add(SelectionClass.RIGHT); }, @@ -59,19 +59,19 @@ return this._cachedDiffBuilder; }, - _diffChanged: function() { + _diffChanged() { this._linesCache = getNewCache(); }, - _handleDown: function(e) { - var lineEl = this.diffBuilder.getLineElByChild(e.target); + _handleDown(e) { + const lineEl = this.diffBuilder.getLineElByChild(e.target); if (!lineEl) { return; } - var commentSelected = + const commentSelected = this._elementDescendedFromClass(e.target, 'gr-diff-comment'); - var side = this.diffBuilder.getSideByLineEl(lineEl); - var targetClasses = []; + const side = this.diffBuilder.getSideByLineEl(lineEl); + const targetClasses = []; targetClasses.push(side === 'left' ? SelectionClass.LEFT : SelectionClass.RIGHT); @@ -80,23 +80,23 @@ targetClasses.push(SelectionClass.COMMENT); } // Remove any selection classes that do not belong. - for (var key in SelectionClass) { + for (const key in SelectionClass) { if (SelectionClass.hasOwnProperty(key)) { - var className = SelectionClass[key]; - if (targetClasses.indexOf(className) === -1) { + const className = SelectionClass[key]; + if (!targetClasses.includes(className)) { this.classList.remove(SelectionClass[key]); } } } // Add new selection classes iff they are not already present. - for (var i = 0; i < targetClasses.length; i++) { - if (!this.classList.contains(targetClasses[i])) { - this.classList.add(targetClasses[i]); + for (const _class of targetClasses) { + if (!this.classList.contains(_class)) { + this.classList.add(_class); } } }, - _getCopyEventTarget: function(e) { + _getCopyEventTarget(e) { return Polymer.dom(e).rootTarget; }, @@ -108,7 +108,7 @@ * @param {!string} className * @return {boolean} */ - _elementDescendedFromClass: function(element, className) { + _elementDescendedFromClass(element, className) { while (!element.classList.contains(className)) { if (!element.parentElement || element === this.diffBuilder.diffElement) { @@ -119,20 +119,20 @@ return true; }, - _handleCopy: function(e) { - var commentSelected = false; - var target = this._getCopyEventTarget(e); + _handleCopy(e) { + let commentSelected = false; + const target = this._getCopyEventTarget(e); if (target.type === 'textarea') { return; } if (!this._elementDescendedFromClass(target, 'diff-row')) { return; } if (this.classList.contains(SelectionClass.COMMENT)) { commentSelected = true; } - var lineEl = this.diffBuilder.getLineElByChild(target); + const lineEl = this.diffBuilder.getLineElByChild(target); if (!lineEl) { return; } - var side = this.diffBuilder.getSideByLineEl(lineEl); - var text = this._getSelectedText(side, commentSelected); + const side = this.diffBuilder.getSideByLineEl(lineEl); + const text = this._getSelectedText(side, commentSelected); if (text) { e.clipboardData.setData('Text', text); e.preventDefault(); @@ -148,19 +148,20 @@ * @param {boolean} Whether or not a comment is selected. * @return {string} The selected text. */ - _getSelectedText: function(side, commentSelected) { - var sel = window.getSelection(); + _getSelectedText(side, commentSelected) { + const sel = window.getSelection(); if (sel.rangeCount != 1) { return; // No multi-select support yet. } if (commentSelected) { return this._getCommentLines(sel, side); } - var range = GrRangeNormalizer.normalize(sel.getRangeAt(0)); - var startLineEl = this.diffBuilder.getLineElByChild(range.startContainer); - var endLineEl = this.diffBuilder.getLineElByChild(range.endContainer); - var startLineNum = parseInt(startLineEl.getAttribute('data-value'), 10); - var endLineNum = parseInt(endLineEl.getAttribute('data-value'), 10); + const range = GrRangeNormalizer.normalize(sel.getRangeAt(0)); + const startLineEl = + this.diffBuilder.getLineElByChild(range.startContainer); + const endLineEl = this.diffBuilder.getLineElByChild(range.endContainer); + const startLineNum = parseInt(startLineEl.getAttribute('data-value'), 10); + const endLineNum = parseInt(endLineEl.getAttribute('data-value'), 10); return this._getRangeFromDiff(startLineNum, range.startOffset, endLineNum, range.endOffset, side); @@ -176,9 +177,9 @@ * @param {!string} side The side that is currently selected. * @return {string} The selected diff text. */ - _getRangeFromDiff: function(startLineNum, startOffset, endLineNum, - endOffset, side) { - var lines = this._getDiffLines(side).slice(startLineNum - 1, endLineNum); + _getRangeFromDiff(startLineNum, startOffset, endLineNum, endOffset, side) { + const lines = + this._getDiffLines(side).slice(startLineNum - 1, endLineNum); if (lines.length) { lines[lines.length - 1] = lines[lines.length - 1] .substring(0, endOffset); @@ -193,17 +194,13 @@ * @param {!string} side The side that is currently selected. * @return {Array.string} An array of strings indexed by line number. */ - _getDiffLines: function(side) { + _getDiffLines(side) { if (this._linesCache[side]) { return this._linesCache[side]; } - var lines = []; - var chunk; - var key = side === 'left' ? 'a' : 'b'; - for (var chunkIndex = 0; - chunkIndex < this.diff.content.length; - chunkIndex++) { - chunk = this.diff.content[chunkIndex]; + let lines = []; + const key = side === 'left' ? 'a' : 'b'; + for (const chunk of this.diff.content) { if (chunk.ab) { lines = lines.concat(chunk.ab); } else if (chunk[key]) { @@ -222,16 +219,16 @@ * @param {!string} side The side that is currently selected. * @return {string} The selected comment text. */ - _getCommentLines: function(sel, side) { - var range = GrRangeNormalizer.normalize(sel.getRangeAt(0)); - var content = []; + _getCommentLines(sel, side) { + const range = GrRangeNormalizer.normalize(sel.getRangeAt(0)); + const content = []; // Query the diffElement for comments. - var messages = this.diffBuilder.diffElement.querySelectorAll( - '.side-by-side [data-side="' + side + - '"] .message *, .unified .message *'); + const messages = this.diffBuilder.diffElement.querySelectorAll( + `.side-by-side [data-side="${side + }"] .message *, .unified .message *`); - for (var i = 0; i < messages.length; i++) { - var el = messages[i]; + for (let i = 0; i < messages.length; i++) { + const el = messages[i]; // Check if the comment element exists inside the selection. if (sel.containsNode(el, true)) { // Padded elements require newlines for accurate spacing. @@ -262,10 +259,10 @@ * @param {Range} range The normalized selection range. * @return {string} The text within the selection. */ - _getTextContentForRange: function(domNode, sel, range) { + _getTextContentForRange(domNode, sel, range) { if (!sel.containsNode(domNode, true)) { return ''; } - var text = ''; + let text = ''; if (domNode instanceof Text) { text = domNode.textContent; if (domNode === range.endContainer) { @@ -275,9 +272,8 @@ text = text.substring(range.startOffset); } } else { - for (var i = 0; i < domNode.childNodes.length; i++) { - text += this._getTextContentForRange(domNode.childNodes[i], - sel, range); + for (const childNode of domNode.childNodes) { + text += this._getTextContentForRange(childNode, sel, range); } } return text; diff --git a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html index 3eeba90f48..39555b4b3a 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html +++ b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html @@ -101,13 +101,13 @@ limitations under the License.