5a9c91f6d1d9a2a998c15b11ad838abae3d436b56a2c514afdabe6d7b20ac665759dc75fac3349fbb53e953899258c5a8ad844e53adcbde1c366934f8cd80e 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { ref, watchEffect, watch, unref, computed, onMounted, nextTick } from 'vue';
  2. import { useEventListener, useResizeObserver } from '@vueuse/core';
  3. import { useFormSize } from '../../../form/src/hooks/use-form-common-props.mjs';
  4. function useStyle(props, layout, store, table) {
  5. const isHidden = ref(false);
  6. const renderExpanded = ref(null);
  7. const resizeProxyVisible = ref(false);
  8. const setDragVisible = (visible) => {
  9. resizeProxyVisible.value = visible;
  10. };
  11. const resizeState = ref({
  12. width: null,
  13. height: null,
  14. headerHeight: null
  15. });
  16. const isGroup = ref(false);
  17. const scrollbarViewStyle = {
  18. display: "inline-block",
  19. verticalAlign: "middle"
  20. };
  21. const tableWidth = ref();
  22. const tableScrollHeight = ref(0);
  23. const bodyScrollHeight = ref(0);
  24. const headerScrollHeight = ref(0);
  25. const footerScrollHeight = ref(0);
  26. const appendScrollHeight = ref(0);
  27. watchEffect(() => {
  28. var _a;
  29. layout.setHeight((_a = props.height) != null ? _a : null);
  30. });
  31. watchEffect(() => {
  32. var _a;
  33. layout.setMaxHeight((_a = props.maxHeight) != null ? _a : null);
  34. });
  35. watch(() => [props.currentRowKey, store.states.rowKey], ([currentRowKey, rowKey]) => {
  36. if (!unref(rowKey) || !unref(currentRowKey))
  37. return;
  38. store.setCurrentRowKey(`${currentRowKey}`);
  39. }, {
  40. immediate: true
  41. });
  42. watch(() => props.data, (data) => {
  43. table.store.commit("setData", data);
  44. }, {
  45. immediate: true,
  46. deep: true
  47. });
  48. watchEffect(() => {
  49. if (props.expandRowKeys) {
  50. store.setExpandRowKeysAdapter(props.expandRowKeys);
  51. }
  52. });
  53. const handleMouseLeave = () => {
  54. table.store.commit("setHoverRow", null);
  55. if (table.hoverState)
  56. table.hoverState = null;
  57. };
  58. const handleHeaderFooterMousewheel = (_event, data) => {
  59. const { pixelX, pixelY } = data;
  60. if (Math.abs(pixelX) >= Math.abs(pixelY)) {
  61. table.refs.bodyWrapper.scrollLeft += data.pixelX / 5;
  62. }
  63. };
  64. const shouldUpdateHeight = computed(() => {
  65. return props.height || props.maxHeight || store.states.fixedColumns.value.length > 0 || store.states.rightFixedColumns.value.length > 0;
  66. });
  67. const tableBodyStyles = computed(() => {
  68. return {
  69. width: layout.bodyWidth.value ? `${layout.bodyWidth.value}px` : ""
  70. };
  71. });
  72. const doLayout = () => {
  73. if (shouldUpdateHeight.value) {
  74. layout.updateElsHeight();
  75. }
  76. layout.updateColumnsWidth();
  77. if (typeof window === "undefined")
  78. return;
  79. requestAnimationFrame(syncPosition);
  80. };
  81. onMounted(async () => {
  82. await nextTick();
  83. store.updateColumns();
  84. bindEvents();
  85. requestAnimationFrame(doLayout);
  86. const el = table.vnode.el;
  87. const tableHeader = table.refs.headerWrapper;
  88. if (props.flexible && el && el.parentElement) {
  89. el.parentElement.style.minWidth = "0";
  90. }
  91. resizeState.value = {
  92. width: tableWidth.value = el.offsetWidth,
  93. height: el.offsetHeight,
  94. headerHeight: props.showHeader && tableHeader ? tableHeader.offsetHeight : null
  95. };
  96. store.states.columns.value.forEach((column) => {
  97. if (column.filteredValue && column.filteredValue.length) {
  98. table.store.commit("filterChange", {
  99. column,
  100. values: column.filteredValue,
  101. silent: true
  102. });
  103. }
  104. });
  105. table.$ready = true;
  106. });
  107. const setScrollClassByEl = (el, className) => {
  108. if (!el)
  109. return;
  110. const classList = Array.from(el.classList).filter((item) => !item.startsWith("is-scrolling-"));
  111. classList.push(layout.scrollX.value ? className : "is-scrolling-none");
  112. el.className = classList.join(" ");
  113. };
  114. const setScrollClass = (className) => {
  115. const { tableWrapper } = table.refs;
  116. setScrollClassByEl(tableWrapper, className);
  117. };
  118. const hasScrollClass = (className) => {
  119. const { tableWrapper } = table.refs;
  120. return !!(tableWrapper && tableWrapper.classList.contains(className));
  121. };
  122. const syncPosition = function() {
  123. if (!table.refs.scrollBarRef)
  124. return;
  125. if (!layout.scrollX.value) {
  126. const scrollingNoneClass = "is-scrolling-none";
  127. if (!hasScrollClass(scrollingNoneClass)) {
  128. setScrollClass(scrollingNoneClass);
  129. }
  130. return;
  131. }
  132. const scrollContainer = table.refs.scrollBarRef.wrapRef;
  133. if (!scrollContainer)
  134. return;
  135. const { scrollLeft, offsetWidth, scrollWidth } = scrollContainer;
  136. const { headerWrapper, footerWrapper } = table.refs;
  137. if (headerWrapper)
  138. headerWrapper.scrollLeft = scrollLeft;
  139. if (footerWrapper)
  140. footerWrapper.scrollLeft = scrollLeft;
  141. const maxScrollLeftPosition = scrollWidth - offsetWidth - 1;
  142. if (scrollLeft >= maxScrollLeftPosition) {
  143. setScrollClass("is-scrolling-right");
  144. } else if (scrollLeft === 0) {
  145. setScrollClass("is-scrolling-left");
  146. } else {
  147. setScrollClass("is-scrolling-middle");
  148. }
  149. };
  150. const bindEvents = () => {
  151. if (!table.refs.scrollBarRef)
  152. return;
  153. if (table.refs.scrollBarRef.wrapRef) {
  154. useEventListener(table.refs.scrollBarRef.wrapRef, "scroll", syncPosition, {
  155. passive: true
  156. });
  157. }
  158. if (props.fit) {
  159. useResizeObserver(table.vnode.el, resizeListener);
  160. } else {
  161. useEventListener(window, "resize", resizeListener);
  162. }
  163. useResizeObserver(table.refs.bodyWrapper, () => {
  164. var _a, _b;
  165. resizeListener();
  166. (_b = (_a = table.refs) == null ? void 0 : _a.scrollBarRef) == null ? void 0 : _b.update();
  167. });
  168. };
  169. const resizeListener = () => {
  170. var _a, _b, _c, _d;
  171. const el = table.vnode.el;
  172. if (!table.$ready || !el)
  173. return;
  174. let shouldUpdateLayout = false;
  175. const {
  176. width: oldWidth,
  177. height: oldHeight,
  178. headerHeight: oldHeaderHeight
  179. } = resizeState.value;
  180. const width = tableWidth.value = el.offsetWidth;
  181. if (oldWidth !== width) {
  182. shouldUpdateLayout = true;
  183. }
  184. const height = el.offsetHeight;
  185. if ((props.height || shouldUpdateHeight.value) && oldHeight !== height) {
  186. shouldUpdateLayout = true;
  187. }
  188. const tableHeader = props.tableLayout === "fixed" ? table.refs.headerWrapper : (_a = table.refs.tableHeaderRef) == null ? void 0 : _a.$el;
  189. if (props.showHeader && (tableHeader == null ? void 0 : tableHeader.offsetHeight) !== oldHeaderHeight) {
  190. shouldUpdateLayout = true;
  191. }
  192. tableScrollHeight.value = ((_b = table.refs.tableWrapper) == null ? void 0 : _b.scrollHeight) || 0;
  193. headerScrollHeight.value = (tableHeader == null ? void 0 : tableHeader.scrollHeight) || 0;
  194. footerScrollHeight.value = ((_c = table.refs.footerWrapper) == null ? void 0 : _c.offsetHeight) || 0;
  195. appendScrollHeight.value = ((_d = table.refs.appendWrapper) == null ? void 0 : _d.offsetHeight) || 0;
  196. bodyScrollHeight.value = tableScrollHeight.value - headerScrollHeight.value - footerScrollHeight.value - appendScrollHeight.value;
  197. if (shouldUpdateLayout) {
  198. resizeState.value = {
  199. width,
  200. height,
  201. headerHeight: props.showHeader && (tableHeader == null ? void 0 : tableHeader.offsetHeight) || 0
  202. };
  203. doLayout();
  204. }
  205. };
  206. const tableSize = useFormSize();
  207. const bodyWidth = computed(() => {
  208. const { bodyWidth: bodyWidth_, scrollY, gutterWidth } = layout;
  209. return bodyWidth_.value ? `${bodyWidth_.value - (scrollY.value ? gutterWidth : 0)}px` : "";
  210. });
  211. const tableLayout = computed(() => {
  212. if (props.maxHeight)
  213. return "fixed";
  214. return props.tableLayout;
  215. });
  216. const emptyBlockStyle = computed(() => {
  217. if (props.data && props.data.length)
  218. return;
  219. let height = "100%";
  220. if (props.height && bodyScrollHeight.value) {
  221. height = `${bodyScrollHeight.value}px`;
  222. }
  223. const width = tableWidth.value;
  224. return {
  225. width: width ? `${width}px` : "",
  226. height
  227. };
  228. });
  229. const scrollbarStyle = computed(() => {
  230. if (props.height) {
  231. return {
  232. height: "100%"
  233. };
  234. }
  235. if (props.maxHeight) {
  236. if (!Number.isNaN(Number(props.maxHeight))) {
  237. return {
  238. maxHeight: `${+props.maxHeight - headerScrollHeight.value - footerScrollHeight.value}px`
  239. };
  240. } else {
  241. return {
  242. maxHeight: `calc(${props.maxHeight} - ${headerScrollHeight.value + footerScrollHeight.value}px)`
  243. };
  244. }
  245. }
  246. return {};
  247. });
  248. return {
  249. isHidden,
  250. renderExpanded,
  251. setDragVisible,
  252. isGroup,
  253. handleMouseLeave,
  254. handleHeaderFooterMousewheel,
  255. tableSize,
  256. emptyBlockStyle,
  257. resizeProxyVisible,
  258. bodyWidth,
  259. resizeState,
  260. doLayout,
  261. tableBodyStyles,
  262. tableLayout,
  263. scrollbarViewStyle,
  264. scrollbarStyle
  265. };
  266. }
  267. export { useStyle as default };
  268. //# sourceMappingURL=style-helper.mjs.map