media-menu.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * UI v1.1.0
  3. * Copyright 2017-2018 Muyao
  4. * Licensed under the Muyao License 1.0
  5. */
  6. (function (window, document, $) {
  7. 'use strict';
  8. /***
  9. * 左侧菜单响应式操作
  10. *
  11. * **/
  12. $.site.menubar = {
  13. opened: null,
  14. folded: null,
  15. top: false,
  16. foldAlt: false,
  17. auto: true,
  18. init: function () {
  19. var self = this,
  20. $body = this.$body = $('body'),
  21. $html = this.$html = $('html'),
  22. $instance = this.$instance = $("#admui-navTabs");
  23. $html.removeClass('css-menubar').addClass('js-menubar');
  24. this.tabId = $('#admui-navbar').find('li.active > a').attr('href');
  25. if (this.tabId === '#') {
  26. this.tabId = $('.nav-tabs li.active').find('ul>li.active>a').attr('href');
  27. }
  28. if (!$instance.length) {
  29. return;
  30. }
  31. // 鼠标经过左侧菜单显示图标
  32. if ($body.is('.site-menubar-fold-alt')) {
  33. this.foldAlt = true;
  34. }
  35. // 鼠标经过左侧菜单显示文字
  36. if ($body.is('.site-menubar-keep')) {
  37. if ($body.hasClass('site-menubar-fold')) { // 收起
  38. this.auto = 'fold';
  39. } else if ($body.hasClass('site-menubar-unfold')) { //展开
  40. this.auto = 'unfold';
  41. }
  42. }
  43. $instance.on('changed.site.menubar', function () {
  44. self.update();
  45. });
  46. $('.nav-tabs li:not(.no-menu)').on('shown.bs.tab', function (event) {
  47. var tabId = self.tabId = $(event.target).attr('href');
  48. if ($body.hasClass('site-menubar-fold')) {
  49. self.hoverscroll.enable(tabId);
  50. } else if ($body.hasClass('site-menubar-unfold')) {
  51. self.slimScroll.enable();
  52. }
  53. });
  54. this.change();
  55. },
  56. change: function () {
  57. var breakpoint = Breakpoints.current();
  58. if (this.auto !== true) {
  59. switch (this.auto) {
  60. case 'fold':
  61. this.reset();
  62. if (breakpoint.name === 'xs') {
  63. this.hide();
  64. } else {
  65. this.fold();
  66. }
  67. return;
  68. case 'unfold':
  69. this.reset();
  70. if (breakpoint.name === 'xs') {
  71. this.hide();
  72. } else {
  73. this.unfold();
  74. }
  75. return;
  76. }
  77. }
  78. this.reset();
  79. if (breakpoint) {
  80. switch (breakpoint.name) {
  81. case 'lg':
  82. this.unfold();
  83. break;
  84. case 'md':
  85. case 'sm':
  86. this.fold();
  87. break;
  88. case 'xs':
  89. this.hide();
  90. break;
  91. }
  92. }
  93. Breakpoints.on('xs', 'leave', function () {
  94. $('#admui-navbar').responsiveHorizontalTabs({
  95. tabParentSelector: '#admui-navTabs',
  96. fnCallback: function (el) {
  97. if ($('#admui-navbar').is(':visible')) {
  98. el.removeClass('is-load');
  99. }
  100. }
  101. });
  102. });
  103. },
  104. animate: function (doing, callback) {
  105. var self = this,
  106. $body = self.$body;
  107. $body.addClass('site-menubar-changing');
  108. doing.call(self);
  109. this.$instance.trigger('changing.site.menubar');
  110. callback.call(self);
  111. $body.removeClass('site-menubar-changing');
  112. self.$instance.trigger('changed.site.menubar');
  113. },
  114. reset: function () {
  115. this.opened = null;
  116. this.folded = null;
  117. this.$body.removeClass('site-menubar-hide site-menubar-open site-menubar-fold site-menubar-unfold');
  118. this.$html.removeClass('disable-scrolling');
  119. },
  120. open: function () {
  121. if (this.opened !== true) {
  122. this.animate(function () {
  123. this.$body.removeClass('site-menubar-hide').addClass('site-menubar-open site-menubar-unfold');
  124. this.opened = true;
  125. this.$html.addClass('disable-scrolling');
  126. }, function () {
  127. this.slimScroll.enable();
  128. });
  129. }
  130. },
  131. hide: function () {
  132. this.hoverscroll.disable();
  133. if (this.opened !== false) {
  134. this.animate(function () {
  135. this.$html.removeClass('disable-scrolling');
  136. this.$body.removeClass('site-menubar-open').addClass('site-menubar-hide site-menubar-unfold');
  137. this.opened = false;
  138. }, function () {
  139. this.slimScroll.enable();
  140. });
  141. }
  142. },
  143. unfold: function () {
  144. this.hoverscroll.disable();
  145. if (this.folded !== false) {
  146. this.animate(function () {
  147. this.$body.removeClass('site-menubar-fold').addClass('site-menubar-unfold');
  148. this.folded = false;
  149. }, function () {
  150. this.slimScroll.enable();
  151. });
  152. }
  153. },
  154. fold: function () {
  155. this.slimScroll.destroy();
  156. if (this.folded !== true) {
  157. this.animate(function () {
  158. this.$body.removeClass('site-menubar-unfold').addClass('site-menubar-fold');
  159. this.folded = true;
  160. }, function () {
  161. this.hoverscroll.enable(this.tabId);
  162. });
  163. }
  164. },
  165. toggle: function () {
  166. var breakpoint = Breakpoints.current(),
  167. folded = this.folded,
  168. opened = this.opened;
  169. switch (breakpoint.name) {
  170. case 'lg':
  171. if (folded === null || folded === false) {
  172. this.fold();
  173. } else {
  174. this.unfold();
  175. }
  176. break;
  177. case 'md':
  178. case 'sm':
  179. if (folded === null || folded === true) {
  180. this.unfold();
  181. } else {
  182. this.fold();
  183. }
  184. $('#admui-navbar').responsiveHorizontalTabs({
  185. tabParentSelector: '#admui-navTabs'
  186. });
  187. break;
  188. case 'xs':
  189. if (opened === null || opened === false) {
  190. this.open();
  191. } else {
  192. this.hide();
  193. }
  194. break;
  195. }
  196. },
  197. update: function () {
  198. this.hoverscroll.update();
  199. },
  200. slimScroll: {
  201. api: null,
  202. native: false,
  203. init: function () {
  204. // if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
  205. // this.native = true;
  206. // $body.addClass('site-menubar-native');
  207. // return;
  208. // }
  209. if ($('body').is('.site-menubar-native')) {
  210. this.native = true;
  211. return;
  212. }
  213. $.site.menubar.$instance.slimScroll($.po('slimScroll'));
  214. },
  215. enable: function () {
  216. if (this.native) {
  217. return;
  218. }
  219. this.init();
  220. },
  221. destroy: function () {
  222. $.site.menubar.$instance.slimScroll({destroy: true});
  223. $.site.menubar.$instance.removeAttr('style');
  224. }
  225. },
  226. hoverscroll: {
  227. api: null,
  228. init: function (tabId) {
  229. $.site.menubar.$instance.find(tabId).children('div').attr('style', '');
  230. this.api = $.site.menubar.$instance.find(tabId).asHoverScroll({
  231. namespace: 'hoverscorll',
  232. direction: 'vertical',
  233. list: '.site-menu',
  234. item: '> li',
  235. exception: '.site-menu-sub',
  236. fixed: false,
  237. boundary: 100,
  238. onEnter: function () {
  239. //$(this).siblings().removeClass('hover');
  240. //$(this).addClass('hover');
  241. },
  242. onLeave: function () {
  243. //$(this).removeClass('hover');
  244. }
  245. }).data('asHoverScroll');
  246. },
  247. update: function () {
  248. if (this.api) {
  249. this.api.update();
  250. }
  251. },
  252. enable: function (tabId) {
  253. if (tabId !== this.tabId) {
  254. this.tabId = tabId;
  255. this.init(tabId);
  256. } else {
  257. this.api.enable();
  258. }
  259. },
  260. disable: function () {
  261. if (this.api) {
  262. this.api.disable();
  263. }
  264. }
  265. }
  266. };
  267. })(window, document, jQuery);