index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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").removeClass("selected");
  204. $(this).parent("li").addClass("selected");
  205. if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
  206. // 选项卡菜单已存在
  207. $('.menuTab').each(function() {
  208. if ($(this).data('id') == dataUrl) {
  209. if (!$(this).hasClass('active')) {
  210. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  211. scrollToTab(this);
  212. // 显示tab对应的内容区
  213. $('.mainContent .RuoYi_iframe').each(function() {
  214. if ($(this).data('id') == dataUrl) {
  215. $(this).show().siblings('.RuoYi_iframe').hide();
  216. return false;
  217. }
  218. });
  219. }
  220. flag = false;
  221. return false;
  222. }
  223. });
  224. // 选项卡菜单不存在
  225. if (flag) {
  226. var str = '<a href="javascript:;" class="active menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  227. $('.menuTab').removeClass('active');
  228. // 添加选项卡对应的iframe
  229. var str1 = '<iframe class="RuoYi_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  230. $('.mainContent').find('iframe.RuoYi_iframe').hide().parents('.mainContent').append(str1);
  231. $.modal.loading("数据加载中,请稍后...");
  232. $('.mainContent iframe:visible').load(function () {
  233. $.modal.closeLoading();
  234. });
  235. // 添加选项卡
  236. $('.menuTabs .page-tabs-content').append(str);
  237. scrollToTab($('.menuTab.active'));
  238. }
  239. return false;
  240. }
  241. function menuBlank() {
  242. // 新窗口打开外网以http://开头,如http://ruoyi.vip
  243. var dataUrl = $(this).attr('href');
  244. window.open(dataUrl);
  245. return false;
  246. }
  247. $('.menuItem').on('click', menuItem);
  248. $('.menuBlank').on('click', menuBlank);
  249. // 关闭选项卡菜单
  250. function closeTab() {
  251. var closeTabId = $(this).parents('.menuTab').data('id');
  252. var currentWidth = $(this).parents('.menuTab').width();
  253. var panelUrl = $(this).parents('.menuTab').data('panel');
  254. // 当前元素处于活动状态
  255. if ($(this).parents('.menuTab').hasClass('active')) {
  256. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  257. if ($(this).parents('.menuTab').next('.menuTab').size()) {
  258. var activeId = $(this).parents('.menuTab').next('.menuTab:eq(0)').data('id');
  259. $(this).parents('.menuTab').next('.menuTab:eq(0)').addClass('active');
  260. $('.mainContent .RuoYi_iframe').each(function() {
  261. if ($(this).data('id') == activeId) {
  262. $(this).show().siblings('.RuoYi_iframe').hide();
  263. return false;
  264. }
  265. });
  266. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  267. if (marginLeftVal < 0) {
  268. $('.page-tabs-content').animate({
  269. marginLeft: (marginLeftVal + currentWidth) + 'px'
  270. },
  271. "fast");
  272. }
  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. }
  283. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  284. if ($(this).parents('.menuTab').prev('.menuTab').size()) {
  285. var activeId = $(this).parents('.menuTab').prev('.menuTab:last').data('id');
  286. $(this).parents('.menuTab').prev('.menuTab:last').addClass('active');
  287. $('.mainContent .RuoYi_iframe').each(function() {
  288. if ($(this).data('id') == activeId) {
  289. $(this).show().siblings('.RuoYi_iframe').hide();
  290. return false;
  291. }
  292. });
  293. // 移除当前选项卡
  294. $(this).parents('.menuTab').remove();
  295. // 移除tab对应的内容区
  296. $('.mainContent .RuoYi_iframe').each(function() {
  297. if ($(this).data('id') == closeTabId) {
  298. $(this).remove();
  299. return false;
  300. }
  301. });
  302. if($.common.isNotEmpty(panelUrl)){
  303. $('.menuTab[data-id="' + panelUrl + '"]').addClass('active').siblings('.menuTab').removeClass('active');
  304. $('.mainContent .RuoYi_iframe').each(function() {
  305. if ($(this).data('id') == panelUrl) {
  306. $(this).show().siblings('.RuoYi_iframe').hide();
  307. return false;
  308. }
  309. });
  310. }
  311. }
  312. }
  313. // 当前元素不处于活动状态
  314. else {
  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. }
  325. scrollToTab($('.menuTab.active'));
  326. return false;
  327. }
  328. $('.menuTabs').on('click', '.menuTab i', closeTab);
  329. //滚动到已激活的选项卡
  330. function showActiveTab() {
  331. scrollToTab($('.menuTab.active'));
  332. }
  333. $('.tabShowActive').on('click', showActiveTab);
  334. // 点击选项卡菜单
  335. function activeTab() {
  336. if (!$(this).hasClass('active')) {
  337. var currentId = $(this).data('id');
  338. // 显示tab对应的内容区
  339. $('.mainContent .RuoYi_iframe').each(function() {
  340. if ($(this).data('id') == currentId) {
  341. $(this).show().siblings('.RuoYi_iframe').hide();
  342. return false;
  343. }
  344. });
  345. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  346. scrollToTab(this);
  347. }
  348. }
  349. // 点击选项卡菜单
  350. $('.menuTabs').on('click', '.menuTab', activeTab);
  351. // 刷新iframe
  352. function refreshTab() {
  353. var currentId = $('.page-tabs-content').find('.active').attr('data-id');
  354. var target = $('.RuoYi_iframe[data-id="' + currentId + '"]');
  355. var url = target.attr('src');
  356. target.attr('src', url).ready();
  357. }
  358. // 关闭当前选项卡
  359. function tabCloseCurrent() {
  360. $('.page-tabs-content').find('.active i').trigger("click");
  361. }
  362. //关闭其他选项卡
  363. function tabCloseOther() {
  364. $('.page-tabs-content').children("[data-id]").not(":first").not(".active").each(function() {
  365. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  366. $(this).remove();
  367. });
  368. $('.page-tabs-content').css("margin-left", "0");
  369. }
  370. // 关闭全部选项卡
  371. function tabCloseAll() {
  372. $('.page-tabs-content').children("[data-id]").not(":first").each(function() {
  373. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  374. $(this).remove();
  375. });
  376. $('.page-tabs-content').children("[data-id]:first").each(function() {
  377. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').show();
  378. $(this).addClass("active");
  379. });
  380. $('.page-tabs-content').css("margin-left", "0");
  381. }
  382. // 全屏显示
  383. $('#fullScreen').on('click', function () {
  384. $(document).toggleFullScreen();
  385. });
  386. // 刷新按钮
  387. $('.tabReload').on('click', refreshTab);
  388. // 双击选项卡全屏显示
  389. $('.menuTabs').on('dblclick', '.menuTab', activeTabMax);
  390. // 左移按扭
  391. $('.tabLeft').on('click', scrollTabLeft);
  392. // 右移按扭
  393. $('.tabRight').on('click', scrollTabRight);
  394. // 关闭当前
  395. $('.tabCloseCurrent').on('click', tabCloseCurrent);
  396. // 关闭其他
  397. $('.tabCloseOther').on('click', tabCloseOther);
  398. // 关闭全部
  399. $('.tabCloseAll').on('click', tabCloseAll);
  400. // tab全屏显示
  401. $('.tabMaxCurrent').on('click', function () {
  402. $('.page-tabs-content').find('.active').trigger("dblclick");
  403. });
  404. // 关闭全屏
  405. $('#ax_close_max').click(function(){
  406. $('#content-main').toggleClass('max');
  407. $('#ax_close_max').hide();
  408. })
  409. // 双击选项卡全屏显示
  410. function activeTabMax() {
  411. $('#content-main').toggleClass('max');
  412. $('#ax_close_max').show();
  413. }
  414. $(window).keydown(function(event) {
  415. if (event.keyCode == 27) {
  416. $('#content-main').removeClass('max');
  417. $('#ax_close_max').hide();
  418. }
  419. });
  420. // 右键菜单实现
  421. $.contextMenu({
  422. selector: ".menuTab",
  423. trigger: 'right',
  424. autoHide: true,
  425. items: {
  426. "close_current": {
  427. name: "关闭当前",
  428. icon: "fa-close",
  429. callback: function(key, opt) {
  430. opt.$trigger.find('i').trigger("click");
  431. }
  432. },
  433. "close_other": {
  434. name: "关闭其他",
  435. icon: "fa-window-close-o",
  436. callback: function(key, opt) {
  437. setActiveTab(this);
  438. tabCloseOther();
  439. }
  440. },
  441. "close_left": {
  442. name: "关闭左侧",
  443. icon: "fa-reply",
  444. callback: function(key, opt) {
  445. setActiveTab(this);
  446. this.prevAll('.menuTab').not(":last").each(function() {
  447. if ($(this).hasClass('active')) {
  448. setActiveTab(this);
  449. }
  450. $('.RuoYi_iframe[data-id="' + $(this).data('id') + '"]').remove();
  451. $(this).remove();
  452. });
  453. $('.page-tabs-content').css("margin-left", "0");
  454. }
  455. },
  456. "close_right": {
  457. name: "关闭右侧",
  458. icon: "fa-share",
  459. callback: function(key, opt) {
  460. setActiveTab(this);
  461. this.nextAll('.menuTab').each(function() {
  462. $('.menuTab[data-id="' + $(this).data('id') + '"]').remove();
  463. $(this).remove();
  464. });
  465. }
  466. },
  467. "close_all": {
  468. name: "全部关闭",
  469. icon: "fa-window-close",
  470. callback: function(key, opt) {
  471. tabCloseAll();
  472. }
  473. },
  474. "step": "---------",
  475. "full": {
  476. name: "全屏显示",
  477. icon: "fa-arrows-alt",
  478. callback: function(key, opt) {
  479. setActiveTab(this);
  480. var target = $('.RuoYi_iframe[data-id="' + this.data('id') + '"]');
  481. target.fullScreen(true);
  482. }
  483. },
  484. "refresh": {
  485. name: "刷新页面",
  486. icon: "fa-refresh",
  487. callback: function(key, opt) {
  488. setActiveTab(this);
  489. var target = $('.RuoYi_iframe[data-id="' + this.data('id') + '"]');
  490. var url = target.attr('src');
  491. target.attr('src', url).ready();
  492. $.modal.loading("数据加载中,请稍后...");
  493. target.attr('src', url).load(function () {
  494. $.modal.closeLoading();
  495. });
  496. }
  497. },
  498. "open": {
  499. name: "新窗口打开",
  500. icon: "fa-link",
  501. callback: function(key, opt) {
  502. var target = $('.RuoYi_iframe[data-id="' + this.data('id') + '"]');
  503. window.open(target.attr('src'));
  504. }
  505. },
  506. }
  507. })
  508. });