mui.init.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * mui.init
  3. * @param {type} $
  4. * @returns {undefined}
  5. */
  6. (function($) {
  7. $.global = $.options = {
  8. gestureConfig: {
  9. tap: true,
  10. doubletap: false,
  11. longtap: false,
  12. hold: false,
  13. flick: true,
  14. swipe: true,
  15. drag: true,
  16. pinch: false
  17. }
  18. };
  19. /**
  20. *
  21. * @param {type} options
  22. * @returns {undefined}
  23. */
  24. $.initGlobal = function(options) {
  25. $.options = $.extend(true, $.global, options);
  26. return this;
  27. };
  28. var inits = {};
  29. /**
  30. * 单页配置 初始化
  31. * @param {object} options
  32. */
  33. $.init = function(options) {
  34. $.options = $.extend(true, $.global, options || {});
  35. $.ready(function() {
  36. $.doAction('inits', function(index, init) {
  37. var isInit = !!(!inits[init.name] || init.repeat);
  38. if (isInit) {
  39. init.handle.call($);
  40. inits[init.name] = true;
  41. }
  42. });
  43. });
  44. return this;
  45. };
  46. /**
  47. * 增加初始化执行流程
  48. * @param {function} init
  49. */
  50. $.addInit = function(init) {
  51. return $.addAction('inits', init);
  52. };
  53. /**
  54. * 处理html5版本subpages
  55. */
  56. $.addInit({
  57. name: 'iframe',
  58. index: 100,
  59. handle: function() {
  60. var options = $.options;
  61. var subpages = options.subpages || [];
  62. if (!$.os.plus && subpages.length) {
  63. //暂时只处理单个subpage。后续可以考虑支持多个subpage
  64. createIframe(subpages[0]);
  65. }
  66. }
  67. });
  68. var createIframe = function(options) {
  69. var wrapper = document.createElement('div');
  70. wrapper.className = $.className('iframe-wrapper');
  71. var styles = options.styles || {};
  72. if (typeof styles.top !== 'string') {
  73. styles.top = '0px';
  74. }
  75. if (typeof styles.bottom !== 'string') {
  76. styles.bottom = '0px';
  77. }
  78. wrapper.style.top = styles.top;
  79. wrapper.style.bottom = styles.bottom;
  80. var iframe = document.createElement('iframe');
  81. iframe.src = options.url;
  82. iframe.id = options.id || options.url;
  83. iframe.name = iframe.id;
  84. wrapper.appendChild(iframe);
  85. document.body.appendChild(wrapper);
  86. //目前仅处理微信
  87. $.os.wechat && handleScroll(wrapper, iframe);
  88. };
  89. function handleScroll(wrapper, iframe) {
  90. var key = 'MUI_SCROLL_POSITION_' + document.location.href + '_' + iframe.src;
  91. var scrollTop = (parseFloat(localStorage.getItem(key)) || 0);
  92. if (scrollTop) {
  93. (function(y) {
  94. iframe.onload = function() {
  95. window.scrollTo(0, y);
  96. };
  97. })(scrollTop);
  98. }
  99. setInterval(function() {
  100. var _scrollTop = window.scrollY;
  101. if (scrollTop !== _scrollTop) {
  102. localStorage.setItem(key, _scrollTop + '');
  103. scrollTop = _scrollTop;
  104. }
  105. }, 100);
  106. };
  107. $(function() {
  108. var classList = document.body.classList;
  109. var os = [];
  110. if ($.os.ios) {
  111. os.push({
  112. os: 'ios',
  113. version: $.os.version
  114. });
  115. classList.add($.className('ios'));
  116. } else if ($.os.android) {
  117. os.push({
  118. os: 'android',
  119. version: $.os.version
  120. });
  121. classList.add($.className('android'));
  122. }
  123. if ($.os.wechat) {
  124. os.push({
  125. os: 'wechat',
  126. version: $.os.wechat.version
  127. });
  128. classList.add($.className('wechat'));
  129. }
  130. if (os.length) {
  131. $.each(os, function(index, osObj) {
  132. var version = '';
  133. var classArray = [];
  134. if (osObj.version) {
  135. $.each(osObj.version.split('.'), function(i, v) {
  136. version = version + (version ? '-' : '') + v;
  137. classList.add($.className(osObj.os + '-' + version));
  138. });
  139. }
  140. });
  141. }
  142. });
  143. })(mui);