From 6112fe8ad080f769572bb738ada99f41acf1de31 Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:58:59 +0400 Subject: [PATCH 1/7] refactor(grids): drop unused close markup method from pivot grid areas --- .../__internal/grids/pivot_grid/area_item/m_area_item.ts | 4 ---- .../grids/pivot_grid/headers_area/m_headers_area.ts | 8 -------- 2 files changed, 12 deletions(-) diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts index efe4a6ce6988..cabaf011ecdf 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts @@ -147,10 +147,6 @@ abstract class AreaItem { return false; } - _getCloseMainElementMarkup() { - return ''; - } - _getColumnIndexOffset() { return this.component._dataController?.getColumnIndexOffset() || 0; } diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts index 7ef7ef7001bd..d28127551153 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts @@ -99,10 +99,6 @@ class HorizontalHeadersArea extends AreaItem { return thead; } - _getCloseMainElementMarkup() { - return ''; - } - setVirtualContentParams(params) { super.setVirtualContentParams(params); @@ -300,10 +296,6 @@ class VerticalHeadersArea extends HorizontalHeadersArea { return tbody; } - _getCloseMainElementMarkup() { - return ''; - } - updateColspans(columnCount) { const { rows } = this.tableElement()[0]; let columnOffset = 0; From 140d1e6a58b9857f95ba40320dea60df31bf7806 Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:27:23 +0400 Subject: [PATCH 2/7] refactor(grids): type pivot grid area item, headers and data areas --- .../grids/pivot_grid/area_item/m_area_item.ts | 472 ++++++++++-------- .../grids/pivot_grid/area_item/types.ts | 72 +++ .../pivot_grid/data_area/m_data_area.test.ts | 28 +- .../grids/pivot_grid/data_area/m_data_area.ts | 52 +- .../pivot_grid/fields_area/m_fields_area.ts | 10 +- .../headers_area/m_headers_area.test.ts | 23 +- .../pivot_grid/headers_area/m_headers_area.ts | 163 +++--- .../js/__internal/grids/pivot_grid/types.ts | 9 + 8 files changed, 508 insertions(+), 321 deletions(-) create mode 100644 packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts index cabaf011ecdf..88b62a7be265 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts @@ -1,25 +1,41 @@ import domAdapter from '@js/core/dom_adapter'; import { getPublicElement } from '@js/core/element'; import Guid from '@js/core/guid'; +import type { dxElementWrapper } from '@js/core/renderer'; import $ from '@js/core/renderer'; +import type { DeferredObj } from '@js/core/utils/deferred'; import { extend } from '@js/core/utils/extend'; import { getBoundingRect } from '@js/core/utils/position'; import { setWidth } from '@js/core/utils/size'; import { setStyle } from '@js/core/utils/style'; import { isDefined } from '@js/core/utils/type'; +import type { ScrollOffset, ScrollToFunc } from '@ts/core/utils/scroll'; import { getMemoizeScrollTo } from '@ts/core/utils/scroll'; import { foreachColumnInfo } from '@ts/grids/grid_core/virtual_columns/virtual_columns_core'; +import type Scrollable from '@ts/ui/scroll_view/scrollable'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; +import type { PivotGridCell } from '../types'; +import type { + AreaItemCellAriaIndexOptions, + AreaItemCellPreparedArgs, + AreaItemComponent, + AreaItemOption, + AreaItemScrollEvent, + AreaItemStyleOptions, + AreaItemTableCss, + AreaItemVirtualContentParams, + AreaName, +} from './types'; const PIVOTGRID_EXPAND_CLASS = 'dx-expand'; -const getRealElementWidth = function (element) { +const getRealElementWidth = (element: HTMLElement): number => { let width = 0; const { offsetWidth } = element; - if (element.getBoundingClientRect) { + if (typeof element.getBoundingClientRect === 'function') { const clientRect = getBoundingRect(element); width = clientRect.width; @@ -34,7 +50,12 @@ const getRealElementWidth = function (element) { return width > 0 ? width : offsetWidth; }; -function getFakeTableOffset(scrollPos, elementOffset, tableSize, viewPortSize) { +function getFakeTableOffset( + scrollPos: number, + elementOffset: number, + tableSize: number, + viewPortSize: number, +): number { let offset = 0; let halfTableCount = 0; const halfTableSize = tableSize / 2; @@ -42,7 +63,7 @@ function getFakeTableOffset(scrollPos, elementOffset, tableSize, viewPortSize) { if (scrollPos + viewPortSize - (elementOffset + tableSize) > 1) { if (scrollPos >= elementOffset + tableSize + halfTableSize) { halfTableCount = parseInt( - ((scrollPos - (elementOffset + tableSize)) / halfTableSize) as any, + String((scrollPos - (elementOffset + tableSize)) / halfTableSize), 10, ); } @@ -51,7 +72,7 @@ function getFakeTableOffset(scrollPos, elementOffset, tableSize, viewPortSize) { } else if (scrollPos < elementOffset) { if (scrollPos <= elementOffset - halfTableSize) { halfTableCount = parseInt( - ((scrollPos - (elementOffset - halfTableSize)) / halfTableSize) as any, + String((scrollPos - (elementOffset - halfTableSize)) / halfTableSize), 10, ); } @@ -65,70 +86,72 @@ function getFakeTableOffset(scrollPos, elementOffset, tableSize, viewPortSize) { } abstract class AreaItem { - component: any; + public component: AreaItemComponent; - _tableElement: any; + protected _tableElement?: dxElementWrapper; - _tableHeight: any; + protected _tableHeight?: number; - _colgroupElement: any; + protected _colgroupElement?: dxElementWrapper; - _tableWidth: any; + protected _tableWidth?: number; - _groupElement: any; + public _groupElement?: dxElementWrapper; - _rootElement: any; + protected _rootElement?: dxElementWrapper; - _virtualContent: any; + protected _virtualContent?: dxElementWrapper; - _fakeTable: any; + protected _fakeTable?: dxElementWrapper | null; - _memoizeScrollTo: any; + protected _memoizeScrollTo?: ScrollToFunc; - constructor(component) { + constructor(component: AreaItemComponent) { this.component = component; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - option(...args: any[]): any { - return this.component.option.apply(this.component, arguments); + public option(optionName: TName): AreaItemOption { + return this.component.option(optionName); } - _getRowElement(index: number): HTMLTableRowElement | null { - if (this._tableElement?.length > 0) { - const tableElement = this._tableElement.get(0) as HTMLTableElement; + protected _getRowElement(index: number): HTMLTableRowElement | null { + if (this._tableElement && this._tableElement.length > 0) { + const tableElement = this._tableElement.get(0); return tableElement.rows[index]; } return null; } - _createGroupElement() { + protected _createGroupElement(): dxElementWrapper { return $('
'); } - _createTableElement() { + protected _createTableElement(): dxElementWrapper { return $('') .attr('id', `dx-${new Guid()}`) .attr('role', 'presentation'); } - _getCellText(cell, encodeHtml) { + protected _getCellText(cell: PivotGridCell, encodeHtml: boolean | undefined): string { let cellText = cell.isWhiteSpace ? ' ' : cell.text || ' '; - if (encodeHtml && (cellText.indexOf('<') !== -1 || cellText.indexOf('>') !== -1)) { - cellText = ($('
') as any).text(cellText).html(); + if (encodeHtml && (cellText.includes('<') || cellText.includes('>'))) { + cellText = $('
').text(cellText).html(); } return cellText; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _getRowClassNames(...args: any[]): any { + protected _getRowClassNames( + rowIndex: number, + cell: PivotGridCell | undefined, + rowClassNames: string[], + ): void; - } + protected _getRowClassNames(): void {} - _applyCustomStyles(options) { + protected _applyCustomStyles(options: AreaItemStyleOptions): void { if (options.cell.width) { options.cssArray.push(`min-width:${options.cell.width}px`); } @@ -137,26 +160,26 @@ abstract class AreaItem { } } - _getMainElementMarkup() { + protected _getMainElementMarkup(): HTMLElement { const tbody = domAdapter.createElement('tbody'); tbody.setAttribute('role', 'presentation'); return tbody; } - _isCellNavigationEnabled() { + protected _isCellNavigationEnabled(): boolean { return false; } - _getColumnIndexOffset() { + protected _getColumnIndexOffset(): number { return this.component._dataController?.getColumnIndexOffset() || 0; } - _getRowIndexOffset() { + protected _getRowIndexOffset(): number { return this.component._dataController?.getRowIndexOffset() || 0; } - _createColumnIndexMap(data) { - const columnIndexMap = new Map(); + protected _createColumnIndexMap(data: PivotGridCell[][]): Map { + const columnIndexMap = new Map(); foreachColumnInfo(data, (cell, visibleIndex, rowIndex, colIndex) => { columnIndexMap.set(`${rowIndex}-${colIndex}`, visibleIndex); @@ -165,8 +188,8 @@ abstract class AreaItem { return columnIndexMap; } - _getCellAriaRole() { - const rolesByArea = { + protected _getCellAriaRole(): string | undefined { + const rolesByArea: Partial> = { column: 'columnheader', row: 'rowheader', data: 'gridcell', @@ -175,7 +198,10 @@ abstract class AreaItem { return rolesByArea[this._getAreaName()]; } - _setCellAriaIndexAttributes(td, options) { + protected _setCellAriaIndexAttributes( + td: HTMLElement, + options: AreaItemCellAriaIndexOptions, + ): void { const { areaName, rowIndex, @@ -187,7 +213,7 @@ abstract class AreaItem { if (areaName === 'column' || areaName === 'data') { const visibleColumnIndex = areaName === 'column' - ? columnIndexMap.get(`${rowIndex}-${columnIndex}`) + ? columnIndexMap?.get(`${rowIndex}-${columnIndex}`) : columnIndex; if (visibleColumnIndex !== undefined) { @@ -200,7 +226,7 @@ abstract class AreaItem { } } - _renderTableContent(tableElement, data) { + protected _renderTableContent(tableElement: dxElementWrapper, data: PivotGridCell[][]): void { const rowsCount = data.length; const rtlEnabled = this.option('rtlEnabled'); const encodeHtml = this.option('encodeHtml'); @@ -221,7 +247,7 @@ abstract class AreaItem { for (let i = 0; i < rowsCount; i += 1) { const row = data[i]; - const rowClassNames = []; + const rowClassNames: string[] = []; const tr = domAdapter.createElement('tr'); tr.setAttribute('role', 'row'); @@ -240,14 +266,18 @@ abstract class AreaItem { const span = domAdapter.createElement('span'); if (cell) { - cell.rowspan && td.setAttribute('rowspan', cell.rowspan || 1); - cell.colspan && td.setAttribute('colspan', cell.colspan || 1); + if (cell.rowspan) { + td.setAttribute('rowspan', String(cell.rowspan)); + } + if (cell.colspan) { + td.setAttribute('colspan', String(cell.colspan)); + } if (isCellNavigationEnabled && !cell.isWhiteSpace) { td.setAttribute('tabindex', '-1'); } - const styleOptions = { + const styleOptions: AreaItemStyleOptions = { cellElement: undefined, cell, cellsCount: row.length, @@ -329,49 +359,44 @@ abstract class AreaItem { this._triggerOnCellPrepared(tableElement, data); } - abstract _getAreaName(): any; + protected abstract _getAreaName(): AreaName; - _triggerOnCellPrepared(tableElement, data) { - const that = this; + protected _triggerOnCellPrepared(tableElement: dxElementWrapper, data: PivotGridCell[][]): void { const rowElements = tableElement.find('tr'); - const areaName = that._getAreaName(); - const onCellPrepared = that.option('onCellPrepared'); - const hasEvent = that.component._eventsStrategy.hasEvent('cellPrepared'); - let rowElement; - let $cellElement; - let onCellPreparedArgs; + const areaName = this._getAreaName(); + const onCellPrepared = this.option('onCellPrepared'); + const hasEvent = this.component._eventsStrategy.hasEvent('cellPrepared'); const defaultActionArgs = this.component._defaultActionArgs(); - let row; - let cell; - let rowIndex; - let columnIndex; - - if (onCellPrepared || hasEvent) { - for (rowIndex = 0; rowIndex < data.length; rowIndex += 1) { - row = data[rowIndex]; - rowElement = rowElements.eq(rowIndex); - - for (columnIndex = 0; columnIndex < row.length; columnIndex += 1) { - cell = row[columnIndex]; - $cellElement = rowElement.children().eq(columnIndex); - onCellPreparedArgs = { - area: areaName, - rowIndex, - columnIndex, - cellElement: getPublicElement($cellElement), - cell, - }; - if (hasEvent) { - that.component._trigger('onCellPrepared', onCellPreparedArgs); - } else { - onCellPrepared(extend(onCellPreparedArgs, defaultActionArgs)); - } + + if (!onCellPrepared && !hasEvent) { + return; + } + + for (let rowIndex = 0; rowIndex < data.length; rowIndex += 1) { + const row = data[rowIndex]; + const rowElement = rowElements.eq(rowIndex); + + for (let columnIndex = 0; columnIndex < row.length; columnIndex += 1) { + const cell = row[columnIndex]; + const $cellElement = rowElement.children().eq(columnIndex); + const onCellPreparedArgs: AreaItemCellPreparedArgs = { + area: areaName, + rowIndex, + columnIndex, + cellElement: getPublicElement($cellElement), + cell, + }; + if (hasEvent) { + this.component._trigger('onCellPrepared', onCellPreparedArgs); + } else { + // @ts-expect-error the default action args are typed for an unknown component + onCellPrepared?.(extend(onCellPreparedArgs, defaultActionArgs)); } } } } - _getRowHeight(index: number): number { + protected _getRowHeight(index: number): number { const row = this._getRowElement(index); if (row?.lastChild) { @@ -383,47 +408,48 @@ abstract class AreaItem { return 0; } - _setRowHeight(index, value) { + protected _setRowHeight(index: number, value: number): void { const row = this._getRowElement(index); if (row) { row.style.height = `${value}px`; } } - getRowsLength() { - const that = this; - if (that._tableElement && that._tableElement.length > 0) { - return that._tableElement[0].rows.length; + protected getRowsLength(): number { + if (this._tableElement && this._tableElement.length > 0) { + const tableElement: HTMLTableElement = this._tableElement[0]; + return tableElement.rows.length; } return 0; } - getRowsHeight() { - const that = this; - const result: any = []; - const rowsLength = that.getRowsLength(); + public getRowsHeight(): number[] { + const result: number[] = []; + const rowsLength = this.getRowsLength(); for (let i = 0; i < rowsLength; i += 1) { - result.push(that._getRowHeight(i)); + result.push(this._getRowHeight(i)); } return result; } - setRowsHeight(values) { - const that = this; + public setRowsHeight(values: number[]): void { let totalHeight = 0; const valuesLength = values.length; for (let i = 0; i < valuesLength; i += 1) { totalHeight += values[i]; - that._setRowHeight(i, values[i]); + this._setRowHeight(i, values[i]); } this._tableHeight = totalHeight; - this._tableElement[0].style.height = `${totalHeight}px`; + const tableElement: HTMLTableElement | undefined = this._tableElement?.[0]; + if (tableElement) { + tableElement.style.height = `${totalHeight}px`; + } } - getColumnsWidth(): number[] { + public getColumnsWidth(): number[] { const rowsLength = this.getRowsLength(); const processedCells: boolean[][] = []; const result: number[] = []; @@ -458,8 +484,14 @@ abstract class AreaItem { return result; } - setColumnsWidth(values: number[]): void { - const tableElement = this._tableElement.get(0); + public setColumnsWidth(values: number[]): void { + const tableElement = this._tableElement?.get(0); + const colgroupElement = this._colgroupElement; + + if (!tableElement || !colgroupElement) { + return; + } + const columnsCount = this.getColumnsCount(); const columnWidth: number[] = []; @@ -473,12 +505,12 @@ abstract class AreaItem { } } - this._colgroupElement.html(''); + colgroupElement.html(''); for (let i = 0; i < columnsCount; i += 1) { const col = domAdapter.createElement('col'); col.style.width = `${columnWidth[i]}px`; - this._colgroupElement.append(col); + colgroupElement.append(col); } this._tableWidth = (columnsCount ? columnWidth : values) @@ -487,32 +519,36 @@ abstract class AreaItem { tableElement.style.tableLayout = 'fixed'; } - resetColumnsWidth() { - setWidth(this._colgroupElement.find('col'), 'auto'); - this._tableElement.css({ + protected resetColumnsWidth(): void { + if (this._colgroupElement) { + setWidth(this._colgroupElement.find('col'), 'auto'); + } + this._tableElement?.css({ width: '', tableLayout: '', }); } - setGroupWidth(value) { - this._getScrollable().option('width', value); + public setGroupWidth(value: number | string): void { + this._getScrollable()?.option('width', value); } - setGroupHeight(value) { - this._getScrollable().option('height', value); + public setGroupHeight(value: number | string): void { + this._getScrollable()?.option('height', value); } - getGroupHeight() { + protected getGroupHeight(): number | null { return this._getGroupElementSize('height'); } - getGroupWidth() { + public getGroupWidth(): number | null { return this._getGroupElementSize('width'); } - _getGroupElementSize(dimension) { - const size = this.groupElement()[0].style[dimension]; + protected _getGroupElementSize(dimension: 'width' | 'height'): number | null { + // @ts-expect-error the group element is rendered here + const groupElement: HTMLElement = this.groupElement()[0]; + const size = groupElement.style[dimension]; if (size.indexOf('px') > 0) { return parseFloat(size); @@ -521,188 +557,206 @@ abstract class AreaItem { return null; } - groupElement() { + public groupElement(): dxElementWrapper | undefined { return this._groupElement; } - tableElement() { + public tableElement(): dxElementWrapper | undefined { return this._tableElement; } - element() { + public element(): dxElementWrapper | undefined { return this._rootElement; } - headElement() { - return this._tableElement.find('thead'); + public headElement(): dxElementWrapper | undefined { + return this._tableElement?.find('thead'); } - _setTableCss(styles) { + protected _setTableCss(styles: AreaItemTableCss): void { if (this.option('rtlEnabled')) { styles.right = styles.left; delete styles.left; } - this.tableElement().css(styles); + this.tableElement()?.css(styles); } - setVirtualContentParams(params) { - this._virtualContent.css({ + public setVirtualContentParams(params: AreaItemVirtualContentParams): void { + this._virtualContent?.css({ width: params.width, height: params.height, }); - this.groupElement().addClass('dx-virtual-mode'); + this.groupElement()?.addClass('dx-virtual-mode'); } - disableVirtualMode() { - this.groupElement().removeClass('dx-virtual-mode'); + public disableVirtualMode(): void { + this.groupElement()?.removeClass('dx-virtual-mode'); } - _renderVirtualContent() { - const that = this; - if (!that._virtualContent && that.option('scrolling.mode') === 'virtual') { - that._virtualContent = $('
').addClass('dx-virtual-content').insertBefore(that._tableElement); + protected _renderVirtualContent(): void { + if ( + !this._virtualContent + && this._tableElement + && this.option('scrolling.mode') === 'virtual' + ) { + this._virtualContent = $('
').addClass('dx-virtual-content').insertBefore(this._tableElement); } } - reset() { - const that = this; - const tableElement = that._tableElement[0]; + public reset(): void { + const tableElement: HTMLTableElement | undefined = this._tableElement?.[0]; - that._fakeTable && that._fakeTable.detach(); - that._fakeTable = null; + this._fakeTable?.detach(); + this._fakeTable = null; - that.disableVirtualMode(); - that.setGroupWidth('100%'); - that.setGroupHeight('auto'); + this.disableVirtualMode(); + this.setGroupWidth('100%'); + this.setGroupHeight('auto'); - that.resetColumnsWidth(); + this.resetColumnsWidth(); if (tableElement) { - for (let i = 0; i < tableElement.rows.length; i += 1) { - tableElement.rows[i].style.height = ''; + for (const row of tableElement.rows) { + row.style.height = ''; } tableElement.style.height = ''; tableElement.style.width = '100%'; } } - _updateFakeTableVisibility() { - const that = this; - const tableElement = that.tableElement()[0]; - const horizontalOffsetName = that.option('rtlEnabled') ? 'right' : 'left'; - const fakeTableElement = that._fakeTable[0]; + protected _updateFakeTableVisibility(): void { + const tableElement: HTMLElement | undefined = this.tableElement()?.[0]; + const horizontalOffsetName = this.option('rtlEnabled') ? 'right' : 'left'; + const fakeTable = this._fakeTable; + + if (!tableElement || !fakeTable) { + return; + } + + const fakeTableElement: HTMLElement = fakeTable[0]; if ( tableElement.style.top === fakeTableElement.style.top && fakeTableElement.style[horizontalOffsetName] === tableElement.style[horizontalOffsetName] ) { - that._fakeTable.addClass('dx-hidden'); + fakeTable.addClass('dx-hidden'); } else { - that._fakeTable.removeClass('dx-hidden'); + fakeTable.removeClass('dx-hidden'); } } - _moveFakeTableHorizontally(scrollPos) { - const that: any = this; - const rtlEnabled = that.option('rtlEnabled'); + protected _moveFakeTableHorizontally(scrollPos: number | undefined): void { + const rtlEnabled = this.option('rtlEnabled'); const offsetStyleName = rtlEnabled ? 'right' : 'left'; - const tableElementOffset = parseFloat(that.tableElement()[0].style[offsetStyleName]); + const tableElement: HTMLElement | undefined = this.tableElement()?.[0]; + const fakeTableElement: HTMLElement | undefined = this._fakeTable?.[0]; + + if (!tableElement || !fakeTableElement) { + return; + } + + const tableElementOffset = parseFloat(tableElement.style[offsetStyleName]); const offset = getFakeTableOffset( - scrollPos, + scrollPos ?? NaN, tableElementOffset, - that._tableWidth, - that.getGroupWidth(), + this._tableWidth ?? NaN, + this.getGroupWidth() ?? 0, ); - if (parseFloat(that._fakeTable[0].style[offsetStyleName]) !== offset) { - that._fakeTable[0].style[offsetStyleName] = `${offset}px`; + if (parseFloat(fakeTableElement.style[offsetStyleName]) !== offset) { + fakeTableElement.style[offsetStyleName] = `${offset}px`; } } - _moveFakeTableTop(scrollPos) { - const that = this; - const tableElementOffsetTop = parseFloat(that.tableElement()[0].style.top); + protected _moveFakeTableTop(scrollPos: number | undefined): void { + const tableElement: HTMLElement | undefined = this.tableElement()?.[0]; + const fakeTableElement: HTMLElement | undefined = this._fakeTable?.[0]; + + if (!tableElement || !fakeTableElement) { + return; + } + + const tableElementOffsetTop = parseFloat(tableElement.style.top); const offsetTop = getFakeTableOffset( - scrollPos, + scrollPos ?? NaN, tableElementOffsetTop, - that._tableHeight, - that.getGroupHeight(), + this._tableHeight ?? NaN, + this.getGroupHeight() ?? 0, ); - if (parseFloat(that._fakeTable[0].style.top) !== offsetTop) { - that._fakeTable[0].style.top = `${offsetTop}px`; + if (parseFloat(fakeTableElement.style.top) !== offsetTop) { + fakeTableElement.style.top = `${offsetTop}px`; } } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - _moveFakeTable(...args: any[]) { + protected _moveFakeTable(scrollPos?: ScrollOffset): void; + + protected _moveFakeTable(): void { this._updateFakeTableVisibility(); } - _createFakeTable() { - const that = this; + protected _createFakeTable(): void { + const tableElement = this.tableElement(); - if (!that._fakeTable) { - that._fakeTable = that.tableElement() + if (!this._fakeTable && tableElement && this._virtualContent) { + this._fakeTable = tableElement .clone() .removeAttr('id') .attr('aria-hidden', 'true') .addClass(FAKE_TABLE_CLASS); - that._fakeTable.find('[tabindex]').removeAttr('tabindex'); - that._fakeTable.appendTo(that._virtualContent); + this._fakeTable.find('[tabindex]').removeAttr('tabindex'); + this._fakeTable.appendTo(this._virtualContent); } } - render(rootElement, data) { - const that = this; - - if (that._tableElement) { + public render(rootElement: dxElementWrapper, data: PivotGridCell[][]): void { + if (this._tableElement) { try { - that._tableElement[0].innerHTML = ''; + this._tableElement[0].innerHTML = ''; } catch (e) { - that._tableElement.empty(); + this._tableElement.empty(); } - that._tableElement.removeAttr('style'); + this._tableElement.removeAttr('style'); } else { - that._groupElement = that._createGroupElement(); - that._tableElement = that._createTableElement(); + this._groupElement = this._createGroupElement(); + this._tableElement = this._createTableElement(); - that._tableElement.appendTo(that._groupElement); - that._groupElement.appendTo(rootElement); - that._rootElement = rootElement; + this._tableElement.appendTo(this._groupElement); + this._groupElement.appendTo(rootElement); + this._rootElement = rootElement; } - that._colgroupElement = $('
').appendTo(that._tableElement); - that._renderTableContent(that._tableElement, data); + this._colgroupElement = $('').appendTo(this._tableElement); + this._renderTableContent(this._tableElement, data); - that._renderVirtualContent(); + this._renderVirtualContent(); } - _getScrollable() { - return this.groupElement().data('dxScrollable'); + public _getScrollable(): Scrollable | undefined { + const scrollable: Scrollable | undefined = this.groupElement()?.data('dxScrollable'); + return scrollable; } - _getMemoizeScrollTo() { - this._memoizeScrollTo = this._memoizeScrollTo - ?? getMemoizeScrollTo(() => this._getScrollable()); + protected _getMemoizeScrollTo(): ScrollToFunc { + // @ts-expect-error the scrollable is rendered here + this._memoizeScrollTo ??= getMemoizeScrollTo(() => this._getScrollable()); return this._memoizeScrollTo; } - _getMaxLeftOffset(scrollable) { + protected _getMaxLeftOffset(scrollable: Scrollable): number { const containerElement = $(scrollable.container()).get(0); return containerElement.scrollWidth - containerElement.clientWidth; } - on(eventName, handler) { - const that = this; - const scrollable = that._getScrollable(); + public on(eventName: string, handler: (e: AreaItemScrollEvent) => void): this { + const scrollable = this._getScrollable(); if (scrollable) { - scrollable.on(eventName, (e) => { - if (that.option('rtlEnabled') && isDefined(e.scrollOffset.left)) { - e.scrollOffset.left = that._getMaxLeftOffset(scrollable) - e.scrollOffset.left; + scrollable.on(eventName, (e: AreaItemScrollEvent): void => { + if (this.option('rtlEnabled') && isDefined(e.scrollOffset.left)) { + e.scrollOffset.left = this._getMaxLeftOffset(scrollable) - e.scrollOffset.left; } handler(e); }); @@ -710,7 +764,7 @@ abstract class AreaItem { return this; } - off(eventName) { + public off(eventName: string): this { const scrollable = this._getScrollable(); if (scrollable) { scrollable.off(eventName); @@ -718,7 +772,7 @@ abstract class AreaItem { return this; } - scrollTo(pos, force = false) { + public scrollTo(pos: ScrollOffset, force = false): void { const scrollable = this._getScrollable(); if (!scrollable) { return; @@ -729,7 +783,7 @@ abstract class AreaItem { const scrollablePos = { ...pos, left: rtlEnabled && (areaName === 'column' || areaName === 'data') - ? this._getMaxLeftOffset(scrollable) - pos.left + ? this._getMaxLeftOffset(scrollable) - (pos.left ?? NaN) : pos.left, }; @@ -742,14 +796,14 @@ abstract class AreaItem { } } - scrollToElement(element) { + public scrollToElement(element: Element | dxElementWrapper): void { const scrollable = this._getScrollable(); if (scrollable) { scrollable.scrollToElement(element); } } - updateScrollable() { + public updateScrollable(): DeferredObj | undefined { const scrollable = this._getScrollable(); if (scrollable) { return scrollable.update(); @@ -758,25 +812,23 @@ abstract class AreaItem { return undefined; } - getColumnsCount() { + public getColumnsCount(): number { let columnCount = 0; const row = this._getRowElement(0); - let cells; if (row) { - cells = row.cells; - // eslint-disable-next-line no-plusplus - for (let i = 0, len = cells.length; i < len; ++i) { - columnCount += cells[i].colSpan; + for (const cell of row.cells) { + columnCount += cell.colSpan; } } return columnCount; } - getData() { + public getData(): PivotGridCell[][] { const tableElement = this._tableElement; - return tableElement ? tableElement.data('data') : []; + const data: PivotGridCell[][] = tableElement ? tableElement.data('data') : []; + return data; } } diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts new file mode 100644 index 000000000000..b850e61720f8 --- /dev/null +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts @@ -0,0 +1,72 @@ +import type { dxElementWrapper } from '@js/core/renderer'; +import type { CellPreparedEvent, Properties as PivotGridProperties } from '@js/ui/pivot_grid'; +import type { ScrollOffset } from '@ts/core/utils/scroll'; +import type { DefaultActionArgs } from '@ts/core/widget/component'; +import type DOMComponent from '@ts/core/widget/dom_component'; + +import type { PivotGridCell } from '../types'; + +export type AreaName = 'column' | 'row' | 'data' | 'fields'; + +export type AreaItemOption = TName extends keyof PivotGridProperties + ? PivotGridProperties[TName] + : unknown; + +export interface AreaItemComponent { + option: (optionName: TName) => AreaItemOption; + $element: () => dxElementWrapper; + _createComponent: typeof DOMComponent.prototype._createComponent; + _dataController?: { + getColumnIndexOffset: () => number; + getRowIndexOffset: () => number; + }; + _eventsStrategy: { + hasEvent: (eventName: string) => boolean; + }; + _defaultActionArgs: () => DefaultActionArgs; + _trigger: (eventName: string, eventArg: unknown) => void; +} + +export interface AreaItemStyleOptions { + cellElement: undefined; + cell: PivotGridCell; + cellsCount: number; + cellIndex: number; + rowElement: undefined; + rowIndex: number; + rowsCount: number; + rtlEnabled: boolean | undefined; + classArray: string[]; + cssArray: string[]; +} + +export interface AreaItemCellAriaIndexOptions { + areaName: AreaName; + rowIndex: number; + columnIndex: number; + columnIndexMap: Map | null; + columnIndexOffset: number; + rowIndexOffset: number; +} + +export type AreaItemCellPreparedArgs = Pick< + CellPreparedEvent, + 'area' | 'rowIndex' | 'columnIndex' | 'cellElement' | 'cell' +>; + +export interface AreaItemVirtualContentParams { + top?: number; + left?: number; + width: number; + height: number; +} + +export interface AreaItemTableCss { + top?: number; + left?: number; + right?: number; +} + +export interface AreaItemScrollEvent { + scrollOffset: ScrollOffset; +} diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts index b6a8c9e31df1..82e608aff68d 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts @@ -9,10 +9,12 @@ import { } from '@jest/globals'; import $ from '@js/core/renderer'; +import type { AreaItemComponent } from '../area_item/types'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; +import type { PivotGridCell } from '../types'; import { DataArea } from './m_data_area'; -const createComponent = (options: Record = {}): unknown => ({ +const createComponent = (options: Record = {}): AreaItemComponent => ({ option: (optionName?: string): unknown => { if (optionName === undefined) { return {}; @@ -26,7 +28,7 @@ const createComponent = (options: Record = {}): unknown => ({ }, _eventsStrategy: { hasEvent: () => false }, _defaultActionArgs: () => ({}), -}); +}) as unknown as AreaItemComponent; describe('DataArea', () => { let container: HTMLElement = document.createElement('div'); @@ -36,7 +38,7 @@ describe('DataArea', () => { [{ text: '3' }, { text: '4' }], ]; - const renderDataArea = (data: unknown[]): DataArea => { + const renderDataArea = (data: PivotGridCell[][]): DataArea => { const area = new DataArea(createComponent()); area.render($(container), data); @@ -77,7 +79,7 @@ describe('DataArea', () => { }); describe('setColumnsWidth', () => { - const getTableWidth = (): string => container.querySelector('table')!.style.width; + const getTableWidth = (): string | undefined => container.querySelector('table')?.style.width; const getColumnWidths = (): string[] => Array .from(container.querySelectorAll('col')) @@ -145,16 +147,16 @@ describe('DataArea', () => { return area; }; - const getFakeTable = (): HTMLElement => container.querySelector(`.${FAKE_TABLE_CLASS}`)!; + const getFakeTable = (): HTMLElement | null => container.querySelector(`.${FAKE_TABLE_CLASS}`); it('should show the fake table at the position scrolled past the rendered table', () => { const area = renderVirtualDataArea(); area.scrollTo({ left: POSITION_PAST_TABLE, top: POSITION_PAST_TABLE }); - expect(getFakeTable().style.left).toBe(`${POSITION_PAST_TABLE}px`); - expect(getFakeTable().style.top).toBe(`${POSITION_PAST_TABLE}px`); - expect(getFakeTable().classList).not.toContain('dx-hidden'); + expect(getFakeTable()?.style.left).toBe(`${POSITION_PAST_TABLE}px`); + expect(getFakeTable()?.style.top).toBe(`${POSITION_PAST_TABLE}px`); + expect(getFakeTable()?.classList).not.toContain('dx-hidden'); }); it('should hide the fake table when scrolled back to the rendered table', () => { @@ -163,9 +165,9 @@ describe('DataArea', () => { area.scrollTo({ left: POSITION_PAST_TABLE, top: POSITION_PAST_TABLE }); area.scrollTo({ left: TABLE_OFFSET, top: TABLE_OFFSET }); - expect(getFakeTable().style.left).toBe(`${TABLE_OFFSET}px`); - expect(getFakeTable().style.top).toBe(`${TABLE_OFFSET}px`); - expect(getFakeTable().classList).toContain('dx-hidden'); + expect(getFakeTable()?.style.left).toBe(`${TABLE_OFFSET}px`); + expect(getFakeTable()?.style.top).toBe(`${TABLE_OFFSET}px`); + expect(getFakeTable()?.classList).toContain('dx-hidden'); }); it('should offset the fake table from the right when rtlEnabled is true', () => { @@ -173,8 +175,8 @@ describe('DataArea', () => { area.scrollTo({ left: POSITION_PAST_TABLE, top: TABLE_OFFSET }); - expect(getFakeTable().style.right).toBe(`${POSITION_PAST_TABLE}px`); - expect(getFakeTable().classList).not.toContain('dx-hidden'); + expect(getFakeTable()?.style.right).toBe(`${POSITION_PAST_TABLE}px`); + expect(getFakeTable()?.classList).not.toContain('dx-hidden'); }); }); }); diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts index 4e6932dceb1f..d7f5bd8da4aa 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts @@ -1,9 +1,17 @@ -/* eslint-disable class-methods-use-this */ +import type { ScrollDirection } from '@js/common'; +import type { dxElementWrapper } from '@js/core/renderer'; import $ from '@js/core/renderer'; import { setTabIndex } from '@js/ui/shared/accessibility'; +import type { ScrollOffset } from '@ts/core/utils/scroll'; import supportUtils from '@ts/core/utils/support'; import { AreaItem } from '../area_item/m_area_item'; +import type { + AreaItemStyleOptions, + AreaItemVirtualContentParams, + AreaName, +} from '../area_item/types'; +import type { PivotGridCell } from '../types'; import { calculateScrollbarWidth } from '../widget_utils'; const PIVOTGRID_AREA_CLASS = 'dx-pivotgrid-area'; @@ -13,32 +21,32 @@ const PIVOTGRID_GRAND_TOTAL_CLASS = 'dx-grandtotal'; const PIVOTGRID_ROW_TOTAL_CLASS = 'dx-row-total'; class DataArea extends AreaItem { - _getAreaName() { + protected _getAreaName(): AreaName { return 'data'; } - render(rootElement, data) { + public render(rootElement: dxElementWrapper, data: PivotGridCell[][]): void { super.render(rootElement, data); this._makeFirstCellFocusable(); } - _makeFirstCellFocusable() { - const firstCell = this.tableElement().get(0)?.querySelector('td'); + protected _makeFirstCellFocusable(): void { + const firstCell = this.tableElement()?.get(0)?.querySelector('td'); if (firstCell) { setTabIndex(this.component, $(firstCell)); } } - _createGroupElement() { + protected _createGroupElement(): dxElementWrapper { return $('
') .addClass(PIVOTGRID_AREA_CLASS) .addClass(PIVOTGRID_AREA_DATA_CLASS) .css('borderTopWidth', 0); } - _applyCustomStyles(options) { + protected _applyCustomStyles(options: AreaItemStyleOptions): void { const { cell } = options; const { classArray } = options; @@ -60,14 +68,15 @@ class DataArea extends AreaItem { super._applyCustomStyles(options); } - _moveFakeTable(scrollPos) { + protected _moveFakeTable(scrollPos: ScrollOffset): void { this._moveFakeTableHorizontally(scrollPos.left); this._moveFakeTableTop(scrollPos.top); super._moveFakeTable(); } - renderScrollable() { - this._groupElement.dxScrollable({ + public renderScrollable(): void { + // @ts-expect-error dxScrollable is added to the renderer at runtime + this._groupElement?.dxScrollable({ useNative: this.getUseNativeValue(), useSimulatedScrollbar: false, rtlEnabled: this.component.option('rtlEnabled'), @@ -77,26 +86,33 @@ class DataArea extends AreaItem { }); } - getUseNativeValue() { - const { useNative } = this.component.option('scrolling'); + public getUseNativeValue(): boolean { + const useNative = this.component.option('scrolling')?.useNative; return useNative === 'auto' ? !!supportUtils.nativeScrolling : !!useNative; } - getScrollbarWidth() { + public getScrollbarWidth(): number { return this.getUseNativeValue() ? calculateScrollbarWidth() : 0; } - updateScrollableOptions({ direction, rtlEnabled }) { + public updateScrollableOptions({ direction, rtlEnabled }: { + direction: ScrollDirection; + rtlEnabled: boolean | undefined; + }): void { const scrollable = this._getScrollable(); + if (!scrollable) { + return; + } + scrollable.option('useNative', this.getUseNativeValue()); scrollable.option({ direction, rtlEnabled }); } - getScrollableDirection(horizontal, vertical) { + public getScrollableDirection(horizontal: boolean, vertical: boolean): ScrollDirection { if (horizontal && !vertical) { return 'horizontal'; } if (!horizontal && vertical) { @@ -106,17 +122,17 @@ class DataArea extends AreaItem { return 'both'; } - reset() { + public reset(): void { super.reset(); if (this._virtualContent) { this._virtualContent.parent().css('height', 'auto'); } } - setVirtualContentParams(params) { + public setVirtualContentParams(params: AreaItemVirtualContentParams): void { super.setVirtualContentParams(params); - this._virtualContent.parent().css('height', params.height); + this._virtualContent?.parent().css('height', params.height); this._setTableCss({ top: params.top, diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts index 5f2ce5e9afc6..eb96941b1221 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts @@ -9,6 +9,7 @@ import Popup from '@js/ui/popup/ui.popup'; import { capitalize } from '@ts/core/utils/capitalize'; import { AreaItem } from '../area_item/m_area_item'; +import type { AreaName } from '../area_item/types'; const DIV = '
'; @@ -34,7 +35,7 @@ class FieldsArea extends AreaItem { this._area = area; } - _getAreaName() { + _getAreaName(): AreaName { return 'fields'; } @@ -74,6 +75,7 @@ class FieldsArea extends AreaItem { icon: 'menu', width: 'auto', onClick() { + // @ts-expect-error dxPopup is added to the renderer at runtime const popup = that.tableElement().find('.dx-fields-area-popup').dxPopup('instance'); if (!popup.option('visible')) { popup.show(); @@ -117,6 +119,7 @@ class FieldsArea extends AreaItem { const that = this; const button = tableElement.find('.dx-button'); const popupOptions = that._getPopupOptions(row, button); + // @ts-expect-error dxPivotGridFieldChooserBase is added to the renderer at runtime const FieldChooserBase = that.component.$element().dxPivotGridFieldChooserBase('instance'); if (that._rowPopup) { @@ -126,6 +129,7 @@ class FieldsArea extends AreaItem { that._rowPopup = that.component._createComponent( $(DIV).appendTo(tableElement), Popup, + // @ts-expect-error the fields area popup options aren't typed yet popupOptions, ); @@ -147,6 +151,7 @@ class FieldsArea extends AreaItem { const that = this; const groupElement = this.groupElement(); const isVisible = this.isVisible(); + // @ts-expect-error dxPivotGridFieldChooserBase is added to the renderer at runtime const fieldChooserBase = that.component.$element().dxPivotGridFieldChooserBase('instance'); const head = $('
') .addClass('dx-pivotgrid-fields-area-head') @@ -155,6 +160,7 @@ class FieldsArea extends AreaItem { const area = that._area; const row = $(''); + // @ts-expect-error the group element is rendered here groupElement.toggleClass('dx-hidden', !isVisible); tableElement.addClass('dx-area-field-container'); @@ -197,6 +203,7 @@ class FieldsArea extends AreaItem { tableElement .attr('role', 'presentation') .removeAttr('aria-label'); + // @ts-expect-error option() returns unknown for a template path $('') .addClass('dx-pivotgrid-fields-area-head') .attr('role', 'presentation') @@ -160,8 +168,7 @@ class FieldsArea extends AreaItem { const area = that._area; const row = $(''); - // @ts-expect-error the group element is rendered here - groupElement.toggleClass('dx-hidden', !isVisible); + groupElement?.toggleClass('dx-hidden', !isVisible); tableElement.addClass('dx-area-field-container'); if (!isVisible) { @@ -225,8 +232,7 @@ class FieldsArea extends AreaItem { reset() { super.reset(); - // @ts-expect-error the group element is rendered here - this.groupElement().css('marginTop', 0); + this.groupElement()?.css('marginTop', 0); } _renderVirtualContent() {} diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts index 9c4340384252..0fbff86e5d69 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts @@ -73,26 +73,32 @@ class HorizontalHeadersArea extends AreaItem { } protected _applyCustomStyles(options: AreaItemStyleOptions): void { - const { cssArray } = options; - const { cell } = options; - const { rowsCount } = options; - const { classArray } = options; - - if (options.cellIndex === options.cellsCount - 1) { - cssArray.push(`${options.rtlEnabled ? 'border-left:' : 'border-right:'}0px`); + const { + cell, + cssArray, + classArray, + cellIndex, + cellsCount, + rowIndex, + rowsCount, + rtlEnabled, + } = options; + + if (cellIndex === cellsCount - 1) { + cssArray.push(`${rtlEnabled ? 'border-left:' : 'border-right:'}0px`); } - if ((cell.rowspan === rowsCount - options.rowIndex) || (options.rowIndex + 1 === rowsCount)) { + if ((cell.rowspan === rowsCount - rowIndex) || (rowIndex + 1 === rowsCount)) { cssArray.push('border-bottom-width:0px'); } if (cell.type === 'T' || cell.type === 'GT') { classArray.push(PIVOTGRID_ROW_TOTAL_CLASS); } - if (options.cell.type === 'T') { + if (cell.type === 'T') { classArray.push(PIVOTGRID_TOTAL_CLASS); } - if (options.cell.type === 'GT') { + if (cell.type === 'GT') { classArray.push(PIVOTGRID_GRAND_TOTAL_CLASS); } @@ -132,8 +138,7 @@ class HorizontalHeadersArea extends AreaItem { } public renderScrollable(): void { - // @ts-expect-error dxScrollable is added to the renderer at runtime - this._groupElement?.dxScrollable({ + this.renderGroupScrollable({ useNative: false, useSimulatedScrollbar: false, showScrollbar: 'never', @@ -176,7 +181,7 @@ class HorizontalHeadersArea extends AreaItem { return undefined; } - const tableOffset = offset - (parseInt(tableElement[0].style.left, 10) || 0); + const tableOffset = offset - (parseInt(tableElement.get(0).style.left, 10) || 0); const cells = tableElement.find('td').toArray() as HTMLTableCellElement[]; const cell = cells.find((td) => td.colSpan === 1 && td.offsetLeft <= tableOffset @@ -239,8 +244,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { } public renderScrollable(): void { - // @ts-expect-error dxScrollable is added to the renderer at runtime - this._groupElement?.dxScrollable({ + this.renderGroupScrollable({ useNative: false, useSimulatedScrollbar: false, showScrollbar: 'never', @@ -285,7 +289,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { return undefined; } - const tableOffset = offset - (parseInt(tableElement[0].style.top, 10) || 0); + const tableOffset = offset - (parseInt(tableElement.get(0).style.top, 10) || 0); const lastCells = tableElement.find('tr').toArray() .map((tr) => tr.childNodes[tr.childNodes.length - 1] as HTMLTableCellElement | undefined); const cell = lastCells.find((td) => td?.rowSpan === 1 @@ -318,7 +322,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { } public updateColspans(columnCount: number): void { - const tableElement: HTMLTableElement | undefined = this.tableElement()?.[0]; + const tableElement = this.tableElement()?.get(0); if (!tableElement) { return; From 271f27351c5e31b5b3dee8e869639ba6a4a561a5 Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:58:46 +0400 Subject: [PATCH 6/7] refactor(grids): drop this aliases in pivot grid fields area --- .../pivot_grid/fields_area/m_fields_area.ts | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts index 498d3d0c755f..8a6d3e5d7235 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts @@ -118,30 +118,29 @@ class FieldsArea extends AreaItem { } _renderPopup(tableElement, row) { - const that = this; const button = tableElement.find('.dx-button'); - const popupOptions = that._getPopupOptions(row, button); - const fieldChooserBase = that.getFieldChooserBase(); + const popupOptions = this._getPopupOptions(row, button); + const fieldChooserBase = this.getFieldChooserBase(); - if (that._rowPopup) { - that._rowPopup.$element().remove(); + if (this._rowPopup) { + this._rowPopup.$element().remove(); } - that._rowPopup = that.component._createComponent( + this._rowPopup = this.component._createComponent( $(DIV).appendTo(tableElement), Popup, popupOptions, ); - that._rowPopup.$element().addClass('dx-fields-area-popup'); - that._rowPopup.content() + this._rowPopup.$element().addClass('dx-fields-area-popup'); + this._rowPopup.content() .addClass('dx-pivotgrid-fields-container'); - that._rowPopup.content().parent() + this._rowPopup.content().parent() .attr('group', 'row'); - fieldChooserBase.subscribeToEvents(that._rowPopup.content()); + fieldChooserBase.subscribeToEvents(this._rowPopup.content()); // @ts-expect-error renderSortable() ignores the element - fieldChooserBase.renderSortable(that._rowPopup.content()); + fieldChooserBase.renderSortable(this._rowPopup.content()); } private getFieldChooserBase(): FieldChooserBase { @@ -157,15 +156,14 @@ class FieldsArea extends AreaItem { } _renderTableContent(tableElement, data) { - const that = this; const groupElement = this.groupElement(); const isVisible = this.isVisible(); - const fieldChooserBase = that.getFieldChooserBase(); + const fieldChooserBase = this.getFieldChooserBase(); const head = $('') .addClass('dx-pivotgrid-fields-area-head') .attr('role', 'presentation') .appendTo(tableElement); - const area = that._area; + const area = this._area; const row = $(''); groupElement?.toggleClass('dx-hidden', !isVisible); @@ -186,7 +184,7 @@ class FieldsArea extends AreaItem { .attr('role', 'none') .append(fieldChooserBase.renderField(field, field.area === 'row')); const indicators = td.find('.dx-column-indicators'); - if (indicators.length && that._shouldCreateButton()) { + if (indicators.length && this._shouldCreateButton()) { indicators.insertAfter((indicators as any).next()); } td.appendTo(row); @@ -214,9 +212,9 @@ class FieldsArea extends AreaItem { $('
').append($(DIV).addClass('dx-empty-area-text').text(this.option(`fieldPanel.texts.${area}FieldArea`))).appendTo(row); } @@ -218,6 +225,7 @@ class FieldsArea extends AreaItem { reset() { super.reset(); + // @ts-expect-error the group element is rendered here this.groupElement().css('marginTop', 0); } diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts index 3f4f63807607..16676b3dfd3d 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts @@ -9,6 +9,7 @@ import { } from '@jest/globals'; import $ from '@js/core/renderer'; +import type { AreaItemComponent } from '../area_item/types'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; import { HorizontalHeadersArea, VerticalHeadersArea } from './m_headers_area'; @@ -16,7 +17,7 @@ const TABLE_OFFSET = 1000; const CELL_SIZES = [100, 120, 300]; const POSITION_PAST_TABLE = TABLE_OFFSET + 520; -const createComponent = (): unknown => ({ +const createComponent = (): AreaItemComponent => ({ option: (optionName?: string): unknown => { if (optionName === undefined) { return {}; @@ -30,12 +31,12 @@ const createComponent = (): unknown => ({ }, _eventsStrategy: { hasEvent: () => false }, _defaultActionArgs: () => ({}), -}); +}) as unknown as AreaItemComponent; describe('Headers areas', () => { let container: HTMLElement = document.createElement('div'); - const getFakeTable = (): HTMLElement => container.querySelector(`.${FAKE_TABLE_CLASS}`)!; + const getFakeTable = (): HTMLElement | null => container.querySelector(`.${FAKE_TABLE_CLASS}`); beforeEach(() => { container = document.createElement('div'); @@ -64,8 +65,8 @@ describe('Headers areas', () => { area.scrollTo({ left: POSITION_PAST_TABLE }); - expect(getFakeTable().style.left).toBe(`${POSITION_PAST_TABLE}px`); - expect(getFakeTable().classList).not.toContain('dx-hidden'); + expect(getFakeTable()?.style.left).toBe(`${POSITION_PAST_TABLE}px`); + expect(getFakeTable()?.classList).not.toContain('dx-hidden'); }); it('should hide the fake table when scrolled back to the rendered table', () => { @@ -74,8 +75,8 @@ describe('Headers areas', () => { area.scrollTo({ left: POSITION_PAST_TABLE }); area.scrollTo({ left: TABLE_OFFSET }); - expect(getFakeTable().style.left).toBe(`${TABLE_OFFSET}px`); - expect(getFakeTable().classList).toContain('dx-hidden'); + expect(getFakeTable()?.style.left).toBe(`${TABLE_OFFSET}px`); + expect(getFakeTable()?.classList).toContain('dx-hidden'); }); }); @@ -97,8 +98,8 @@ describe('Headers areas', () => { area.scrollTo({ top: POSITION_PAST_TABLE }); - expect(getFakeTable().style.top).toBe(`${POSITION_PAST_TABLE}px`); - expect(getFakeTable().classList).not.toContain('dx-hidden'); + expect(getFakeTable()?.style.top).toBe(`${POSITION_PAST_TABLE}px`); + expect(getFakeTable()?.classList).not.toContain('dx-hidden'); }); it('should hide the fake table when scrolled back to the rendered table', () => { @@ -107,8 +108,8 @@ describe('Headers areas', () => { area.scrollTo({ top: POSITION_PAST_TABLE }); area.scrollTo({ top: TABLE_OFFSET }); - expect(getFakeTable().style.top).toBe(`${TABLE_OFFSET}px`); - expect(getFakeTable().classList).toContain('dx-hidden'); + expect(getFakeTable()?.style.top).toBe(`${TABLE_OFFSET}px`); + expect(getFakeTable()?.classList).toContain('dx-hidden'); }); }); }); diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts index d28127551153..94b01cc2dfeb 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts @@ -1,15 +1,23 @@ /* eslint-disable max-classes-per-file */ import domAdapter from '@js/core/dom_adapter'; +import type { dxElementWrapper } from '@js/core/renderer'; import $ from '@js/core/renderer'; -import { each } from '@js/core/utils/iterator'; import { setHeight, setWidth } from '@js/core/utils/size'; import { isDefined } from '@js/core/utils/type'; +import type { ScrollOffset } from '@ts/core/utils/scroll'; import { AreaItem } from '../area_item/m_area_item'; +import type { + AreaItemComponent, + AreaItemStyleOptions, + AreaItemVirtualContentParams, + AreaName, +} from '../area_item/types'; import { HORIZONTAL_HEADERS_AREA_CLASS as PIVOTGRID_AREA_COLUMN_CLASS, VERTICAL_HEADERS_AREA_CLASS as PIVOTGRID_AREA_ROW_CLASS, } from '../keyboard_navigation/const'; +import type { PivotGridCell } from '../types'; const PIVOTGRID_AREA_CLASS = 'dx-pivotgrid-area'; const PIVOTGRID_TOTAL_CLASS = 'dx-total'; @@ -21,10 +29,13 @@ const PIVOTGRID_LAST_CELL_CLASS = 'dx-last-cell'; const PIVOTGRID_VERTICAL_SCROLL_CLASS = 'dx-vertical-scroll'; const PIVOTGRID_EXPAND_BORDER = 'dx-expand-border'; -function getCellPath(tableElement, cell) { +function getCellPath( + tableElement: dxElementWrapper, + cell: HTMLTableCellElement | undefined, +): PivotGridCell['path'] { if (cell) { - const { data } = tableElement.data(); - const { rowIndex } = cell.parentNode; + const { data } = tableElement.data<{ data: PivotGridCell[][] }>(); + const { rowIndex } = cell.parentNode as HTMLTableRowElement; const { cellIndex } = cell; return data[rowIndex] && data[rowIndex][cellIndex] && data[rowIndex][cellIndex].path; @@ -34,34 +45,34 @@ function getCellPath(tableElement, cell) { } class HorizontalHeadersArea extends AreaItem { - _scrollBarWidth: any; + protected _scrollBarWidth: number; - _virtualContentWidth: any; + protected _virtualContentWidth?: number; - constructor(component) { + constructor(component: AreaItemComponent) { super(component); this._scrollBarWidth = 0; } - _getAreaName() { + protected _getAreaName(): AreaName { return 'column'; } - _getAreaClassName() { + protected _getAreaClassName(): string { return PIVOTGRID_AREA_COLUMN_CLASS; } - _isCellNavigationEnabled() { + protected _isCellNavigationEnabled(): boolean { return true; } - _createGroupElement() { + protected _createGroupElement(): dxElementWrapper { return $('
') .addClass(this._getAreaClassName()) .addClass(PIVOTGRID_AREA_CLASS); } - _applyCustomStyles(options) { + protected _applyCustomStyles(options: AreaItemStyleOptions): void { const { cssArray } = options; const { cell } = options; const { rowsCount } = options; @@ -92,14 +103,14 @@ class HorizontalHeadersArea extends AreaItem { super._applyCustomStyles(options); } - _getMainElementMarkup() { + protected _getMainElementMarkup(): HTMLElement { const thead = domAdapter.createElement('thead'); thead.setAttribute('class', this._getAreaClassName()); thead.setAttribute('role', 'presentation'); return thead; } - setVirtualContentParams(params) { + public setVirtualContentParams(params: AreaItemVirtualContentParams): void { super.setVirtualContentParams(params); this._setTableCss({ @@ -110,7 +121,7 @@ class HorizontalHeadersArea extends AreaItem { this._virtualContentWidth = params.width; } - hasScroll() { + public hasScroll(): boolean { const tableWidth = this._virtualContent ? this._virtualContentWidth : this._tableWidth; const groupWidth = this.getGroupWidth(); @@ -120,8 +131,9 @@ class HorizontalHeadersArea extends AreaItem { return false; } - renderScrollable() { - this._groupElement.dxScrollable({ + public renderScrollable(): void { + // @ts-expect-error dxScrollable is added to the renderer at runtime + this._groupElement?.dxScrollable({ useNative: false, useSimulatedScrollbar: false, showScrollbar: 'never', @@ -133,7 +145,13 @@ class HorizontalHeadersArea extends AreaItem { }); } - processScrollBarSpacing(scrollBarWidth) { + public processScrollBarSpacing(scrollBarWidth: number): void { + const groupElement = this._groupElement; + + if (!groupElement) { + return; + } + const groupAlignment = this.option('rtlEnabled') ? 'right' : 'left'; const groupWidth = this.getGroupWidth(); @@ -142,47 +160,45 @@ class HorizontalHeadersArea extends AreaItem { } if (this._scrollBarWidth) { - this._groupElement.next().remove(); + groupElement.next().remove(); } - this._groupElement.toggleClass(PIVOTGRID_VERTICAL_SCROLL_CLASS, scrollBarWidth > 0); + groupElement.toggleClass(PIVOTGRID_VERTICAL_SCROLL_CLASS, scrollBarWidth > 0); - setWidth(this._groupElement.css('float', groupAlignment), this.getGroupHeight()); + setWidth(groupElement.css('float', groupAlignment), this.getGroupHeight()); this._scrollBarWidth = scrollBarWidth; } - getScrollPath(offset) { + public getScrollPath(offset: number): PivotGridCell['path'] { const tableElement = this.tableElement(); - let cell; - - offset -= parseInt(tableElement[0].style.left, 10) || 0; - - each(tableElement.find('td'), (_, td) => { - if (td.colSpan === 1 && td.offsetLeft <= offset && td.offsetWidth + td.offsetLeft > offset) { - cell = td; - return false; - } + if (!tableElement) { return undefined; - }); + } + + const tableOffset = offset - (parseInt(tableElement[0].style.left, 10) || 0); + const cells = tableElement.find('td').toArray() as HTMLTableCellElement[]; + const cell = cells.find((td) => td.colSpan === 1 + && td.offsetLeft <= tableOffset + && td.offsetWidth + td.offsetLeft > tableOffset); return getCellPath(tableElement, cell); } - _moveFakeTable(scrollPos) { + protected _moveFakeTable(scrollPos: ScrollOffset): void { this._moveFakeTableHorizontally(scrollPos.left); super._moveFakeTable(); } } class VerticalHeadersArea extends HorizontalHeadersArea { - _virtualContentHeight: any; + protected _virtualContentHeight?: number; - _getAreaClassName() { + protected _getAreaClassName(): string { return PIVOTGRID_AREA_ROW_CLASS; } - _applyCustomStyles(options) { + protected _applyCustomStyles(options: AreaItemStyleOptions): void { super._applyCustomStyles(options); if (options.cellIndex === options.cellsCount - 1) { @@ -198,11 +214,11 @@ class VerticalHeadersArea extends HorizontalHeadersArea { } } - _getAreaName() { + protected _getAreaName(): AreaName { return 'row'; } - setVirtualContentParams(params) { + public setVirtualContentParams(params: AreaItemVirtualContentParams): void { super.setVirtualContentParams(params); this._setTableCss({ @@ -212,7 +228,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { this._virtualContentHeight = params.height; } - hasScroll() { + public hasScroll(): boolean { const tableHeight = this._virtualContent ? this._virtualContentHeight : this._tableHeight; const groupHeight = this.getGroupHeight(); @@ -222,8 +238,9 @@ class VerticalHeadersArea extends HorizontalHeadersArea { return false; } - renderScrollable() { - this._groupElement.dxScrollable({ + public renderScrollable(): void { + // @ts-expect-error dxScrollable is added to the renderer at runtime + this._groupElement?.dxScrollable({ useNative: false, useSimulatedScrollbar: false, showScrollbar: 'never', @@ -234,7 +251,13 @@ class VerticalHeadersArea extends HorizontalHeadersArea { }); } - processScrollBarSpacing(scrollBarWidth) { + public processScrollBarSpacing(scrollBarWidth: number): void { + const groupElement = this._groupElement; + + if (!groupElement) { + return; + } + const groupHeight = this.getGroupHeight(); if (groupHeight) { @@ -242,64 +265,68 @@ class VerticalHeadersArea extends HorizontalHeadersArea { } if (this._scrollBarWidth) { - this._groupElement.next().remove(); + groupElement.next().remove(); } if (scrollBarWidth) { const $div = $('
'); setWidth($div, '100%'); setHeight($div, scrollBarWidth - 1); - this._groupElement.after($div); + groupElement.after($div); } this._scrollBarWidth = scrollBarWidth; } - getScrollPath(offset) { + public getScrollPath(offset: number): PivotGridCell['path'] { const tableElement = this.tableElement(); - let cell; - - offset -= parseInt(tableElement[0].style.top, 10) || 0; - - each(tableElement.find('tr'), (_, tr) => { - const td = tr.childNodes[tr.childNodes.length - 1]; - - if (td && td.rowSpan === 1 - && td.offsetTop <= offset - && td.offsetHeight + td.offsetTop > offset) { - cell = td; - return false; - } + if (!tableElement) { return undefined; - }); + } + + const tableOffset = offset - (parseInt(tableElement[0].style.top, 10) || 0); + const lastCells = tableElement.find('tr').toArray() + .map((tr) => tr.childNodes[tr.childNodes.length - 1] as HTMLTableCellElement | undefined); + const cell = lastCells.find((td) => td?.rowSpan === 1 + && td.offsetTop <= tableOffset + && td.offsetHeight + td.offsetTop > tableOffset); return getCellPath(tableElement, cell); } - _moveFakeTable(scrollPos) { + protected _moveFakeTable(scrollPos: ScrollOffset): void { this._moveFakeTableTop(scrollPos.top); super._moveFakeTable(scrollPos); } - _getRowClassNames(rowIndex, cell, rowClassNames) { - // @ts-expect-error - if (rowIndex !== 0 & cell.expanded && !rowClassNames.includes(PIVOTGRID_EXPAND_BORDER)) { + protected _getRowClassNames( + rowIndex: number, + cell: PivotGridCell | undefined, + rowClassNames: string[], + ): void { + if (rowIndex !== 0 && cell?.expanded && !rowClassNames.includes(PIVOTGRID_EXPAND_BORDER)) { rowClassNames.push(PIVOTGRID_EXPAND_BORDER); } } - _getMainElementMarkup() { + protected _getMainElementMarkup(): HTMLElement { const tbody = domAdapter.createElement('tbody'); tbody.classList.add(this._getAreaClassName()); tbody.setAttribute('role', 'presentation'); return tbody; } - updateColspans(columnCount) { - const { rows } = this.tableElement()[0]; + public updateColspans(columnCount: number): void { + const tableElement: HTMLTableElement | undefined = this.tableElement()?.[0]; + + if (!tableElement) { + return; + } + + const { rows } = tableElement; let columnOffset = 0; - const columnOffsetResetIndexes: any = []; + const columnOffsetResetIndexes: number[] = []; if (this.getColumnsCount() - columnCount > 0) { return; @@ -321,7 +348,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { cell.colSpan += diff; } - columnOffsetResetIndexes[i + rowSpan] = (columnOffsetResetIndexes[i + rowSpan] || 0) + columnOffsetResetIndexes[i + rowSpan] = (columnOffsetResetIndexes[i + rowSpan] ?? 0) + cell.colSpan; columnOffset += cell.colSpan; } diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/types.ts b/packages/devextreme/js/__internal/grids/pivot_grid/types.ts index 7878112600cc..6537843e616a 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/types.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/types.ts @@ -1,9 +1,11 @@ import type { DataSource } from '@js/common/data/data_source/data_source'; import type { ScalarFilterValue } from '@js/common/grids'; import type { DeferredObj } from '@js/core/utils/deferred'; +import type { Cell } from '@js/ui/pivot_grid'; import type PivotGridDataSource from '@js/ui/pivot_grid/data_source'; import type { Field } from '@js/ui/pivot_grid/data_source'; import type { StoreLoadOptions } from '@ts/data/data_source/types'; +import type { ColumnInfoCell } from '@ts/grids/grid_core/virtual_columns/types'; export type PivotGridField = Field & { index?: number; @@ -108,6 +110,13 @@ export interface PivotGridLoadResult { grandTotalColumnIndex: number; } +export type PivotGridCell = Cell & ColumnInfoCell & { + isWhiteSpace?: boolean; + width?: number; + sorted?: boolean; + wordWrapEnabled?: boolean; +}; + export interface PivotGridStore { getFields: (fields?: PivotGridField[]) => DeferredObj; load: (options: PivotGridLoadOptions) => DeferredObj; From 2c4976a08df0e7ca4c2ff6c35c2b7cd5bdfd2a98 Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:27:36 +0400 Subject: [PATCH 3/7] refactor(grids): extract pivot grid area cell rendering --- .../grids/pivot_grid/area_item/m_area_item.ts | 132 ++++++++++-------- .../grids/pivot_grid/area_item/types.ts | 10 ++ .../headers_area/m_headers_area.test.ts | 68 ++++++++- 3 files changed, 154 insertions(+), 56 deletions(-) diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts index 88b62a7be265..8d3273de4d18 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts @@ -18,6 +18,7 @@ import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; import type { PivotGridCell } from '../types'; import type { AreaItemCellAriaIndexOptions, + AreaItemCellContext, AreaItemCellPreparedArgs, AreaItemComponent, AreaItemOption, @@ -226,6 +227,77 @@ abstract class AreaItem { } } + private renderCellContent( + td: HTMLElement, + span: HTMLElement, + cell: PivotGridCell, + context: AreaItemCellContext, + ): string { + const { encodeHtml, isCellNavigationEnabled } = context; + + if (cell.rowspan) { + td.setAttribute('rowspan', String(cell.rowspan)); + } + if (cell.colspan) { + td.setAttribute('colspan', String(cell.colspan)); + } + + if (isCellNavigationEnabled && !cell.isWhiteSpace) { + td.setAttribute('tabindex', '-1'); + } + + const styleOptions: AreaItemStyleOptions = { + cellElement: undefined, + cell, + cellsCount: context.cellsCount, + cellIndex: context.cellIndex, + rowElement: undefined, + rowIndex: context.rowIndex, + rowsCount: context.rowsCount, + rtlEnabled: context.rtlEnabled, + classArray: [], + cssArray: [], + }; + + this._applyCustomStyles(styleOptions); + + if (styleOptions.cssArray.length) { + setStyle(td, styleOptions.cssArray.join(';')); + } + + if (styleOptions.classArray.length) { + td.setAttribute('class', styleOptions.classArray.join(' ')); + } + + if (isDefined(cell.expanded)) { + const div = domAdapter.createElement('div'); + div.classList.add('dx-expand-icon-container'); + const expandSpan = domAdapter.createElement('span'); + expandSpan.classList.add(PIVOTGRID_EXPAND_CLASS); + div.appendChild(expandSpan); + td.appendChild(div); + const ariaLabel = String(cell.text ?? cell.value ?? ''); + div.setAttribute('role', 'button'); + div.setAttribute('aria-label', encodeHtml ? ariaLabel : $('
').html(ariaLabel).text()); + div.setAttribute('aria-expanded', String(cell.expanded)); + div.setAttribute('tabindex', isCellNavigationEnabled ? '-1' : '0'); + + span.setAttribute('aria-hidden', 'true'); + + if (isCellNavigationEnabled) { + td.setAttribute('aria-expanded', String(cell.expanded)); + } + } + + const cellText = this._getCellText(cell, encodeHtml); + + if (isDefined(cell.wordWrapEnabled)) { + span.style.whiteSpace = cell.wordWrapEnabled ? 'normal' : 'nowrap'; + } + + return cellText; + } + protected _renderTableContent(tableElement: dxElementWrapper, data: PivotGridCell[][]): void { const rowsCount = data.length; const rtlEnabled = this.option('rtlEnabled'); @@ -266,65 +338,15 @@ abstract class AreaItem { const span = domAdapter.createElement('span'); if (cell) { - if (cell.rowspan) { - td.setAttribute('rowspan', String(cell.rowspan)); - } - if (cell.colspan) { - td.setAttribute('colspan', String(cell.colspan)); - } - - if (isCellNavigationEnabled && !cell.isWhiteSpace) { - td.setAttribute('tabindex', '-1'); - } - - const styleOptions: AreaItemStyleOptions = { - cellElement: undefined, - cell, - cellsCount: row.length, + cellText = this.renderCellContent(td, span, cell, { cellIndex: j, - rowElement: undefined, + cellsCount: row.length, rowIndex: i, rowsCount, rtlEnabled, - classArray: [], - cssArray: [], - }; - - this._applyCustomStyles(styleOptions); - - if (styleOptions.cssArray.length) { - setStyle(td, styleOptions.cssArray.join(';')); - } - - if (styleOptions.classArray.length) { - td.setAttribute('class', styleOptions.classArray.join(' ')); - } - - if (isDefined(cell.expanded)) { - const div = domAdapter.createElement('div'); - div.classList.add('dx-expand-icon-container'); - const expandSpan = domAdapter.createElement('span'); - expandSpan.classList.add(PIVOTGRID_EXPAND_CLASS); - div.appendChild(expandSpan); - td.appendChild(div); - const ariaLabel = String(cell.text ?? cell.value ?? ''); - div.setAttribute('role', 'button'); - div.setAttribute('aria-label', encodeHtml ? ariaLabel : $('
').html(ariaLabel).text()); - div.setAttribute('aria-expanded', String(cell.expanded)); - div.setAttribute('tabindex', isCellNavigationEnabled ? '-1' : '0'); - - span.setAttribute('aria-hidden', 'true'); - - if (isCellNavigationEnabled) { - td.setAttribute('aria-expanded', String(cell.expanded)); - } - } - - cellText = this._getCellText(cell, encodeHtml); - - if (isDefined(cell.wordWrapEnabled)) { - span.style.whiteSpace = cell.wordWrapEnabled ? 'normal' : 'nowrap'; - } + encodeHtml, + isCellNavigationEnabled, + }); } span.innerHTML = cellText; diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts index b850e61720f8..fbee4657d983 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts @@ -40,6 +40,16 @@ export interface AreaItemStyleOptions { cssArray: string[]; } +export interface AreaItemCellContext { + cellIndex: number; + cellsCount: number; + rowIndex: number; + rowsCount: number; + rtlEnabled: boolean | undefined; + encodeHtml: boolean | undefined; + isCellNavigationEnabled: boolean; +} + export interface AreaItemCellAriaIndexOptions { areaName: AreaName; rowIndex: number; diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts index 16676b3dfd3d..91e9f44418d0 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts @@ -11,13 +11,14 @@ import $ from '@js/core/renderer'; import type { AreaItemComponent } from '../area_item/types'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; +import type { PivotGridCell } from '../types'; import { HorizontalHeadersArea, VerticalHeadersArea } from './m_headers_area'; const TABLE_OFFSET = 1000; const CELL_SIZES = [100, 120, 300]; const POSITION_PAST_TABLE = TABLE_OFFSET + 520; -const createComponent = (): AreaItemComponent => ({ +const createComponent = (options: Record = {}): AreaItemComponent => ({ option: (optionName?: string): unknown => { if (optionName === undefined) { return {}; @@ -27,6 +28,7 @@ const createComponent = (): AreaItemComponent => ({ rtlEnabled: false, encodeHtml: false, 'scrolling.mode': 'virtual', + ...options, }[optionName]; }, _eventsStrategy: { hasEvent: () => false }, @@ -47,6 +49,70 @@ describe('Headers areas', () => { container.remove(); }); + describe('HorizontalHeadersArea render cells', () => { + const renderArea = ( + data: PivotGridCell[][], + options: Record = {}, + ): HorizontalHeadersArea => { + const area = new HorizontalHeadersArea(createComponent(options)); + + area.render($(container), data); + + return area; + }; + + const getCell = (): HTMLTableCellElement | null => container.querySelector('td'); + + const getCellText = (): HTMLElement | null | undefined => getCell()?.querySelector(':scope > span'); + + it('should set the row and column spans of a cell', () => { + renderArea([[{ text: 'A', rowspan: 2, colspan: 3 }]]); + + expect(getCell()?.getAttribute('rowspan')).toBe('2'); + expect(getCell()?.getAttribute('colspan')).toBe('3'); + }); + + it('should make a cell focusable but not a white space cell', () => { + renderArea([[{ text: 'A' }, { isWhiteSpace: true }]]); + + const cells = Array.from(container.querySelectorAll('td')); + + expect(cells.map((cell) => cell.getAttribute('tabindex'))).toEqual(['-1', null]); + }); + + it('should apply the width and the classes of a cell', () => { + renderArea([[{ text: 'A', width: 50, type: 'T' }]]); + + expect(getCell()?.style.minWidth).toBe('50px'); + expect(getCell()?.classList).toContain('dx-total'); + }); + + it('should render an expand icon for an expandable cell', () => { + renderArea([[{ text: 'A', expanded: false }]]); + + const icon = getCell()?.querySelector('.dx-expand-icon-container'); + + expect(icon?.getAttribute('role')).toBe('button'); + expect(icon?.getAttribute('aria-label')).toBe('A'); + expect(icon?.getAttribute('aria-expanded')).toBe('false'); + expect(icon?.getAttribute('tabindex')).toBe('-1'); + expect(getCell()?.getAttribute('aria-expanded')).toBe('false'); + expect(getCellText()?.getAttribute('aria-hidden')).toBe('true'); + }); + + it('should set the white space of a cell with word wrap', () => { + renderArea([[{ text: 'A', wordWrapEnabled: false }]]); + + expect(getCellText()?.style.whiteSpace).toBe('nowrap'); + }); + + it('should encode the cell text when encodeHtml is true', () => { + renderArea([[{ text: 'A' }]], { encodeHtml: true }); + + expect(getCellText()?.innerHTML).toBe('<b>A</b>'); + }); + }); + describe('HorizontalHeadersArea scrollTo with virtual scrolling', () => { const renderVirtualArea = (): HorizontalHeadersArea => { const area = new HorizontalHeadersArea(createComponent()); From 5f9e88a47464fb0fda99c2d805b9de7714de104d Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:23:17 +0400 Subject: [PATCH 4/7] refactor(grids): type pivot grid area cells with data controller types --- .../grids/pivot_grid/area_item/m_area_item.ts | 23 +++++++++++-------- .../grids/pivot_grid/area_item/types.ts | 7 ++++-- .../pivot_grid/data_area/m_data_area.test.ts | 5 ++-- .../grids/pivot_grid/data_area/m_data_area.ts | 4 ++-- .../headers_area/m_headers_area.test.ts | 5 ++-- .../pivot_grid/headers_area/m_headers_area.ts | 12 +++++----- .../js/__internal/grids/pivot_grid/types.ts | 9 -------- 7 files changed, 30 insertions(+), 35 deletions(-) diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts index 8d3273de4d18..0b925e9cad76 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts @@ -15,8 +15,8 @@ import { foreachColumnInfo } from '@ts/grids/grid_core/virtual_columns/virtual_c import type Scrollable from '@ts/ui/scroll_view/scrollable'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; -import type { PivotGridCell } from '../types'; import type { + AreaItemCell, AreaItemCellAriaIndexOptions, AreaItemCellContext, AreaItemCellPreparedArgs, @@ -134,7 +134,7 @@ abstract class AreaItem { .attr('role', 'presentation'); } - protected _getCellText(cell: PivotGridCell, encodeHtml: boolean | undefined): string { + protected _getCellText(cell: AreaItemCell, encodeHtml: boolean | undefined): string { let cellText = cell.isWhiteSpace ? ' ' : cell.text || ' '; if (encodeHtml && (cellText.includes('<') || cellText.includes('>'))) { @@ -146,7 +146,7 @@ abstract class AreaItem { protected _getRowClassNames( rowIndex: number, - cell: PivotGridCell | undefined, + cell: AreaItemCell | undefined, rowClassNames: string[], ): void; @@ -179,7 +179,7 @@ abstract class AreaItem { return this.component._dataController?.getRowIndexOffset() || 0; } - protected _createColumnIndexMap(data: PivotGridCell[][]): Map { + protected _createColumnIndexMap(data: AreaItemCell[][]): Map { const columnIndexMap = new Map(); foreachColumnInfo(data, (cell, visibleIndex, rowIndex, colIndex) => { @@ -230,7 +230,7 @@ abstract class AreaItem { private renderCellContent( td: HTMLElement, span: HTMLElement, - cell: PivotGridCell, + cell: AreaItemCell, context: AreaItemCellContext, ): string { const { encodeHtml, isCellNavigationEnabled } = context; @@ -276,6 +276,8 @@ abstract class AreaItem { expandSpan.classList.add(PIVOTGRID_EXPAND_CLASS); div.appendChild(expandSpan); td.appendChild(div); + /* eslint-disable-next-line @typescript-eslint/no-base-to-string + -- header values are primitives or dates */ const ariaLabel = String(cell.text ?? cell.value ?? ''); div.setAttribute('role', 'button'); div.setAttribute('aria-label', encodeHtml ? ariaLabel : $('
').html(ariaLabel).text()); @@ -298,7 +300,7 @@ abstract class AreaItem { return cellText; } - protected _renderTableContent(tableElement: dxElementWrapper, data: PivotGridCell[][]): void { + protected _renderTableContent(tableElement: dxElementWrapper, data: AreaItemCell[][]): void { const rowsCount = data.length; const rtlEnabled = this.option('rtlEnabled'); const encodeHtml = this.option('encodeHtml'); @@ -383,7 +385,7 @@ abstract class AreaItem { protected abstract _getAreaName(): AreaName; - protected _triggerOnCellPrepared(tableElement: dxElementWrapper, data: PivotGridCell[][]): void { + protected _triggerOnCellPrepared(tableElement: dxElementWrapper, data: AreaItemCell[][]): void { const rowElements = tableElement.find('tr'); const areaName = this._getAreaName(); const onCellPrepared = this.option('onCellPrepared'); @@ -406,6 +408,7 @@ abstract class AreaItem { rowIndex, columnIndex, cellElement: getPublicElement($cellElement), + // @ts-expect-error the data controller types the cell path as unknown[] cell, }; if (hasEvent) { @@ -732,7 +735,7 @@ abstract class AreaItem { } } - public render(rootElement: dxElementWrapper, data: PivotGridCell[][]): void { + public render(rootElement: dxElementWrapper, data: AreaItemCell[][]): void { if (this._tableElement) { try { this._tableElement[0].innerHTML = ''; @@ -847,9 +850,9 @@ abstract class AreaItem { return columnCount; } - public getData(): PivotGridCell[][] { + public getData(): AreaItemCell[][] { const tableElement = this._tableElement; - const data: PivotGridCell[][] = tableElement ? tableElement.data('data') : []; + const data: AreaItemCell[][] = tableElement ? tableElement.data('data') : []; return data; } } diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts index fbee4657d983..0f5384892eef 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts @@ -4,10 +4,13 @@ import type { ScrollOffset } from '@ts/core/utils/scroll'; import type { DefaultActionArgs } from '@ts/core/widget/component'; import type DOMComponent from '@ts/core/widget/dom_component'; -import type { PivotGridCell } from '../types'; +import type { PivotGridCellInfo, PivotGridColumnInfo } from '../data_controller/types'; export type AreaName = 'column' | 'row' | 'data' | 'fields'; +export type AreaItemCell = PivotGridColumnInfo + & Partial>; + export type AreaItemOption = TName extends keyof PivotGridProperties ? PivotGridProperties[TName] : unknown; @@ -29,7 +32,7 @@ export interface AreaItemComponent { export interface AreaItemStyleOptions { cellElement: undefined; - cell: PivotGridCell; + cell: AreaItemCell; cellsCount: number; cellIndex: number; rowElement: undefined; diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts index 82e608aff68d..732dc8a6867b 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.test.ts @@ -9,9 +9,8 @@ import { } from '@jest/globals'; import $ from '@js/core/renderer'; -import type { AreaItemComponent } from '../area_item/types'; +import type { AreaItemCell, AreaItemComponent } from '../area_item/types'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; -import type { PivotGridCell } from '../types'; import { DataArea } from './m_data_area'; const createComponent = (options: Record = {}): AreaItemComponent => ({ @@ -38,7 +37,7 @@ describe('DataArea', () => { [{ text: '3' }, { text: '4' }], ]; - const renderDataArea = (data: PivotGridCell[][]): DataArea => { + const renderDataArea = (data: AreaItemCell[][]): DataArea => { const area = new DataArea(createComponent()); area.render($(container), data); diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts index d7f5bd8da4aa..ff1e99e18a31 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts @@ -7,11 +7,11 @@ import supportUtils from '@ts/core/utils/support'; import { AreaItem } from '../area_item/m_area_item'; import type { + AreaItemCell, AreaItemStyleOptions, AreaItemVirtualContentParams, AreaName, } from '../area_item/types'; -import type { PivotGridCell } from '../types'; import { calculateScrollbarWidth } from '../widget_utils'; const PIVOTGRID_AREA_CLASS = 'dx-pivotgrid-area'; @@ -25,7 +25,7 @@ class DataArea extends AreaItem { return 'data'; } - public render(rootElement: dxElementWrapper, data: PivotGridCell[][]): void { + public render(rootElement: dxElementWrapper, data: AreaItemCell[][]): void { super.render(rootElement, data); this._makeFirstCellFocusable(); diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts index 91e9f44418d0..f6c65dfc2393 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.test.ts @@ -9,9 +9,8 @@ import { } from '@jest/globals'; import $ from '@js/core/renderer'; -import type { AreaItemComponent } from '../area_item/types'; +import type { AreaItemCell, AreaItemComponent } from '../area_item/types'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; -import type { PivotGridCell } from '../types'; import { HorizontalHeadersArea, VerticalHeadersArea } from './m_headers_area'; const TABLE_OFFSET = 1000; @@ -51,7 +50,7 @@ describe('Headers areas', () => { describe('HorizontalHeadersArea render cells', () => { const renderArea = ( - data: PivotGridCell[][], + data: AreaItemCell[][], options: Record = {}, ): HorizontalHeadersArea => { const area = new HorizontalHeadersArea(createComponent(options)); diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts index 94b01cc2dfeb..9c4340384252 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/headers_area/m_headers_area.ts @@ -8,6 +8,7 @@ import type { ScrollOffset } from '@ts/core/utils/scroll'; import { AreaItem } from '../area_item/m_area_item'; import type { + AreaItemCell, AreaItemComponent, AreaItemStyleOptions, AreaItemVirtualContentParams, @@ -17,7 +18,6 @@ import { HORIZONTAL_HEADERS_AREA_CLASS as PIVOTGRID_AREA_COLUMN_CLASS, VERTICAL_HEADERS_AREA_CLASS as PIVOTGRID_AREA_ROW_CLASS, } from '../keyboard_navigation/const'; -import type { PivotGridCell } from '../types'; const PIVOTGRID_AREA_CLASS = 'dx-pivotgrid-area'; const PIVOTGRID_TOTAL_CLASS = 'dx-total'; @@ -32,9 +32,9 @@ const PIVOTGRID_EXPAND_BORDER = 'dx-expand-border'; function getCellPath( tableElement: dxElementWrapper, cell: HTMLTableCellElement | undefined, -): PivotGridCell['path'] { +): AreaItemCell['path'] { if (cell) { - const { data } = tableElement.data<{ data: PivotGridCell[][] }>(); + const { data } = tableElement.data<{ data: AreaItemCell[][] }>(); const { rowIndex } = cell.parentNode as HTMLTableRowElement; const { cellIndex } = cell; @@ -169,7 +169,7 @@ class HorizontalHeadersArea extends AreaItem { this._scrollBarWidth = scrollBarWidth; } - public getScrollPath(offset: number): PivotGridCell['path'] { + public getScrollPath(offset: number): AreaItemCell['path'] { const tableElement = this.tableElement(); if (!tableElement) { @@ -278,7 +278,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { this._scrollBarWidth = scrollBarWidth; } - public getScrollPath(offset: number): PivotGridCell['path'] { + public getScrollPath(offset: number): AreaItemCell['path'] { const tableElement = this.tableElement(); if (!tableElement) { @@ -302,7 +302,7 @@ class VerticalHeadersArea extends HorizontalHeadersArea { protected _getRowClassNames( rowIndex: number, - cell: PivotGridCell | undefined, + cell: AreaItemCell | undefined, rowClassNames: string[], ): void { if (rowIndex !== 0 && cell?.expanded && !rowClassNames.includes(PIVOTGRID_EXPAND_BORDER)) { diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/types.ts b/packages/devextreme/js/__internal/grids/pivot_grid/types.ts index 6537843e616a..7878112600cc 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/types.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/types.ts @@ -1,11 +1,9 @@ import type { DataSource } from '@js/common/data/data_source/data_source'; import type { ScalarFilterValue } from '@js/common/grids'; import type { DeferredObj } from '@js/core/utils/deferred'; -import type { Cell } from '@js/ui/pivot_grid'; import type PivotGridDataSource from '@js/ui/pivot_grid/data_source'; import type { Field } from '@js/ui/pivot_grid/data_source'; import type { StoreLoadOptions } from '@ts/data/data_source/types'; -import type { ColumnInfoCell } from '@ts/grids/grid_core/virtual_columns/types'; export type PivotGridField = Field & { index?: number; @@ -110,13 +108,6 @@ export interface PivotGridLoadResult { grandTotalColumnIndex: number; } -export type PivotGridCell = Cell & ColumnInfoCell & { - isWhiteSpace?: boolean; - width?: number; - sorted?: boolean; - wordWrapEnabled?: boolean; -}; - export interface PivotGridStore { getFields: (fields?: PivotGridField[]) => DeferredObj; load: (options: PivotGridLoadOptions) => DeferredObj; From e3c05ab2d3439ec19005b01474d125d6ee890344 Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:59:59 +0400 Subject: [PATCH 5/7] refactor(grids): simplify pivot grid area types and directives --- .../grids/pivot_grid/area_item/m_area_item.ts | 54 +++++++++++-------- .../grids/pivot_grid/area_item/types.ts | 43 ++++++++------- .../grids/pivot_grid/data_area/m_data_area.ts | 16 +++--- .../pivot_grid/fields_area/m_fields_area.ts | 32 ++++++----- .../pivot_grid/headers_area/m_headers_area.ts | 38 +++++++------ 5 files changed, 105 insertions(+), 78 deletions(-) diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts index 0b925e9cad76..f8c1eff7d1d8 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts @@ -13,6 +13,7 @@ import type { ScrollOffset, ScrollToFunc } from '@ts/core/utils/scroll'; import { getMemoizeScrollTo } from '@ts/core/utils/scroll'; import { foreachColumnInfo } from '@ts/grids/grid_core/virtual_columns/virtual_columns_core'; import type Scrollable from '@ts/ui/scroll_view/scrollable'; +import type { ScrollableProperties } from '@ts/ui/scroll_view/scrollable'; import { FAKE_TABLE_CLASS } from '../keyboard_navigation/const'; import type { @@ -408,13 +409,12 @@ abstract class AreaItem { rowIndex, columnIndex, cellElement: getPublicElement($cellElement), - // @ts-expect-error the data controller types the cell path as unknown[] cell, }; if (hasEvent) { this.component._trigger('onCellPrepared', onCellPreparedArgs); } else { - // @ts-expect-error the default action args are typed for an unknown component + // @ts-expect-error the cell prepared args use internal cell and component types onCellPrepared?.(extend(onCellPreparedArgs, defaultActionArgs)); } } @@ -442,7 +442,7 @@ abstract class AreaItem { protected getRowsLength(): number { if (this._tableElement && this._tableElement.length > 0) { - const tableElement: HTMLTableElement = this._tableElement[0]; + const tableElement = this._tableElement.get(0); return tableElement.rows.length; } return 0; @@ -468,7 +468,7 @@ abstract class AreaItem { } this._tableHeight = totalHeight; - const tableElement: HTMLTableElement | undefined = this._tableElement?.[0]; + const tableElement = this._tableElement?.get(0); if (tableElement) { tableElement.style.height = `${totalHeight}px`; } @@ -571,8 +571,12 @@ abstract class AreaItem { } protected _getGroupElementSize(dimension: 'width' | 'height'): number | null { - // @ts-expect-error the group element is rendered here - const groupElement: HTMLElement = this.groupElement()[0]; + const groupElement = this.groupElement()?.get(0); + + if (!groupElement) { + return null; + } + const size = groupElement.style[dimension]; if (size.indexOf('px') > 0) { @@ -631,7 +635,7 @@ abstract class AreaItem { } public reset(): void { - const tableElement: HTMLTableElement | undefined = this._tableElement?.[0]; + const tableElement = this._tableElement?.get(0); this._fakeTable?.detach(); this._fakeTable = null; @@ -652,7 +656,7 @@ abstract class AreaItem { } protected _updateFakeTableVisibility(): void { - const tableElement: HTMLElement | undefined = this.tableElement()?.[0]; + const tableElement = this.tableElement()?.get(0); const horizontalOffsetName = this.option('rtlEnabled') ? 'right' : 'left'; const fakeTable = this._fakeTable; @@ -660,7 +664,7 @@ abstract class AreaItem { return; } - const fakeTableElement: HTMLElement = fakeTable[0]; + const fakeTableElement = fakeTable.get(0); if ( tableElement.style.top === fakeTableElement.style.top @@ -675,8 +679,8 @@ abstract class AreaItem { protected _moveFakeTableHorizontally(scrollPos: number | undefined): void { const rtlEnabled = this.option('rtlEnabled'); const offsetStyleName = rtlEnabled ? 'right' : 'left'; - const tableElement: HTMLElement | undefined = this.tableElement()?.[0]; - const fakeTableElement: HTMLElement | undefined = this._fakeTable?.[0]; + const tableElement = this.tableElement()?.get(0); + const fakeTableElement = this._fakeTable?.get(0); if (!tableElement || !fakeTableElement) { return; @@ -684,9 +688,9 @@ abstract class AreaItem { const tableElementOffset = parseFloat(tableElement.style[offsetStyleName]); const offset = getFakeTableOffset( - scrollPos ?? NaN, + Number(scrollPos), tableElementOffset, - this._tableWidth ?? NaN, + Number(this._tableWidth), this.getGroupWidth() ?? 0, ); if (parseFloat(fakeTableElement.style[offsetStyleName]) !== offset) { @@ -695,8 +699,8 @@ abstract class AreaItem { } protected _moveFakeTableTop(scrollPos: number | undefined): void { - const tableElement: HTMLElement | undefined = this.tableElement()?.[0]; - const fakeTableElement: HTMLElement | undefined = this._fakeTable?.[0]; + const tableElement = this.tableElement()?.get(0); + const fakeTableElement = this._fakeTable?.get(0); if (!tableElement || !fakeTableElement) { return; @@ -704,9 +708,9 @@ abstract class AreaItem { const tableElementOffsetTop = parseFloat(tableElement.style.top); const offsetTop = getFakeTableOffset( - scrollPos ?? NaN, + Number(scrollPos), tableElementOffsetTop, - this._tableHeight ?? NaN, + Number(this._tableHeight), this.getGroupHeight() ?? 0, ); @@ -738,7 +742,7 @@ abstract class AreaItem { public render(rootElement: dxElementWrapper, data: AreaItemCell[][]): void { if (this._tableElement) { try { - this._tableElement[0].innerHTML = ''; + this._tableElement.get(0).innerHTML = ''; } catch (e) { this._tableElement.empty(); } @@ -763,9 +767,13 @@ abstract class AreaItem { return scrollable; } - protected _getMemoizeScrollTo(): ScrollToFunc { - // @ts-expect-error the scrollable is rendered here - this._memoizeScrollTo ??= getMemoizeScrollTo(() => this._getScrollable()); + protected renderGroupScrollable(options: ScrollableProperties): void { + // @ts-expect-error dxScrollable is not defined in dxElementWrapper + this._groupElement?.dxScrollable(options); + } + + protected _getMemoizeScrollTo(scrollable: Scrollable): ScrollToFunc { + this._memoizeScrollTo ??= getMemoizeScrollTo(() => scrollable); return this._memoizeScrollTo; } @@ -808,11 +816,11 @@ abstract class AreaItem { const scrollablePos = { ...pos, left: rtlEnabled && (areaName === 'column' || areaName === 'data') - ? this._getMaxLeftOffset(scrollable) - (pos.left ?? NaN) + ? this._getMaxLeftOffset(scrollable) - Number(pos.left) : pos.left, }; - const memoizeScrollTo = this._getMemoizeScrollTo(); + const memoizeScrollTo = this._getMemoizeScrollTo(scrollable); memoizeScrollTo(scrollablePos, force); if (this._virtualContent) { diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts index 0f5384892eef..22b6049d5da7 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/area_item/types.ts @@ -1,15 +1,19 @@ +import type { DxElement } from '@js/core/element'; import type { dxElementWrapper } from '@js/core/renderer'; -import type { CellPreparedEvent, Properties as PivotGridProperties } from '@js/ui/pivot_grid'; +import type { Properties as PivotGridProperties } from '@js/ui/pivot_grid'; import type { ScrollOffset } from '@ts/core/utils/scroll'; import type { DefaultActionArgs } from '@ts/core/widget/component'; import type DOMComponent from '@ts/core/widget/dom_component'; -import type { PivotGridCellInfo, PivotGridColumnInfo } from '../data_controller/types'; +import type { HeaderItemType, PivotGridColumnInfo } from '../data_controller/types'; export type AreaName = 'column' | 'row' | 'data' | 'fields'; -export type AreaItemCell = PivotGridColumnInfo - & Partial>; +export interface AreaItemCell extends PivotGridColumnInfo { + value?: unknown; + rowType?: HeaderItemType; + columnType?: HeaderItemType; +} export type AreaItemOption = TName extends keyof PivotGridProperties ? PivotGridProperties[TName] @@ -30,25 +34,23 @@ export interface AreaItemComponent { _trigger: (eventName: string, eventArg: unknown) => void; } -export interface AreaItemStyleOptions { - cellElement: undefined; - cell: AreaItemCell; - cellsCount: number; +export interface AreaItemCellPosition { cellIndex: number; - rowElement: undefined; + cellsCount: number; rowIndex: number; rowsCount: number; rtlEnabled: boolean | undefined; +} + +export interface AreaItemStyleOptions extends AreaItemCellPosition { + cellElement: undefined; + cell: AreaItemCell; + rowElement: undefined; classArray: string[]; cssArray: string[]; } -export interface AreaItemCellContext { - cellIndex: number; - cellsCount: number; - rowIndex: number; - rowsCount: number; - rtlEnabled: boolean | undefined; +export interface AreaItemCellContext extends AreaItemCellPosition { encodeHtml: boolean | undefined; isCellNavigationEnabled: boolean; } @@ -62,10 +64,13 @@ export interface AreaItemCellAriaIndexOptions { rowIndexOffset: number; } -export type AreaItemCellPreparedArgs = Pick< - CellPreparedEvent, - 'area' | 'rowIndex' | 'columnIndex' | 'cellElement' | 'cell' ->; +export interface AreaItemCellPreparedArgs { + area: AreaName; + rowIndex: number; + columnIndex: number; + cellElement: DxElement; + cell: AreaItemCell; +} export interface AreaItemVirtualContentParams { top?: number; diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts index ff1e99e18a31..f698dc764cd4 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/data_area/m_data_area.ts @@ -47,8 +47,13 @@ class DataArea extends AreaItem { } protected _applyCustomStyles(options: AreaItemStyleOptions): void { - const { cell } = options; - const { classArray } = options; + const { + cell, + classArray, + cssArray, + rowIndex, + rowsCount, + } = options; if (cell.rowType === 'T' || cell.columnType === 'T') { classArray.push(PIVOTGRID_TOTAL_CLASS); @@ -61,8 +66,8 @@ class DataArea extends AreaItem { classArray.push(PIVOTGRID_ROW_TOTAL_CLASS); } - if (options.rowIndex === options.rowsCount - 1) { - options.cssArray.push('border-bottom: 0px'); + if (rowIndex === rowsCount - 1) { + cssArray.push('border-bottom: 0px'); } super._applyCustomStyles(options); @@ -75,8 +80,7 @@ class DataArea extends AreaItem { } public renderScrollable(): void { - // @ts-expect-error dxScrollable is added to the renderer at runtime - this._groupElement?.dxScrollable({ + this.renderGroupScrollable({ useNative: this.getUseNativeValue(), useSimulatedScrollbar: false, rtlEnabled: this.component.option('rtlEnabled'), diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts b/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts index eb96941b1221..498d3d0c755f 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/fields_area/m_fields_area.ts @@ -7,9 +7,11 @@ import { setHeight, setWidth } from '@js/core/utils/style'; import Button from '@js/ui/button'; import Popup from '@js/ui/popup/ui.popup'; import { capitalize } from '@ts/core/utils/capitalize'; +import type { PopupProperties } from '@ts/ui/popup/popup'; import { AreaItem } from '../area_item/m_area_item'; import type { AreaName } from '../area_item/types'; +import type { FieldChooserBase } from '../field_chooser/m_field_chooser_base'; const DIV = '
'; @@ -75,7 +77,7 @@ class FieldsArea extends AreaItem { icon: 'menu', width: 'auto', onClick() { - // @ts-expect-error dxPopup is added to the renderer at runtime + // @ts-expect-error dxPopup is not defined in dxElementWrapper const popup = that.tableElement().find('.dx-fields-area-popup').dxPopup('instance'); if (!popup.option('visible')) { popup.show(); @@ -85,7 +87,7 @@ class FieldsArea extends AreaItem { button.$element().addClass('dx-pivotgrid-fields-area-hamburger'); } - _getPopupOptions(row, button) { + _getPopupOptions(row, button): PopupProperties { return { contentTemplate() { return $('
').append( @@ -119,8 +121,7 @@ class FieldsArea extends AreaItem { const that = this; const button = tableElement.find('.dx-button'); const popupOptions = that._getPopupOptions(row, button); - // @ts-expect-error dxPivotGridFieldChooserBase is added to the renderer at runtime - const FieldChooserBase = that.component.$element().dxPivotGridFieldChooserBase('instance'); + const fieldChooserBase = that.getFieldChooserBase(); if (that._rowPopup) { that._rowPopup.$element().remove(); @@ -129,7 +130,6 @@ class FieldsArea extends AreaItem { that._rowPopup = that.component._createComponent( $(DIV).appendTo(tableElement), Popup, - // @ts-expect-error the fields area popup options aren't typed yet popupOptions, ); @@ -139,8 +139,17 @@ class FieldsArea extends AreaItem { that._rowPopup.content().parent() .attr('group', 'row'); - FieldChooserBase.subscribeToEvents(that._rowPopup.content()); - FieldChooserBase.renderSortable(that._rowPopup.content()); + fieldChooserBase.subscribeToEvents(that._rowPopup.content()); + // @ts-expect-error renderSortable() ignores the element + fieldChooserBase.renderSortable(that._rowPopup.content()); + } + + private getFieldChooserBase(): FieldChooserBase { + const $element = this.component.$element(); + // @ts-expect-error dxPivotGridFieldChooserBase is not defined in dxElementWrapper + const fieldChooserBase: FieldChooserBase = $element.dxPivotGridFieldChooserBase('instance'); + + return fieldChooserBase; } _shouldCreateButton() { @@ -151,8 +160,7 @@ class FieldsArea extends AreaItem { const that = this; const groupElement = this.groupElement(); const isVisible = this.isVisible(); - // @ts-expect-error dxPivotGridFieldChooserBase is added to the renderer at runtime - const fieldChooserBase = that.component.$element().dxPivotGridFieldChooserBase('instance'); + const fieldChooserBase = that.getFieldChooserBase(); const head = $('
').append($(DIV).addClass('dx-empty-area-text').text(this.option(`fieldPanel.texts.${area}FieldArea`))).appendTo(row); } - if (that._shouldCreateButton()) { - that._renderButton(head); - that._renderPopup(tableElement, row); + if (this._shouldCreateButton()) { + this._renderButton(head); + this._renderPopup(tableElement, row); } else { head.append(row); } From 97d1b7cd450e413daf5fd084b790a3d4a980e457 Mon Sep 17 00:00:00 2001 From: Maksim Zakharov <251575087+bit-byte0@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:14:01 +0400 Subject: [PATCH 7/7] refactor(grids): guard pivot grid export against unrendered columns area --- .../js/__internal/grids/pivot_grid/export/m_export.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/devextreme/js/__internal/grids/pivot_grid/export/m_export.ts b/packages/devextreme/js/__internal/grids/pivot_grid/export/m_export.ts index 91378c00cb1c..036a2536b15d 100644 --- a/packages/devextreme/js/__internal/grids/pivot_grid/export/m_export.ts +++ b/packages/devextreme/js/__internal/grids/pivot_grid/export/m_export.ts @@ -83,7 +83,7 @@ class DataProvider { public getColumnsWidths(): number[] { const colsArea = this._options.columnsArea; const { rowsArea, columns } = this._options; - const useDefaultWidth = !hasWindow() || colsArea.option('scrolling.mode') === 'virtual' || colsArea.element().is(':hidden'); + const useDefaultWidth = !hasWindow() || colsArea.option('scrolling.mode') === 'virtual' || colsArea.element()?.is(':hidden'); return useDefaultWidth ? columns.map(() => DEFAUL_COLUMN_WIDTH) : rowsArea.getColumnsWidth().concat(colsArea.getColumnsWidth());