index.js 21 KB

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