pull-down-refresh.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. const { prefix } = config;
  11. const name = `${prefix}-pull-down-refresh`;
  12. let PullDownRefresh = class PullDownRefresh extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.pixelRatio = 1;
  16. this.startPoint = null;
  17. this.isPulling = false;
  18. this.maxBarHeight = 0;
  19. this.loadingBarHeight = 200;
  20. this.maxRefreshAnimateTimeFlag = 0;
  21. this.closingAnimateTimeFlag = 0;
  22. this.externalClasses = [`${prefix}-class`, `${prefix}-class-loading`, `${prefix}-class-tex`, `${prefix}-class-indicator`];
  23. this.options = {
  24. multipleSlots: true,
  25. };
  26. this.properties = props;
  27. this.data = {
  28. prefix,
  29. classPrefix: name,
  30. barHeight: 0,
  31. refreshStatus: -1,
  32. loosing: false,
  33. enableToRefresh: true,
  34. scrollTop: 0,
  35. };
  36. this.lifetimes = {
  37. attached() {
  38. const { screenWidth } = wx.getSystemInfoSync();
  39. const { maxBarHeight, loadingBarHeight } = this.properties;
  40. this.pixelRatio = 750 / screenWidth;
  41. if (maxBarHeight) {
  42. this.maxBarHeight = this.toRpx(maxBarHeight);
  43. }
  44. if (loadingBarHeight) {
  45. this.setData({
  46. computedLoadingBarHeight: this.toRpx(loadingBarHeight),
  47. });
  48. this.loadingBarHeight = this.toRpx(loadingBarHeight);
  49. }
  50. },
  51. detached() {
  52. clearTimeout(this.maxRefreshAnimateTimeFlag);
  53. clearTimeout(this.closingAnimateTimeFlag);
  54. },
  55. };
  56. this.observers = {
  57. value(val) {
  58. if (!val) {
  59. clearTimeout(this.maxRefreshAnimateTimeFlag);
  60. this.setData({ refreshStatus: 3 });
  61. this.close();
  62. }
  63. },
  64. maxBarHeight(val) {
  65. this.maxBarHeight = this.toRpx(val);
  66. },
  67. loadingBarHeight(val) {
  68. this.setData({
  69. computedLoadingBarHeight: this.toRpx(val),
  70. });
  71. this.loadingBarHeight = this.toRpx(val);
  72. },
  73. };
  74. this.methods = {
  75. onScrollToBottom() {
  76. this.triggerEvent('scrolltolower');
  77. },
  78. onScrollToTop() {
  79. this.setData({
  80. enableToRefresh: true,
  81. });
  82. },
  83. onScroll(e) {
  84. this.setData({
  85. enableToRefresh: e.detail.scrollTop === 0,
  86. });
  87. },
  88. onTouchStart(e) {
  89. if (this.isPulling || !this.data.enableToRefresh)
  90. return;
  91. const { touches } = e;
  92. if (touches.length !== 1)
  93. return;
  94. const { pageX, pageY } = touches[0];
  95. this.setData({ loosing: false });
  96. this.startPoint = { pageX, pageY };
  97. this.isPulling = true;
  98. },
  99. onTouchMove(e) {
  100. if (!this.startPoint)
  101. return;
  102. const { touches } = e;
  103. if (touches.length !== 1)
  104. return;
  105. const { pageY } = touches[0];
  106. const offset = pageY - this.startPoint.pageY;
  107. const barHeight = this.toRpx(offset);
  108. if (barHeight > 0) {
  109. if (barHeight > this.maxBarHeight) {
  110. this.setRefreshBarHeight(this.maxBarHeight);
  111. }
  112. else {
  113. this.setRefreshBarHeight(barHeight);
  114. }
  115. }
  116. },
  117. onTouchEnd(e) {
  118. if (!this.startPoint)
  119. return;
  120. const { changedTouches } = e;
  121. if (changedTouches.length !== 1)
  122. return;
  123. const { pageY } = changedTouches[0];
  124. const barHeight = this.toRpx(pageY - this.startPoint.pageY);
  125. this.startPoint = null;
  126. this.setData({ loosing: true });
  127. if (barHeight > this.loadingBarHeight) {
  128. this.setData({
  129. barHeight: this.loadingBarHeight,
  130. refreshStatus: 2,
  131. });
  132. this.triggerEvent('change', { value: true });
  133. this.triggerEvent('refresh');
  134. this.maxRefreshAnimateTimeFlag = setTimeout(() => {
  135. this.maxRefreshAnimateTimeFlag = null;
  136. if (this.data.refreshStatus === 2) {
  137. this.triggerEvent('timeout');
  138. this.close();
  139. }
  140. }, this.properties.refreshTimeout);
  141. }
  142. else {
  143. this.close();
  144. }
  145. },
  146. toRpx(v) {
  147. if (typeof v === 'number')
  148. return v * this.pixelRatio;
  149. return parseInt(v, 10);
  150. },
  151. toPx(v) {
  152. return v / this.pixelRatio;
  153. },
  154. setRefreshBarHeight(barHeight) {
  155. const data = { barHeight };
  156. if (barHeight >= this.loadingBarHeight) {
  157. data.refreshStatus = 1;
  158. }
  159. else {
  160. data.refreshStatus = 0;
  161. }
  162. return new Promise((resolve) => {
  163. this.setData(data, () => resolve(barHeight));
  164. });
  165. },
  166. close() {
  167. const animationDuration = 240;
  168. this.setData({ barHeight: 0 });
  169. this.triggerEvent('change', { value: false });
  170. this.closingAnimateTimeFlag = setTimeout(() => {
  171. this.closingAnimateTimeFlag = null;
  172. this.setData({ refreshStatus: -1 });
  173. this.isPulling = false;
  174. }, animationDuration);
  175. },
  176. setScrollTop(scrollTop) {
  177. this.setData({ scrollTop });
  178. },
  179. scrollToTop() {
  180. this.setScrollTop(0);
  181. },
  182. };
  183. }
  184. };
  185. PullDownRefresh = __decorate([
  186. wxComponent()
  187. ], PullDownRefresh);
  188. export default PullDownRefresh;