import { IStoreState } from '../../index.data'; import * as R from 'rambda'; import { getFrozenWidth } from '../../store/selectors/dimensions'; import { ROW_INDICATOR_WIDTH } from '../../constants'; export class HScrollBarHelper { public barOffset: number; public barSize: number; // public thumbSize: number; // public thumbOffset: number; private _allVisibleColumnWidth: number = 0; private state: IStoreState; constructor(state: IStoreState) { this.state = state; if ( state.containerBox.w <= 0 || state.containerBox.h <= 0 || R.isEmpty(state.colDimensions) || R.isEmpty(state.rowDimensions) ) { this.barOffset = 0; this.barSize = 0; // this.thumbOffset = 0; // this.thumbSize = 0; } else { this.barOffset = this.getBarOffset(); this.barSize = this.getBarSize(); this._allVisibleColumnWidth = get_allVisibleColumnWidth_exceptFrozen(this.state); // this.thumbSize = this.getThumbSize(); // this.thumbOffset = this.getThumbOffset(); } } getBarOffset() { return getFrozenWidth(this.state) + ROW_INDICATOR_WIDTH; } getBarSize() { const state = this.state; return Math.max(state.containerBox.w - this.barOffset - 20, 0); } getThumbOffset() { const state = this.state; if (state.containerBox.w - this.barOffset <= 0) return 0; return (this.barSize / (this._allVisibleColumnWidth + state.padding.right)) * state.scrollX; } getThumbSize() { const state = this.state; return ( (this.barSize / (this._allVisibleColumnWidth + state.padding.right)) * (this.barSize + 20) /*即容器真实高度 - 冻结行高度*/ ); } } function isFrozenColumnByJ(state: IStoreState, j: number): boolean { return j <= state.freezeAt.j; } export function get_allVisibleColumnWidth_exceptFrozen(state: IStoreState): number { const { columns } = state; let width = 0; for (let j = 0; j < columns.length; j++) { const column = columns[j]; if (!isFrozenColumnByJ(state, j)) { width += column.width; } } return width; } /** * 找到某个区域内的全部可加载列 */ export function getAllLoadableColumnFlags(state: IStoreState): boolean[] { const loadingWidth = state.containerBox.w + state.padding.right; return _getAllLoadableColumnFlags(state, loadingWidth, state.scrollX); } export function _getAllLoadableColumnFlags( state: IStoreState, loadingWidth: number, scrollX: number, ): boolean[] { if (state.freezeAt.j < 0) { return R.repeat(true, state.columns.length); } // console.log('计算时, 一定要注意: scrollX不包含在frozen区域中'); const { columns, colDimensions } = state; const freezeAtJ = state.freezeAt.j; const result = R.repeat(false, columns.length); for (let j = 0; j < freezeAtJ + 1; j++) { result[j] = true; } const lastFrozenCol = freezeAtJ >= 0 ? columns[freezeAtJ] : null; const frozenWidth = lastFrozenCol ? colDimensions[lastFrozenCol.id!].width + colDimensions[lastFrozenCol.id!].left : 0; let usedWidth = frozenWidth; for (let j = freezeAtJ + 1; j < columns.length; j++) { if (usedWidth >= scrollX + loadingWidth) break; const colDim = colDimensions[columns[j].id!]; if (!colDim) { result[j] = true; continue; } const colWidth = colDim.width; if (usedWidth + colWidth <= scrollX + frozenWidth) { usedWidth += colWidth; continue; } result[j] = true; usedWidth += colWidth; } return result; }