swipe-cell.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { getRect } from '../common/utils';
  11. import { getObserver } from '../common/wechat';
  12. let ARRAY = [];
  13. const { prefix } = config;
  14. const name = `${prefix}-swipe-cell`;
  15. const ContainerClass = `.${name}`;
  16. let SwiperCell = class SwiperCell extends SuperComponent {
  17. constructor() {
  18. super(...arguments);
  19. this.externalClasses = [`${prefix}-class`];
  20. this.options = {
  21. multipleSlots: true,
  22. };
  23. this.properties = props;
  24. this.data = {
  25. prefix,
  26. wrapperStyle: '',
  27. closed: true,
  28. classPrefix: name,
  29. skipMove: false,
  30. };
  31. this.observers = {
  32. 'left, right'() {
  33. this.setSwipeWidth();
  34. },
  35. };
  36. this.lifetimes = {
  37. attached() {
  38. ARRAY.push(this);
  39. },
  40. ready() {
  41. this.setSwipeWidth();
  42. },
  43. detached() {
  44. ARRAY = ARRAY.filter((item) => item !== this);
  45. },
  46. };
  47. }
  48. setSwipeWidth() {
  49. Promise.all([getRect(this, `${ContainerClass}__left`), getRect(this, `${ContainerClass}__right`)]).then(([leftRect, rightRect]) => {
  50. if (leftRect.width === 0 && rightRect.width === 0 && !this._hasObserved) {
  51. this._hasObserved = true;
  52. getObserver(this, `.${name}`).then(() => {
  53. this.setSwipeWidth();
  54. });
  55. }
  56. this.setData({
  57. leftWidth: leftRect.width,
  58. rightWidth: rightRect.width,
  59. });
  60. });
  61. }
  62. skipMove() {
  63. if (!this.data.skipMove) {
  64. this.setData({ skipMove: true });
  65. }
  66. }
  67. catchMove() {
  68. if (this.data.skipMove) {
  69. this.setData({ skipMove: false });
  70. }
  71. }
  72. open() {
  73. this.setData({ opened: true });
  74. }
  75. close() {
  76. this.setData({ opened: false });
  77. }
  78. closeOther() {
  79. ARRAY.filter((item) => item !== this).forEach((item) => item.close());
  80. }
  81. onTap() {
  82. this.close();
  83. }
  84. onActionTap(event) {
  85. const { currentTarget: { dataset: { action }, }, } = event;
  86. this.triggerEvent('click', action);
  87. }
  88. };
  89. SwiperCell = __decorate([
  90. wxComponent()
  91. ], SwiperCell);
  92. export default SwiperCell;