util.js 12 KB

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