98b2d482731b8c9d123bdfbb6f9b66a3013fede0da7de211357d12a267cd3c25ab019493ba3e950f2d518f5091e6197e6fd385ce84ec3672e7ebf84d690abe 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { computed, unref } from 'vue';
  2. import { SortOrder, oppositeOrderMap } from '../constants.mjs';
  3. import { placeholderSign } from '../private.mjs';
  4. import { calcColumnStyle } from './utils.mjs';
  5. import { isObject } from '@vue/shared';
  6. function useColumns(props, columns, fixed) {
  7. const _columns = computed(() => unref(columns).map((column, index) => {
  8. var _a, _b;
  9. return {
  10. ...column,
  11. key: (_b = (_a = column.key) != null ? _a : column.dataKey) != null ? _b : index
  12. };
  13. }));
  14. const visibleColumns = computed(() => {
  15. return unref(_columns).filter((column) => !column.hidden);
  16. });
  17. const fixedColumnsOnLeft = computed(() => unref(visibleColumns).filter((column) => column.fixed === "left" || column.fixed === true));
  18. const fixedColumnsOnRight = computed(() => unref(visibleColumns).filter((column) => column.fixed === "right"));
  19. const normalColumns = computed(() => unref(visibleColumns).filter((column) => !column.fixed));
  20. const mainColumns = computed(() => {
  21. const ret = [];
  22. unref(fixedColumnsOnLeft).forEach((column) => {
  23. ret.push({
  24. ...column,
  25. placeholderSign
  26. });
  27. });
  28. unref(normalColumns).forEach((column) => {
  29. ret.push(column);
  30. });
  31. unref(fixedColumnsOnRight).forEach((column) => {
  32. ret.push({
  33. ...column,
  34. placeholderSign
  35. });
  36. });
  37. return ret;
  38. });
  39. const hasFixedColumns = computed(() => {
  40. return unref(fixedColumnsOnLeft).length || unref(fixedColumnsOnRight).length;
  41. });
  42. const columnsStyles = computed(() => {
  43. return unref(_columns).reduce((style, column) => {
  44. style[column.key] = calcColumnStyle(column, unref(fixed), props.fixed);
  45. return style;
  46. }, {});
  47. });
  48. const columnsTotalWidth = computed(() => {
  49. return unref(visibleColumns).reduce((width, column) => width + column.width, 0);
  50. });
  51. const getColumn = (key) => {
  52. return unref(_columns).find((column) => column.key === key);
  53. };
  54. const getColumnStyle = (key) => {
  55. return unref(columnsStyles)[key];
  56. };
  57. const updateColumnWidth = (column, width) => {
  58. column.width = width;
  59. };
  60. function onColumnSorted(e) {
  61. var _a;
  62. const { key } = e.currentTarget.dataset;
  63. if (!key)
  64. return;
  65. const { sortState, sortBy } = props;
  66. let order = SortOrder.ASC;
  67. if (isObject(sortState)) {
  68. order = oppositeOrderMap[sortState[key]];
  69. } else {
  70. order = oppositeOrderMap[sortBy.order];
  71. }
  72. (_a = props.onColumnSort) == null ? void 0 : _a.call(props, { column: getColumn(key), key, order });
  73. }
  74. return {
  75. columns: _columns,
  76. columnsStyles,
  77. columnsTotalWidth,
  78. fixedColumnsOnLeft,
  79. fixedColumnsOnRight,
  80. hasFixedColumns,
  81. mainColumns,
  82. normalColumns,
  83. visibleColumns,
  84. getColumn,
  85. getColumnStyle,
  86. updateColumnWidth,
  87. onColumnSorted
  88. };
  89. }
  90. export { useColumns };
  91. //# sourceMappingURL=use-columns.mjs.map