pull-down-refresh.js 7.6 KB

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