pull-down-refresh.js 7.3 KB

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