index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /**
  2. * 首页方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. layer.config({
  6. extend: 'moon/style.css',
  7. skin: 'layer-ext-moon'
  8. });
  9. var isMobile = false;
  10. var sidebarHeight = isMobile ? '100%' : '96%';
  11. $(function() {
  12. // MetsiMenu
  13. $('#side-menu').metisMenu();
  14. // 固定菜单栏
  15. $('.sidebar-collapse').slimScroll({
  16. height: sidebarHeight,
  17. railOpacity: 0.9,
  18. alwaysVisible: false
  19. });
  20. // 菜单切换
  21. $('.navbar-minimalize').click(function() {
  22. if (isMobile) {
  23. $("body").toggleClass("canvas-menu");
  24. } else {
  25. $("body").toggleClass("mini-navbar");
  26. }
  27. SmoothlyMenu();
  28. });
  29. $('#side-menu>li').click(function() {
  30. if ($('body').hasClass('canvas-menu mini-navbar')) {
  31. NavToggle();
  32. }
  33. });
  34. $('#side-menu>li li a:not(:has(span))').click(function() {
  35. if ($(window).width() < 769) {
  36. NavToggle();
  37. }
  38. });
  39. $('.nav-close').click(NavToggle);
  40. //ios浏览器兼容性处理
  41. if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  42. $('#content-main').css('overflow-y', 'auto');
  43. }
  44. });
  45. $(window).bind("load resize", function() {
  46. isMobile = $.common.isMobile() || $(window).width() < 769;
  47. if (isMobile) {
  48. $('body').addClass('canvas-menu');
  49. $("body").removeClass("mini-navbar");
  50. $("nav .logo").addClass("hide");
  51. $(".slimScrollDiv").css({ "overflow": "hidden" });
  52. $('.navbar-static-side').fadeOut();
  53. } else {
  54. if($('body').hasClass('canvas-menu')) {
  55. $('body').addClass('fixed-sidebar');
  56. $('body').removeClass('canvas-menu');
  57. $("body").removeClass("mini-navbar");
  58. $("nav .logo").removeClass("hide");
  59. $(".slimScrollDiv").css({ "overflow": "visible" });
  60. $('.navbar-static-side').fadeIn();
  61. }
  62. }
  63. });
  64. function NavToggle() {
  65. $('.navbar-minimalize').trigger('click');
  66. }
  67. function fixedSidebar() {
  68. $('#side-menu').hide();
  69. $("nav .logo").addClass("hide");
  70. setTimeout(function() {
  71. $('#side-menu').fadeIn(500);
  72. },
  73. 100);
  74. }
  75. function SmoothlyMenu() {
  76. if (isMobile && !$('body').hasClass('canvas-menu')) {
  77. $('.navbar-static-side').fadeIn();
  78. fixedSidebar();
  79. } else if (!isMobile &&!$('body').hasClass('mini-navbar')) {
  80. fixedSidebar();
  81. $("nav .logo").removeClass("hide");
  82. } else if (isMobile && $('body').hasClass('fixed-sidebar')) {
  83. $('.navbar-static-side').fadeOut();
  84. fixedSidebar();
  85. } else if (!isMobile && $('body').hasClass('fixed-sidebar')) {
  86. fixedSidebar();
  87. } else {
  88. $('#side-menu').removeAttr('style');
  89. }
  90. }
  91. /**
  92. * iframe处理
  93. */
  94. $(function() {
  95. //计算元素集合的总宽度
  96. function calSumWidth(elements) {
  97. var width = 0;
  98. $(elements).each(function() {
  99. width += $(this).outerWidth(true);
  100. });
  101. return width;
  102. }
  103. // 激活指定选项卡
  104. function setActiveTab(element) {
  105. if (!$(element).hasClass('active')) {
  106. var currentId = $(element).data('id');
  107. // 显示tab对应的内容区
  108. $('.RuoYi_iframe').each(function() {
  109. if ($(this).data('id') == currentId) {
  110. $(this).show().siblings('.RuoYi_iframe').hide();
  111. }
  112. });
  113. $(element).addClass('active').siblings('.menuTab').removeClass('active');
  114. scrollToTab(element);
  115. }
  116. }
  117. //滚动到指定选项卡
  118. function scrollToTab(element) {
  119. var marginLeftVal = calSumWidth($(element).prevAll()),
  120. marginRightVal = calSumWidth($(element).nextAll());
  121. // 可视区域非tab宽度
  122. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
  123. //可视区域tab宽度
  124. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  125. //实际滚动宽度
  126. var scrollVal = 0;
  127. if ($(".page-tabs-content").outerWidth() < visibleWidth) {
  128. scrollVal = 0;
  129. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  130. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  131. scrollVal = marginLeftVal;
  132. var tabElement = element;
  133. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
  134. scrollVal -= $(tabElement).prev().outerWidth();
  135. tabElement = $(tabElement).prev();
  136. }
  137. }
  138. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  139. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  140. }
  141. $('.page-tabs-content').animate({
  142. marginLeft: 0 - scrollVal + 'px'
  143. },
  144. "fast");
  145. }
  146. //查看左侧隐藏的选项卡
  147. function scrollTabLeft() {
  148. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  149. // 可视区域非tab宽度
  150. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
  151. //可视区域tab宽度
  152. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  153. //实际滚动宽度
  154. var scrollVal = 0;
  155. if (($(".page-tabs-content").width()) < visibleWidth) {
  156. return false;
  157. } else {
  158. var tabElement = $(".menuTab:first");
  159. var offsetVal = 0;
  160. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) { //找到离当前tab最近的元素
  161. offsetVal += $(tabElement).outerWidth(true);
  162. tabElement = $(tabElement).next();
  163. }
  164. offsetVal = 0;
  165. if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
  166. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  167. offsetVal += $(tabElement).outerWidth(true);
  168. tabElement = $(tabElement).prev();
  169. }
  170. scrollVal = calSumWidth($(tabElement).prevAll());
  171. }
  172. }
  173. $('.page-tabs-content').animate({
  174. marginLeft: 0 - scrollVal + 'px'
  175. },
  176. "fast");
  177. }
  178. //查看右侧隐藏的选项卡
  179. function scrollTabRight() {
  180. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  181. // 可视区域非tab宽度
  182. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
  183. //可视区域tab宽度
  184. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  185. //实际滚动宽度
  186. var scrollVal = 0;
  187. if ($(".page-tabs-content").width() < visibleWidth) {
  188. return false;
  189. } else {
  190. var tabElement = $(".menuTab:first");
  191. var offsetVal = 0;
  192. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) { //找到离当前tab最近的元素
  193. offsetVal += $(tabElement).outerWidth(true);
  194. tabElement = $(tabElement).next();
  195. }
  196. offsetVal = 0;
  197. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  198. offsetVal += $(tabElement).outerWidth(true);
  199. tabElement = $(tabElement).next();
  200. }
  201. scrollVal = calSumWidth($(tabElement).prevAll());
  202. if (scrollVal > 0) {
  203. $('.page-tabs-content').animate({
  204. marginLeft: 0 - scrollVal + 'px'
  205. },
  206. "fast");
  207. }
  208. }
  209. }
  210. //通过遍历给菜单项加上data-index属性
  211. $(".menuItem").each(function(index) {
  212. if (!$(this).attr('data-index')) {
  213. $(this).attr('data-index', index);
  214. }
  215. });
  216. function menuItem() {
  217. // 获取标识数据
  218. var dataUrl = $(this).attr('href'),
  219. dataIndex = $(this).data('index'),
  220. menuName = $.trim($(this).text()),
  221. flag = true;
  222. if (!$('a[href$="' + dataUrl + '"]').hasClass("noactive")) {
  223. $(".nav ul li, .nav li").removeClass("selected");
  224. $(this).parent("li").addClass("selected");
  225. }
  226. setIframeUrl(dataUrl);
  227. if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
  228. // 选项卡菜单已存在
  229. $('.menuTab').each(function() {
  230. if ($(this).data('id') == dataUrl) {
  231. if (!$(this).hasClass('active')) {
  232. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  233. scrollToTab(this);
  234. // 显示tab对应的内容区
  235. $('.mainContent .RuoYi_iframe').each(function() {
  236. if ($(this).data('id') == dataUrl) {
  237. $(this).show().siblings('.RuoYi_iframe').hide();
  238. return false;
  239. }
  240. });
  241. }
  242. flag = false;
  243. return false;
  244. }
  245. });
  246. // 选项卡菜单不存在
  247. if (flag) {
  248. var str = '<a href="javascript:;" class="active menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  249. $('.menuTab').removeClass('active');
  250. // 添加选项卡对应的iframe
  251. var str1 = '<iframe class="RuoYi_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  252. $('.mainContent').find('iframe.RuoYi_iframe').hide().parents('.mainContent').append(str1);
  253. $.modal.loading("数据加载中,请稍后...");
  254. $('.mainContent iframe:visible').load(function () {
  255. $.modal.closeLoading();
  256. });
  257. // 添加选项卡
  258. $('.menuTabs .page-tabs-content').append(str);
  259. scrollToTab($('.menuTab.active'));
  260. }
  261. return false;
  262. }
  263. function menuBlank() {
  264. // 新窗口打开外网以http://开头,如http://ruoyi.vip
  265. var dataUrl = $(this).attr('href');
  266. window.open(dataUrl);
  267. return false;
  268. }
  269. $('.menuItem').on('click', menuItem);
  270. $('.menuBlank').on('click', menuBlank);
  271. // 关闭选项卡菜单
  272. function closeTab() {
  273. var closeTabId = $(this).parents('.menuTab').data('id');
  274. var currentWidth = $(this).parents('.menuTab').width();
  275. var panelUrl = $(this).parents('.menuTab').data('panel');
  276. // 当前元素处于活动状态
  277. if ($(this).parents('.menuTab').hasClass('active')) {
  278. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  279. if ($(this).parents('.menuTab').next('.menuTab').size()) {
  280. var activeId = $(this).parents('.menuTab').next('.menuTab:eq(0)').data('id');
  281. $(this).parents('.menuTab').next('.menuTab:eq(0)').addClass('active');
  282. $('.mainContent .RuoYi_iframe').each(function() {
  283. if ($(this).data('id') == activeId) {
  284. $(this).show().siblings('.RuoYi_iframe').hide();
  285. return false;
  286. }
  287. });
  288. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  289. if (marginLeftVal < 0) {
  290. $('.page-tabs-content').animate({
  291. marginLeft: (marginLeftVal + currentWidth) + 'px'
  292. },
  293. "fast");
  294. }
  295. // 移除当前选项卡
  296. $(this).parents('.menuTab').remove();
  297. // 移除tab对应的内容区
  298. $('.mainContent .RuoYi_iframe').each(function() {
  299. if ($(this).data('id') == closeTabId) {
  300. $(this).remove();
  301. return false;
  302. }
  303. });
  304. }
  305. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  306. if ($(this).parents('.menuTab').prev('.menuTab').size()) {
  307. var activeId = $(this).parents('.menuTab').prev('.menuTab:last').data('id');
  308. $(this).parents('.menuTab').prev('.menuTab:last').addClass('active');
  309. $('.mainContent .RuoYi_iframe').each(function() {
  310. if ($(this).data('id') == activeId) {
  311. $(this).show().siblings('.RuoYi_iframe').hide();
  312. return false;
  313. }
  314. });
  315. // 移除当前选项卡
  316. $(this).parents('.menuTab').remove();
  317. // 移除tab对应的内容区
  318. $('.mainContent .RuoYi_iframe').each(function() {
  319. if ($(this).data('id') == closeTabId) {
  320. $(this).remove();
  321. return false;
  322. }
  323. });
  324. if($.common.isNotEmpty(panelUrl)){
  325. $('.menuTab[data-id="' + panelUrl + '"]').addClass('active').siblings('.menuTab').removeClass('active');
  326. $('.mainContent .RuoYi_iframe').each(function() {
  327. if ($(this).data('id') == panelUrl) {
  328. $(this).show().siblings('.RuoYi_iframe').hide();
  329. return false;
  330. }
  331. });
  332. }
  333. }
  334. }
  335. // 当前元素不处于活动状态
  336. else {
  337. // 移除当前选项卡
  338. $(this).parents('.menuTab').remove();
  339. // 移除相应tab对应的内容区
  340. $('.mainContent .RuoYi_iframe').each(function() {
  341. if ($(this).data('id') == closeTabId) {
  342. $(this).remove();
  343. return false;
  344. }
  345. });
  346. }
  347. scrollToTab($('.menuTab.active'));
  348. setIframeUrl($('.page-tabs-content').find('.active').attr('data-id'));
  349. return false;
  350. }
  351. $('.menuTabs').on('click', '.menuTab i', closeTab);
  352. //滚动到已激活的选项卡
  353. function showActiveTab() {
  354. scrollToTab($('.menuTab.active'));
  355. }
  356. $('.tabShowActive').on('click', showActiveTab);
  357. // 点击选项卡菜单
  358. function activeTab() {
  359. if (!$(this).hasClass('active')) {
  360. var currentId = $(this).data('id');
  361. // 显示tab对应的内容区
  362. $('.mainContent .RuoYi_iframe').each(function() {
  363. if ($(this).data('id') == currentId) {
  364. $(this).show().siblings('.RuoYi_iframe').hide();
  365. return false;
  366. }
  367. });
  368. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  369. scrollToTab(this);
  370. }
  371. }
  372. // 点击选项卡菜单
  373. $('.menuTabs').on('click', '.menuTab', activeTab);
  374. // 刷新iframe
  375. function refreshTab() {
  376. var currentId = $('.page-tabs-content').find('.active').attr('data-id');
  377. var target = $('.RuoYi_iframe[data-id="' + currentId + '"]');
  378. var url = target.attr('src');
  379. target.attr('src', url).ready();
  380. }
  381. // 页签全屏
  382. function fullScreenTab() {
  383. var currentId = $('.page-tabs-content').find('.active').attr('data-id');
  384. var target = $('.RuoYi_iframe[data-id="' + currentId + '"]');
  385. target.fullScreen(true);
  386. }
  387. // 关闭当前选项卡
  388. function tabCloseCurrent() {
  389. $('.page-tabs-content').find('.active i').trigger("click");
  390. }
  391. //关闭其他选项卡
  392. function tabCloseOther() {
  393. $('.page-tabs-content').children("[data-id]").not(":first").not(".active").each(function() {
  394. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  395. $(this).remove();
  396. });
  397. $('.page-tabs-content').css("margin-left", "0");
  398. setIframeUrl($('.page-tabs-content').find('.active').attr('data-id'));
  399. }
  400. // 关闭全部选项卡
  401. function tabCloseAll() {
  402. $('.page-tabs-content').children("[data-id]").not(":first").each(function() {
  403. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  404. $(this).remove();
  405. });
  406. $('.page-tabs-content').children("[data-id]:first").each(function() {
  407. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').show();
  408. $(this).addClass("active");
  409. });
  410. $('.page-tabs-content').css("margin-left", "0");
  411. setIframeUrl($('.page-tabs-content').find('.active').attr('data-id'));
  412. }
  413. // 全屏显示
  414. $('#fullScreen').on('click', function () {
  415. $(document).toggleFullScreen();
  416. });
  417. // 页签刷新按钮
  418. $('.tabReload').on('click', refreshTab);
  419. // 页签全屏按钮
  420. $('.tabFullScreen').on('click', fullScreenTab);
  421. // 双击选项卡全屏显示
  422. $('.menuTabs').on('dblclick', '.menuTab', activeTabMax);
  423. // 左移按扭
  424. $('.tabLeft').on('click', scrollTabLeft);
  425. // 右移按扭
  426. $('.tabRight').on('click', scrollTabRight);
  427. // 关闭当前
  428. $('.tabCloseCurrent').on('click', tabCloseCurrent);
  429. // 关闭其他
  430. $('.tabCloseOther').on('click', tabCloseOther);
  431. // 关闭全部
  432. $('.tabCloseAll').on('click', tabCloseAll);
  433. // tab全屏显示
  434. $('.tabMaxCurrent').on('click', function () {
  435. $('.page-tabs-content').find('.active').trigger("dblclick");
  436. });
  437. // 关闭全屏
  438. $('#ax_close_max').click(function(){
  439. $('#content-main').toggleClass('max');
  440. $('#ax_close_max').hide();
  441. })
  442. // 双击选项卡全屏显示
  443. function activeTabMax() {
  444. $('#content-main').toggleClass('max');
  445. $('#ax_close_max').show();
  446. }
  447. // 设置锚点
  448. function setIframeUrl(href) {
  449. if($.common.equals("history", mode)) {
  450. storage.set('publicPath', href);
  451. } else {
  452. var nowUrl = window.location.href;
  453. var newUrl = nowUrl.substring(0, nowUrl.indexOf("#"));
  454. window.location.href = newUrl + "#" + href;
  455. }
  456. }
  457. $(window).keydown(function(event) {
  458. if (event.keyCode == 27) {
  459. $('#content-main').removeClass('max');
  460. $('#ax_close_max').hide();
  461. }
  462. });
  463. window.onhashchange = function() {
  464. var hash = location.hash;
  465. var url = hash.substring(1, hash.length);
  466. $('a[href$="' + url + '"]').click();
  467. };
  468. // 右键菜单实现
  469. $.contextMenu({
  470. selector: ".menuTab",
  471. trigger: 'right',
  472. autoHide: true,
  473. items: {
  474. "close_current": {
  475. name: "关闭当前",
  476. icon: "fa-close",
  477. callback: function(key, opt) {
  478. opt.$trigger.find('i').trigger("click");
  479. }
  480. },
  481. "close_other": {
  482. name: "关闭其他",
  483. icon: "fa-window-close-o",
  484. callback: function(key, opt) {
  485. setActiveTab(this);
  486. tabCloseOther();
  487. }
  488. },
  489. "close_left": {
  490. name: "关闭左侧",
  491. icon: "fa-reply",
  492. callback: function(key, opt) {
  493. setActiveTab(this);
  494. this.prevAll('.menuTab').not(":last").each(function() {
  495. if ($(this).hasClass('active')) {
  496. setActiveTab(this);
  497. }
  498. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  499. $(this).remove();
  500. });
  501. $('.page-tabs-content').css("margin-left", "0");
  502. }
  503. },
  504. "close_right": {
  505. name: "关闭右侧",
  506. icon: "fa-share",
  507. callback: function(key, opt) {
  508. setActiveTab(this);
  509. this.nextAll('.menuTab').each(function() {
  510. $('.menuTab[data-id="' + $(this).data('id') + '"]').remove();
  511. $(this).remove();
  512. });
  513. }
  514. },
  515. "close_all": {
  516. name: "全部关闭",
  517. icon: "fa-window-close",
  518. callback: function(key, opt) {
  519. tabCloseAll();
  520. }
  521. },
  522. "step": "---------",
  523. "full": {
  524. name: "全屏显示",
  525. icon: "fa-arrows-alt",
  526. callback: function(key, opt) {
  527. setActiveTab(this);
  528. var target = $('.RuoYi_iframe[data-id="' + this.data('id') + '"]');
  529. target.fullScreen(true);
  530. }
  531. },
  532. "refresh": {
  533. name: "刷新页面",
  534. icon: "fa-refresh",
  535. callback: function(key, opt) {
  536. setActiveTab(this);
  537. var target = $('.RuoYi_iframe[data-id="' + this.data('id') + '"]');
  538. var url = target.attr('src');
  539. $.modal.loading("数据加载中,请稍后...");
  540. target.attr('src', url).load(function () {
  541. $.modal.closeLoading();
  542. });
  543. }
  544. },
  545. "open": {
  546. name: "新窗口打开",
  547. icon: "fa-link",
  548. callback: function(key, opt) {
  549. var target = $('.RuoYi_iframe[data-id="' + this.data('id') + '"]');
  550. window.open(target.attr('src'));
  551. }
  552. },
  553. }
  554. })
  555. });