bodyTab.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. @Author: 请叫我马哥
  3. @Time: 2017-04
  4. @Tittle: tab
  5. @Description: 点击对应按钮添加新窗口
  6. */
  7. var tabFilter, menu = [],
  8. liIndex, curNav, delMenu;
  9. layui.define(["element", "jquery"], function (exports) {
  10. var element = layui.element,
  11. $ = layui.jquery,
  12. layId,
  13. Tab = function () {
  14. this.tabConfig = {
  15. closed: true,
  16. openTabNum: 100,
  17. tabFilter: "bodyTab"
  18. }
  19. };
  20. var _this = this;
  21. var navs = layui.sessionData(window.hywa.sessionTable.tableName).userData.menus.subMenuList;
  22. //显示左侧菜单
  23. if ($(".navBar").html() == '') {
  24. $(".navBar").html(navBar(navs)).height($(window).height() - 60);
  25. $('.navBar li').find('cite').each(function () {
  26. if ($(this).text() == '系统维护') {
  27. $(this).parents('li').addClass('layui-this')
  28. }
  29. });
  30. element.init(); //初始化页面元素
  31. $(window).resize(function () {
  32. $(".navBar").height($(window).height() - 60);
  33. })
  34. }
  35. // console.log('地址', location.href.replace(/[^\/]+$/, ''))
  36. // let href = location.href.replace(/[^\/]+$/, '')
  37. // $.ajax({
  38. // url: href+'js/json/menu.json',
  39. // data: {
  40. // requestMode: 0
  41. // },
  42. // async: false,
  43. // success: function (res) {
  44. //
  45. // //layer.closeAll('loading'); //关闭加载层
  46. // if (!(res.retHead.errCode - 0)) {
  47. // navs = res.retBody.list;
  48. // if (!navs) {
  49. // return;
  50. // }
  51. // //显示左侧菜单
  52. // if ($(".navBar").html() == '') {
  53. // $(".navBar").html(navBar(navs)).height($(window).height() - 60);
  54. // $('.layui-nav-tree li:first-child').addClass('layui-this')
  55. // element.init(); //初始化页面元素
  56. // $(window).resize(function () {
  57. // $(".navBar").height($(window).height() - 60);
  58. // })
  59. // }
  60. // } else if ((res.retHead.errCode - 0) == 1004) {
  61. // top.location.href = window.hywa.jumpPage.login;
  62. // }
  63. // }
  64. // });
  65. //显示左侧菜单
  66. // if ($(".navBar").html() == '') {
  67. // $(".navBar").html(navBar(navs)).height($(window).height() - 230);
  68. // element.init(); //初始化页面元素
  69. // $(window).resize(function () {
  70. // $(".navBar").height($(window).height() - 230);
  71. // })
  72. // }
  73. //参数设置
  74. Tab.prototype.set = function (option) {
  75. var _this = this;
  76. $.extend(true, _this.tabConfig, option);
  77. return _this;
  78. };
  79. //通过title获取lay-id
  80. Tab.prototype.getLayId = function (title) {
  81. $(".layui-tab-title.top_tab li").each(function () {
  82. if ($(this).find("cite").text() == title) {
  83. layId = $(this).attr("lay-id");
  84. }
  85. })
  86. return layId;
  87. }
  88. //通过title判断tab是否存在
  89. Tab.prototype.hasTab = function (title) {
  90. var tabIndex = -1;
  91. $(".layui-tab-title.top_tab li").each(function () {
  92. if ($(this).find("cite").text() == title) {
  93. tabIndex = 1;
  94. }
  95. })
  96. return tabIndex;
  97. }
  98. //右侧内容tab操作
  99. var tabIdIndex = 0;
  100. Tab.prototype.tabAdd = function (_this) {
  101. if (window.sessionStorage.getItem("menu")) {
  102. menu = JSON.parse(window.sessionStorage.getItem("menu"));
  103. }
  104. var that = this;
  105. var closed = that.tabConfig.closed,
  106. openTabNum = that.tabConfig.openTabNum;
  107. tabFilter = that.tabConfig.tabFilter;
  108. // $(".layui-nav .layui-nav-item a").on("click",function(){
  109. if (_this.find("i.iconfont,i.layui-icon").attr("data-icon") != undefined) {
  110. var title = '';
  111. if (that.hasTab(_this.find("cite").text()) == -1 && _this.siblings("dl.layui-nav-child").length ==
  112. 0) {
  113. if ($(".layui-tab-title.top_tab li").length == openTabNum) {
  114. layer.msg('只能同时打开' + openTabNum + '个选项卡哦。不然系统会卡的!');
  115. return;
  116. }
  117. tabIdIndex++;
  118. // if (_this.find("i.iconfont").attr("data-icon") != undefined) {
  119. // title += '<i class="iconfont ' + _this.find("i.iconfont").attr("data-icon") + '"></i>';
  120. // } else {
  121. // title += '<i class="layui-icon">' + _this.find("i.layui-icon").attr("data-icon") +
  122. // '</i>';
  123. // }
  124. title += '<cite>' + _this.find("cite").text() + '</cite>';
  125. title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + tabIdIndex +
  126. '">&#x1006;</i>';
  127. element.tabAdd(tabFilter, {
  128. title: title,
  129. content: "<iframe src='" + _this.attr("data-url") + "' data-id='" + tabIdIndex +
  130. "'></frame>",
  131. id: new Date().getTime()
  132. })
  133. //当前窗口内容
  134. var curmenu = {
  135. "icon": _this.find("i.iconfont").attr("data-icon") != undefined ? _this.find(
  136. "i.iconfont").attr("data-icon") : _this.find("i.layui-icon").attr(
  137. "data-icon"),
  138. "title": _this.find("cite").text(),
  139. "href": _this.attr("data-url"),
  140. "layId": new Date().getTime()
  141. }
  142. menu.push(curmenu);
  143. window.sessionStorage.setItem("menu", JSON.stringify(menu)); //打开的窗口
  144. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu)); //当前的窗口
  145. element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
  146. } else {
  147. //当前窗口内容
  148. var curmenu = {
  149. "icon": _this.find("i.iconfont").attr("data-icon") != undefined ? _this.find(
  150. "i.iconfont").attr("data-icon") : _this.find("i.layui-icon").attr(
  151. "data-icon"),
  152. "title": _this.find("cite").text(),
  153. "href": _this.attr("data-url"),
  154. "layId": new Date().getTime()
  155. }
  156. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu)); //当前的窗口
  157. element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
  158. }
  159. }
  160. // })
  161. }
  162. $("body").on("click", ".top_tab li", function () {
  163. //切换后获取当前窗口的内容
  164. var curmenu = '';
  165. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  166. curmenu = menu[$(this).index() - 1];
  167. if ($(this).index() == 0) {
  168. window.sessionStorage.setItem("curmenu", '');
  169. } else {
  170. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu));
  171. if (window.sessionStorage.getItem("curmenu") == "undefined") {
  172. //如果删除的不是当前选中的tab,则将curmenu设置成当前选中的tab
  173. if (curNav != JSON.stringify(delMenu)) {
  174. window.sessionStorage.setItem("curmenu", curNav);
  175. } else {
  176. window.sessionStorage.setItem("curmenu", JSON.stringify(menu[liIndex - 1]));
  177. }
  178. }
  179. }
  180. element.tabChange(tabFilter, $(this).attr("lay-id")).init();
  181. })
  182. //删除tab
  183. $("body").on("click", ".top_tab li i.layui-tab-close", function () {
  184. //删除tab后重置session中的menu和curmenu
  185. liIndex = $(this).parent("li").index();
  186. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  187. //获取被删除元素
  188. delMenu = menu[liIndex - 1];
  189. var curmenu = window.sessionStorage.getItem("curmenu") == "undefined" ? "undefined" : window.sessionStorage
  190. .getItem("curmenu") == "" ? '' : JSON.parse(window.sessionStorage.getItem("curmenu"));
  191. if (JSON.stringify(curmenu) != JSON.stringify(menu[liIndex - 1])) { //如果删除的不是当前选中的tab
  192. // window.sessionStorage.setItem("curmenu",JSON.stringify(curmenu));
  193. curNav = JSON.stringify(curmenu);
  194. } else {
  195. if ($(this).parent("li").length > liIndex) {
  196. window.sessionStorage.setItem("curmenu", curmenu);
  197. curNav = curmenu;
  198. } else {
  199. window.sessionStorage.setItem("curmenu", JSON.stringify(menu[liIndex - 1]));
  200. curNav = JSON.stringify(menu[liIndex - 1]);
  201. }
  202. }
  203. menu.splice((liIndex - 1), 1);
  204. window.sessionStorage.setItem("menu", JSON.stringify(menu));
  205. element.tabDelete("bodyTab", $(this).parent("li").attr("lay-id")).init();
  206. });
  207. var bodyTab = new Tab();
  208. exports("bodyTab", function (option) {
  209. return bodyTab.set(option);
  210. });
  211. });