mui.class.scroll.pullrefresh.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. (function($, window, document, undefined) {
  2. var CLASS_VISIBILITY = $.className('visibility');
  3. var CLASS_HIDDEN = $.className('hidden');
  4. var PullRefresh = $.Scroll.extend($.extend({
  5. handleEvent: function(e) {
  6. this._super(e);
  7. if (e.type === 'scrollbottom') {
  8. if (e.target === this.scroller) {
  9. this._scrollbottom();
  10. }
  11. }
  12. },
  13. _scrollbottom: function() {
  14. if (!this.pulldown && !this.loading) {
  15. this.pulldown = false;
  16. this._initPullupRefresh();
  17. this.pullupLoading();
  18. }
  19. },
  20. _start: function(e) {
  21. //仅下拉刷新在start阻止默认事件
  22. if (e.touches && e.touches.length && e.touches[0].clientX > 30) {
  23. e.target && !this._preventDefaultException(e.target, this.options.preventDefaultException) && e.preventDefault();
  24. }
  25. if (!this.loading) {
  26. this.pulldown = this.pullPocket = this.pullCaption = this.pullLoading = false
  27. }
  28. this._super(e);
  29. },
  30. _drag: function(e) {
  31. if (this.y >= 0 && this.disablePulldown && e.detail.direction === 'down') { //禁用下拉刷新
  32. return;
  33. }
  34. this._super(e);
  35. if (!this.pulldown && !this.loading && this.topPocket && e.detail.direction === 'down' && this.y >= 0) {
  36. this._initPulldownRefresh();
  37. }
  38. if (this.pulldown) {
  39. this._setCaption(this.y > this.options.down.height ? this.options.down.contentover : this.options.down.contentdown);
  40. }
  41. },
  42. _reLayout: function() {
  43. this.hasVerticalScroll = true;
  44. this._super();
  45. },
  46. //API
  47. resetPosition: function(time) {
  48. if (this.pulldown && !this.disablePulldown) {
  49. if (this.y >= this.options.down.height) {
  50. this.pulldownLoading(undefined, time || 0);
  51. return true;
  52. } else {
  53. !this.loading && this.topPocket.classList.remove(CLASS_VISIBILITY);
  54. }
  55. }
  56. return this._super(time);
  57. },
  58. pulldownLoading: function(y, time) {
  59. typeof y === 'undefined' && (y = this.options.down.height); //默认高度
  60. this.scrollTo(0, y, time, this.options.bounceEasing);
  61. if (this.loading) {
  62. return;
  63. }
  64. // if (!this.pulldown) {
  65. this._initPulldownRefresh();
  66. // }
  67. this._setCaption(this.options.down.contentrefresh);
  68. this.loading = true;
  69. this.indicators.map(function(indicator) {
  70. indicator.fade(0);
  71. });
  72. var callback = this.options.down.callback;
  73. callback && callback.call(this);
  74. },
  75. endPulldownToRefresh: function() {
  76. var self = this;
  77. if (self.topPocket && self.loading && this.pulldown) {
  78. self.scrollTo(0, 0, self.options.bounceTime, self.options.bounceEasing);
  79. self.loading = false;
  80. self._setCaption(self.options.down.contentdown, true);
  81. setTimeout(function() {
  82. self.loading || self.topPocket.classList.remove(CLASS_VISIBILITY);
  83. }, 350);
  84. }
  85. },
  86. pullupLoading: function(callback, x, time) {
  87. x = x || 0;
  88. this.scrollTo(x, this.maxScrollY, time, this.options.bounceEasing);
  89. if (this.loading) {
  90. return;
  91. }
  92. this._initPullupRefresh();
  93. this._setCaption(this.options.up.contentrefresh);
  94. this.indicators.map(function(indicator) {
  95. indicator.fade(0);
  96. });
  97. this.loading = true;
  98. callback = callback || this.options.up.callback;
  99. callback && callback.call(this);
  100. },
  101. endPullupToRefresh: function(finished) {
  102. var self = this;
  103. if (self.bottomPocket) { // && self.loading && !this.pulldown
  104. self.loading = false;
  105. if (finished) {
  106. this.finished = true;
  107. self._setCaption(self.options.up.contentnomore);
  108. // self.bottomPocket.classList.remove(CLASS_VISIBILITY);
  109. // self.bottomPocket.classList.add(CLASS_HIDDEN);
  110. self.wrapper.removeEventListener('scrollbottom', self);
  111. } else {
  112. self._setCaption(self.options.up.contentdown);
  113. // setTimeout(function() {
  114. self.loading || self.bottomPocket.classList.remove(CLASS_VISIBILITY);
  115. // }, 300);
  116. }
  117. }
  118. },
  119. disablePullupToRefresh: function() {
  120. this._initPullupRefresh();
  121. this.bottomPocket.className = $.className('pull-bottom-pocket') + ' ' + CLASS_HIDDEN;
  122. this.wrapper.removeEventListener('scrollbottom', this);
  123. },
  124. disablePulldownToRefresh: function() {
  125. this._initPulldownRefresh();
  126. this.topPocket.className = $.className('pull-top-pocket') + ' ' + CLASS_HIDDEN;
  127. this.disablePulldown = true;
  128. },
  129. enablePulldownToRefresh: function() {
  130. this._initPulldownRefresh();
  131. this.topPocket.classList.remove(CLASS_HIDDEN);
  132. this._setCaption(this.options.down.contentdown);
  133. this.disablePulldown = false;
  134. },
  135. enablePullupToRefresh: function() {
  136. this._initPullupRefresh();
  137. this.bottomPocket.classList.remove(CLASS_HIDDEN);
  138. this._setCaption(this.options.up.contentdown);
  139. this.wrapper.addEventListener('scrollbottom', this);
  140. },
  141. refresh: function(isReset) {
  142. if (isReset && this.finished) {
  143. this.enablePullupToRefresh();
  144. this.finished = false;
  145. }
  146. this._super();
  147. },
  148. }, $.PullRefresh));
  149. $.fn.pullRefresh = function(options) {
  150. if (this.length === 1) {
  151. var self = this[0];
  152. var pullRefreshApi = null;
  153. var id = self.getAttribute('data-pullrefresh');
  154. if (!id && typeof options === 'undefined') {
  155. return false;
  156. }
  157. options = options || {};
  158. if (!id) {
  159. id = ++$.uuid;
  160. $.data[id] = pullRefreshApi = new PullRefresh(self, options);
  161. self.setAttribute('data-pullrefresh', id);
  162. } else {
  163. pullRefreshApi = $.data[id];
  164. }
  165. if (options.down && options.down.auto) { //如果设置了auto,则自动下拉一次
  166. pullRefreshApi.pulldownLoading(options.down.autoY);
  167. } else if (options.up && options.up.auto) { //如果设置了auto,则自动上拉一次
  168. pullRefreshApi.pullupLoading();
  169. }
  170. //暂不提供这种调用方式吧
  171. // if (typeof options === 'string') {
  172. // var methodValue = pullRefreshApi[options].apply(pullRefreshApi, $.slice.call(arguments, 1));
  173. // if (methodValue !== undefined) {
  174. // return methodValue;
  175. // }
  176. // }
  177. return pullRefreshApi;
  178. }
  179. };
  180. })(mui, window, document);