index-73d5a905.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { c as createNamespace, m as makeNumericProp, n as numericProp, k as isDef, C as useClickAway, D as useEventListener, E as useRect, p as preventDefault, s as clamp, w as withInstall } from "./index-487cde8c.js";
  2. import { u as useTouch } from "./index-eef3af38.js";
  3. import { u as useExpose } from "./use-scope-id-0b5b8615.js";
  4. import { d as defineComponent, r as ref, n as reactive, a as computed, c as createVNode } from "./index-5e4623ce.js";
  5. import { c as callInterceptor } from "./on-popup-reopen-c5ca1603.js";
  6. const [name, bem] = createNamespace("swipe-cell");
  7. const swipeCellProps = {
  8. name: makeNumericProp(""),
  9. disabled: Boolean,
  10. leftWidth: numericProp,
  11. rightWidth: numericProp,
  12. beforeClose: Function,
  13. stopPropagation: Boolean
  14. };
  15. var stdin_default = defineComponent({
  16. name,
  17. props: swipeCellProps,
  18. emits: ["open", "close", "click"],
  19. setup(props, {
  20. emit,
  21. slots
  22. }) {
  23. let opened;
  24. let lockClick;
  25. let startOffset;
  26. let isInBeforeClosing;
  27. const root = ref();
  28. const leftRef = ref();
  29. const rightRef = ref();
  30. const state = reactive({
  31. offset: 0,
  32. dragging: false
  33. });
  34. const touch = useTouch();
  35. const getWidthByRef = (ref2) => ref2.value ? useRect(ref2).width : 0;
  36. const leftWidth = computed(() => isDef(props.leftWidth) ? +props.leftWidth : getWidthByRef(leftRef));
  37. const rightWidth = computed(() => isDef(props.rightWidth) ? +props.rightWidth : getWidthByRef(rightRef));
  38. const open = (side) => {
  39. state.offset = side === "left" ? leftWidth.value : -rightWidth.value;
  40. if (!opened) {
  41. opened = true;
  42. emit("open", {
  43. name: props.name,
  44. position: side
  45. });
  46. }
  47. };
  48. const close = (position) => {
  49. state.offset = 0;
  50. if (opened) {
  51. opened = false;
  52. emit("close", {
  53. name: props.name,
  54. position
  55. });
  56. }
  57. };
  58. const toggle = (side) => {
  59. const offset = Math.abs(state.offset);
  60. const THRESHOLD = 0.15;
  61. const threshold = opened ? 1 - THRESHOLD : THRESHOLD;
  62. const width = side === "left" ? leftWidth.value : rightWidth.value;
  63. if (width && offset > width * threshold) {
  64. open(side);
  65. } else {
  66. close(side);
  67. }
  68. };
  69. const onTouchStart = (event) => {
  70. if (!props.disabled) {
  71. startOffset = state.offset;
  72. touch.start(event);
  73. }
  74. };
  75. const onTouchMove = (event) => {
  76. if (props.disabled) {
  77. return;
  78. }
  79. const {
  80. deltaX
  81. } = touch;
  82. touch.move(event);
  83. if (touch.isHorizontal()) {
  84. lockClick = true;
  85. state.dragging = true;
  86. const isEdge = !opened || deltaX.value * startOffset < 0;
  87. if (isEdge) {
  88. preventDefault(event, props.stopPropagation);
  89. }
  90. state.offset = clamp(deltaX.value + startOffset, -rightWidth.value, leftWidth.value);
  91. }
  92. };
  93. const onTouchEnd = () => {
  94. if (state.dragging) {
  95. state.dragging = false;
  96. toggle(state.offset > 0 ? "left" : "right");
  97. setTimeout(() => {
  98. lockClick = false;
  99. }, 0);
  100. }
  101. };
  102. const onClick = (position = "outside", event) => {
  103. if (isInBeforeClosing)
  104. return;
  105. emit("click", position);
  106. if (opened && !lockClick) {
  107. isInBeforeClosing = true;
  108. callInterceptor(props.beforeClose, {
  109. args: [{
  110. event,
  111. name: props.name,
  112. position
  113. }],
  114. done: () => {
  115. isInBeforeClosing = false;
  116. close(position);
  117. },
  118. canceled: () => isInBeforeClosing = false,
  119. error: () => isInBeforeClosing = false
  120. });
  121. }
  122. };
  123. const getClickHandler = (position, stop) => (event) => {
  124. if (stop) {
  125. event.stopPropagation();
  126. }
  127. if (lockClick) {
  128. return;
  129. }
  130. onClick(position, event);
  131. };
  132. const renderSideContent = (side, ref2) => {
  133. const contentSlot = slots[side];
  134. if (contentSlot) {
  135. return createVNode("div", {
  136. "ref": ref2,
  137. "class": bem(side),
  138. "onClick": getClickHandler(side, true)
  139. }, [contentSlot()]);
  140. }
  141. };
  142. useExpose({
  143. open,
  144. close
  145. });
  146. useClickAway(root, (event) => onClick("outside", event), {
  147. eventName: "touchstart"
  148. });
  149. useEventListener("touchmove", onTouchMove, {
  150. target: root
  151. });
  152. return () => {
  153. var _a;
  154. const wrapperStyle = {
  155. transform: `translate3d(${state.offset}px, 0, 0)`,
  156. transitionDuration: state.dragging ? "0s" : ".6s"
  157. };
  158. return createVNode("div", {
  159. "ref": root,
  160. "class": bem(),
  161. "onClick": getClickHandler("cell", lockClick),
  162. "onTouchstartPassive": onTouchStart,
  163. "onTouchend": onTouchEnd,
  164. "onTouchcancel": onTouchEnd
  165. }, [createVNode("div", {
  166. "class": bem("wrapper"),
  167. "style": wrapperStyle
  168. }, [renderSideContent("left", leftRef), (_a = slots.default) == null ? void 0 : _a.call(slots), renderSideContent("right", rightRef)])]);
  169. };
  170. }
  171. });
  172. const SwipeCell = withInstall(stdin_default);
  173. const index = "";
  174. export {
  175. SwipeCell as S
  176. };