index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. import { SwipeActionDefaultProps } from './props';
  13. import fmtEvent from '../_util/fmtEvent';
  14. import { compareVersion } from '../_util/compareVersion';
  15. import '../_util/assert-component2';
  16. import { platform } from '../_util/platform';
  17. import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
  18. import { transformOptions } from './wechat';
  19. var setStyleObj1 = function (buttons, inertiaWidth) {
  20. var _a, _b, _c;
  21. var widthPos = buttons.length === 2 ? 0.5 : 0.3333;
  22. return [
  23. { marginLeft: 0 },
  24. {
  25. marginLeft: "".concat(-(((_a = buttons[0]) === null || _a === void 0 ? void 0 : _a.width) + 1 + (inertiaWidth || 0) * widthPos) / 2, "px"),
  26. },
  27. {
  28. marginLeft: "".concat(-(((_b = buttons[0]) === null || _b === void 0 ? void 0 : _b.width) +
  29. ((_c = buttons[1]) === null || _c === void 0 ? void 0 : _c.width) +
  30. 1 +
  31. (inertiaWidth || 0) * 0.6666) / 2, "px"),
  32. },
  33. ];
  34. };
  35. var setStyleObj2 = function (buttons, inertiaWidth) {
  36. var _a, _b, _c, _d;
  37. var length = buttons.length;
  38. if (length === 2) {
  39. return [
  40. { marginRight: 0 },
  41. {
  42. marginRight: "-".concat((((_a = buttons[0]) === null || _a === void 0 ? void 0 : _a.width) + (inertiaWidth || 0) * 0.5) / 2, "px"),
  43. },
  44. ];
  45. }
  46. if (length === 3) {
  47. return [
  48. { marginRight: 0 },
  49. {
  50. marginRight: "-".concat((((_b = buttons[0]) === null || _b === void 0 ? void 0 : _b.width) + (inertiaWidth || 0) * 0.3333) / 2, "px"),
  51. },
  52. {
  53. marginRight: "-".concat((((_c = buttons[0]) === null || _c === void 0 ? void 0 : _c.width) +
  54. ((_d = buttons[1]) === null || _d === void 0 ? void 0 : _d.width) +
  55. (inertiaWidth || 0) * 0.6666) /
  56. 2, "px"),
  57. },
  58. ];
  59. }
  60. return [{ marginRight: 0 }];
  61. };
  62. var myTimeOut = null;
  63. var getDirectionLeft = function (arr) {
  64. if (arr.length < 2) {
  65. arr = [0, arr[0]];
  66. }
  67. return arr[0] + arr[1] <= 0;
  68. };
  69. var isOldVersion = getIsOldVersion();
  70. function getIsOldVersion() {
  71. if (typeof my === 'undefined') {
  72. return false;
  73. }
  74. var SDKVersion = my.SDKVersion;
  75. return compareVersion(SDKVersion, '2.0.0') < 0;
  76. }
  77. function getInitReady() {
  78. if (platform() === 'wechat') {
  79. return false;
  80. }
  81. if (isOldVersion) {
  82. return false;
  83. }
  84. return true;
  85. }
  86. Component(transformOptions({
  87. props: SwipeActionDefaultProps,
  88. didMount: function () {
  89. var _this = this;
  90. var _a = this.getProps(), defaultSwiped = _a.defaultSwiped, elasticity = _a.elasticity;
  91. this.setButtonItemWidth();
  92. this.setData({ inertiaWidth: !isOldVersion && elasticity ? 20 : 0 });
  93. if (defaultSwiped) {
  94. this.initWidth(function (maxSwipe) {
  95. maxSwipe &&
  96. _this.setData({
  97. swipeX: (maxSwipe + 0.01) * (defaultSwiped === 'right' ? -1 : 1),
  98. swipedR: defaultSwiped === 'right',
  99. swipedL: defaultSwiped === 'left',
  100. });
  101. });
  102. }
  103. },
  104. didUpdate: function (prevProp) {
  105. var _a = this.getProps(), swiped = _a.swiped, damping = _a.damping, elasticity = _a.elasticity;
  106. // 设置不同的滑动位置时需要重置
  107. var rs = prevProp.swiped !== swiped && !swiped;
  108. var is = prevProp.elasticity !== elasticity;
  109. var ds = prevProp.damping !== damping;
  110. if (rs || is || ds) {
  111. this.setData({
  112. swipeX: 0,
  113. swipedR: false,
  114. swipedL: false,
  115. tapTypeL: '',
  116. tapTypeR: '',
  117. });
  118. }
  119. if (is) {
  120. this.setData({ inertiaWidth: elasticity ? 20 : 0 });
  121. }
  122. },
  123. data: {
  124. // 在微信小程序与支付宝小程序基础库 1.0
  125. // 组件初始化时的 margin-left 决定了后续可移动的距离,所以需要等组件初始化以后再加载
  126. ready: getInitReady(),
  127. swipeLeft: true,
  128. swipeX: 0,
  129. moveX: 0,
  130. tapTypeL: '',
  131. tapTypeR: '',
  132. leftWidth: 20,
  133. rightWidth: 20,
  134. maxSwipeL: 0,
  135. maxSwipeR: 0,
  136. inTouch: false,
  137. swipedR: false,
  138. swipedL: false,
  139. changeArr: [0, 0],
  140. myStyle: {},
  141. inertiaWidth: 20,
  142. animation: !isOldVersion,
  143. _leftButtons: [],
  144. _rightButtons: [],
  145. },
  146. methods: {
  147. setWidth: function () {
  148. var _a = this.data, _leftButtons = _a._leftButtons, _rightButtons = _a._rightButtons;
  149. var _rightArr = _rightButtons || [];
  150. var _leftArr = _leftButtons || [];
  151. this.setData({
  152. ready: true,
  153. rightWidth: _rightArr.reduce(function (tolal, cur) {
  154. return tolal + cur.width;
  155. }, 0),
  156. leftWidth: _leftArr.length
  157. ? _leftArr.reduce(function (tolal, cur) {
  158. return tolal + cur.width;
  159. }, 0)
  160. : 0,
  161. });
  162. },
  163. setButtonItemWidth: function () {
  164. var _this = this;
  165. var _a = this.getProps(), leftButtons = _a.leftButtons, rightButtons = _a.rightButtons;
  166. leftButtons.forEach(function (i) {
  167. i.width = i.width || 150;
  168. });
  169. rightButtons.forEach(function (i) {
  170. i.width = i.width || 150;
  171. });
  172. this.setData({ _leftButtons: leftButtons, _rightButtons: rightButtons }, function () {
  173. _this.setWidth();
  174. });
  175. },
  176. getProps: function () {
  177. var _this = this;
  178. if (platform() === 'wechat') {
  179. return __assign(__assign({}, this.data), { onSwipeStart: function (v) {
  180. _this.triggerEvent('swipestart', v);
  181. }, onSwipeEnd: function (v) {
  182. _this.triggerEvent('swipeend', v);
  183. }, onButtonTap: function (v) {
  184. _this.triggerEvent('buttontap', v);
  185. } });
  186. }
  187. return this.props;
  188. },
  189. initWidth: function (func) {
  190. var _this = this;
  191. var _a = this.getProps(), leftButtons = _a.leftButtons, rightButtons = _a.rightButtons;
  192. // 获取宽度信息,设置滑轨的宽度、初始化滑动位置
  193. // 如果没有获取到该信息则把左滑禁用掉
  194. rightButtons.length > 0 &&
  195. this.boundingClientRect(".ant-swipe-action-movable-right".concat(this.$id ? '-' + this.$id : '')).then(function (ret) {
  196. if (ret && ret.width) {
  197. _this.setData({ maxSwipeR: ret.width });
  198. func && func(ret.width);
  199. }
  200. });
  201. leftButtons.length > 0 &&
  202. this.boundingClientRect(".ant-swipe-action-movable-left".concat(this.$id ? '-' + this.$id : '')).then(function (ret) {
  203. if (ret && ret.width) {
  204. _this.setData({ maxSwipeL: ret.width });
  205. func && func(ret.width);
  206. }
  207. });
  208. },
  209. boundingClientRect: function (id) {
  210. if (typeof my === 'undefined') {
  211. return getInstanceBoundingClientRect(this, id);
  212. }
  213. return getInstanceBoundingClientRect(my, id);
  214. },
  215. // 向外透出事件
  216. onTouchStart: function () {
  217. var onSwipeStart = this.getProps().onSwipeStart;
  218. var _a = this.data, swipedR = _a.swipedR, swipedL = _a.swipedL;
  219. this.initWidth();
  220. this.setData({ tapTypeL: '', tapTypeR: '', inTouch: true }); // 清空confirmType = auto 的表现
  221. onSwipeStart({
  222. swiped: !!(swipedR || swipedL),
  223. direction: swipedL ? 'left' : swipedR ? 'right' : '',
  224. }, fmtEvent(this.getProps()));
  225. },
  226. onTouchEnd: function (e) {
  227. // 因为微信小程序没有 onChangeEnd 事件
  228. // 所以用 onTouchEnd 模拟 onChangeEnd
  229. if (platform() === 'wechat') {
  230. this.onChangeEnd(e);
  231. }
  232. this.setData({ inTouch: false });
  233. },
  234. // 滑动过程中的事件,是内部事件不向外透出,用于控制右侧按钮的位置信息
  235. onChange: function (e) {
  236. var _this = this;
  237. var _a = this.data, changeArr = _a.changeArr, maxSwipeR = _a.maxSwipeR, maxSwipeL = _a.maxSwipeL, inTouch = _a.inTouch, swipedR = _a.swipedR, swipedL = _a.swipedL, inertiaWidth = _a.inertiaWidth;
  238. var x = e.detail.x;
  239. var L = x;
  240. // changeArr用于精准的控制滑动的方向
  241. var newArr = changeArr[1] === L ? [changeArr] : [changeArr[1], L];
  242. this.setData({ moveX: L, changeArr: newArr });
  243. var ridx = this.getProps().rightButtons.findIndex(function (u) { return u.confirmType === 'move'; });
  244. var lidx = this.getProps().leftButtons.findIndex(function (u) { return u.confirmType === 'move'; });
  245. if (ridx === -1 && lidx === -1)
  246. return;
  247. var isRight = getDirectionLeft(changeArr);
  248. // 左滑时的滑动确认、收起处理
  249. if (isRight) {
  250. if (L < 0 && Math.abs(L) >= maxSwipeR && inTouch && !swipedR) {
  251. clearTimeout(myTimeOut);
  252. myTimeOut = setTimeout(function () {
  253. var _a = _this.data, changeArr = _a.changeArr, maxSwipeR = _a.maxSwipeR, inTouch = _a.inTouch, swipedR = _a.swipedR, moveX = _a.moveX;
  254. var _left = getDirectionLeft(changeArr) && changeArr[0] >= changeArr[1];
  255. if (inTouch &&
  256. maxSwipeR + inertiaWidth + 2 >= Math.abs(moveX) &&
  257. _left &&
  258. !swipedR) {
  259. _this.onSetCheck(true);
  260. }
  261. }, 100);
  262. }
  263. if (changeArr[0] < changeArr[1] && maxSwipeR > L + 4 && inTouch) {
  264. this.setData({ tapTypeR: '', myStyle: {} });
  265. }
  266. }
  267. else {
  268. if (L > 0 && L + 1 >= maxSwipeL && inTouch && !swipedL) {
  269. clearTimeout(myTimeOut);
  270. myTimeOut = setTimeout(function () {
  271. var _a = _this.data, changeArr = _a.changeArr, maxSwipeL = _a.maxSwipeL, inTouch = _a.inTouch, swipedL = _a.swipedL, moveX = _a.moveX;
  272. var _right = !getDirectionLeft(changeArr) && changeArr[1] >= changeArr[0];
  273. if (inTouch && maxSwipeL <= moveX + 1 && _right && !swipedL) {
  274. _this.onSetCheck(false);
  275. }
  276. }, 100);
  277. }
  278. if (changeArr[0] > changeArr[1] && maxSwipeL > L + 2 && inTouch) {
  279. this.setData({ tapTypeL: '', myStyle: {} });
  280. }
  281. }
  282. },
  283. onSetCheck: function (isRight) {
  284. var _a = this.getProps(), rightButtons = _a.rightButtons, leftButtons = _a.leftButtons;
  285. var _b = this.data, inertiaWidth = _b.inertiaWidth, _leftButtons = _b._leftButtons, _rightButtons = _b._rightButtons;
  286. var arr = isRight ? rightButtons : leftButtons;
  287. var idx = arr.findIndex(function (u) { return u.confirmType === 'move'; });
  288. if (idx === -1)
  289. return;
  290. typeof my !== 'undefined' && my.vibrateShort({ success: function () { } });
  291. var styArr = isRight
  292. ? setStyleObj1(_rightButtons, inertiaWidth)
  293. : setStyleObj2(_leftButtons, inertiaWidth);
  294. var sty = styArr[idx];
  295. this.setData({ myStyle: sty });
  296. isRight
  297. ? this.setData({ tapTypeR: 'R-' + idx })
  298. : this.setData({ tapTypeL: 'L-' + idx });
  299. },
  300. // 意外中断了滑动,则立即开始结算滑动动作
  301. onTouchCancel: function (e) {
  302. this.onChangeEnd(e);
  303. },
  304. onChangeEnd: function (e) {
  305. var x = e.detail.x;
  306. var changeArr = this.data.changeArr;
  307. // 如果是停留在初始态返回
  308. if (x === 0)
  309. return;
  310. // 判断是否是左滑
  311. var isRight = getDirectionLeft(changeArr);
  312. this.setData({ swipeLeft: isRight });
  313. isRight ? this.onSetSwipeRight(x) : this.onSetSwipeLeft(x);
  314. },
  315. onSetSwipeRight: function (x, needBack) {
  316. var _a = this.data, swipedR = _a.swipedR, inTouch = _a.inTouch, tapTypeR = _a.tapTypeR, changeArr = _a.changeArr;
  317. var onButtonTap = this.getProps().onButtonTap;
  318. var isRight = Math.abs(changeArr[1]) > Math.abs(changeArr[0]);
  319. if (!swipedR && Math.abs(x) < 10) {
  320. isRight = false;
  321. }
  322. needBack && (isRight = false);
  323. !isRight && this.setData({ tapTypeR: '', myStyle: {} });
  324. if (inTouch && !!tapTypeR) {
  325. this.setData({ tapTypeR: '', myStyle: {} });
  326. onButtonTap({
  327. direction: 'right',
  328. btnIdx: parseInt(tapTypeR.replace('R-', '')),
  329. }, fmtEvent(this.getProps()));
  330. this.onSwipeRight(false);
  331. return;
  332. }
  333. this.setData({ changeArr: [0, 0] });
  334. this.onSwipeRight(isRight);
  335. },
  336. onSetSwipeLeft: function (x, needBack) {
  337. var _a = this.data, swipedL = _a.swipedL, inTouch = _a.inTouch, tapTypeL = _a.tapTypeL, changeArr = _a.changeArr;
  338. var onButtonTap = this.getProps().onButtonTap;
  339. // true:初始是往右滑,false: 右滑收起
  340. var isRight = changeArr[1] >= changeArr[0];
  341. if (!swipedL && Math.abs(x) < 10) {
  342. isRight = false;
  343. }
  344. needBack && (isRight = false);
  345. // 清空二次确认的状态
  346. !isRight && this.setData({ tapTypeL: '', myStyle: {} });
  347. // 处理滑动-触发事件
  348. if (inTouch && !!tapTypeL) {
  349. this.setData({ tapTypeL: '', myStyle: {} });
  350. onButtonTap({ direction: 'left', btnIdx: parseInt(tapTypeL.replace('L-', '')) }, fmtEvent(this.getProps()));
  351. this.onSwipeLeft(false);
  352. return;
  353. }
  354. // 清空滑动方向
  355. this.setData({ changeArr: [0, 0] });
  356. this.onSwipeLeft(isRight);
  357. },
  358. // 结算
  359. onSwipeLeft: function (isRight) {
  360. var _this = this;
  361. var onSwipeEnd = this.getProps().onSwipeEnd;
  362. var _a = this.data, maxSwipeL = _a.maxSwipeL, inertiaWidth = _a.inertiaWidth;
  363. // 为了处理到重置状态的效果
  364. var maxX = maxSwipeL + 0.01 + inertiaWidth;
  365. this.setData({
  366. swipeX: isRight ? maxX : -0.01,
  367. }, function () {
  368. var flag = _this.data.swipeX === -0.01;
  369. setTimeout(function () {
  370. _this.setData({
  371. swipeX: flag ? 0 : maxSwipeL + 0.01,
  372. swipedL: !flag,
  373. }, function () {
  374. onSwipeEnd({ direction: 'left', swiped: !!isRight }, fmtEvent(_this.getProps()));
  375. });
  376. }, inertiaWidth > 0 ? 180 : 0);
  377. });
  378. },
  379. onSwipeRight: function (isRight) {
  380. var _this = this;
  381. var _a = this.data, maxSwipeR = _a.maxSwipeR, inertiaWidth = _a.inertiaWidth;
  382. var onSwipeEnd = this.getProps().onSwipeEnd;
  383. // 处理x的位置,两次setData X是因为x如果相同位置不会变,刚好也把弹性在这里处理了
  384. // 为了处理到重置状态的效果
  385. var maxX = -(maxSwipeR + 0.01 + inertiaWidth);
  386. this.setData({
  387. swipeX: isRight ? maxX : -0.01,
  388. }, function () {
  389. var flag = _this.data.swipeX === -0.01;
  390. setTimeout(function () {
  391. _this.setData({
  392. swipeX: flag ? 0 : -(maxSwipeR + 0.01),
  393. swipedR: !flag,
  394. }, function () {
  395. onSwipeEnd({ direction: 'right', swiped: !!isRight }, fmtEvent(_this.getProps()));
  396. });
  397. }, inertiaWidth > 0 ? 180 : 0);
  398. });
  399. },
  400. // 处理右侧点击事件
  401. onItemTap: function (e) {
  402. var _a = this.data, swipeLeft = _a.swipeLeft, tapTypeL = _a.tapTypeL, tapTypeR = _a.tapTypeR, _leftButtons = _a._leftButtons, _rightButtons = _a._rightButtons;
  403. var onButtonTap = this.getProps().onButtonTap;
  404. if (!onButtonTap)
  405. return;
  406. var _b = e.currentTarget.dataset.item, itemL = _b.itemL, idx = _b.idx, item = _b.item;
  407. var confirmType = (swipeLeft ? item : itemL).confirmType;
  408. if (tapTypeL === 'L-' + idx) {
  409. this.onSetSwipeLeft(0, true);
  410. this.setData({ tapTypeL: '', myStyle: {} });
  411. onButtonTap({ direction: 'left', btnIdx: idx }, fmtEvent(this.getProps()));
  412. return;
  413. }
  414. if (tapTypeR === 'R-' + idx) {
  415. this.onSetSwipeRight(0, true);
  416. this.setData({ tapTypeR: '', myStyle: {} });
  417. onButtonTap({ direction: 'right', btnIdx: idx }, fmtEvent(this.getProps()));
  418. return;
  419. }
  420. // auto 是展开按钮二次确认的效果
  421. if (confirmType === 'tap' || confirmType === 'move') {
  422. var styArr = swipeLeft
  423. ? setStyleObj1(_rightButtons)
  424. : setStyleObj2(_leftButtons);
  425. var sty = styArr[idx];
  426. this.setData({ myStyle: sty });
  427. !swipeLeft
  428. ? this.setData({ tapTypeL: 'L-' + idx })
  429. : this.setData({ tapTypeR: 'R-' + idx });
  430. }
  431. else {
  432. !swipeLeft
  433. ? this.onSetSwipeLeft(0, true)
  434. : this.onSetSwipeRight(0, true);
  435. onButtonTap({ direction: !swipeLeft ? 'left' : 'right', btnIdx: idx }, fmtEvent(this.getProps()));
  436. }
  437. },
  438. },
  439. }));