UNPKG

@grafana/ui

Version:
413 lines (406 loc) • 14.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var lodash = require('lodash'); var memoize = require('micro-memoize'); var data = require('@grafana/data'); var schema = require('@grafana/schema'); var ActionsCell = require('./ActionsCell.cjs'); var BarGaugeCell = require('./Cells/BarGaugeCell.cjs'); var DataLinksCell = require('./Cells/DataLinksCell.cjs'); var DefaultCell = require('./Cells/DefaultCell.cjs'); var GeoCell = require('./Cells/GeoCell.cjs'); var ImageCell = require('./Cells/ImageCell.cjs'); var JSONViewCell = require('./Cells/JSONViewCell.cjs'); var SparklineCell = require('./Cells/SparklineCell.cjs'); var FooterRow = require('./TableRT/FooterRow.cjs'); var RowExpander = require('./TableRT/RowExpander.cjs'); var cellUtils = require('./cellUtils.cjs'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; } var memoize__default = /*#__PURE__*/_interopDefaultCompat(memoize); "use strict"; const EXPANDER_WIDTH = 50; function getColumns(data$1, availableWidth, columnMinWidth, expander, footerValues, isCountRowsSet) { var _a, _b, _c; const columns = []; let fieldCountWithoutWidth = 0; if (expander) { columns.push({ // Make an expander cell Header: () => null, // No header id: "expander", // It needs an ID // @ts-expect-error // TODO fix type error here Cell: RowExpander.RowExpander, width: EXPANDER_WIDTH, minWidth: EXPANDER_WIDTH, filter: (_rows, _id, _filterValues) => { return []; }, justifyContent: "left", field: data$1.fields[0], sortType: "basic" }); availableWidth -= EXPANDER_WIDTH; } for (const [fieldIndex, field] of data$1.fields.entries()) { const fieldTableOptions = field.config.custom || {}; if ( // @ts-ignore this was the former hidden option; we support it for legacy use cases while TableRT is being sunset. fieldTableOptions.hidden || ((_a = fieldTableOptions.hideFrom) == null ? void 0 : _a.viz) || field.type === data.FieldType.nestedFrames ) { continue; } if (fieldTableOptions.width) { availableWidth -= fieldTableOptions.width; } else { fieldCountWithoutWidth++; } const selectSortType = (type) => { switch (type) { case data.FieldType.number: case data.FieldType.frame: return "number"; case data.FieldType.time: return "basic"; default: return "alphanumeric-insensitive"; } }; const Cell = getCellComponent((_b = fieldTableOptions.cellOptions) == null ? void 0 : _b.type, field); columns.push({ // @ts-expect-error // TODO fix type error here Cell, id: fieldIndex.toString(), field, Header: fieldTableOptions.hideHeader ? "" : data.getFieldDisplayName(field, data$1), accessor: (_row, i) => field.values[i], sortType: selectSortType(field.type), width: fieldTableOptions.width, minWidth: (_c = fieldTableOptions.minWidth) != null ? _c : columnMinWidth, filter: memoize__default.default(filterByValue(field)), justifyContent: cellUtils.getTextAlign(field), Footer: FooterRow.getFooterValue(fieldIndex, footerValues, isCountRowsSet) }); } let sharedWidth = availableWidth / fieldCountWithoutWidth; for (let i = fieldCountWithoutWidth; i > 0; i--) { for (const column of columns) { if (!column.width && column.minWidth > sharedWidth) { column.width = column.minWidth; availableWidth -= column.width; fieldCountWithoutWidth -= 1; sharedWidth = availableWidth / fieldCountWithoutWidth; } } } for (const column of columns) { if (!column.width) { column.width = sharedWidth; } column.minWidth = 50; } return columns; } function getCellComponent(displayMode, field) { switch (displayMode) { case schema.TableCellDisplayMode.Custom: case schema.TableCellDisplayMode.ColorText: case schema.TableCellDisplayMode.ColorBackground: return DefaultCell.DefaultCell; case schema.TableCellDisplayMode.Image: return ImageCell.ImageCell; case schema.TableCellDisplayMode.Gauge: return BarGaugeCell.BarGaugeCell; case schema.TableCellDisplayMode.Sparkline: return SparklineCell.SparklineCell; case schema.TableCellDisplayMode.JSONView: return JSONViewCell.JSONViewCell; case schema.TableCellDisplayMode.DataLinks: return DataLinksCell.DataLinksCell; case schema.TableCellDisplayMode.Actions: return ActionsCell.ActionsCell; case schema.TableCellDisplayMode.Pill: return DefaultCell.DefaultCell; } if (field.type === data.FieldType.geo) { return GeoCell.GeoCell; } if (field.type === data.FieldType.frame) { const firstValue = field.values[0]; if (data.isDataFrame(firstValue) && data.isTimeSeriesFrame(firstValue)) { return SparklineCell.SparklineCell; } return JSONViewCell.JSONViewCell; } if (field.type === data.FieldType.other) { return JSONViewCell.JSONViewCell; } return DefaultCell.DefaultCell; } function filterByValue(field) { return function(rows, id, filterValues) { if (rows.length === 0) { return rows; } if (!filterValues) { return rows; } if (!field) { return rows; } return rows.filter((row) => { if (!row.values.hasOwnProperty(id)) { return false; } const value = rowToFieldValue(row, field); return filterValues.find((filter) => filter.value === value) !== void 0; }); }; } function calculateUniqueFieldValues(rows, field) { if (!field || rows.length === 0) { return {}; } const set = {}; for (let index = 0; index < rows.length; index++) { const value = rowToFieldValue(rows[index], field); set[value || "(Blanks)"] = value; } return set; } function rowToFieldValue(row, field) { if (!field || !row) { return ""; } const fieldValue = field.values[row.index]; const displayValue = field.display ? field.display(fieldValue) : fieldValue; const value = field.display ? data.formattedValueToString(displayValue) : displayValue; return value; } function valuesToOptions(unique) { return Object.keys(unique).reduce((all, key) => all.concat({ value: unique[key], label: key }), []).sort(sortOptions); } function sortOptions(a, b) { if (a.label === void 0 && b.label === void 0) { return 0; } if (a.label === void 0 && b.label !== void 0) { return -1; } if (a.label !== void 0 && b.label === void 0) { return 1; } if (a.label < b.label) { return -1; } if (a.label > b.label) { return 1; } return 0; } function getFilteredOptions(options, filterValues) { if (!filterValues) { return []; } return options.filter((option) => filterValues.some((filtered) => filtered.value === option.value)); } function sortCaseInsensitive(a, b, id) { return String(a.values[id]).localeCompare(String(b.values[id]), void 0, { sensitivity: "base" }); } function sortNumber(rowA, rowB, id) { const a = toNumber(rowA.values[id]); const b = toNumber(rowB.values[id]); return a === b ? 0 : a > b ? 1 : -1; } function toNumber(value) { var _a; if (data.isDataFrameWithValue(value)) { return (_a = value.value) != null ? _a : Number.NEGATIVE_INFINITY; } if (value === null || value === void 0 || value === "" || isNaN(value)) { return Number.NEGATIVE_INFINITY; } if (typeof value === "number") { return value; } return Number(value); } function getFooterItems(filterFields, values, options, theme2) { addMissingColumnIndex(filterFields); return filterFields.map((data$1, i) => { var _a; if (((_a = data$1 == null ? void 0 : data$1.field) == null ? void 0 : _a.type) !== data.FieldType.number) { if (i === 0 && options.reducer && options.reducer.length > 0) { const reducer = data.fieldReducers.get(options.reducer[0]); return reducer.name; } return void 0; } let newField = lodash.clone(data$1.field); newField.values = values[data$1.id]; newField.state = void 0; data$1.field = newField; if (options.fields && options.fields.length > 0) { const f = options.fields.find((f2) => { var _a2; return f2 === ((_a2 = data$1 == null ? void 0 : data$1.field) == null ? void 0 : _a2.name); }); if (f) { return getFormattedValue(data$1.field, options.reducer, theme2); } return void 0; } return getFormattedValue(data$1.field, options.reducer || [], theme2); }); } function getFormattedValue(field, reducer, theme) { var _a; const calc = reducer[0]; if (calc === void 0) { return ""; } const format = (_a = field.display) != null ? _a : data.getDisplayProcessor({ field, theme }); const fieldCalcValue = data.reduceField({ field, reducers: reducer })[calc]; const reducerInfo = data.fieldReducers.get(calc); if (reducerInfo.preservesUnits) { return data.formattedValueToString(format(fieldCalcValue)); } return data.formattedValueToString({ text: fieldCalcValue }); } function createFooterCalculationValues(rows) { const values = []; for (const key in rows) { for (const [valKey, val] of Object.entries(rows[key].values)) { if (values[valKey] === void 0) { values[valKey] = []; } values[valKey].push(val); } } return values; } function addMissingColumnIndex(columns) { var _a; const missingIndex = columns.findIndex((field, index) => (field == null ? void 0 : field.id) !== String(index)); if (missingIndex === -1 || ((_a = columns[missingIndex]) == null ? void 0 : _a.id) === "expander") { return; } columns.splice(missingIndex, 0, { id: String(missingIndex) }); addMissingColumnIndex(columns); } function isPointTimeValAroundTableTimeVal(pointTime, rowTime, threshold) { return Math.abs(Math.floor(pointTime) - rowTime) < threshold; } function calculateAroundPointThreshold(timeField) { let max = -Number.MAX_VALUE; let min = Number.MAX_VALUE; if (timeField.values.length < 2) { return 0; } for (let i = 0; i < timeField.values.length; i++) { const value = timeField.values[i]; if (value > max) { max = value; } if (value < min) { min = value; } } return (max - min) / timeField.values.length; } function guessTextBoundingBox(text, headerGroup, osContext, lineHeight, defaultRowHeight, padding = 0) { var _a; const width = Number((_a = headerGroup == null ? void 0 : headerGroup.width) != null ? _a : 300); const LINE_SCALE_FACTOR = 1.17; const LOW_LINE_PAD = 42; const PADDING = padding * 2; if (osContext !== null && typeof text === "string") { const words = text.split(/\s/); const lines = []; let currentLine = ""; let wordCount = 0; let extraLines = 0; for (let i = 0; i < words.length; i++) { const currentWord = words[i]; let lineWidth = osContext.measureText(currentLine + " " + currentWord).width; if (lineWidth < width - PADDING) { currentLine += " " + currentWord; wordCount++; } else { lines.push({ width: lineWidth, line: currentLine }); currentLine = currentWord; wordCount = 0; } } for (let i = 0; i < lines.length; i++) { if (lines[i].width > width) { let extra = Math.floor(lines[i].width / width) - 1; extraLines += extra; } } let lineNumber = lines.length + extraLines; let height = 38; if (lineNumber > 5) { height = lineNumber * lineHeight * LINE_SCALE_FACTOR; } else { height = lineNumber * lineHeight + LOW_LINE_PAD; } height += PADDING; return { width, height }; } return { width, height: defaultRowHeight }; } function guessLongestField(fieldConfig, data$1) { var _a, _b, _c, _d, _e, _f, _g; let longestField = void 0; const SAMPLE_SIZE = 3; if ((_b = (_a = fieldConfig.defaults.custom) == null ? void 0 : _a.cellOptions) == null ? void 0 : _b.wrapText) { const stringFields = data$1.fields.filter((field) => field.type === data.FieldType.string); if (stringFields.length >= 1 && stringFields[0].values.length > 0) { const numValues = stringFields[0].values.length; let longestLength = 0; if (numValues <= 30) { for (const field of stringFields) { const fieldLength = (_d = (_c = field.values.find((v) => v != null)) == null ? void 0 : _c.length) != null ? _d : 0; if (fieldLength > longestLength) { longestLength = fieldLength; longestField = field; } } } else { for (const field of stringFields) { const vals = lodash.sampleSize(field.values, SAMPLE_SIZE); const meanLength = (((_e = vals[0]) == null ? void 0 : _e.length) + ((_f = vals[1]) == null ? void 0 : _f.length) + ((_g = vals[2]) == null ? void 0 : _g.length)) / 3; if (meanLength > longestLength) { longestLength = meanLength; longestField = field; } } } } } return longestField; } exports.EXPANDER_WIDTH = EXPANDER_WIDTH; exports.calculateAroundPointThreshold = calculateAroundPointThreshold; exports.calculateUniqueFieldValues = calculateUniqueFieldValues; exports.createFooterCalculationValues = createFooterCalculationValues; exports.filterByValue = filterByValue; exports.getColumns = getColumns; exports.getFilteredOptions = getFilteredOptions; exports.getFooterItems = getFooterItems; exports.guessLongestField = guessLongestField; exports.guessTextBoundingBox = guessTextBoundingBox; exports.isPointTimeValAroundTableTimeVal = isPointTimeValAroundTableTimeVal; exports.rowToFieldValue = rowToFieldValue; exports.sortCaseInsensitive = sortCaseInsensitive; exports.sortNumber = sortNumber; exports.sortOptions = sortOptions; exports.valuesToOptions = valuesToOptions; //# sourceMappingURL=utils.cjs.map