index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. var touch_1 = require("../mixins/touch");
  5. var utils_1 = require("../common/utils");
  6. var THRESHOLD = 0.3;
  7. var ARRAY = [];
  8. component_1.VantComponent({
  9. props: {
  10. disabled: Boolean,
  11. leftWidth: {
  12. type: Number,
  13. value: 0,
  14. observer: function (leftWidth) {
  15. if (leftWidth === void 0) { leftWidth = 0; }
  16. if (this.offset > 0) {
  17. this.swipeMove(leftWidth);
  18. }
  19. }
  20. },
  21. rightWidth: {
  22. type: Number,
  23. value: 0,
  24. observer: function (rightWidth) {
  25. if (rightWidth === void 0) { rightWidth = 0; }
  26. if (this.offset < 0) {
  27. this.swipeMove(-rightWidth);
  28. }
  29. }
  30. },
  31. asyncClose: Boolean,
  32. name: {
  33. type: [Number, String],
  34. value: ''
  35. }
  36. },
  37. mixins: [touch_1.touch],
  38. data: {
  39. catchMove: false
  40. },
  41. created: function () {
  42. this.offset = 0;
  43. ARRAY.push(this);
  44. },
  45. destroyed: function () {
  46. var _this = this;
  47. ARRAY = ARRAY.filter(function (item) { return item !== _this; });
  48. },
  49. methods: {
  50. open: function (position) {
  51. var _a = this.data, leftWidth = _a.leftWidth, rightWidth = _a.rightWidth;
  52. var offset = position === 'left' ? leftWidth : -rightWidth;
  53. this.swipeMove(offset);
  54. this.$emit('open', {
  55. position: position,
  56. name: this.data.name
  57. });
  58. },
  59. close: function () {
  60. this.swipeMove(0);
  61. },
  62. swipeMove: function (offset) {
  63. if (offset === void 0) { offset = 0; }
  64. this.offset = utils_1.range(offset, -this.data.rightWidth, this.data.leftWidth);
  65. var transform = "translate3d(" + this.offset + "px, 0, 0)";
  66. var transition = this.dragging
  67. ? 'none'
  68. : 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  69. this.setData({
  70. wrapperStyle: "\n -webkit-transform: " + transform + ";\n -webkit-transition: " + transition + ";\n transform: " + transform + ";\n transition: " + transition + ";\n "
  71. });
  72. },
  73. swipeLeaveTransition: function () {
  74. var _a = this.data, leftWidth = _a.leftWidth, rightWidth = _a.rightWidth;
  75. var offset = this.offset;
  76. if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
  77. this.open('right');
  78. }
  79. else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
  80. this.open('left');
  81. }
  82. else {
  83. this.swipeMove(0);
  84. }
  85. this.setData({ catchMove: false });
  86. },
  87. startDrag: function (event) {
  88. if (this.data.disabled) {
  89. return;
  90. }
  91. this.startOffset = this.offset;
  92. this.touchStart(event);
  93. },
  94. noop: function () { },
  95. onDrag: function (event) {
  96. var _this = this;
  97. if (this.data.disabled) {
  98. return;
  99. }
  100. this.touchMove(event);
  101. if (this.direction !== 'horizontal') {
  102. return;
  103. }
  104. this.dragging = true;
  105. ARRAY.filter(function (item) { return item !== _this; }).forEach(function (item) { return item.close(); });
  106. this.setData({ catchMove: true });
  107. this.swipeMove(this.startOffset + this.deltaX);
  108. },
  109. endDrag: function () {
  110. if (this.data.disabled) {
  111. return;
  112. }
  113. this.dragging = false;
  114. this.swipeLeaveTransition();
  115. },
  116. onClick: function (event) {
  117. var _a = event.currentTarget.dataset.key, position = _a === void 0 ? 'outside' : _a;
  118. this.$emit('click', position);
  119. if (!this.offset) {
  120. return;
  121. }
  122. if (this.data.asyncClose) {
  123. this.$emit('close', {
  124. position: position,
  125. instance: this,
  126. name: this.data.name
  127. });
  128. }
  129. else {
  130. this.swipeMove(0);
  131. }
  132. }
  133. }
  134. });