ddf57ed6204e449870b1186dfde2f270d71bf7e18c93311e1a025a4c86c5b31866e2e0db28db56960dea57ba7df735cc49ad1187291f8b39a871a2bf2cfd01 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { createVNode, render, isVNode } from 'vue';
  2. import { merge, flatMap, castArray, get, isNull } from 'lodash-unified';
  3. import { ElTooltip } from '../../tooltip/index.mjs';
  4. import { isArray, isString, isFunction, hasOwn, isObject } from '@vue/shared';
  5. import { throwError } from '../../../utils/error.mjs';
  6. import { isUndefined, isNumber, isBoolean } from '../../../utils/types.mjs';
  7. import { getProp } from '../../../utils/objects.mjs';
  8. const getCell = function(event) {
  9. var _a;
  10. return (_a = event.target) == null ? void 0 : _a.closest("td");
  11. };
  12. const orderBy = function(array, sortKey, reverse, sortMethod, sortBy) {
  13. if (!sortKey && !sortMethod && (!sortBy || isArray(sortBy) && !sortBy.length)) {
  14. return array;
  15. }
  16. if (isString(reverse)) {
  17. reverse = reverse === "descending" ? -1 : 1;
  18. } else {
  19. reverse = reverse && reverse < 0 ? -1 : 1;
  20. }
  21. const getKey = sortMethod ? null : function(value, index) {
  22. if (sortBy) {
  23. return flatMap(castArray(sortBy), (by) => {
  24. if (isString(by)) {
  25. return get(value, by);
  26. } else {
  27. return by(value, index, array);
  28. }
  29. });
  30. }
  31. if (sortKey !== "$key") {
  32. if (isObject(value) && "$value" in value)
  33. value = value.$value;
  34. }
  35. return [
  36. isObject(value) ? sortKey ? get(value, sortKey) : null : value
  37. ];
  38. };
  39. const compare = function(a, b) {
  40. var _a, _b, _c, _d, _e, _f;
  41. if (sortMethod) {
  42. return sortMethod(a.value, b.value);
  43. }
  44. for (let i = 0, len = (_b = (_a = a.key) == null ? void 0 : _a.length) != null ? _b : 0; i < len; i++) {
  45. if (((_c = a.key) == null ? void 0 : _c[i]) < ((_d = b.key) == null ? void 0 : _d[i])) {
  46. return -1;
  47. }
  48. if (((_e = a.key) == null ? void 0 : _e[i]) > ((_f = b.key) == null ? void 0 : _f[i])) {
  49. return 1;
  50. }
  51. }
  52. return 0;
  53. };
  54. return array.map((value, index) => {
  55. return {
  56. value,
  57. index,
  58. key: getKey ? getKey(value, index) : null
  59. };
  60. }).sort((a, b) => {
  61. let order = compare(a, b);
  62. if (!order) {
  63. order = a.index - b.index;
  64. }
  65. return order * +reverse;
  66. }).map((item) => item.value);
  67. };
  68. const getColumnById = function(table, columnId) {
  69. let column = null;
  70. table.columns.forEach((item) => {
  71. if (item.id === columnId) {
  72. column = item;
  73. }
  74. });
  75. return column;
  76. };
  77. const getColumnByKey = function(table, columnKey) {
  78. let column = null;
  79. for (let i = 0; i < table.columns.length; i++) {
  80. const item = table.columns[i];
  81. if (item.columnKey === columnKey) {
  82. column = item;
  83. break;
  84. }
  85. }
  86. if (!column)
  87. throwError("ElTable", `No column matching with column-key: ${columnKey}`);
  88. return column;
  89. };
  90. const getColumnByCell = function(table, cell, namespace) {
  91. const matches = (cell.className || "").match(new RegExp(`${namespace}-table_[^\\s]+`, "gm"));
  92. if (matches) {
  93. return getColumnById(table, matches[0]);
  94. }
  95. return null;
  96. };
  97. const getRowIdentity = (row, rowKey) => {
  98. if (!row)
  99. throw new Error("Row is required when get row identity");
  100. if (isString(rowKey)) {
  101. if (!rowKey.includes(".")) {
  102. return `${row[rowKey]}`;
  103. }
  104. const key = rowKey.split(".");
  105. let current = row;
  106. for (const element of key) {
  107. current = current[element];
  108. }
  109. return `${current}`;
  110. } else if (isFunction(rowKey)) {
  111. return rowKey.call(null, row);
  112. }
  113. return "";
  114. };
  115. const getKeysMap = function(array, rowKey, flatten = false, childrenKey = "children") {
  116. const data = array || [];
  117. const arrayMap = {};
  118. data.forEach((row, index) => {
  119. arrayMap[getRowIdentity(row, rowKey)] = { row, index };
  120. if (flatten) {
  121. const children = row[childrenKey];
  122. if (isArray(children)) {
  123. Object.assign(arrayMap, getKeysMap(children, rowKey, true, childrenKey));
  124. }
  125. }
  126. });
  127. return arrayMap;
  128. };
  129. function mergeOptions(defaults, config) {
  130. const options = {};
  131. let key;
  132. for (key in defaults) {
  133. options[key] = defaults[key];
  134. }
  135. for (key in config) {
  136. if (hasOwn(config, key)) {
  137. const value = config[key];
  138. if (!isUndefined(value)) {
  139. options[key] = value;
  140. }
  141. }
  142. }
  143. return options;
  144. }
  145. function parseWidth(width) {
  146. if (width === "")
  147. return width;
  148. if (!isUndefined(width)) {
  149. width = Number.parseInt(width, 10);
  150. if (Number.isNaN(width)) {
  151. width = "";
  152. }
  153. }
  154. return width;
  155. }
  156. function parseMinWidth(minWidth) {
  157. if (minWidth === "")
  158. return minWidth;
  159. if (!isUndefined(minWidth)) {
  160. minWidth = parseWidth(minWidth);
  161. if (Number.isNaN(minWidth)) {
  162. minWidth = 80;
  163. }
  164. }
  165. return minWidth;
  166. }
  167. function parseHeight(height) {
  168. if (isNumber(height)) {
  169. return height;
  170. }
  171. if (isString(height)) {
  172. if (/^\d+(?:px)?$/.test(height)) {
  173. return Number.parseInt(height, 10);
  174. } else {
  175. return height;
  176. }
  177. }
  178. return null;
  179. }
  180. function compose(...funcs) {
  181. if (funcs.length === 0) {
  182. return (arg) => arg;
  183. }
  184. if (funcs.length === 1) {
  185. return funcs[0];
  186. }
  187. return funcs.reduce((a, b) => (...args) => a(b(...args)));
  188. }
  189. function toggleRowStatus(statusArr, row, newVal, tableTreeProps, selectable, rowIndex, rowKey) {
  190. let _rowIndex = rowIndex != null ? rowIndex : 0;
  191. let changed = false;
  192. const getIndex = () => {
  193. if (!rowKey) {
  194. return statusArr.indexOf(row);
  195. }
  196. const id = getRowIdentity(row, rowKey);
  197. return statusArr.findIndex((item) => getRowIdentity(item, rowKey) === id);
  198. };
  199. const index = getIndex();
  200. const included = index !== -1;
  201. const isRowSelectable = selectable == null ? void 0 : selectable.call(null, row, _rowIndex);
  202. const toggleStatus = (type) => {
  203. if (type === "add") {
  204. statusArr.push(row);
  205. } else {
  206. statusArr.splice(index, 1);
  207. }
  208. changed = true;
  209. };
  210. const getChildrenCount = (row2) => {
  211. let count = 0;
  212. const children = (tableTreeProps == null ? void 0 : tableTreeProps.children) && row2[tableTreeProps.children];
  213. if (children && isArray(children)) {
  214. count += children.length;
  215. children.forEach((item) => {
  216. count += getChildrenCount(item);
  217. });
  218. }
  219. return count;
  220. };
  221. if (!selectable || isRowSelectable) {
  222. if (isBoolean(newVal)) {
  223. if (newVal && !included) {
  224. toggleStatus("add");
  225. } else if (!newVal && included) {
  226. toggleStatus("remove");
  227. }
  228. } else {
  229. included ? toggleStatus("remove") : toggleStatus("add");
  230. }
  231. }
  232. if (!(tableTreeProps == null ? void 0 : tableTreeProps.checkStrictly) && (tableTreeProps == null ? void 0 : tableTreeProps.children) && isArray(row[tableTreeProps.children])) {
  233. row[tableTreeProps.children].forEach((item) => {
  234. const childChanged = toggleRowStatus(statusArr, item, newVal != null ? newVal : !included, tableTreeProps, selectable, _rowIndex + 1, rowKey);
  235. _rowIndex += getChildrenCount(item) + 1;
  236. if (childChanged) {
  237. changed = childChanged;
  238. }
  239. });
  240. }
  241. return changed;
  242. }
  243. function walkTreeNode(root, cb, childrenKey = "children", lazyKey = "hasChildren", lazy = false) {
  244. const isNil = (array) => !(isArray(array) && array.length);
  245. function _walker(parent, children, level) {
  246. cb(parent, children, level);
  247. children.forEach((item) => {
  248. if (item[lazyKey] && lazy) {
  249. cb(item, null, level + 1);
  250. return;
  251. }
  252. const children2 = item[childrenKey];
  253. if (!isNil(children2)) {
  254. _walker(item, children2, level + 1);
  255. }
  256. });
  257. }
  258. root.forEach((item) => {
  259. if (item[lazyKey] && lazy) {
  260. cb(item, null, 0);
  261. return;
  262. }
  263. const children = item[childrenKey];
  264. if (!isNil(children)) {
  265. _walker(item, children, 0);
  266. }
  267. });
  268. }
  269. const getTableOverflowTooltipProps = (props, innerText, row, column) => {
  270. const popperOptions = {
  271. strategy: "fixed",
  272. ...props.popperOptions
  273. };
  274. const tooltipFormatterContent = isFunction(column == null ? void 0 : column.tooltipFormatter) ? column.tooltipFormatter({
  275. row,
  276. column,
  277. cellValue: getProp(row, column.property).value
  278. }) : void 0;
  279. if (isVNode(tooltipFormatterContent)) {
  280. return {
  281. slotContent: tooltipFormatterContent,
  282. content: null,
  283. ...props,
  284. popperOptions
  285. };
  286. }
  287. return {
  288. slotContent: null,
  289. content: tooltipFormatterContent != null ? tooltipFormatterContent : innerText,
  290. ...props,
  291. popperOptions
  292. };
  293. };
  294. let removePopper = null;
  295. function createTablePopper(props, popperContent, row, column, trigger, table) {
  296. var _a;
  297. const tableOverflowTooltipProps = getTableOverflowTooltipProps(props, popperContent, row, column);
  298. const mergedProps = {
  299. ...tableOverflowTooltipProps,
  300. slotContent: void 0
  301. };
  302. if ((removePopper == null ? void 0 : removePopper.trigger) === trigger) {
  303. const comp = (_a = removePopper.vm) == null ? void 0 : _a.component;
  304. merge(comp == null ? void 0 : comp.props, mergedProps);
  305. if (comp && tableOverflowTooltipProps.slotContent) {
  306. comp.slots.content = () => [tableOverflowTooltipProps.slotContent];
  307. }
  308. return;
  309. }
  310. removePopper == null ? void 0 : removePopper();
  311. const parentNode = table == null ? void 0 : table.refs.tableWrapper;
  312. const ns = parentNode == null ? void 0 : parentNode.dataset.prefix;
  313. const vm = createVNode(ElTooltip, {
  314. virtualTriggering: true,
  315. virtualRef: trigger,
  316. appendTo: parentNode,
  317. placement: "top",
  318. transition: "none",
  319. offset: 0,
  320. hideAfter: 0,
  321. ...mergedProps
  322. }, tableOverflowTooltipProps.slotContent ? {
  323. content: () => tableOverflowTooltipProps.slotContent
  324. } : void 0);
  325. vm.appContext = { ...table.appContext, ...table };
  326. const container = document.createElement("div");
  327. render(vm, container);
  328. vm.component.exposed.onOpen();
  329. const scrollContainer = parentNode == null ? void 0 : parentNode.querySelector(`.${ns}-scrollbar__wrap`);
  330. removePopper = () => {
  331. var _a2, _b;
  332. if ((_b = (_a2 = vm.component) == null ? void 0 : _a2.exposed) == null ? void 0 : _b.onClose) {
  333. vm.component.exposed.onClose();
  334. }
  335. render(null, container);
  336. const currentRemovePopper = removePopper;
  337. scrollContainer == null ? void 0 : scrollContainer.removeEventListener("scroll", currentRemovePopper);
  338. currentRemovePopper.trigger = void 0;
  339. currentRemovePopper.vm = void 0;
  340. removePopper = null;
  341. };
  342. removePopper.trigger = trigger != null ? trigger : void 0;
  343. removePopper.vm = vm;
  344. scrollContainer == null ? void 0 : scrollContainer.addEventListener("scroll", removePopper);
  345. }
  346. function getCurrentColumns(column) {
  347. if (column.children) {
  348. return flatMap(column.children, getCurrentColumns);
  349. } else {
  350. return [column];
  351. }
  352. }
  353. function getColSpan(colSpan, column) {
  354. return colSpan + column.colSpan;
  355. }
  356. const isFixedColumn = (index, fixed, store, realColumns) => {
  357. let start = 0;
  358. let after = index;
  359. const columns = store.states.columns.value;
  360. if (realColumns) {
  361. const curColumns = getCurrentColumns(realColumns[index]);
  362. const preColumns = columns.slice(0, columns.indexOf(curColumns[0]));
  363. start = preColumns.reduce(getColSpan, 0);
  364. after = start + curColumns.reduce(getColSpan, 0) - 1;
  365. } else {
  366. start = index;
  367. }
  368. let fixedLayout;
  369. switch (fixed) {
  370. case "left":
  371. if (after < store.states.fixedLeafColumnsLength.value) {
  372. fixedLayout = "left";
  373. }
  374. break;
  375. case "right":
  376. if (start >= columns.length - store.states.rightFixedLeafColumnsLength.value) {
  377. fixedLayout = "right";
  378. }
  379. break;
  380. default:
  381. if (after < store.states.fixedLeafColumnsLength.value) {
  382. fixedLayout = "left";
  383. } else if (start >= columns.length - store.states.rightFixedLeafColumnsLength.value) {
  384. fixedLayout = "right";
  385. }
  386. }
  387. return fixedLayout ? {
  388. direction: fixedLayout,
  389. start,
  390. after
  391. } : {};
  392. };
  393. const getFixedColumnsClass = (namespace, index, fixed, store, realColumns, offset = 0) => {
  394. const classes = [];
  395. const { direction, start, after } = isFixedColumn(index, fixed, store, realColumns);
  396. if (direction) {
  397. const isLeft = direction === "left";
  398. classes.push(`${namespace}-fixed-column--${direction}`);
  399. if (isLeft && after + offset === store.states.fixedLeafColumnsLength.value - 1) {
  400. classes.push("is-last-column");
  401. } else if (!isLeft && start - offset === store.states.columns.value.length - store.states.rightFixedLeafColumnsLength.value) {
  402. classes.push("is-first-column");
  403. }
  404. }
  405. return classes;
  406. };
  407. function getOffset(offset, column) {
  408. return offset + (isNull(column.realWidth) || Number.isNaN(column.realWidth) ? Number(column.width) : column.realWidth);
  409. }
  410. const getFixedColumnOffset = (index, fixed, store, realColumns) => {
  411. const {
  412. direction,
  413. start = 0,
  414. after = 0
  415. } = isFixedColumn(index, fixed, store, realColumns);
  416. if (!direction) {
  417. return;
  418. }
  419. const styles = {};
  420. const isLeft = direction === "left";
  421. const columns = store.states.columns.value;
  422. if (isLeft) {
  423. styles.left = columns.slice(0, start).reduce(getOffset, 0);
  424. } else {
  425. styles.right = columns.slice(after + 1).reverse().reduce(getOffset, 0);
  426. }
  427. return styles;
  428. };
  429. const ensurePosition = (style, key) => {
  430. if (!style)
  431. return;
  432. if (!Number.isNaN(style[key])) {
  433. style[key] = `${style[key]}px`;
  434. }
  435. };
  436. export { compose, createTablePopper, ensurePosition, getCell, getColumnByCell, getColumnById, getColumnByKey, getFixedColumnOffset, getFixedColumnsClass, getKeysMap, getRowIdentity, isFixedColumn, mergeOptions, orderBy, parseHeight, parseMinWidth, parseWidth, removePopper, toggleRowStatus, walkTreeNode };
  437. //# sourceMappingURL=util.mjs.map