util.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import { createVNode, render, isVNode } from 'vue';
  2. import { merge, get, flatMap, 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. if (!isArray(sortBy)) {
  24. sortBy = [sortBy];
  25. }
  26. return sortBy.map((by) => {
  27. if (isString(by)) {
  28. return get(value, by);
  29. } else {
  30. return by(value, index, array);
  31. }
  32. });
  33. }
  34. if (sortKey !== "$key") {
  35. if (isObject(value) && "$value" in value)
  36. value = value.$value;
  37. }
  38. return [isObject(value) ? get(value, sortKey) : value];
  39. };
  40. const compare = function(a, b) {
  41. if (sortMethod) {
  42. return sortMethod(a.value, b.value);
  43. }
  44. for (let i = 0, len = a.key.length; i < len; i++) {
  45. if (a.key[i] < b.key[i]) {
  46. return -1;
  47. }
  48. if (a.key[i] > b.key[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. };
  114. const getKeysMap = function(array, rowKey, flatten = false, childrenKey = "children") {
  115. const data = array || [];
  116. const arrayMap = {};
  117. data.forEach((row, index) => {
  118. arrayMap[getRowIdentity(row, rowKey)] = { row, index };
  119. if (flatten) {
  120. const children = row[childrenKey];
  121. if (isArray(children)) {
  122. Object.assign(arrayMap, getKeysMap(children, rowKey, true, childrenKey));
  123. }
  124. }
  125. });
  126. return arrayMap;
  127. };
  128. function mergeOptions(defaults, config) {
  129. const options = {};
  130. let key;
  131. for (key in defaults) {
  132. options[key] = defaults[key];
  133. }
  134. for (key in config) {
  135. if (hasOwn(config, key)) {
  136. const value = config[key];
  137. if (!isUndefined(value)) {
  138. options[key] = value;
  139. }
  140. }
  141. }
  142. return options;
  143. }
  144. function parseWidth(width) {
  145. if (width === "")
  146. return width;
  147. if (!isUndefined(width)) {
  148. width = Number.parseInt(width, 10);
  149. if (Number.isNaN(width)) {
  150. width = "";
  151. }
  152. }
  153. return width;
  154. }
  155. function parseMinWidth(minWidth) {
  156. if (minWidth === "")
  157. return minWidth;
  158. if (!isUndefined(minWidth)) {
  159. minWidth = parseWidth(minWidth);
  160. if (Number.isNaN(minWidth)) {
  161. minWidth = 80;
  162. }
  163. }
  164. return minWidth;
  165. }
  166. function parseHeight(height) {
  167. if (isNumber(height)) {
  168. return height;
  169. }
  170. if (isString(height)) {
  171. if (/^\d+(?:px)?$/.test(height)) {
  172. return Number.parseInt(height, 10);
  173. } else {
  174. return height;
  175. }
  176. }
  177. return null;
  178. }
  179. function compose(...funcs) {
  180. if (funcs.length === 0) {
  181. return (arg) => arg;
  182. }
  183. if (funcs.length === 1) {
  184. return funcs[0];
  185. }
  186. return funcs.reduce((a, b) => (...args) => a(b(...args)));
  187. }
  188. function toggleRowStatus(statusArr, row, newVal, tableTreeProps, selectable, rowIndex) {
  189. let _rowIndex = rowIndex != null ? rowIndex : 0;
  190. let changed = false;
  191. const index = statusArr.indexOf(row);
  192. const included = index !== -1;
  193. const isRowSelectable = selectable == null ? void 0 : selectable.call(null, row, _rowIndex);
  194. const toggleStatus = (type) => {
  195. if (type === "add") {
  196. statusArr.push(row);
  197. } else {
  198. statusArr.splice(index, 1);
  199. }
  200. changed = true;
  201. };
  202. const getChildrenCount = (row2) => {
  203. let count = 0;
  204. const children = (tableTreeProps == null ? void 0 : tableTreeProps.children) && row2[tableTreeProps.children];
  205. if (children && isArray(children)) {
  206. count += children.length;
  207. children.forEach((item) => {
  208. count += getChildrenCount(item);
  209. });
  210. }
  211. return count;
  212. };
  213. if (!selectable || isRowSelectable) {
  214. if (isBoolean(newVal)) {
  215. if (newVal && !included) {
  216. toggleStatus("add");
  217. } else if (!newVal && included) {
  218. toggleStatus("remove");
  219. }
  220. } else {
  221. included ? toggleStatus("remove") : toggleStatus("add");
  222. }
  223. }
  224. if (!(tableTreeProps == null ? void 0 : tableTreeProps.checkStrictly) && (tableTreeProps == null ? void 0 : tableTreeProps.children) && isArray(row[tableTreeProps.children])) {
  225. row[tableTreeProps.children].forEach((item) => {
  226. const childChanged = toggleRowStatus(statusArr, item, newVal != null ? newVal : !included, tableTreeProps, selectable, _rowIndex + 1);
  227. _rowIndex += getChildrenCount(item) + 1;
  228. if (childChanged) {
  229. changed = childChanged;
  230. }
  231. });
  232. }
  233. return changed;
  234. }
  235. function walkTreeNode(root, cb, childrenKey = "children", lazyKey = "hasChildren") {
  236. const isNil = (array) => !(isArray(array) && array.length);
  237. function _walker(parent, children, level) {
  238. cb(parent, children, level);
  239. children.forEach((item) => {
  240. if (item[lazyKey]) {
  241. cb(item, null, level + 1);
  242. return;
  243. }
  244. const children2 = item[childrenKey];
  245. if (!isNil(children2)) {
  246. _walker(item, children2, level + 1);
  247. }
  248. });
  249. }
  250. root.forEach((item) => {
  251. if (item[lazyKey]) {
  252. cb(item, null, 0);
  253. return;
  254. }
  255. const children = item[childrenKey];
  256. if (!isNil(children)) {
  257. _walker(item, children, 0);
  258. }
  259. });
  260. }
  261. const getTableOverflowTooltipProps = (props, innerText, row, column) => {
  262. const popperOptions = {
  263. strategy: "fixed",
  264. ...props.popperOptions
  265. };
  266. const tooltipFormatterContent = isFunction(column.tooltipFormatter) ? column.tooltipFormatter({
  267. row,
  268. column,
  269. cellValue: getProp(row, column.property).value
  270. }) : void 0;
  271. if (isVNode(tooltipFormatterContent)) {
  272. return {
  273. slotContent: tooltipFormatterContent,
  274. content: null,
  275. ...props,
  276. popperOptions
  277. };
  278. }
  279. return {
  280. slotContent: null,
  281. content: tooltipFormatterContent != null ? tooltipFormatterContent : innerText,
  282. ...props,
  283. popperOptions
  284. };
  285. };
  286. let removePopper = null;
  287. function createTablePopper(props, popperContent, row, column, trigger, table) {
  288. const tableOverflowTooltipProps = getTableOverflowTooltipProps(props, popperContent, row, column);
  289. const mergedProps = {
  290. ...tableOverflowTooltipProps,
  291. slotContent: void 0
  292. };
  293. if ((removePopper == null ? void 0 : removePopper.trigger) === trigger) {
  294. const comp = removePopper.vm.component;
  295. merge(comp.props, mergedProps);
  296. if (tableOverflowTooltipProps.slotContent) {
  297. comp.slots.content = () => [tableOverflowTooltipProps.slotContent];
  298. }
  299. return;
  300. }
  301. removePopper == null ? void 0 : removePopper();
  302. const parentNode = table == null ? void 0 : table.refs.tableWrapper;
  303. const ns = parentNode == null ? void 0 : parentNode.dataset.prefix;
  304. const vm = createVNode(ElTooltip, {
  305. virtualTriggering: true,
  306. virtualRef: trigger,
  307. appendTo: parentNode,
  308. placement: "top",
  309. transition: "none",
  310. offset: 0,
  311. hideAfter: 0,
  312. ...mergedProps
  313. }, tableOverflowTooltipProps.slotContent ? {
  314. content: () => tableOverflowTooltipProps.slotContent
  315. } : void 0);
  316. vm.appContext = { ...table.appContext, ...table };
  317. const container = document.createElement("div");
  318. render(vm, container);
  319. vm.component.exposed.onOpen();
  320. const scrollContainer = parentNode == null ? void 0 : parentNode.querySelector(`.${ns}-scrollbar__wrap`);
  321. removePopper = () => {
  322. render(null, container);
  323. scrollContainer == null ? void 0 : scrollContainer.removeEventListener("scroll", removePopper);
  324. removePopper = null;
  325. };
  326. removePopper.trigger = trigger;
  327. removePopper.vm = vm;
  328. scrollContainer == null ? void 0 : scrollContainer.addEventListener("scroll", removePopper);
  329. }
  330. function getCurrentColumns(column) {
  331. if (column.children) {
  332. return flatMap(column.children, getCurrentColumns);
  333. } else {
  334. return [column];
  335. }
  336. }
  337. function getColSpan(colSpan, column) {
  338. return colSpan + column.colSpan;
  339. }
  340. const isFixedColumn = (index, fixed, store, realColumns) => {
  341. let start = 0;
  342. let after = index;
  343. const columns = store.states.columns.value;
  344. if (realColumns) {
  345. const curColumns = getCurrentColumns(realColumns[index]);
  346. const preColumns = columns.slice(0, columns.indexOf(curColumns[0]));
  347. start = preColumns.reduce(getColSpan, 0);
  348. after = start + curColumns.reduce(getColSpan, 0) - 1;
  349. } else {
  350. start = index;
  351. }
  352. let fixedLayout;
  353. switch (fixed) {
  354. case "left":
  355. if (after < store.states.fixedLeafColumnsLength.value) {
  356. fixedLayout = "left";
  357. }
  358. break;
  359. case "right":
  360. if (start >= columns.length - store.states.rightFixedLeafColumnsLength.value) {
  361. fixedLayout = "right";
  362. }
  363. break;
  364. default:
  365. if (after < store.states.fixedLeafColumnsLength.value) {
  366. fixedLayout = "left";
  367. } else if (start >= columns.length - store.states.rightFixedLeafColumnsLength.value) {
  368. fixedLayout = "right";
  369. }
  370. }
  371. return fixedLayout ? {
  372. direction: fixedLayout,
  373. start,
  374. after
  375. } : {};
  376. };
  377. const getFixedColumnsClass = (namespace, index, fixed, store, realColumns, offset = 0) => {
  378. const classes = [];
  379. const { direction, start, after } = isFixedColumn(index, fixed, store, realColumns);
  380. if (direction) {
  381. const isLeft = direction === "left";
  382. classes.push(`${namespace}-fixed-column--${direction}`);
  383. if (isLeft && after + offset === store.states.fixedLeafColumnsLength.value - 1) {
  384. classes.push("is-last-column");
  385. } else if (!isLeft && start - offset === store.states.columns.value.length - store.states.rightFixedLeafColumnsLength.value) {
  386. classes.push("is-first-column");
  387. }
  388. }
  389. return classes;
  390. };
  391. function getOffset(offset, column) {
  392. return offset + (isNull(column.realWidth) || Number.isNaN(column.realWidth) ? Number(column.width) : column.realWidth);
  393. }
  394. const getFixedColumnOffset = (index, fixed, store, realColumns) => {
  395. const {
  396. direction,
  397. start = 0,
  398. after = 0
  399. } = isFixedColumn(index, fixed, store, realColumns);
  400. if (!direction) {
  401. return;
  402. }
  403. const styles = {};
  404. const isLeft = direction === "left";
  405. const columns = store.states.columns.value;
  406. if (isLeft) {
  407. styles.left = columns.slice(0, start).reduce(getOffset, 0);
  408. } else {
  409. styles.right = columns.slice(after + 1).reverse().reduce(getOffset, 0);
  410. }
  411. return styles;
  412. };
  413. const ensurePosition = (style, key) => {
  414. if (!style)
  415. return;
  416. if (!Number.isNaN(style[key])) {
  417. style[key] = `${style[key]}px`;
  418. }
  419. };
  420. export { compose, createTablePopper, ensurePosition, getCell, getColumnByCell, getColumnById, getColumnByKey, getFixedColumnOffset, getFixedColumnsClass, getKeysMap, getRowIdentity, isFixedColumn, mergeOptions, orderBy, parseHeight, parseMinWidth, parseWidth, removePopper, toggleRowStatus, walkTreeNode };
  421. //# sourceMappingURL=util.mjs.map