admin.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. var click = device.mobile() ? 'touchstart' : 'click';
  2. $(function() {
  3. // 侧边栏操作按钮
  4. $(document).on(click, '#guide', function() {
  5. $(this).toggleClass('toggled');
  6. $('#sidebar').toggleClass('toggled');
  7. });
  8. // 侧边栏二级菜单
  9. $(document).on('click', '.sub-menu a', function() {
  10. $(this).next().slideToggle(200);
  11. $(this).parent().toggleClass('toggled');
  12. });
  13. // 个人资料
  14. $(document).on('click', '.s-profile a', function() {
  15. $(this).next().slideToggle(200);
  16. $(this).parent().toggleClass('toggled');
  17. });
  18. // Waves初始化
  19. Waves.displayEffect();
  20. // 滚动条初始化
  21. $('#sidebar').mCustomScrollbar({
  22. theme: 'minimal-dark',
  23. scrollInertia: 100,
  24. axis: 'yx',
  25. mouseWheel: {
  26. enable: true,
  27. axis: 'y',
  28. preventDefault: true
  29. }
  30. });
  31. });
  32. // iframe高度自适应
  33. function changeFrameHeight(ifm) {
  34. ifm.height = document.documentElement.clientHeight - 118;
  35. }
  36. function resizeFrameHeight() {
  37. $('.tab_iframe').css('height', document.documentElement.clientHeight - 118);
  38. $('md-tab-content').css('left', '0');
  39. }
  40. window.onresize = function() {
  41. resizeFrameHeight();
  42. initScrollShow();
  43. initScrollState();
  44. }
  45. // ========== 选项卡操作 ==========
  46. $(function() {
  47. // 选项卡点击
  48. $(document).on('click', '.content_tab li', function() {
  49. // 切换选项卡
  50. $('.content_tab li').removeClass('cur');
  51. $(this).addClass('cur');
  52. // 切换iframe
  53. $('.iframe').removeClass('cur');
  54. $('#iframe_' + $(this).data('index')).addClass('cur');
  55. var marginLeft = ($('#tabs').css('marginLeft').replace('px', ''));
  56. // 滚动到可视区域:在左侧
  57. if ($(this).position().left < marginLeft) {
  58. var left = $('.content_tab>ul').scrollLeft() + $(this).position().left - marginLeft;
  59. $('.content_tab>ul').animate({scrollLeft: left}, 200, function() {
  60. initScrollState();
  61. });
  62. }
  63. // 滚动到可视区域:在右侧
  64. if(($(this).position().left + $(this).width() - marginLeft) > document.getElementById('tabs').clientWidth) {
  65. var left = $('.content_tab>ul').scrollLeft() + (($(this).position().left + $(this).width() - marginLeft) - document.getElementById('tabs').clientWidth);
  66. $('.content_tab>ul').animate({scrollLeft: left}, 200, function() {
  67. initScrollState();
  68. });
  69. }
  70. });
  71. // 控制选项卡滚动位置
  72. $(document).on('click', '.tab_left>a', function() {
  73. $('.content_tab>ul').animate({scrollLeft: $('.content_tab>ul').scrollLeft() - 300}, 200, function() {
  74. initScrollState();
  75. });
  76. });
  77. // 向右箭头
  78. $(document).on('click', '.tab_right>a', function() {
  79. $('.content_tab>ul').animate({scrollLeft: $('.content_tab>ul').scrollLeft() + 300}, 200, function() {
  80. initScrollState();
  81. });
  82. });
  83. // 初始化箭头状态
  84. // 选项卡右键菜单
  85. var menu = new BootstrapMenu('.tabs li', {
  86. fetchElementData: function(item) {
  87. return item;
  88. },
  89. actionsGroups: [
  90. ['close', 'refresh'],
  91. ['closeOther', 'closeAll'],
  92. ['closeRight', 'closeLeft']
  93. ],
  94. actions: {
  95. close: {
  96. name: '关闭',
  97. iconClass: 'zmdi zmdi-close',
  98. onClick: function(item) {
  99. Tab.closeTab($(item));
  100. }
  101. },
  102. closeOther: {
  103. name: '关闭其他',
  104. iconClass: 'zmdi zmdi-arrow-split',
  105. onClick: function(item) {
  106. var index = $(item).data('index');
  107. $('.content_tab li').each(function() {
  108. if ($(this).data('index') != index) {
  109. Tab.closeTab($(this));
  110. }
  111. });
  112. }
  113. },
  114. closeAll: {
  115. name: '关闭全部',
  116. iconClass: 'zmdi zmdi-swap',
  117. onClick: function() {
  118. $('.content_tab li').each(function() {
  119. Tab.closeTab($(this));
  120. });
  121. }
  122. },
  123. closeRight: {
  124. name: '关闭右侧所有',
  125. iconClass: 'zmdi zmdi-arrow-right',
  126. onClick: function(item) {
  127. var index = $(item).data('index');
  128. $($('.content_tab li').toArray().reverse()).each(function() {
  129. if ($(this).data('index') != index) {
  130. Tab.closeTab($(this));
  131. } else {
  132. return false;
  133. }
  134. });
  135. }
  136. },
  137. closeLeft: {
  138. name: '关闭左侧所有',
  139. iconClass: 'zmdi zmdi-arrow-left',
  140. onClick: function(item) {
  141. var index = $(item).data('index');
  142. $('.content_tab li').each(function() {
  143. if ($(this).data('index') != index) {
  144. Tab.closeTab($(this));
  145. } else {
  146. return false;
  147. }
  148. });
  149. }
  150. },
  151. refresh: {
  152. name: '刷新',
  153. iconClass: 'zmdi zmdi-refresh',
  154. onClick: function(item) {
  155. var index = $(item).data('index');
  156. var $iframe = $('#iframe_' + index).find('iframe');
  157. $iframe.attr('src', $iframe.attr('src'));
  158. }
  159. }
  160. }
  161. });
  162. });
  163. // 选项卡对象
  164. var Tab = {
  165. addTab: function(title, url) {
  166. var index = url.replace('.', '_');
  167. // 如果存在选项卡,则激活,否则创建新选项卡
  168. if ($('#tab_' + index).length == 0) {
  169. // 添加选项卡
  170. $('.content_tab li').removeClass('cur');
  171. var tab = '<li id="tab_' + index +'" data-index="' + index + '" class="cur"><a class="waves-effect waves-light" href="javascript:;">' + title + '</a></li>';//<i class="zmdi zmdi-close"></i><
  172. $('.content_tab>ul').append(tab);
  173. // 添加iframe
  174. $('.iframe').removeClass('cur');
  175. var iframe = '<div id="iframe_' + index + '" class="iframe cur"><iframe class="tab_iframe" src="' + url + '" width="100%" frameborder="0" scrolling="auto" onload="changeFrameHeight(this)"></iframe></div>';
  176. $('.content_main').append(iframe);
  177. initScrollShow();
  178. $('.content_tab>ul').animate({scrollLeft: document.getElementById('tabs').scrollWidth - document.getElementById('tabs').clientWidth}, 200, function() {
  179. initScrollState();
  180. });
  181. } else {
  182. $('#tab_' + index).trigger('click');
  183. }
  184. // 关闭侧边栏
  185. $('#guide').trigger(click);
  186. },
  187. closeTab: function($item) {
  188. var closeable = $item.data('closeable');
  189. if (closeable != false) {
  190. // 如果当前时激活状态则关闭后激活左边选项卡
  191. if($item.hasClass('cur')) {
  192. $item.prev().trigger('click');
  193. }
  194. // 关闭当前选项卡
  195. var index = $item.data('index');
  196. $('#iframe_' + index).remove();
  197. $item.remove();
  198. }
  199. initScrollShow();
  200. }
  201. }
  202. function initScrollShow() {
  203. if (document.getElementById('tabs').scrollWidth > document.getElementById('tabs').clientWidth) {
  204. $('.content_tab').addClass('scroll');
  205. } else {
  206. $('.content_tab').removeClass('scroll');
  207. }
  208. }
  209. function initScrollState() {
  210. if ($('.content_tab>ul').scrollLeft() == 0) {
  211. $('.tab_left>a').removeClass('active');
  212. } else {
  213. $('.tab_left>a').addClass('active');
  214. }
  215. if (($('.content_tab>ul').scrollLeft() + document.getElementById('tabs').clientWidth) >= document.getElementById('tabs').scrollWidth) {
  216. $('.tab_right>a').removeClass('active');
  217. } else {
  218. $('.tab_right>a').addClass('active');
  219. }
  220. }