90c2ac9741182e368462f0bb79fb93f94ede6d9c2ba86c3fe59f063aa59048a8e847f18a5ec9e0eab644b79ccc7faa8005886bd0743c330be5cbff3d334ae3 14 KB

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