mui.pullrefresh.5+.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * pullRefresh 5+
  3. * @param {type} $
  4. * @returns {undefined}
  5. */
  6. (function($, document) {
  7. if (!($.os.plus)) { //仅在5+android支持多webview的使用
  8. return;
  9. }
  10. $.plusReady(function() {
  11. if (window.__NWin_Enable__ === false) { //不支持多webview,则不用5+下拉刷新
  12. return;
  13. }
  14. var CLASS_PLUS_PULLREFRESH = $.className('plus-pullrefresh');
  15. var CLASS_VISIBILITY = $.className('visibility');
  16. var CLASS_HIDDEN = $.className('hidden');
  17. var CLASS_BLOCK = $.className('block');
  18. var CLASS_PULL_CAPTION = $.className('pull-caption');
  19. var CLASS_PULL_CAPTION_DOWN = $.className('pull-caption-down');
  20. var CLASS_PULL_CAPTION_REFRESH = $.className('pull-caption-refresh');
  21. var CLASS_PULL_CAPTION_NOMORE = $.className('pull-caption-nomore');
  22. var PlusPullRefresh = $.Class.extend({
  23. init: function(element, options) {
  24. this.element = element;
  25. this.options = options;
  26. this.wrapper = this.scroller = element;
  27. this._init();
  28. this._initPulldownRefreshEvent();
  29. },
  30. _init: function() {
  31. var self = this;
  32. //document.addEventListener('plusscrollbottom', this);
  33. window.addEventListener('dragup', self);
  34. document.addEventListener("plusscrollbottom", self);
  35. self.scrollInterval = window.setInterval(function() {
  36. if (self.isScroll && !self.loading) {
  37. if (window.pageYOffset + window.innerHeight + 10 >= document.documentElement.scrollHeight) {
  38. self.isScroll = false; //放在这里是因为快速滚动的话,有可能检测时,还没到底,所以只要有滚动,没到底之前一直检测高度变化
  39. if (self.bottomPocket) {
  40. self.pullupLoading();
  41. }
  42. }
  43. }
  44. }, 100);
  45. },
  46. _initPulldownRefreshEvent: function() {
  47. var self = this;
  48. $.plusReady(function() {
  49. if (self.options.down.style == "circle") {
  50. //单webview、原生转圈
  51. self.options.webview = plus.webview.currentWebview();
  52. self.options.webview.setPullToRefresh({
  53. support: true,
  54. color: self.options.down.color || '#2BD009',
  55. height: self.options.down.height || '50px',
  56. range: self.options.down.range || '100px',
  57. style: 'circle',
  58. offset: self.options.down.offset || '0px'
  59. }, function() {
  60. self.options.down.callback();
  61. });
  62. } else if (self.topPocket && self.options.webviewId) {
  63. var webview = plus.webview.getWebviewById(self.options.webviewId); //子窗口
  64. if (!webview) {
  65. return;
  66. }
  67. self.options.webview = webview;
  68. var downOptions = self.options.down;
  69. var height = downOptions.height;
  70. webview.addEventListener('close', function() {
  71. var attrWebviewId = self.options.webviewId && self.options.webviewId.replace(/\//g, "_"); //替换所有"/"
  72. self.element.removeAttribute('data-pullrefresh-plus-' + attrWebviewId);
  73. });
  74. webview.addEventListener("dragBounce", function(e) {
  75. if (!self.pulldown) {
  76. self._initPulldownRefresh();
  77. } else {
  78. self.pullPocket.classList.add(CLASS_BLOCK);
  79. }
  80. switch (e.status) {
  81. case "beforeChangeOffset": //下拉可刷新状态
  82. self._setCaption(downOptions.contentdown);
  83. break;
  84. case "afterChangeOffset": //松开可刷新状态
  85. self._setCaption(downOptions.contentover);
  86. break;
  87. case "dragEndAfterChangeOffset": //正在刷新状态
  88. //执行下拉刷新所在webview的回调函数
  89. webview.evalJS("window.mui&&mui.options.pullRefresh.down.callback()");
  90. self._setCaption(downOptions.contentrefresh);
  91. break;
  92. default:
  93. break;
  94. }
  95. }, false);
  96. webview.setBounce({
  97. position: {
  98. top: height * 2 + 'px'
  99. },
  100. changeoffset: {
  101. top: height + 'px'
  102. }
  103. });
  104. }
  105. });
  106. },
  107. handleEvent: function(e) {
  108. var self = this;
  109. if (self.stopped) {
  110. return;
  111. }
  112. self.isScroll = false;
  113. if (e.type === 'dragup' || e.type === 'plusscrollbottom') {
  114. self.isScroll = true;
  115. setTimeout(function() {
  116. self.isScroll = false;
  117. }, 1000);
  118. }
  119. }
  120. }).extend($.extend({
  121. setStopped: function(stopped) { //该方法是子页面调用的
  122. this.stopped = !!stopped;
  123. // TODO 此处需要设置当前webview的bounce为none,目前5+有BUG
  124. if (this.stopped) {
  125. this.disablePullupToRefresh();
  126. this.disablePulldownToRefresh();
  127. } else {
  128. this.enablePullupToRefresh();
  129. this.enablePulldownToRefresh();
  130. }
  131. },
  132. beginPulldown: function() {
  133. var self = this;
  134. $.plusReady(function() {
  135. //这里延时的目的是为了保证下拉刷新组件初始化完成,后续应该做成有状态的
  136. setTimeout(function() {
  137. if (self.options.down.style == "circle") { //单webview下拉刷新
  138. plus.webview.currentWebview().beginPullToRefresh();
  139. } else { //双webview模式
  140. var webview = self.options.webview;
  141. if (webview) {
  142. webview.setBounce({
  143. offset: {
  144. top: self.options.down.height + "px"
  145. }
  146. });
  147. }
  148. }
  149. }, 15);
  150. }.bind(this));
  151. },
  152. pulldownLoading: function() { //该方法是子页面调用的,兼容老的历史API
  153. this.beginPulldown();
  154. },
  155. _pulldownLoading: function() { //该方法是父页面调用的
  156. var self = this;
  157. $.plusReady(function() {
  158. var childWebview = plus.webview.getWebviewById(self.options.webviewId);
  159. childWebview && childWebview.setBounce({
  160. offset: {
  161. top: self.options.down.height + "px"
  162. }
  163. });
  164. });
  165. },
  166. endPulldown: function() {
  167. var _wv = plus.webview.currentWebview();
  168. //双webview的下拉刷新,需要修改父窗口提示信息
  169. if (_wv.parent() && this.options.down.style !== "circle") {
  170. _wv.parent().evalJS("mui&&mui(document.querySelector('.mui-content')).pullRefresh('" + JSON.stringify({
  171. webviewId: _wv.id
  172. }) + "')._endPulldownToRefresh()");
  173. } else {
  174. _wv.endPullToRefresh();
  175. }
  176. },
  177. endPulldownToRefresh: function() { //该方法是子页面调用的,兼容老的历史API
  178. this.endPulldown();
  179. },
  180. _endPulldownToRefresh: function() { //该方法是父页面调用的
  181. var self = this;
  182. if (self.topPocket && self.options.webview) {
  183. self.options.webview.endPullToRefresh(); //下拉刷新所在webview回弹
  184. self.loading = false;
  185. self._setCaption(self.options.down.contentdown, true);
  186. setTimeout(function() {
  187. self.loading || self.topPocket.classList.remove(CLASS_BLOCK);
  188. }, 350);
  189. }
  190. },
  191. beginPullup: function(callback) { //开始上拉加载
  192. var self = this;
  193. if (self.isLoading) return;
  194. self.isLoading = true;
  195. if (self.pulldown !== false) {
  196. self._initPullupRefresh();
  197. } else {
  198. this.pullPocket.classList.add(CLASS_BLOCK);
  199. }
  200. setTimeout(function() {
  201. self.pullLoading.classList.add(CLASS_VISIBILITY);
  202. self.pullLoading.classList.remove(CLASS_HIDDEN);
  203. self.pullCaption.innerHTML = ''; //修正5+里边第一次加载时,文字显示的bug(还会显示出来个“多”,猜测应该是渲染问题导致的)
  204. self.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_REFRESH;
  205. self.pullCaption.innerHTML = self.options.up.contentrefresh;
  206. callback = callback || self.options.up.callback;
  207. callback && callback.call(self);
  208. }, 300);
  209. },
  210. pullupLoading: function(callback) { //兼容老的API
  211. this.beginPullup(callback);
  212. },
  213. endPullup: function(finished) { //上拉加载结束
  214. var self = this;
  215. if (self.pullLoading) {
  216. self.pullLoading.classList.remove(CLASS_VISIBILITY);
  217. self.pullLoading.classList.add(CLASS_HIDDEN);
  218. self.isLoading = false;
  219. if (finished) {
  220. self.finished = true;
  221. self.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_NOMORE;
  222. self.pullCaption.innerHTML = self.options.up.contentnomore;
  223. //取消5+的plusscrollbottom事件
  224. document.removeEventListener('plusscrollbottom', self);
  225. window.removeEventListener('dragup', self);
  226. } else { //初始化时隐藏,后续不再隐藏
  227. self.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_DOWN;
  228. self.pullCaption.innerHTML = self.options.up.contentdown;
  229. }
  230. }
  231. },
  232. endPullupToRefresh: function(finished) { //上拉加载结束,兼容老的API
  233. this.endPullup(finished);
  234. },
  235. disablePulldownToRefresh: function() {
  236. var webview = plus.webview.currentWebview();
  237. if (this.options.down.style && this.options.down.style == 'circle') { // 单webview模式禁止原生下拉刷新
  238. this.options.webview.setPullToRefresh({
  239. support: false,
  240. style: 'circle'
  241. });
  242. } else { // 双webview模式禁止下拉刷新
  243. webview.setStyle({
  244. bounce: 'none'
  245. });
  246. webview.setBounce({
  247. position: {
  248. top: 'none'
  249. }
  250. });
  251. }
  252. },
  253. enablePulldownToRefresh: function() {
  254. var self = this,
  255. webview = plus.webview.currentWebview(),
  256. height = this.options.down.height;
  257. // 单webview模式禁止原生下拉刷新
  258. if (this.options.down.style && this.options.down.style == 'circle') {
  259. webview.setPullToRefresh({
  260. support: true,
  261. height: height || '50px',
  262. range: self.options.down.range || '100px',
  263. style: 'circle',
  264. offset: self.options.down.offset || '0px'
  265. });
  266. } else { // 重新初始化双webview模式下拉刷新
  267. webview.setStyle({
  268. bounce: 'vertical'
  269. });
  270. webview.setBounce({
  271. position: {
  272. top: height * 2 + 'px'
  273. },
  274. changeoffset: {
  275. top: height + 'px'
  276. }
  277. });
  278. }
  279. },
  280. disablePullupToRefresh: function() {
  281. this._initPullupRefresh();
  282. this.bottomPocket.className = $.className('pull-bottom-pocket') + ' ' + CLASS_HIDDEN;
  283. window.removeEventListener('dragup', this);
  284. },
  285. enablePullupToRefresh: function() {
  286. this._initPullupRefresh();
  287. this.bottomPocket.classList.remove(CLASS_HIDDEN);
  288. this.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_DOWN;
  289. this.pullCaption.innerHTML = this.options.up.contentdown;
  290. document.addEventListener("plusscrollbottom", this);
  291. window.addEventListener('dragup', this);
  292. },
  293. scrollTo: function(x, y, time) {
  294. $.scrollTo(y, time);
  295. },
  296. scrollToBottom: function(time) {
  297. $.scrollTo(document.documentElement.scrollHeight, time);
  298. },
  299. refresh: function(isReset) {
  300. if (isReset && this.finished) {
  301. this.enablePullupToRefresh();
  302. this.finished = false;
  303. }
  304. }
  305. }, $.PullRefresh));
  306. //override h5 pullRefresh
  307. $.fn.pullRefresh_native = function(options) {
  308. var self;
  309. if (this.length === 0) {
  310. self = document.createElement('div');
  311. self.className = 'mui-content';
  312. document.body.appendChild(self);
  313. } else {
  314. self = this[0];
  315. }
  316. var args = options;
  317. //一个父需要支持多个子下拉刷新
  318. options = options || {}
  319. if (typeof options === 'string') {
  320. options = $.parseJSON(options);
  321. };
  322. !options.webviewId && (options.webviewId = (plus.webview.currentWebview().id || plus.webview.currentWebview().getURL()));
  323. var pullRefreshApi = null;
  324. var attrWebviewId = options.webviewId && options.webviewId.replace(/\//g, "_"); //替换所有"/"
  325. var id = self.getAttribute('data-pullrefresh-plus-' + attrWebviewId);
  326. if (!id && typeof args === 'undefined') {
  327. return false;
  328. }
  329. if (!id) { //避免重复初始化5+ pullrefresh
  330. id = ++$.uuid;
  331. self.setAttribute('data-pullrefresh-plus-' + attrWebviewId, id);
  332. document.body.classList.add(CLASS_PLUS_PULLREFRESH);
  333. $.data[id] = pullRefreshApi = new PlusPullRefresh(self, options);
  334. } else {
  335. pullRefreshApi = $.data[id];
  336. }
  337. if (options.down && options.down.auto) { //如果设置了auto,则自动下拉一次
  338. //pullRefreshApi._pulldownLoading(); //parent webview
  339. pullRefreshApi.beginPulldown();
  340. } else if (options.up && options.up.auto) { //如果设置了auto,则自动上拉一次
  341. pullRefreshApi.beginPullup();
  342. }
  343. return pullRefreshApi;
  344. };
  345. });
  346. })(mui, document);