mui.pullrefresh.5+.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. var webview = plus.webview.currentWebview();
  125. if (this.stopped) {
  126. webview.setStyle({
  127. bounce: 'none'
  128. });
  129. webview.setBounce({
  130. position: {
  131. top: 'none'
  132. }
  133. });
  134. } else {
  135. var height = this.options.down.height;
  136. webview.setStyle({
  137. bounce: 'vertical'
  138. });
  139. webview.setBounce({
  140. position: {
  141. top: height * 2 + 'px'
  142. },
  143. changeoffset: {
  144. top: height + 'px'
  145. }
  146. });
  147. }
  148. },
  149. beginPulldown:function() {
  150. var self = this;
  151. $.plusReady(function() {
  152. //这里延时的目的是为了保证下拉刷新组件初始化完成,后续应该做成有状态的
  153. setTimeout(function () {
  154. if(self.options.down.style == "circle"){//单webview下拉刷新
  155. plus.webview.currentWebview().beginPullToRefresh();
  156. }else{//双webview模式
  157. var webview = self.options.webview;
  158. if(webview){
  159. webview.setBounce({
  160. offset: {
  161. top: self.options.down.height + "px"
  162. }
  163. });
  164. }
  165. }
  166. },15);
  167. }.bind(this));
  168. },
  169. pulldownLoading: function () {//该方法是子页面调用的,兼容老的历史API
  170. this.beginPulldown();
  171. },
  172. _pulldownLoading: function() { //该方法是父页面调用的
  173. var self = this;
  174. $.plusReady(function() {
  175. var childWebview = plus.webview.getWebviewById(self.options.webviewId);
  176. childWebview && childWebview.setBounce({
  177. offset: {
  178. top: self.options.down.height + "px"
  179. }
  180. });
  181. });
  182. },
  183. endPulldown:function(){
  184. var _wv = plus.webview.currentWebview();
  185. //双webview的下拉刷新,需要修改父窗口提示信息
  186. if(_wv.parent() && this.options.down.style !== "circle"){
  187. _wv.parent().evalJS("mui&&mui(document.querySelector('.mui-content')).pullRefresh('" + JSON.stringify({
  188. webviewId: _wv.id
  189. }) + "')._endPulldownToRefresh()");
  190. }else{
  191. _wv.endPullToRefresh();
  192. }
  193. },
  194. endPulldownToRefresh: function () {//该方法是子页面调用的,兼容老的历史API
  195. this.endPulldown();
  196. },
  197. _endPulldownToRefresh: function() { //该方法是父页面调用的
  198. var self = this;
  199. if (self.topPocket && self.options.webview) {
  200. self.options.webview.endPullToRefresh(); //下拉刷新所在webview回弹
  201. self.loading = false;
  202. self._setCaption(self.options.down.contentdown, true);
  203. setTimeout(function() {
  204. self.loading || self.topPocket.classList.remove(CLASS_BLOCK);
  205. }, 350);
  206. }
  207. },
  208. beginPullup:function(callback) {//开始上拉加载
  209. var self = this;
  210. if (self.isLoading) return;
  211. self.isLoading = true;
  212. if (self.pulldown !== false) {
  213. self._initPullupRefresh();
  214. } else {
  215. this.pullPocket.classList.add(CLASS_BLOCK);
  216. }
  217. setTimeout(function() {
  218. self.pullLoading.classList.add(CLASS_VISIBILITY);
  219. self.pullLoading.classList.remove(CLASS_HIDDEN);
  220. self.pullCaption.innerHTML = ''; //修正5+里边第一次加载时,文字显示的bug(还会显示出来个“多”,猜测应该是渲染问题导致的)
  221. self.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_REFRESH;
  222. self.pullCaption.innerHTML = self.options.up.contentrefresh;
  223. callback = callback || self.options.up.callback;
  224. callback && callback.call(self);
  225. }, 300);
  226. },
  227. pullupLoading:function (callback) {//兼容老的API
  228. this.beginPullup(callback);
  229. },
  230. endPullup:function(finished) {//上拉加载结束
  231. var self = this;
  232. if (self.pullLoading) {
  233. self.pullLoading.classList.remove(CLASS_VISIBILITY);
  234. self.pullLoading.classList.add(CLASS_HIDDEN);
  235. self.isLoading = false;
  236. if (finished) {
  237. self.finished = true;
  238. self.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_NOMORE;
  239. self.pullCaption.innerHTML = self.options.up.contentnomore;
  240. //取消5+的plusscrollbottom事件
  241. document.removeEventListener('plusscrollbottom', self);
  242. window.removeEventListener('dragup', self);
  243. } else { //初始化时隐藏,后续不再隐藏
  244. self.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_DOWN;
  245. self.pullCaption.innerHTML = self.options.up.contentdown;
  246. }
  247. }
  248. },
  249. endPullupToRefresh: function (finished) {//上拉加载结束,兼容老的API
  250. this.endPullup(finished);
  251. },
  252. disablePullupToRefresh: function() {
  253. this._initPullupRefresh();
  254. this.bottomPocket.className = $.className('pull-bottom-pocket') + ' ' + CLASS_HIDDEN;
  255. window.removeEventListener('dragup', this);
  256. },
  257. enablePullupToRefresh: function() {
  258. this._initPullupRefresh();
  259. this.bottomPocket.classList.remove(CLASS_HIDDEN);
  260. this.pullCaption.className = CLASS_PULL_CAPTION + ' ' + CLASS_PULL_CAPTION_DOWN;
  261. this.pullCaption.innerHTML = this.options.up.contentdown;
  262. document.addEventListener("plusscrollbottom", this);
  263. window.addEventListener('dragup', this);
  264. },
  265. scrollTo: function(x, y, time) {
  266. $.scrollTo(y, time);
  267. },
  268. scrollToBottom: function(time) {
  269. $.scrollTo(document.documentElement.scrollHeight, time);
  270. },
  271. refresh: function(isReset) {
  272. if (isReset && this.finished) {
  273. this.enablePullupToRefresh();
  274. this.finished = false;
  275. }
  276. }
  277. }, $.PullRefresh));
  278. //override h5 pullRefresh
  279. $.fn.pullRefresh_native = function(options) {
  280. var self;
  281. if (this.length === 0) {
  282. self = document.createElement('div');
  283. self.className = 'mui-content';
  284. document.body.appendChild(self);
  285. } else {
  286. self = this[0];
  287. }
  288. var args = options;
  289. //一个父需要支持多个子下拉刷新
  290. options = options || {}
  291. if (typeof options === 'string') {
  292. options = $.parseJSON(options);
  293. };
  294. !options.webviewId && (options.webviewId = (plus.webview.currentWebview().id || plus.webview.currentWebview().getURL()));
  295. var pullRefreshApi = null;
  296. var attrWebviewId = options.webviewId && options.webviewId.replace(/\//g, "_"); //替换所有"/"
  297. var id = self.getAttribute('data-pullrefresh-plus-' + attrWebviewId);
  298. if (!id && typeof args === 'undefined') {
  299. return false;
  300. }
  301. if (!id) { //避免重复初始化5+ pullrefresh
  302. id = ++$.uuid;
  303. self.setAttribute('data-pullrefresh-plus-' + attrWebviewId, id);
  304. document.body.classList.add(CLASS_PLUS_PULLREFRESH);
  305. $.data[id] = pullRefreshApi = new PlusPullRefresh(self, options);
  306. } else {
  307. pullRefreshApi = $.data[id];
  308. }
  309. if (options.down && options.down.auto) { //如果设置了auto,则自动下拉一次
  310. //pullRefreshApi._pulldownLoading(); //parent webview
  311. pullRefreshApi.beginPulldown();
  312. } else if (options.up && options.up.auto) { //如果设置了auto,则自动上拉一次
  313. pullRefreshApi.beginPullup();
  314. }
  315. return pullRefreshApi;
  316. };
  317. });
  318. })(mui, document);