@elastic/eui
Version:
Elastic UI Component Library
87 lines (82 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createEuiTableStore = void 0;
var _publisher = require("../../../utils/publisher");
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
/**
* @internal
*/
/**
* @internal
*/
/**
* EuiTable store that helps with column and width synchronization between
* the original table and the "virtual" sticky header table.
*
* This store is currently only used for these synchronization purposes, but
* that may extend as we implement resizable columns.
* If that's not implemented by the time per-axis `position: sticky`
* is supported in all browsers natively, this can be removed.
* @internal
*/
var createEuiTableStore = exports.createEuiTableStore = function createEuiTableStore() {
var columns = new Map();
var columnsPublisher = (0, _publisher.createPublisher)();
var columnWidths = new Map();
var columnWidthsPublisher = (0, _publisher.createPublisher)();
var registerColumn = function registerColumn(id, data) {
var _data$currentWidth;
if (columns.has(id)) {
throw new Error("[EuiTableStore] Column '".concat(id, "' already registered"));
}
columns.set(id, data);
// Initialize column in the columnWidths map to keep both maps in sync
columnWidths.set(id, (_data$currentWidth = data.currentWidth) !== null && _data$currentWidth !== void 0 ? _data$currentWidth : 0);
columnsPublisher.notify(columns);
return function () {
columns.delete(id);
columnWidths.delete(id);
columnsPublisher.notify(columns);
};
};
var updateColumn = function updateColumn(id, data) {
var currentData = columns.get(id);
if (!currentData) {
throw new Error("[EuiTableStore] Column '".concat(id, "' not found"));
}
columns.set(id, data);
columnsPublisher.notify(columns);
};
var updateColumnWidth = function updateColumnWidth(id, width) {
var currentWidth = columnWidths.get(id);
if (currentWidth === undefined) {
throw new Error("[EuiTableStore] No width stored for column '".concat(id, "'"));
}
if (currentWidth === width) {
return;
}
columnWidths.set(id, width);
columnWidthsPublisher.notify(columnWidths);
};
return {
registerColumn: registerColumn,
updateColumn: updateColumn,
updateColumnWidth: updateColumnWidth,
subscribe: columnsPublisher.subscribe,
subscribeToColumnWidths: columnWidthsPublisher.subscribe,
getColumns: function getColumns() {
return columns;
},
getColumnWidths: function getColumnWidths() {
return columnWidths;
}
};
};