You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
26 KiB
1 line
26 KiB
11 months ago
|
{"version":3,"file":"util.mjs","sources":["../../../../../../packages/components/table/src/util.ts"],"sourcesContent":["import { hasOwn } from '@vue/shared'\nimport { createPopper } from '@popperjs/core'\nimport { PopupManager } from '@element-plus/utils/popup-manager'\nimport { getValueByPath } from '@element-plus/utils/util'\nimport scrollbarWidth from '@element-plus/utils/scrollbar-width'\nimport { off, on } from '@element-plus/utils/dom'\n\nimport type {\n PopperInstance,\n IPopperOptions,\n} from '@element-plus/components/popper'\nimport type { Indexable, Nullable } from '@element-plus/utils/types'\nimport type { TableColumnCtx } from './table-column/defaults'\n\nexport const getCell = function (event: Event): HTMLElement {\n let cell = event.target as HTMLElement\n\n while (cell && cell.tagName.toUpperCase() !== 'HTML') {\n if (cell.tagName.toUpperCase() === 'TD') {\n return cell\n }\n cell = cell.parentNode as HTMLElement\n }\n\n return null\n}\n\nconst isObject = function (obj: unknown): boolean {\n return obj !== null && typeof obj === 'object'\n}\n\nexport const orderBy = function <T>(\n array: T[],\n sortKey: string,\n reverse: string | number,\n sortMethod,\n sortBy: string | (string | ((a: T, b: T, array?: T[]) => number))[]\n) {\n if (\n !sortKey &&\n !sortMethod &&\n (!sortBy || (Array.isArray(sortBy) && !sortBy.length))\n ) {\n return array\n }\n if (typeof reverse === 'string') {\n reverse = reverse === 'descending' ? -1 : 1\n } else {\n reverse = reverse && reverse < 0 ? -1 : 1\n }\n const getKey = sortMethod\n ? null\n : function (value, index) {\n if (sortBy) {\n if (!Array.isArray(sortBy)) {\n sortBy = [sortBy]\n }\n return sortBy.map(function (by) {\n if (typeof by === 'string') {\n return getValueByPath(value, by)\n } else {\n return by(value, index, array)\n }\n })\n }\n if (sortKey !== '$key') {\n if (isObject(value) && '$value' in value) value = value.$value\n }\n return [isObject(value) ? getValueByPath(value, sortKey) : value]\n }\n const compare = function (a, b) {\n if (sortMethod) {\n return sortMethod(a.value, b.value)\n }\n for (let i = 0, len = a.key.length; i < len; i++) {\n if (a.key[i] < b.key[i]) {\n return -1\n }\n if (a.key[i] > b.key[i]) {\n return 1\n }\n }\n return 0\n }\n return array\n .map(function (value, index) {\n return {\n value,\n index,\n key: getKey ? getKey(value, index) : null,\n }\n })\n .sort(function (a, b) {\n let order = compare(a, b)\n if (!order) {\n // make stable https://en.wikipedia.org/wiki/Sorting_algorithm#Stability\n order = a.index - b.index\n }\n return order * +reverse\n })\n .map((item) => item.value)\n}\n\nexport const getColumnById = function <T>(\n table: {\n columns: TableColumnCtx<T>[]\n },\n columnId: string\n): null | TableColumnCtx<T> {\n let column = null\n table.columns.forEach(function (item) {\n if (item.id === columnId) {\n column = item\n }\n })\n return column\n}\n\nexport const getColumnByKey = function <T>(\n table: {\n columns: TableColumnCtx<T>[]\n },\n columnKey: string\n): TableColumnCtx<T> {\n let column = null\n for (let i = 0; i < table.columns.length; i++) {\n const item = table.columns[i]\n if (item.columnKey === columnKey) {\n column = item\n break\n }\n }\n return column\n}\n\nexport const getColumnByCell = function <T>(\n table: {\n columns: TableColumnCtx<T>[]\n },\n cell: HTMLElement\n): null | TableColumnCtx<T> {\n const matches = (cell.className || '').match(/el-table_[^\\s]+/gm)\n if (matches) {\n return getColumnById(table, matches[0])\n }\n return null\n}\n\nexport const getRowIdentity = <T>(\n row: T,\n rowKey: string | ((row: T) => any)\n): string => {\n if (!row) throw new Error('Row is required when g
|