main.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import $ from 'jquery';
  2. import Vue from 'vue';
  3. // import Vuex from 'vuex';
  4. import App from './components/app.vue';
  5. import lib from 'lib';
  6. import BDY from 'BDY';
  7. // import NProgress from './depend/nprogress/nprogress';
  8. // import User from './store/user';
  9. // Vue.use(Vuex);
  10. // const store = new Vuex.Store({
  11. // mutations: {},
  12. // actions: {},
  13. // getters: {},
  14. // modules: {
  15. // }
  16. // })
  17. Vue.component('loading', resolve => require(['./components/loading.vue'], resolve));
  18. Vue.component('hero-index', resolve => require(['./page/hero/index.vue'], resolve));
  19. Vue.component('equip-build', resolve => require(['./page/equip/build.vue'], resolve));
  20. Vue.component('equip-info', resolve => require(['./page/equip/info.vue'], resolve));
  21. Vue.component('ability-info', resolve => require(['./page/ability/info.vue'], resolve));
  22. Vue.component('match-info', resolve => require(['./page/match/info.vue'], resolve));
  23. Vue.component('match-detail', resolve => require(['./page/match/detail.vue'], resolve));
  24. var pages = {};
  25. var oldPage;
  26. var VM = new Vue({
  27. // store,
  28. el: "#app",
  29. data: {
  30. currentView: "",
  31. isForward: true,
  32. isInit: true,
  33. transitionName: "fade"
  34. },
  35. computed: {
  36. transitionMode () {
  37. return this.isForward ? "out-int" : "in-out";
  38. }
  39. },
  40. mounted() {
  41. var _self = this;
  42. var page = lib.getParam("page") || "hero-index";
  43. lib.setParam("page", page);
  44. _self._redirectPage(page);
  45. // 点击事件
  46. $('body').off(BDY.click + ".data-href").on(BDY.click + ".data-href", '[data-href]', function () {
  47. var $this = $(this);
  48. var href = $this.attr('data-href') || $this.attr('href');
  49. var clientParam = $this.attr('data-client');
  50. var replace = $this.attr('data-replace');
  51. if (clientParam != null) {
  52. _self.redirectClient(href, clientParam);
  53. } else if (replace != null) {
  54. _self.redirect2(href);
  55. } else {
  56. _self.redirect(href);
  57. }
  58. return false;
  59. });
  60. $('body').off(BDY.click + ".data-url").on(BDY.click + ".data-url", '[data-url]', function () {
  61. var $this = $(this);
  62. var href = $this.attr('data-url');
  63. location.href = href;
  64. return false;
  65. });
  66. window.onpopstate = function (event) {
  67. // 支持onpopstate
  68. var count = 0;
  69. for (var page in pages) {
  70. if (count++ > 1) {
  71. break;
  72. };
  73. }
  74. // 防止首次进入就触发onpopstate导致再次刷新
  75. if (count > 1) {
  76. var newPage = lib.getParam('page') || 'hero-index';
  77. _self._redirectPage(newPage);
  78. }
  79. }
  80. initActiveEvent();
  81. },
  82. methods: {
  83. afterEnter: function () {
  84. if (VM.isForward) {
  85. scrollTo(0, 0);
  86. } else {
  87. var scrollTop = pages[lib.getParam("page")]['scrollTop'] || 0;
  88. scrollTo(0, scrollTop);
  89. }
  90. VM.isForward = false;
  91. // NProgress.done();
  92. },
  93. _redirectPage(_page) {
  94. var _self = this;
  95. /** 当前页面不重复请求 */
  96. if (_self.currentView == _page) {
  97. return;
  98. }
  99. // var mainPage = ['hero-index', 'bbs-index', 'user-center'];
  100. // if (mainPage.indexOf(_page) > -1) {
  101. // $('.weui-tabbar').show();
  102. // $('.weui-bar__item_on').removeClass('weui-bar__item_on');
  103. // $('#' + _page).addClass('weui-bar__item_on');
  104. // } else {
  105. // $('.weui-tabbar').hide();
  106. // }
  107. pages[_page] = pages[_page] || {};
  108. // NProgress.start();
  109. if (oldPage) {
  110. pages[oldPage] = pages[oldPage] || {};
  111. //保存滚动条位置$pageswarp
  112. // pages[oldPage]['scrollTop'] = document.body.scrollTop;
  113. //console.log("oldPage:" + oldPage + ", scrollTop:" + pages[oldPage]['scrollTop']);
  114. // pages[oldPage]['height'] = $pageswarp.height();
  115. lib.clearRes();
  116. }
  117. _self.currentView = _page;
  118. oldPage = _page;
  119. },
  120. redirect(param) {
  121. this._redirect(param, true);
  122. },
  123. _redirect(param, causeHistory) {
  124. oldPage = this.currentView;
  125. pages[oldPage] = pages[oldPage] || {};
  126. pages[oldPage]['scrollTop'] = _getScrollTop();
  127. var values = lib.parseHash(param);
  128. this.isForward = true;
  129. lib.setParam(values, causeHistory);
  130. // 这里需要自己渲染
  131. this._redirectPage(values['page']);
  132. },
  133. redirect2(param) {
  134. var oldUrl = document.location.href;
  135. this._redirect(param, false);
  136. if (oldUrl != document.location.href) {
  137. var newPage = lib.getParam('page') || 'hero-index';
  138. this._redirectPage(newPage);
  139. }
  140. },
  141. redirectClient(param, clientParam, title) {
  142. var _self = this;
  143. oldPage = this.currentView;
  144. _self.redirect(param);
  145. },
  146. historyBack() {
  147. var curPage = this.currentView;
  148. pages[curPage] = pages[curPage] || {};
  149. pages[curPage]['scrollTop'] = 0;
  150. history.back();
  151. }
  152. }
  153. })
  154. function initActiveEvent() {
  155. var selector = "input[type='submit'], [data-active]";
  156. var $elem, $activeElem;
  157. var timeHandler = 0;
  158. var LONG_TAP_TIME = 500;
  159. $(document).off(BDY.touchstart + ".body").on(BDY.touchstart + ".body", function (e) {
  160. $elem = $(e.target);
  161. $activeElem = $elem;
  162. if (!$activeElem.filter(selector).length) {
  163. $activeElem = $activeElem.parents(selector);
  164. }
  165. var activeStyle = $activeElem.attr("data-active") || "active";
  166. $activeElem.addClass(activeStyle);
  167. timeHandler = lib.setTimeout(function () {
  168. $elem && $elem.trigger('longTap');
  169. _removeActive();
  170. }, LONG_TAP_TIME);
  171. });
  172. $(document).off("touchmove.body, scroll.body").on("touchmove.body, scroll.body", function (event) {
  173. clearTimeout(timeHandler);
  174. timeHandler = 0;
  175. _removeActive();
  176. $elem = null;
  177. });
  178. $(document).off("mouseout.body").on("mouseout.body", selector, function (event) {
  179. _removeActive();
  180. $elem = null;
  181. });
  182. window.onResume = document.createEvent('Event');
  183. window.onResume.initEvent('onResume', true, true);
  184. $(document).on('onResume', function () {
  185. console.log('onResume');
  186. });
  187. var lastTime = 0;
  188. $(document).off(BDY.touchend + ".body").on(BDY.touchend + ".body", function (e) {
  189. clearTimeout(timeHandler);
  190. timeHandler = 0;
  191. if ($elem) {
  192. var currentTime = (new Date()).getTime();
  193. var duration = currentTime - lastTime;
  194. var MIN_TRIGGER_INTERVAL = 300;
  195. if (duration > MIN_TRIGGER_INTERVAL) {
  196. $elem.trigger('touchclick');
  197. // 记录当前时间
  198. lastTime = currentTime;
  199. }
  200. }
  201. _removeActive();
  202. });
  203. function _removeActive() {
  204. if ($activeElem != null) {
  205. var activeStyle = $activeElem.attr("data-active") || "active";
  206. $activeElem.blur();
  207. $activeElem.removeClass(activeStyle);
  208. $activeElem = null;
  209. }
  210. $elem = null;
  211. }
  212. ['longTap', 'touchclick'].forEach(function (m) {
  213. $.fn[m] = function (callback) {
  214. return this.on(m, callback);
  215. };
  216. });
  217. }
  218. function _getScrollTop() {
  219. return document.documentElement.scrollTop || document.body && document.body.scrollTop;
  220. }