mui.pullToRefresh.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. (function($, window, document) {
  2. var STATE_BEFORECHANGEOFFSET = 'beforeChangeOffset';
  3. var STATE_AFTERCHANGEOFFSET = 'afterChangeOffset';
  4. var EVENT_PULLSTART = 'pullstart';
  5. var EVENT_PULLING = 'pulling';
  6. var EVENT_BEFORECHANGEOFFSET = STATE_BEFORECHANGEOFFSET;
  7. var EVENT_AFTERCHANGEOFFSET = STATE_AFTERCHANGEOFFSET;
  8. var EVENT_DRAGENDAFTERCHANGEOFFSET = 'dragEndAfterChangeOffset';
  9. var CLASS_TRANSITIONING = $.className('transitioning');
  10. var CLASS_PULL_TOP_TIPS = $.className('pull-top-tips');
  11. var CLASS_PULL_BOTTOM_TIPS = $.className('pull-bottom-tips');
  12. var CLASS_PULL_LOADING = $.className('pull-loading');
  13. var CLASS_SCROLL = $.className('scroll');
  14. var CLASS_PULL_TOP_ARROW = $.className('pull-loading') + ' ' + $.className('icon') + ' ' + $.className('icon-pulldown');
  15. var CLASS_PULL_TOP_ARROW_REVERSE = CLASS_PULL_TOP_ARROW + ' ' + $.className('reverse');
  16. var CLASS_PULL_TOP_SPINNER = $.className('pull-loading') + ' ' + $.className('spinner');
  17. var CLASS_HIDDEN = $.className('hidden');
  18. var SELECTOR_PULL_LOADING = '.' + CLASS_PULL_LOADING;
  19. $.PullToRefresh = $.Class.extend({
  20. init: function(element, options) {
  21. this.element = element;
  22. this.options = $.extend(true, {
  23. down: {
  24. height: 75,
  25. callback: false,
  26. },
  27. up: {
  28. auto: false,
  29. offset: 100, //距离底部高度(到达该高度即触发)
  30. show: true,
  31. contentinit: '上拉显示更多',
  32. contentdown: '上拉显示更多',
  33. contentrefresh: '正在加载...',
  34. contentnomore: '没有更多数据了',
  35. callback: false
  36. },
  37. preventDefaultException: {
  38. tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/
  39. }
  40. }, options);
  41. this.stopped = this.isNeedRefresh = this.isDragging = false;
  42. this.state = STATE_BEFORECHANGEOFFSET;
  43. this.isInScroll = this.element.classList.contains(CLASS_SCROLL);
  44. this.initPullUpTips();
  45. this.initEvent();
  46. },
  47. _preventDefaultException: function(el, exceptions) {
  48. for (var i in exceptions) {
  49. if (exceptions[i].test(el[i])) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. },
  55. initEvent: function() {
  56. if ($.isFunction(this.options.down.callback)) {
  57. this.element.addEventListener($.EVENT_START, this);
  58. this.element.addEventListener('drag', this);
  59. this.element.addEventListener('dragend', this);
  60. }
  61. if (this.pullUpTips) {
  62. this.element.addEventListener('dragup', this);
  63. if (this.isInScroll) {
  64. this.element.addEventListener('scrollbottom', this);
  65. } else {
  66. window.addEventListener('scroll', this);
  67. }
  68. }
  69. },
  70. handleEvent: function(e) {
  71. switch (e.type) {
  72. case $.EVENT_START:
  73. this.isInScroll && this._canPullDown() && e.target && !this._preventDefaultException(e.target, this.options.preventDefaultException) && e.preventDefault();
  74. break;
  75. case 'drag':
  76. this._drag(e);
  77. break;
  78. case 'dragend':
  79. this._dragend(e);
  80. break;
  81. case 'webkitTransitionEnd':
  82. this._transitionEnd(e);
  83. break;
  84. case 'dragup':
  85. case 'scroll':
  86. this._dragup(e);
  87. break;
  88. case 'scrollbottom':
  89. if (e.target === this.element) {
  90. this.pullUpLoading(e);
  91. }
  92. break;
  93. }
  94. },
  95. initPullDownTips: function() {
  96. var self = this;
  97. if ($.isFunction(self.options.down.callback)) {
  98. self.pullDownTips = (function() {
  99. var element = document.querySelector('.' + CLASS_PULL_TOP_TIPS);
  100. if (element) {
  101. element.parentNode.removeChild(element);
  102. }
  103. if (!element) {
  104. element = document.createElement('div');
  105. element.classList.add(CLASS_PULL_TOP_TIPS);
  106. element.innerHTML = '<div class="mui-pull-top-wrapper"><span class="mui-pull-loading mui-icon mui-icon-pulldown"></span></div>';
  107. element.addEventListener('webkitTransitionEnd', self);
  108. }
  109. self.pullDownTipsIcon = element.querySelector(SELECTOR_PULL_LOADING);
  110. document.body.appendChild(element);
  111. return element;
  112. }());
  113. }
  114. },
  115. initPullUpTips: function() {
  116. var self = this;
  117. if ($.isFunction(self.options.up.callback)) {
  118. self.pullUpTips = (function() {
  119. var element = self.element.querySelector('.' + CLASS_PULL_BOTTOM_TIPS);
  120. if (!element) {
  121. element = document.createElement('div');
  122. element.classList.add(CLASS_PULL_BOTTOM_TIPS);
  123. if (!self.options.up.show) {
  124. element.classList.add(CLASS_HIDDEN);
  125. }
  126. element.innerHTML = '<div class="mui-pull-bottom-wrapper"><span class="mui-pull-loading">' + self.options.up.contentinit + '</span></div>';
  127. self.element.appendChild(element);
  128. }
  129. self.pullUpTipsIcon = element.querySelector(SELECTOR_PULL_LOADING);
  130. return element;
  131. }());
  132. }
  133. },
  134. _transitionEnd: function(e) {
  135. if (e.target === this.pullDownTips && this.removing) {
  136. this.removePullDownTips();
  137. }
  138. },
  139. _dragup: function(e) {
  140. var self = this;
  141. if (self.loading) {
  142. return;
  143. }
  144. if (e && e.detail && $.gestures.session.drag) {
  145. self.isDraggingUp = true;
  146. } else {
  147. if (!self.isDraggingUp) { //scroll event
  148. return;
  149. }
  150. }
  151. if (!self.isDragging) {
  152. if (self._canPullUp()) {
  153. self.pullUpLoading(e);
  154. }
  155. }
  156. },
  157. _canPullUp: function() {
  158. if (this.removing) {
  159. return false;
  160. }
  161. if (this.isInScroll) {
  162. var scrollId = this.element.parentNode.getAttribute('data-scroll');
  163. if (scrollId) {
  164. var scrollApi = $.data[scrollId];
  165. return scrollApi.y === scrollApi.maxScrollY;
  166. }
  167. }
  168. return window.pageYOffset + window.innerHeight + this.options.up.offset >= document.documentElement.scrollHeight;
  169. },
  170. _canPullDown: function() {
  171. if (this.removing) {
  172. return false;
  173. }
  174. if (this.isInScroll) {
  175. var scrollId = this.element.parentNode.getAttribute('data-scroll');
  176. if (scrollId) {
  177. var scrollApi = $.data[scrollId];
  178. return scrollApi.y === 0;
  179. }
  180. }
  181. return document.body.scrollTop === 0;
  182. },
  183. _drag: function(e) {
  184. if (this.loading || this.stopped) {
  185. e.stopPropagation();
  186. e.detail.gesture.preventDefault();
  187. return;
  188. }
  189. var detail = e.detail;
  190. if (!this.isDragging) {
  191. if (detail.direction === 'down' && this._canPullDown()) {
  192. if (document.querySelector('.' + CLASS_PULL_TOP_TIPS)) {
  193. e.stopPropagation();
  194. e.detail.gesture.preventDefault();
  195. return;
  196. }
  197. this.isDragging = true;
  198. this.removing = false;
  199. this.startDeltaY = detail.deltaY;
  200. $.gestures.session.lockDirection = true; //锁定方向
  201. $.gestures.session.startDirection = detail.direction;
  202. this._pullStart(this.startDeltaY);
  203. }
  204. }
  205. if (this.isDragging) {
  206. e.stopPropagation();
  207. e.detail.gesture.preventDefault();
  208. var deltaY = detail.deltaY - this.startDeltaY;
  209. deltaY = Math.min(deltaY, 1.5 * this.options.down.height);
  210. this.deltaY = deltaY;
  211. this._pulling(deltaY);
  212. var state = deltaY > this.options.down.height ? STATE_AFTERCHANGEOFFSET : STATE_BEFORECHANGEOFFSET;
  213. if (this.state !== state) {
  214. this.state = state;
  215. if (this.state === STATE_AFTERCHANGEOFFSET) {
  216. this.removing = false;
  217. this.isNeedRefresh = true;
  218. } else {
  219. this.removing = true;
  220. this.isNeedRefresh = false;
  221. }
  222. this['_' + state](deltaY);
  223. }
  224. if ($.os.ios && parseFloat($.os.version) >= 8) {
  225. var clientY = detail.gesture.touches[0].clientY;
  226. if ((clientY + 10) > window.innerHeight || clientY < 10) {
  227. this._dragend(e);
  228. return;
  229. }
  230. }
  231. }
  232. },
  233. _dragend: function(e) {
  234. var self = this;
  235. if (self.isDragging) {
  236. self.isDragging = false;
  237. self._dragEndAfterChangeOffset(self.isNeedRefresh);
  238. }
  239. if (self.isPullingUp) {
  240. if (self.pullingUpTimeout) {
  241. clearTimeout(self.pullingUpTimeout);
  242. }
  243. self.pullingUpTimeout = setTimeout(function() {
  244. self.isPullingUp = false;
  245. }, 1000);
  246. }
  247. },
  248. _pullStart: function(startDeltaY) {
  249. this.pullStart(startDeltaY);
  250. $.trigger(this.element, EVENT_PULLSTART, {
  251. api: this,
  252. startDeltaY: startDeltaY
  253. });
  254. },
  255. _pulling: function(deltaY) {
  256. this.pulling(deltaY);
  257. $.trigger(this.element, EVENT_PULLING, {
  258. api: this,
  259. deltaY: deltaY
  260. });
  261. },
  262. _beforeChangeOffset: function(deltaY) {
  263. this.beforeChangeOffset(deltaY);
  264. $.trigger(this.element, EVENT_BEFORECHANGEOFFSET, {
  265. api: this,
  266. deltaY: deltaY
  267. });
  268. },
  269. _afterChangeOffset: function(deltaY) {
  270. this.afterChangeOffset(deltaY);
  271. $.trigger(this.element, EVENT_AFTERCHANGEOFFSET, {
  272. api: this,
  273. deltaY: deltaY
  274. });
  275. },
  276. _dragEndAfterChangeOffset: function(isNeedRefresh) {
  277. this.dragEndAfterChangeOffset(isNeedRefresh);
  278. $.trigger(this.element, EVENT_DRAGENDAFTERCHANGEOFFSET, {
  279. api: this,
  280. isNeedRefresh: isNeedRefresh
  281. });
  282. },
  283. removePullDownTips: function() {
  284. if (this.pullDownTips) {
  285. try {
  286. this.pullDownTips.parentNode && this.pullDownTips.parentNode.removeChild(this.pullDownTips);
  287. this.pullDownTips = null;
  288. this.removing = false;
  289. } catch (e) {}
  290. }
  291. },
  292. pullStart: function(startDeltaY) {
  293. this.initPullDownTips(startDeltaY);
  294. },
  295. pulling: function(deltaY) {
  296. this.pullDownTips.style.webkitTransform = 'translate3d(0,' + deltaY + 'px,0)';
  297. },
  298. beforeChangeOffset: function(deltaY) {
  299. this.pullDownTipsIcon.className = CLASS_PULL_TOP_ARROW;
  300. },
  301. afterChangeOffset: function(deltaY) {
  302. this.pullDownTipsIcon.className = CLASS_PULL_TOP_ARROW_REVERSE;
  303. },
  304. dragEndAfterChangeOffset: function(isNeedRefresh) {
  305. if (isNeedRefresh) {
  306. this.pullDownTipsIcon.className = CLASS_PULL_TOP_SPINNER;
  307. this.pullDownLoading();
  308. } else {
  309. this.pullDownTipsIcon.className = CLASS_PULL_TOP_ARROW;
  310. this.endPullDownToRefresh();
  311. }
  312. },
  313. pullDownLoading: function() {
  314. if (this.loading) {
  315. return;
  316. }
  317. if (!this.pullDownTips) {
  318. this.initPullDownTips();
  319. this.dragEndAfterChangeOffset(true);
  320. return;
  321. }
  322. this.loading = true;
  323. this.pullDownTips.classList.add(CLASS_TRANSITIONING);
  324. this.pullDownTips.style.webkitTransform = 'translate3d(0,' + this.options.down.height + 'px,0)';
  325. this.options.down.callback.apply(this);
  326. },
  327. pullUpLoading: function(e) {
  328. if (this.loading || this.finished) {
  329. return;
  330. }
  331. this.loading = true;
  332. this.isDraggingUp = false;
  333. this.pullUpTips.classList.remove(CLASS_HIDDEN);
  334. e && e.detail && e.detail.gesture && e.detail.gesture.preventDefault();
  335. this.pullUpTipsIcon.innerHTML = this.options.up.contentrefresh;
  336. this.options.up.callback.apply(this);
  337. },
  338. endPullDownToRefresh: function() {
  339. this.loading = false;
  340. this.pullUpTips && this.pullUpTips.classList.remove(CLASS_HIDDEN);
  341. this.pullDownTips.classList.add(CLASS_TRANSITIONING);
  342. this.pullDownTips.style.webkitTransform = 'translate3d(0,0,0)';
  343. if (this.deltaY <= 0) {
  344. this.removePullDownTips();
  345. } else {
  346. this.removing = true;
  347. }
  348. if (this.isInScroll) {
  349. $(this.element.parentNode).scroll().refresh();
  350. }
  351. },
  352. endPullUpToRefresh: function(finished) {
  353. if (finished) {
  354. this.finished = true;
  355. this.pullUpTipsIcon.innerHTML = this.options.up.contentnomore;
  356. this.element.removeEventListener('dragup', this);
  357. window.removeEventListener('scroll', this);
  358. } else {
  359. this.pullUpTipsIcon.innerHTML = this.options.up.contentdown;
  360. }
  361. this.loading = false;
  362. if (this.isInScroll) {
  363. $(this.element.parentNode).scroll().refresh();
  364. }
  365. },
  366. setStopped: function(stopped) {
  367. if (stopped != this.stopped) {
  368. this.stopped = stopped;
  369. this.pullUpTips && this.pullUpTips.classList[stopped ? 'add' : 'remove'](CLASS_HIDDEN);
  370. }
  371. },
  372. refresh: function(isReset) {
  373. if (isReset && this.finished && this.pullUpTipsIcon) {
  374. this.pullUpTipsIcon.innerHTML = this.options.up.contentdown;
  375. this.element.addEventListener('dragup', this);
  376. window.addEventListener('scroll', this);
  377. this.finished = false;
  378. }
  379. }
  380. });
  381. $.fn.pullToRefresh = function(options) {
  382. var pullRefreshApis = [];
  383. options = options || {};
  384. this.each(function() {
  385. var self = this;
  386. var pullRefreshApi = null;
  387. var id = self.getAttribute('data-pullToRefresh');
  388. if (!id) {
  389. id = ++$.uuid;
  390. $.data[id] = pullRefreshApi = new $.PullToRefresh(self, options);
  391. self.setAttribute('data-pullToRefresh', id);
  392. } else {
  393. pullRefreshApi = $.data[id];
  394. }
  395. if (options.up && options.up.auto) { //如果设置了auto,则自动上拉一次
  396. pullRefreshApi.pullUpLoading();
  397. }
  398. pullRefreshApis.push(pullRefreshApi);
  399. });
  400. return pullRefreshApis.length === 1 ? pullRefreshApis[0] : pullRefreshApis;
  401. }
  402. })(mui, window, document);