Skip to content

Commit 77b1224

Browse files
authored
refactor: use helper function to iterate DOM children (#5059)
1 parent 4b181e2 commit 77b1224

8 files changed

Lines changed: 57 additions & 44 deletions

‎packages/grid/src/vaadin-grid-a11y-mixin.js‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2016 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { iterateChildren } from './vaadin-grid-helpers.js';
67

78
/**
89
* @polymerMixin
@@ -42,16 +43,16 @@ export const A11yMixin = (superClass) =>
4243

4344
/** @protected */
4445
_a11yUpdateHeaderRows() {
45-
Array.from(this.$.header.children).forEach((headerRow, index) =>
46-
headerRow.setAttribute('aria-rowindex', index + 1),
47-
);
46+
iterateChildren(this.$.header, (headerRow, index) => {
47+
headerRow.setAttribute('aria-rowindex', index + 1);
48+
});
4849
}
4950

5051
/** @protected */
5152
_a11yUpdateFooterRows() {
52-
Array.from(this.$.footer.children).forEach((footerRow, index) =>
53-
footerRow.setAttribute('aria-rowindex', this._a11yGetHeaderRowCount(this._columnTree) + this.size + index + 1),
54-
);
53+
iterateChildren(this.$.footer, (footerRow, index) => {
54+
footerRow.setAttribute('aria-rowindex', this._a11yGetHeaderRowCount(this._columnTree) + this.size + index + 1);
55+
});
5556
}
5657

5758
/**
@@ -71,7 +72,9 @@ export const A11yMixin = (superClass) =>
7172
_a11yUpdateRowSelected(row, selected) {
7273
// Jaws reads selection only for rows, NVDA only for cells
7374
row.setAttribute('aria-selected', Boolean(selected));
74-
Array.from(row.children).forEach((cell) => cell.setAttribute('aria-selected', Boolean(selected)));
75+
iterateChildren(row, (cell) => {
76+
cell.setAttribute('aria-selected', Boolean(selected));
77+
});
7578
}
7679

7780
/**
@@ -108,7 +111,7 @@ export const A11yMixin = (superClass) =>
108111
* @protected
109112
*/
110113
_a11ySetRowDetailsCell(row, detailsCell) {
111-
Array.from(row.children).forEach((cell) => {
114+
iterateChildren(row, (cell) => {
112115
if (cell !== detailsCell) {
113116
cell.setAttribute('aria-controls', detailsCell.id);
114117
}

‎packages/grid/src/vaadin-grid-column-reordering-mixin.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { addListener } from '@vaadin/component-base/src/gestures.js';
7-
import { updateColumnOrders } from './vaadin-grid-helpers.js';
7+
import { iterateChildren, updateColumnOrders } from './vaadin-grid-helpers.js';
88

99
/**
1010
* @polymerMixin
@@ -304,11 +304,11 @@ export const ColumnReorderingMixin = (superClass) =>
304304
* @protected
305305
*/
306306
_setSiblingsReorderStatus(column, status) {
307-
Array.from(column.parentNode.children)
308-
.filter((child) => /column/.test(child.localName) && this._isSwapAllowed(child, column))
309-
.forEach((sibling) => {
307+
iterateChildren(column.parentNode, (sibling) => {
308+
if (/column/.test(sibling.localName) && this._isSwapAllowed(sibling, column)) {
310309
sibling._reorderStatus = status;
311-
});
310+
}
311+
});
312312
}
313313

314314
/** @protected */

‎packages/grid/src/vaadin-grid-data-provider-mixin.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { timeOut } from '@vaadin/component-base/src/async.js';
77
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
8-
import { updateRowAndCells } from './vaadin-grid-helpers.js';
8+
import { iterateChildren, updateRowAndCells } from './vaadin-grid-helpers.js';
99

1010
/**
1111
* @private
@@ -400,14 +400,14 @@ export const DataProviderMixin = (superClass) =>
400400
this._cache.updateSize();
401401
this._effectiveSize = this._cache.effectiveSize;
402402

403-
Array.from(this.$.items.children)
404-
.filter((row) => !row.hidden)
405-
.forEach((row) => {
403+
iterateChildren(this.$.items, (row) => {
404+
if (!row.hidden) {
406405
const cachedItem = this._cache.getItemForIndex(row.index);
407406
if (cachedItem) {
408407
this._getItem(row.index, row);
409408
}
410-
});
409+
}
410+
});
411411

412412
this.__scrollToPendingIndex();
413413
});

‎packages/grid/src/vaadin-grid-drag-and-drop-mixin.js‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2016 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { updateRowAndCells } from './vaadin-grid-helpers.js';
6+
import { iterateChildren, updateRowAndCells } from './vaadin-grid-helpers.js';
77

88
const DropMode = {
99
BETWEEN: 'between',
@@ -309,7 +309,7 @@ export const DragAndDropMixin = (superClass) =>
309309
/** @protected */
310310
_clearDragStyles() {
311311
this.removeAttribute('dragover');
312-
Array.from(this.$.items.children).forEach((row) => {
312+
iterateChildren(this.$.items, (row) => {
313313
updateRowAndCells(row, 'dragover', null, true);
314314
});
315315
}
@@ -373,11 +373,11 @@ export const DragAndDropMixin = (superClass) =>
373373
* the conditions change.
374374
*/
375375
filterDragAndDrop() {
376-
Array.from(this.$.items.children)
377-
.filter((row) => !row.hidden)
378-
.forEach((row) => {
376+
iterateChildren(this.$.items, (row) => {
377+
if (!row.hidden) {
379378
this._filterDragAndDrop(row, this.__getRowModel(row));
380-
});
379+
}
380+
});
381381
}
382382

383383
/**
@@ -390,13 +390,11 @@ export const DragAndDropMixin = (superClass) =>
390390
const dragDisabled = !this.rowsDraggable || loading || (this.dragFilter && !this.dragFilter(model));
391391
const dropDisabled = !this.dropMode || loading || (this.dropFilter && !this.dropFilter(model));
392392

393-
const draggableElements = Array.from(row.children).map((cell) => cell._content);
394-
395-
draggableElements.forEach((e) => {
393+
iterateChildren(row, (cell) => {
396394
if (dragDisabled) {
397-
e.removeAttribute('draggable');
395+
cell._content.removeAttribute('draggable');
398396
} else {
399-
e.setAttribute('draggable', true);
397+
cell._content.setAttribute('draggable', true);
400398
}
401399
});
402400

‎packages/grid/src/vaadin-grid-helpers.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
*/
66
import { addValueToAttribute, removeValueFromAttribute } from '@vaadin/component-base/src/dom-utils.js';
77

8+
/**
9+
* @param {HTMLElement} container the DOM element with children
10+
* @param {Function} callback function to call on each child
11+
*/
12+
export function iterateChildren(container, callback) {
13+
[...container.children].forEach(callback);
14+
}
15+
816
/**
917
* @param {Array<Object>} columns array of columns to be modified
1018
* @param {number} scope multiplier added to base order for each column

‎packages/grid/src/vaadin-grid-row-details-mixin.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2016 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { iterateChildren } from './vaadin-grid-helpers.js';
67

78
/**
89
* @polymerMixin
@@ -78,7 +79,7 @@ export const RowDetailsMixin = (superClass) =>
7879

7980
if (this._columnTree) {
8081
// Only update the rows if the column tree has already been initialized
81-
Array.from(this.$.items.children).forEach((row) => {
82+
iterateChildren(this.$.items, (row) => {
8283
if (!row.querySelector('[part~=details-cell]')) {
8384
this._updateRow(row, this._columnTree[this._columnTree.length - 1]);
8485
const isDetailsOpened = this._isDetailsOpened(row._item);
@@ -95,7 +96,7 @@ export const RowDetailsMixin = (superClass) =>
9596
return;
9697
}
9798

98-
[...this.$.items.children].forEach((row) => {
99+
iterateChildren(this.$.items, (row) => {
99100
// Re-renders the row to possibly close the previously opened details.
100101
if (row.hasAttribute('details-opened')) {
101102
this._updateItem(row, row._item);
@@ -162,7 +163,7 @@ export const RowDetailsMixin = (superClass) =>
162163

163164
/** @protected */
164165
_updateDetailsCellHeights() {
165-
[...this.$.items.children].forEach((row) => {
166+
iterateChildren(this.$.items, (row) => {
166167
this._updateDetailsCellHeight(row);
167168
});
168169
}

‎packages/grid/src/vaadin-grid-styling-mixin.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2016 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { iterateChildren } from './vaadin-grid-helpers.js';
67

78
/**
89
* @polymerMixin
@@ -49,14 +50,16 @@ export const StylingMixin = (superClass) =>
4950
* the conditions change.
5051
*/
5152
generateCellClassNames() {
52-
Array.from(this.$.items.children)
53-
.filter((row) => !row.hidden && !row.hasAttribute('loading'))
54-
.forEach((row) => this._generateCellClassNames(row, this.__getRowModel(row)));
53+
iterateChildren(this.$.items, (row) => {
54+
if (!row.hidden && !row.hasAttribute('loading')) {
55+
this._generateCellClassNames(row, this.__getRowModel(row));
56+
}
57+
});
5558
}
5659

5760
/** @private */
5861
_generateCellClassNames(row, model) {
59-
Array.from(row.children).forEach((cell) => {
62+
iterateChildren(row, (cell) => {
6063
if (cell.__generatedClasses) {
6164
cell.__generatedClasses.forEach((className) => cell.classList.remove(className));
6265
}

‎packages/grid/src/vaadin-grid.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { DragAndDropMixin } from './vaadin-grid-drag-and-drop-mixin.js';
2525
import { DynamicColumnsMixin } from './vaadin-grid-dynamic-columns-mixin.js';
2626
import { EventContextMixin } from './vaadin-grid-event-context-mixin.js';
2727
import { FilterMixin } from './vaadin-grid-filter-mixin.js';
28-
import { updateRowAndCells, updateRowBodyCellsPart } from './vaadin-grid-helpers.js';
28+
import { iterateChildren, updateRowAndCells, updateRowBodyCellsPart } from './vaadin-grid-helpers.js';
2929
import { KeyboardNavigationMixin } from './vaadin-grid-keyboard-navigation-mixin.js';
3030
import { RowDetailsMixin } from './vaadin-grid-row-details-mixin.js';
3131
import { ScrollMixin } from './vaadin-grid-scroll-mixin.js';
@@ -788,7 +788,7 @@ class Grid extends ElementMixin(
788788

789789
const contentsFragment = document.createDocumentFragment();
790790

791-
Array.from(row.children).forEach((cell) => {
791+
iterateChildren(row, (cell) => {
792792
cell._vacant = true;
793793
});
794794
row.innerHTML = '';
@@ -937,9 +937,9 @@ class Grid extends ElementMixin(
937937
* @protected
938938
*/
939939
_renderColumnTree(columnTree) {
940-
Array.from(this.$.items.children).forEach((row) =>
941-
this._updateRow(row, columnTree[columnTree.length - 1], null, false, true),
942-
);
940+
iterateChildren(this.$.items, (row) => {
941+
this._updateRow(row, columnTree[columnTree.length - 1], null, false, true);
942+
});
943943

944944
while (this.$.header.children.length < columnTree.length) {
945945
const headerRow = document.createElement('tr');
@@ -959,14 +959,14 @@ class Grid extends ElementMixin(
959959
this.$.footer.removeChild(this.$.footer.firstElementChild);
960960
}
961961

962-
Array.from(this.$.header.children).forEach((headerRow, index, rows) => {
962+
iterateChildren(this.$.header, (headerRow, index, rows) => {
963963
this._updateRow(headerRow, columnTree[index], 'header', index === columnTree.length - 1);
964964

965965
updateRowBodyCellsPart(headerRow, 'first-header-row-cell', index === 0);
966966
updateRowBodyCellsPart(headerRow, 'last-header-row-cell', index === rows.length - 1);
967967
});
968968

969-
Array.from(this.$.footer.children).forEach((footerRow, index, rows) => {
969+
iterateChildren(this.$.footer, (footerRow, index, rows) => {
970970
this._updateRow(footerRow, columnTree[columnTree.length - 1 - index], 'footer', index === 0);
971971

972972
updateRowBodyCellsPart(footerRow, 'first-footer-row-cell', index === 0);
@@ -1020,7 +1020,7 @@ class Grid extends ElementMixin(
10201020
this._generateCellClassNames(row, model);
10211021
this._filterDragAndDrop(row, model);
10221022

1023-
Array.from(row.children).forEach((cell) => {
1023+
iterateChildren(row, (cell) => {
10241024
if (cell._renderer) {
10251025
const owner = cell._column || this;
10261026
cell._renderer.call(owner, cell._content, owner, model);

0 commit comments

Comments
 (0)