index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * 菜单处理
  3. */
  4. $(function() {
  5. // MetsiMenu
  6. $('#side-menu').metisMenu();
  7. //固定菜单栏
  8. $(function() {
  9. $('.sidebar-collapse').slimScroll({
  10. height: '100%',
  11. railOpacity: 0.9,
  12. alwaysVisible: false
  13. });
  14. });
  15. // 菜单切换
  16. $('.navbar-minimalize').click(function() {
  17. $("body").toggleClass("mini-navbar");
  18. SmoothlyMenu();
  19. });
  20. $('#side-menu>li').click(function() {
  21. if ($('body').hasClass('mini-navbar')) {
  22. NavToggle();
  23. }
  24. });
  25. $('#side-menu>li li a').click(function() {
  26. if ($(window).width() < 769) {
  27. NavToggle();
  28. }
  29. });
  30. $('.nav-close').click(NavToggle);
  31. //ios浏览器兼容性处理
  32. if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  33. $('#content-main').css('overflow-y', 'auto');
  34. }
  35. });
  36. $(window).bind("load resize",
  37. function() {
  38. if ($(this).width() < 769) {
  39. $('body').addClass('mini-navbar');
  40. $('.navbar-static-side').fadeIn();
  41. }
  42. });
  43. function NavToggle() {
  44. $('.navbar-minimalize').trigger('click');
  45. }
  46. function SmoothlyMenu() {
  47. if (!$('body').hasClass('mini-navbar')) {
  48. $('#side-menu').hide();
  49. setTimeout(function() {
  50. $('#side-menu').fadeIn(500);
  51. },
  52. 100);
  53. } else if ($('body').hasClass('fixed-sidebar')) {
  54. $('#side-menu').hide();
  55. setTimeout(function() {
  56. $('#side-menu').fadeIn(500);
  57. },
  58. 300);
  59. } else {
  60. $('#side-menu').removeAttr('style');
  61. }
  62. }
  63. /**
  64. * iframe处理
  65. */
  66. $(function() {
  67. //计算元素集合的总宽度
  68. function calSumWidth(elements) {
  69. var width = 0;
  70. $(elements).each(function() {
  71. width += $(this).outerWidth(true);
  72. });
  73. return width;
  74. }
  75. //滚动到指定选项卡
  76. function scrollToTab(element) {
  77. var marginLeftVal = calSumWidth($(element).prevAll()),
  78. marginRightVal = calSumWidth($(element).nextAll());
  79. // 可视区域非tab宽度
  80. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
  81. //可视区域tab宽度
  82. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  83. //实际滚动宽度
  84. var scrollVal = 0;
  85. if ($(".page-tabs-content").outerWidth() < visibleWidth) {
  86. scrollVal = 0;
  87. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  88. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  89. scrollVal = marginLeftVal;
  90. var tabElement = element;
  91. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
  92. scrollVal -= $(tabElement).prev().outerWidth();
  93. tabElement = $(tabElement).prev();
  94. }
  95. }
  96. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  97. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  98. }
  99. $('.page-tabs-content').animate({
  100. marginLeft: 0 - scrollVal + 'px'
  101. },
  102. "fast");
  103. }
  104. //查看左侧隐藏的选项卡
  105. function scrollTabLeft() {
  106. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  107. // 可视区域非tab宽度
  108. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
  109. //可视区域tab宽度
  110. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  111. //实际滚动宽度
  112. var scrollVal = 0;
  113. if ($(".page-tabs-content").width() < visibleWidth) {
  114. return false;
  115. } else {
  116. var tabElement = $(".menuTab:first");
  117. var offsetVal = 0;
  118. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) { //找到离当前tab最近的元素
  119. offsetVal += $(tabElement).outerWidth(true);
  120. tabElement = $(tabElement).next();
  121. }
  122. offsetVal = 0;
  123. if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
  124. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  125. offsetVal += $(tabElement).outerWidth(true);
  126. tabElement = $(tabElement).prev();
  127. }
  128. scrollVal = calSumWidth($(tabElement).prevAll());
  129. }
  130. }
  131. $('.page-tabs-content').animate({
  132. marginLeft: 0 - scrollVal + 'px'
  133. },
  134. "fast");
  135. }
  136. //查看右侧隐藏的选项卡
  137. function scrollTabRight() {
  138. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  139. // 可视区域非tab宽度
  140. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
  141. //可视区域tab宽度
  142. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  143. //实际滚动宽度
  144. var scrollVal = 0;
  145. if ($(".page-tabs-content").width() < visibleWidth) {
  146. return false;
  147. } else {
  148. var tabElement = $(".menuTab:first");
  149. var offsetVal = 0;
  150. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) { //找到离当前tab最近的元素
  151. offsetVal += $(tabElement).outerWidth(true);
  152. tabElement = $(tabElement).next();
  153. }
  154. offsetVal = 0;
  155. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  156. offsetVal += $(tabElement).outerWidth(true);
  157. tabElement = $(tabElement).next();
  158. }
  159. scrollVal = calSumWidth($(tabElement).prevAll());
  160. if (scrollVal > 0) {
  161. $('.page-tabs-content').animate({
  162. marginLeft: 0 - scrollVal + 'px'
  163. },
  164. "fast");
  165. }
  166. }
  167. }
  168. //通过遍历给菜单项加上data-index属性
  169. $(".menuItem").each(function(index) {
  170. if (!$(this).attr('data-index')) {
  171. $(this).attr('data-index', index);
  172. }
  173. });
  174. function menuItem() {
  175. // 获取标识数据
  176. var dataUrl = $(this).attr('href'),
  177. dataIndex = $(this).data('index'),
  178. menuName = $.trim($(this).text()),
  179. flag = true;
  180. if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
  181. // 选项卡菜单已存在
  182. $('.menuTab').each(function() {
  183. if ($(this).data('id') == dataUrl) {
  184. if (!$(this).hasClass('active')) {
  185. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  186. scrollToTab(this);
  187. // 显示tab对应的内容区
  188. $('.mainContent .RuoYi_iframe').each(function() {
  189. if ($(this).data('id') == dataUrl) {
  190. $(this).show().siblings('.RuoYi_iframe').hide();
  191. return false;
  192. }
  193. });
  194. }
  195. flag = false;
  196. return false;
  197. }
  198. });
  199. // 选项卡菜单不存在
  200. if (flag) {
  201. var str = '<a href="javascript:;" class="active menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  202. $('.menuTab').removeClass('active');
  203. // 添加选项卡对应的iframe
  204. var str1 = '<iframe class="RuoYi_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  205. $('.mainContent').find('iframe.RuoYi_iframe').hide().parents('.mainContent').append(str1);
  206. $.modal.loading("数据加载中,请稍后...");
  207. $('.mainContent iframe:visible').load(function () {
  208. $.modal.closeLoading();
  209. });
  210. // 添加选项卡
  211. $('.menuTabs .page-tabs-content').append(str);
  212. scrollToTab($('.menuTab.active'));
  213. }
  214. return false;
  215. }
  216. $('.menuItem').on('click', menuItem);
  217. // 关闭选项卡菜单
  218. function closeTab() {
  219. var closeTabId = $(this).parents('.menuTab').data('id');
  220. var currentWidth = $(this).parents('.menuTab').width();
  221. // 当前元素处于活动状态
  222. if ($(this).parents('.menuTab').hasClass('active')) {
  223. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  224. if ($(this).parents('.menuTab').next('.menuTab').size()) {
  225. var activeId = $(this).parents('.menuTab').next('.menuTab:eq(0)').data('id');
  226. $(this).parents('.menuTab').next('.menuTab:eq(0)').addClass('active');
  227. $('.mainContent .RuoYi_iframe').each(function() {
  228. if ($(this).data('id') == activeId) {
  229. $(this).show().siblings('.RuoYi_iframe').hide();
  230. return false;
  231. }
  232. });
  233. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  234. if (marginLeftVal < 0) {
  235. $('.page-tabs-content').animate({
  236. marginLeft: (marginLeftVal + currentWidth) + 'px'
  237. },
  238. "fast");
  239. }
  240. // 移除当前选项卡
  241. $(this).parents('.menuTab').remove();
  242. // 移除tab对应的内容区
  243. $('.mainContent .RuoYi_iframe').each(function() {
  244. if ($(this).data('id') == closeTabId) {
  245. $(this).remove();
  246. return false;
  247. }
  248. });
  249. }
  250. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  251. if ($(this).parents('.menuTab').prev('.menuTab').size()) {
  252. var activeId = $(this).parents('.menuTab').prev('.menuTab:last').data('id');
  253. $(this).parents('.menuTab').prev('.menuTab:last').addClass('active');
  254. $('.mainContent .RuoYi_iframe').each(function() {
  255. if ($(this).data('id') == activeId) {
  256. $(this).show().siblings('.RuoYi_iframe').hide();
  257. return false;
  258. }
  259. });
  260. // 移除当前选项卡
  261. $(this).parents('.menuTab').remove();
  262. // 移除tab对应的内容区
  263. $('.mainContent .RuoYi_iframe').each(function() {
  264. if ($(this).data('id') == closeTabId) {
  265. $(this).remove();
  266. return false;
  267. }
  268. });
  269. }
  270. }
  271. // 当前元素不处于活动状态
  272. else {
  273. // 移除当前选项卡
  274. $(this).parents('.menuTab').remove();
  275. // 移除相应tab对应的内容区
  276. $('.mainContent .RuoYi_iframe').each(function() {
  277. if ($(this).data('id') == closeTabId) {
  278. $(this).remove();
  279. return false;
  280. }
  281. });
  282. scrollToTab($('.menuTab.active'));
  283. }
  284. return false;
  285. }
  286. $('.menuTabs').on('click', '.menuTab i', closeTab);
  287. //关闭其他选项卡
  288. function closeOtherTabs() {
  289. $('.page-tabs-content').children("[data-id]").not(":first").not(".active").each(function() {
  290. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  291. $(this).remove();
  292. });
  293. $('.page-tabs-content').css("margin-left", "0");
  294. }
  295. $('.tabCloseOther').on('click', closeOtherTabs);
  296. //滚动到已激活的选项卡
  297. function showActiveTab() {
  298. scrollToTab($('.menuTab.active'));
  299. }
  300. $('.tabShowActive').on('click', showActiveTab);
  301. // 点击选项卡菜单
  302. function activeTab() {
  303. if (!$(this).hasClass('active')) {
  304. var currentId = $(this).data('id');
  305. // 显示tab对应的内容区
  306. $('.mainContent .RuoYi_iframe').each(function() {
  307. if ($(this).data('id') == currentId) {
  308. $(this).show().siblings('.RuoYi_iframe').hide();
  309. return false;
  310. }
  311. });
  312. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  313. scrollToTab(this);
  314. }
  315. }
  316. // 点击选项卡菜单
  317. $('.menuTabs').on('click', '.menuTab', activeTab);
  318. //刷新iframe
  319. function refreshTab() {
  320. var currentId = $('.page-tabs-content').find('.active').attr('data-id');
  321. var target = $('.RuoYi_iframe[data-id="' + currentId + '"]');
  322. var url = target.attr('src');
  323. target.attr('src', url).ready();
  324. }
  325. // 全屏显示
  326. $('#fullScreen').on('click', function () {
  327. $('#wrapper').fullScreen();
  328. });
  329. // 刷新按钮
  330. $('.tabReload').on('click', refreshTab);
  331. $('.menuTabs').on('dblclick', '.menuTab', refreshTab);
  332. // 左移按扭
  333. $('.tabLeft').on('click', scrollTabLeft);
  334. // 右移按扭
  335. $('.tabRight').on('click', scrollTabRight);
  336. // 关闭当前
  337. $('.tabCloseCurrent').on('click', function () {
  338. $('.page-tabs-content').find('.active i').trigger("click");
  339. });
  340. // 关闭全部
  341. $('.tabCloseAll').on('click', function() {
  342. $('.page-tabs-content').children("[data-id]").not(":first").each(function() {
  343. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  344. $(this).remove();
  345. });
  346. $('.page-tabs-content').children("[data-id]:first").each(function() {
  347. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').show();
  348. $(this).addClass("active");
  349. });
  350. $('.page-tabs-content').css("margin-left", "0");
  351. });
  352. });