useStickyOffsets.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { computed } from 'vue';
  2. /**
  3. * Get sticky column offset width
  4. */
  5. function useStickyOffsets(colWidthsRef, columnCountRef, directionRef) {
  6. const stickyOffsets = computed(() => {
  7. const leftOffsets = [];
  8. const rightOffsets = [];
  9. let left = 0;
  10. let right = 0;
  11. const colWidths = colWidthsRef.value;
  12. const columnCount = columnCountRef.value;
  13. const direction = directionRef.value;
  14. for (let start = 0; start < columnCount; start += 1) {
  15. if (direction === 'rtl') {
  16. // Left offset
  17. rightOffsets[start] = right;
  18. right += colWidths[start] || 0;
  19. // Right offset
  20. const end = columnCount - start - 1;
  21. leftOffsets[end] = left;
  22. left += colWidths[end] || 0;
  23. } else {
  24. // Left offset
  25. leftOffsets[start] = left;
  26. left += colWidths[start] || 0;
  27. // Right offset
  28. const end = columnCount - start - 1;
  29. rightOffsets[end] = right;
  30. right += colWidths[end] || 0;
  31. }
  32. }
  33. return {
  34. left: leftOffsets,
  35. right: rightOffsets
  36. };
  37. });
  38. return stickyOffsets;
  39. }
  40. export default useStickyOffsets;