UNPKG

@elastic/eui

Version:

Elastic UI Component Library

82 lines (77 loc) 2.75 kB
/* * 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. */ import { createPublisher } from '../../../utils/publisher'; /** * @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 */ export var createEuiTableStore = function createEuiTableStore() { var columns = new Map(); var columnsPublisher = createPublisher(); var columnWidths = new Map(); var columnWidthsPublisher = 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; } }; };