webviewGroup.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. var webviewGroup = function(id, options) {
  2. this.id = id;
  3. this.options = options;
  4. this.styles = options.styles;
  5. this.items = options.items;
  6. this.onChange = options.onChange
  7. this.options.index = options.index || 0;
  8. this.webviews = {};
  9. this.webviewContexts = {};
  10. this.currentWebview = false;
  11. this._init();
  12. };
  13. var proto = webviewGroup.prototype;
  14. proto._init = function() {
  15. this._initParent();
  16. this._initNativeView();
  17. this._initWebviewContexts(this.options.index);
  18. };
  19. proto._initParent = function() {
  20. this.parent = plus.webview.getWebviewById(this.id);
  21. if(!this.parent) {
  22. this.parent = plus.webview.create(this.id, this.id);
  23. this.parent.show('none');
  24. }
  25. };
  26. proto._initNativeView = function() {
  27. this.nativeView = new plus.nativeObj.View('__MUI_TAB_NATIVE', {
  28. 'top': '40px',//这个需要根据顶部导航及顶部选项卡高度自动调整
  29. 'height': (window.screen.height - 40)+"px",
  30. 'left': '100%',
  31. 'width': '100%',
  32. "backgroundColor":"#ffffff"
  33. });
  34. this.nativeView.show();
  35. };
  36. proto._initWebviewContexts = function() {
  37. for(var len = this.items.length, i = len - 1; i >= 0; i--) {
  38. var webviewOptions = this.items[i];
  39. var id = webviewOptions.id;
  40. var isFirst = i === 0;
  41. var isLast = i === (len - 1);
  42. var isCurrent = this.options.index === i;
  43. var extras = webviewOptions.extras;
  44. extras.__mui_url = webviewOptions.url;
  45. extras.__mui_index = i;
  46. extras.__mui_left = isFirst ? '' : this.items[i - 1].id;
  47. extras.__mui_right = isLast ? '' : this.items[i + 1].id;
  48. var styles = webviewOptions.styles || {};
  49. if(i > this.options.index) {
  50. styles.left = '100%';
  51. } else if(i < this.options.index) {
  52. styles.left = '-100%';
  53. } else {
  54. styles.left = '0';
  55. }
  56. var webviewContext = new webviewGroupContext(id, webviewOptions, this);
  57. this.webviewContexts[id] = webviewContext;
  58. if(isCurrent) {
  59. webviewContext.webview = plus.webview.getWebviewById(id);
  60. webviewContext.createWebview();
  61. webviewContext.webview.show("none");
  62. this._initDrags(webviewContext.webview);
  63. this.currentWebview = webviewContext.webview;
  64. }
  65. }
  66. };
  67. proto._onChange = function(webview) {
  68. this.currentWebview = webview;
  69. this.onChange({
  70. index: webview.__mui_index
  71. });
  72. };
  73. proto._dragCallback = function(dir, fromWebview, view, viewId) {
  74. if(view === this.nativeView) { //需要创建webview
  75. //第一步:初始化目标webview
  76. this.webviewContexts[viewId].createWebview('drag');
  77. var targetWebview = this.webviewContexts[viewId].webview;
  78. targetWebview.show();
  79. this.nativeView.setStyle({
  80. left: '100%'
  81. });
  82. //第二步:初始化目标webview的drag
  83. this._initDrags(targetWebview);
  84. this._onChange(targetWebview);
  85. //第三步:校验目标webview的左右webview的drag初始化
  86. this._checkDrags(targetWebview);
  87. } else {
  88. this._onChange(view);
  89. }
  90. };
  91. proto._initDrag = function(webview, dir) {
  92. var flag = ('__mui_drag_' + dir + '_flag');
  93. if(webview[flag]) {
  94. return;
  95. }
  96. var viewId = webview['__mui_' + (dir === 'left' ? 'right' : 'left')];
  97. if(viewId) {
  98. var view = plus.webview.getWebviewById(viewId);
  99. if(!view) { //如果目标webview不存在,使用nativeView替换
  100. view = this.nativeView;
  101. } else {
  102. webview[flag] = true;
  103. }
  104. webview.drag({
  105. 'direction': dir,
  106. 'moveMode': 'followFinger'
  107. }, {
  108. 'view': view,
  109. 'moveMode': 'follow'
  110. },
  111. function(res) {
  112. if(res.type === 'end' && res.result) { //拖拽完成
  113. this._dragCallback(dir, webview, view, viewId);
  114. }
  115. }.bind(this)
  116. )
  117. } else {
  118. webview[flag] = true;
  119. }
  120. };
  121. proto._initDrags = function(webview) {
  122. this._initDrag(webview, 'left');
  123. this._initDrag(webview, 'right');
  124. };
  125. proto._checkDrags = function(webview) {
  126. var left = webview.__mui_left;
  127. var right = webview.__mui_right;
  128. if(left) {
  129. var leftWebview = plus.webview.getWebviewById(left);
  130. if(leftWebview && !leftWebview.__mui_drag_left_flag) {
  131. this._initDrag(leftWebview, 'left');
  132. }
  133. }
  134. if(right) {
  135. var rightWebview = plus.webview.getWebviewById(right);
  136. if(rightWebview && !rightWebview.__mui_drag_right_flag) {
  137. this._initDrag(rightWebview, 'right');
  138. }
  139. }
  140. };
  141. proto.getCurrentWebview = function() {
  142. return this.currentWebview;
  143. };
  144. proto.getCurrentWebviewContext = function() {
  145. if(this.currentWebview) {
  146. return this.webviewContexts[this.currentWebview.id];
  147. }
  148. return false;
  149. };
  150. proto.switchTab = function(id) {
  151. id = id.replace('_0', ''); //首页需要替换为appid
  152. var fromWebview = this.currentWebview;
  153. if(id === fromWebview.id) {
  154. return;
  155. }
  156. var toWebviewContext = this.webviewContexts[id];
  157. var toWebview = toWebviewContext.webview;
  158. var fromToLeft = '100%';
  159. var toFromLeft = '-100%';
  160. if(toWebviewContext.options.extras.__mui_index > fromWebview.__mui_index) {
  161. fromToLeft = '-100%';
  162. toFromLeft = '100%';
  163. }
  164. var isNew = false;
  165. if(!toWebview) {
  166. isNew = true;
  167. toWebviewContext.createWebview('startAnimation');
  168. toWebview = toWebviewContext.webview;
  169. // toWebview.showBehind(plus.webview.getSecondWebview());
  170. toWebview.show();
  171. this._initDrags(toWebview);
  172. this._checkDrags(toWebview); //新建的时候均需校验
  173. }
  174. var self = this;
  175. // console.log("current:" + fromWebview.id + ",to:" + fromToLeft);
  176. // console.log("next:" + toWebview.id + ",from:" + toFromLeft);
  177. plus.webview.startAnimation({
  178. 'view': fromWebview,
  179. 'styles': {
  180. 'fromLeft': '0',
  181. 'toLeft': fromToLeft
  182. },
  183. 'action': 'show'
  184. }, {
  185. 'view': toWebview,
  186. 'styles': {
  187. 'fromLeft': toFromLeft,
  188. 'toLeft': '0'
  189. },
  190. 'action': 'show'
  191. },
  192. function(e) {
  193. //console.log("startAnimation callback...");
  194. if(e.id === toWebview.id) {
  195. isNew && plus.nativeUI.showWaiting();
  196. this.currentWebview = toWebview;
  197. this.onChange({
  198. index: toWebview.__mui_index
  199. });
  200. }
  201. }.bind(this)
  202. )
  203. };
  204. /**
  205. * @param {Object} id
  206. * @param {Object} webviewOptions
  207. */
  208. var webviewGroupContext = function(id, webviewOptions, groupContext) {
  209. this.id = id;
  210. this.url = webviewOptions.url;
  211. this.options = webviewOptions;
  212. this.groupContext = groupContext;
  213. this.webview = false;
  214. this.inited = false;
  215. };
  216. var _proto = webviewGroupContext.prototype;
  217. _proto.createWebview = function(from) {
  218. var options = this.options;
  219. options.styles = options.styles || {
  220. top: "33px",
  221. bottom: "0px",
  222. render: "always"
  223. };
  224. options.styles.popGesture = 'none';
  225. if(this.webview) {
  226. this.webview.setStyle(options.styles);
  227. for(var key in options.extras) {
  228. this.webview[key] = options.extras[key];
  229. }
  230. } else {
  231. options.styles.left = '100%';
  232. if(from !== 'startAnimation') {
  233. options.styles.left = '0';
  234. plus.nativeUI.showWaiting();
  235. }
  236. this.webview = plus.webview.create(this.url, this.id, options.styles, options.extras);
  237. //append进去,避免返回时闪屏
  238. plus.webview.currentWebview().append(this.webview);
  239. }
  240. this._initWebview();
  241. this.inited = true;
  242. };
  243. _proto._initWebview = function() {
  244. var options = this.options;
  245. if(!this.webview) {
  246. return;
  247. }
  248. this.webview.addEventListener('rendering', function() {
  249. setTimeout(function() {
  250. plus.nativeUI.closeWaiting();
  251. }, 500);
  252. });
  253. if(options.pullToRefresh && options.pullToRefresh.support && support.pullToRefresh()) {
  254. var callback = options.pullToRefresh.callback;
  255. this.webview.setPullToRefresh(options.pullToRefresh, function() {
  256. if(callback) { //如果指定了下拉回调
  257. callback(this.webview);
  258. } else { //下拉刷新回调,默认reload当前页面
  259. var self = this;
  260. var titleUpdate = function() {
  261. setTimeout(function() {
  262. self.webview.endPullToRefresh();
  263. }.bind(this), 1000);
  264. self.webview.removeEventListener('titleUpdate', titleUpdate);
  265. };
  266. this.webview.addEventListener('titleUpdate', titleUpdate);
  267. this.webview.reload();
  268. }
  269. }.bind(this));
  270. }
  271. };
  272. proto.removeWvgItem = function(id){
  273. var temp = plus.webview.getWebviewById(id);
  274. var itemStr = getItemStr();
  275. re = new RegExp('"', "g");
  276. var tempArr = itemStr.replace(re,'').split(",");
  277. var inArrIndex = $.inArray(id, tempArr);
  278. if(inArrIndex>-1){
  279. tempArr.splice(inArrIndex,1);
  280. //alert(tempArr);
  281. localStorage.setItem("initItem",tempArr);
  282. }
  283. //console.dir(this.webviews)
  284. // plus.webview.close(id)
  285. // alert(temp)
  286. }