1 line
34 KiB
Plaintext
1 line
34 KiB
Plaintext
|
|
{"version":3,"file":"util.mjs","sources":["../../../../../../packages/components/table/src/util.ts"],"sourcesContent":["import { createVNode, isVNode, render } from 'vue'\nimport { flatMap, get, isNull, merge } from 'lodash-unified'\nimport {\n ensureArray,\n getProp,\n hasOwn,\n isArray,\n isBoolean,\n isFunction,\n isNumber,\n isObject,\n isString,\n isUndefined,\n throwError,\n} from '@element-plus/utils'\nimport ElTooltip, {\n type ElTooltipProps,\n} from '@element-plus/components/tooltip'\n\nimport type { DefaultRow, Table, TreeProps } from './table/defaults'\nimport type { TableColumnCtx } from './table-column/defaults'\nimport type { CSSProperties, VNode } from 'vue'\n\nexport type TableOverflowTooltipOptions = Partial<\n Pick<\n ElTooltipProps,\n | 'appendTo'\n | 'effect'\n | 'enterable'\n | 'hideAfter'\n | 'offset'\n | 'placement'\n | 'popperClass'\n | 'popperOptions'\n | 'showAfter'\n | 'showArrow'\n | 'transition'\n >\n>\n\nexport type TableOverflowTooltipFormatter<T extends DefaultRow> = (data: {\n row: T\n column: TableColumnCtx<T>\n cellValue: any\n}) => VNode | string\n\ntype RemovePopperFn = (() => void) & {\n trigger?: HTMLElement\n vm?: VNode\n}\n\ntype CompareValue<T> = {\n value: T\n index: number\n key: any[] | null\n}\n\nexport const getCell = function (event: Event) {\n return (event.target as HTMLElement)?.closest('td')\n}\n\nexport const orderBy = function <T extends DefaultRow>(\n array: T[],\n sortKey: string | null,\n reverse: string | number | null,\n sortMethod: TableColumnCtx<T>['sortMethod'] | null,\n sortBy: string | string[] | ((a: T, index: number, array?: T[]) => string)\n) {\n if (\n !sortKey &&\n !sortMethod &&\n (!sortBy || (isArray(sortBy) && !sortBy.length))\n ) {\n return array\n }\n if (isString(reverse)) {\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: T, index: number) {\n if (sortBy) {\n return flatMap(ensureArray(sortBy), (by) => {\n if (isString(by)) {\n return get(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 [\n isObject(value) ? (sortKey ? get(value, sortKey) : null) : value,\n ]\n }\n const compare = function (a: CompareValue<T>, b: CompareValue<T>) {\n if (sortMethod) {\n return sortMethod(a.value, b.value)\n }\n for (let i = 0, len = a.key?.length ?? 0; 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<CompareValue<T>>((value: T, index) => {\n return {\n value,\n index,\n key: getKey ? getKey(value, index) : null,\n }\n })\n .sort((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 extends DefaultRow>(\n table: {\n columns: TableColumnCtx<T>[]\n },\n columnId: string\n): null | TableColumnCtx<T> {\n let column = null\n table.columns.forEach((item) => {\n if (item.id === columnId) {\n column = item\n }\n })\n return column\n}\n\nexport const getColumnByKey = function <T extends DefaultRow>(\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 if (!column)\n throwError('ElTable', `No co
|