Private
Public Access
1
0
Files
power-apps-codeapps-blog-part2/node_modules/@fluentui/react-table/lib-commonjs/utils/columnResizeUtils.js

171 lines
6.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
adjustColumnWidthsToFitContainer: function() {
return adjustColumnWidthsToFitContainer;
},
columnDefinitionsToState: function() {
return columnDefinitionsToState;
},
getColumnById: function() {
return getColumnById;
},
getColumnByIndex: function() {
return getColumnByIndex;
},
getColumnWidth: function() {
return getColumnWidth;
},
getLength: function() {
return getLength;
},
getTotalWidth: function() {
return getTotalWidth;
},
setColumnProperty: function() {
return setColumnProperty;
}
});
const DEFAULT_WIDTH = 150;
const DEFAULT_MIN_WIDTH = 100;
function columnDefinitionsToState(columns, state = [], columnSizingOptions = {}) {
let updated = false;
const stateMap = new Map(state.map((s)=>[
s.columnId,
s
]));
const updatedState = columns.map((column)=>{
const existingColumnState = stateMap.get(column.columnId);
if (existingColumnState) {
var _columnSizingOptions_column_columnId;
const { idealWidth = existingColumnState.idealWidth, minWidth = existingColumnState.minWidth, padding = existingColumnState.padding } = (_columnSizingOptions_column_columnId = columnSizingOptions[column.columnId]) !== null && _columnSizingOptions_column_columnId !== void 0 ? _columnSizingOptions_column_columnId : {};
if (idealWidth !== existingColumnState.idealWidth || minWidth !== existingColumnState.minWidth || padding !== existingColumnState.padding) {
updated = true;
return {
...existingColumnState,
idealWidth,
width: idealWidth,
minWidth,
padding
};
}
return existingColumnState;
}
var _columnSizingOptions_column_columnId1;
const { defaultWidth, idealWidth = DEFAULT_WIDTH, minWidth = DEFAULT_MIN_WIDTH, padding } = (_columnSizingOptions_column_columnId1 = columnSizingOptions[column.columnId]) !== null && _columnSizingOptions_column_columnId1 !== void 0 ? _columnSizingOptions_column_columnId1 : {};
updated = true;
return {
columnId: column.columnId,
width: Math.max(defaultWidth !== null && defaultWidth !== void 0 ? defaultWidth : idealWidth, minWidth),
minWidth,
idealWidth: Math.max(defaultWidth !== null && defaultWidth !== void 0 ? defaultWidth : idealWidth, minWidth),
padding: padding !== null && padding !== void 0 ? padding : 16
};
});
// If the length of the new state changed (column was added or removed) or any of
// the individual states has a new reference (column was replaced),
// we have to reset the column widths to their ideal width (because the column which was last may not be last now).
// Then the adjustColumnWidthsToFitContainer can do its job and properly stretch the last column.
if (updatedState.length !== state.length || updated) {
const column = updatedState.find((col)=>col.width > col.idealWidth);
if (column) {
column.width = column.idealWidth;
}
updated = true;
}
return updated ? updatedState : state;
}
function getColumnById(state, columnId) {
return state.find((c)=>c.columnId === columnId);
}
function getColumnByIndex(state, index) {
return state[index];
}
function getTotalWidth(state) {
return state.reduce((sum, column)=>sum + column.width + column.padding, 0);
}
function getLength(state) {
return state.length;
}
function getColumnWidth(state, columnId) {
const column = getColumnById(state, columnId);
var _column_width;
return (_column_width = column === null || column === void 0 ? void 0 : column.width) !== null && _column_width !== void 0 ? _column_width : 0;
}
function setColumnProperty(localState, columnId, property, value) {
const currentColumn = getColumnById(localState, columnId);
if (!currentColumn || (currentColumn === null || currentColumn === void 0 ? void 0 : currentColumn[property]) === value) {
return localState;
}
const updatedColumn = {
...currentColumn,
[property]: value
};
const newState = localState.reduce((acc, current)=>{
if (current.columnId === updatedColumn.columnId) {
return [
...acc,
updatedColumn
];
}
return [
...acc,
current
];
}, []);
return newState;
}
function adjustColumnWidthsToFitContainer(state, containerWidth) {
let newState = state;
const totalWidth = getTotalWidth(newState);
// The total width is smaller, we are expanding columns
if (totalWidth < containerWidth) {
let difference = containerWidth - totalWidth;
let i = 0;
// We start at the beginning and assign the columns their ideal width
while(i < newState.length && difference > 0){
const currentCol = getColumnByIndex(newState, i);
if (!currentCol) {
break;
}
const colAdjustment = Math.min(currentCol.idealWidth - currentCol.width, difference);
newState = setColumnProperty(newState, currentCol.columnId, 'width', currentCol.width + colAdjustment);
difference -= colAdjustment;
// if there is still empty space, after all columns are their ideal sizes, assign it to the last column
if (i === newState.length - 1 && difference !== 0) {
const lastCol = getColumnByIndex(newState, i);
if (lastCol) {
newState = setColumnProperty(newState, lastCol.columnId, 'width', lastCol.width + difference);
}
}
i++;
}
} else if (totalWidth >= containerWidth) {
let difference = totalWidth - containerWidth;
// We start with the last column
let j = newState.length - 1;
while(j >= 0 && difference > 0){
const currentCol = getColumnByIndex(newState, j);
if (!currentCol) {
j--;
continue;
}
if (currentCol.width > currentCol.minWidth) {
const colAdjustment = Math.min(currentCol.width - currentCol.minWidth, difference);
difference -= colAdjustment;
newState = setColumnProperty(newState, currentCol.columnId, 'width', currentCol.width - colAdjustment);
}
j--;
}
}
return newState;
}