bodyTab.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. var tabFilter, menu = [],
  2. liIndex, curNav, delMenu;
  3. layui.define(["element", "jquery"], function (exports) {
  4. var element = layui.element,
  5. $ = layui.jquery,
  6. layId,
  7. Tab = function () {
  8. this.tabConfig = {
  9. closed: true,
  10. openTabNum: 100,
  11. tabFilter: "bodyTab"
  12. }
  13. };
  14. //获取二级菜单数据
  15. Tab.prototype.render = function () {
  16. var _this = this;
  17. $.ajax({
  18. url: 'mainMenu?sysId=' + location.searchObj().sysId,
  19. data: {
  20. requestMode: 0
  21. },
  22. async: false,
  23. success: function (res) {
  24. //layer.closeAll('loading'); //关闭加载层
  25. if (!(res.retHead.errCode - 0)) {
  26. navs = res.retBody.list;
  27. if (!navs) {
  28. return;
  29. }
  30. //显示左侧菜单
  31. if ($(".navBar").html() == '') {
  32. $(".navBar").html(navBar(navs)).height($(window).height() - 60);
  33. element.init(); //初始化页面元素
  34. $(window).resize(function () {
  35. $(".navBar").height($(window).height() - 60);
  36. })
  37. }
  38. } else if ((res.retHead.errCode - 0) == 1004) {
  39. top.location.href = window.hywa.jumpPage.login;
  40. }
  41. }
  42. });
  43. }
  44. //参数设置
  45. Tab.prototype.set = function (option) {
  46. var _this = this;
  47. $.extend(true, _this.tabConfig, option);
  48. return _this;
  49. };
  50. //通过title获取lay-id
  51. Tab.prototype.getLayId = function (title) {
  52. $(".layui-tab-title.top_tab li").each(function () {
  53. if ($(this).find("cite").text() == title) {
  54. layId = $(this).attr("lay-id");
  55. }
  56. })
  57. return layId;
  58. }
  59. //通过title判断tab是否存在
  60. Tab.prototype.hasTab = function (title) {
  61. var tabIndex = -1;
  62. $(".layui-tab-title.top_tab li").each(function () {
  63. if ($(this).find("cite").text() == title) {
  64. tabIndex = 1;
  65. }
  66. })
  67. return tabIndex;
  68. }
  69. //右侧内容tab操作
  70. var tabIdIndex = 0;
  71. Tab.prototype.tabAdd = function (_this) {
  72. if (window.sessionStorage.getItem("menu")) {
  73. menu = JSON.parse(window.sessionStorage.getItem("menu"));
  74. }
  75. var that = this;
  76. var closed = that.tabConfig.closed,
  77. openTabNum = that.tabConfig.openTabNum;
  78. tabFilter = that.tabConfig.tabFilter;
  79. if (_this.find("i.iconfont,i.layui-icon").attr("data-icon") != undefined) {
  80. var title = '';
  81. if (that.hasTab(_this.find("cite").text()) == -1 && _this.siblings("dl.layui-nav-child").length ==
  82. 0) {
  83. if ($(".layui-tab-title.top_tab li").length == openTabNum) {
  84. layer.msg('只能同时打开' + openTabNum + '个选项卡哦。不然系统会卡的!');
  85. return;
  86. }
  87. tabIdIndex++;
  88. title += '<cite>' + _this.find("cite").text() + '</cite>';
  89. title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + tabIdIndex +
  90. '">&#x1006;</i>';
  91. element.tabAdd(tabFilter, {
  92. title: title,
  93. content: "<iframe src='views/" + _this.attr("data-url") + "' data-id='" + tabIdIndex +
  94. "'></frame>",
  95. id: new Date().getTime()
  96. })
  97. //当前窗口内容
  98. var curmenu = {
  99. "icon": _this.find("i.iconfont").attr("data-icon") != undefined ? _this.find(
  100. "i.iconfont").attr("data-icon") : _this.find("i.layui-icon").attr(
  101. "data-icon"),
  102. "title": _this.find("cite").text(),
  103. "href": _this.attr("data-url"),
  104. "layId": new Date().getTime()
  105. }
  106. menu.push(curmenu);
  107. window.sessionStorage.setItem("menu", JSON.stringify(menu)); //打开的窗口
  108. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu)); //当前的窗口
  109. element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
  110. that.tabMove(); //顶部窗口是否可滚动
  111. } else {
  112. //当前窗口内容
  113. var curmenu = {
  114. "icon": _this.find("i.iconfont").attr("data-icon") != undefined ? _this.find(
  115. "i.iconfont").attr("data-icon") : _this.find("i.layui-icon").attr(
  116. "data-icon"),
  117. "title": _this.find("cite").text(),
  118. "href": _this.attr("data-url"),
  119. "layId": new Date().getTime()
  120. }
  121. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu)); //当前的窗口
  122. element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
  123. that.tabMove(); //顶部窗口是否可滚动
  124. }
  125. }
  126. }
  127. //顶部窗口移动
  128. Tab.prototype.tabMove = function () {
  129. $(window).on("resize", function () {
  130. var topTabsBox = $("#top_tabs_box"),
  131. topTabsBoxWidth = $("#top_tabs_box").width(),
  132. topTabs = $("#top_tabs"),
  133. topTabsWidth = $("#top_tabs").width(),
  134. tabLi = topTabs.find("li.layui-this"),
  135. top_tabs = document.getElementById("top_tabs");;
  136. if (topTabsWidth > topTabsBoxWidth) {
  137. if (tabLi.position().left > topTabsBoxWidth || tabLi.position().left + topTabsBoxWidth > topTabsWidth) {
  138. topTabs.css("left", topTabsBoxWidth - topTabsWidth);
  139. } else {
  140. topTabs.css("left", -tabLi.position().left);
  141. }
  142. //拖动效果
  143. var flag = false;
  144. var cur = {
  145. x: 0,
  146. y: 0
  147. }
  148. var nx, dx, x;
  149. function down() {
  150. flag = true;
  151. var touch;
  152. if (event.touches) {
  153. touch = event.touches[0];
  154. } else {
  155. touch = event;
  156. }
  157. cur.x = touch.clientX;
  158. dx = top_tabs.offsetLeft;
  159. }
  160. function move() {
  161. var self = this;
  162. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  163. if (flag) {
  164. var touch;
  165. if (event.touches) {
  166. touch = event.touches[0];
  167. } else {
  168. touch = event;
  169. }
  170. nx = touch.clientX - cur.x;
  171. x = dx + nx;
  172. if (x > 0) {
  173. x = 0;
  174. } else {
  175. if (x < topTabsBoxWidth - topTabsWidth) {
  176. x = topTabsBoxWidth - topTabsWidth;
  177. } else {
  178. x = dx + nx;
  179. }
  180. }
  181. top_tabs.style.left = x + "px";
  182. //阻止页面的滑动默认事件
  183. document.addEventListener("touchmove", function () {
  184. event.preventDefault();
  185. }, false);
  186. }
  187. }
  188. //鼠标释放时候的函数
  189. function end() {
  190. flag = false;
  191. }
  192. //pc端拖动效果
  193. topTabs.on("mousedown", down);
  194. topTabs.on("mousemove", move);
  195. $(document).on("mouseup", end);
  196. //移动端拖动效果
  197. topTabs.on("touchstart", down);
  198. topTabs.on("touchmove", move);
  199. topTabs.on("touchend", end);
  200. } else {
  201. //移除pc端拖动效果
  202. topTabs.off("mousedown", down);
  203. topTabs.off("mousemove", move);
  204. topTabs.off("mouseup", end);
  205. //移除移动端拖动效果
  206. topTabs.off("touchstart", down);
  207. topTabs.off("touchmove", move);
  208. topTabs.off("touchend", end);
  209. topTabs.removeAttr("style");
  210. return false;
  211. }
  212. }).resize();
  213. }
  214. $("body").on("click", ".top_tab li", function () {
  215. //切换后获取当前窗口的内容
  216. var curmenu = '';
  217. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  218. curmenu = menu[$(this).index() - 1];
  219. if ($(this).index() == 0) {
  220. window.sessionStorage.setItem("curmenu", '');
  221. } else {
  222. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu));
  223. if (window.sessionStorage.getItem("curmenu") == "undefined") {
  224. //如果删除的不是当前选中的tab,则将curmenu设置成当前选中的tab
  225. if (curNav != JSON.stringify(delMenu)) {
  226. window.sessionStorage.setItem("curmenu", curNav);
  227. } else {
  228. window.sessionStorage.setItem("curmenu", JSON.stringify(menu[liIndex - 1]));
  229. }
  230. }
  231. }
  232. element.tabChange(tabFilter, $(this).attr("lay-id")).init();
  233. })
  234. //删除tab
  235. $("body").on("click", ".top_tab li i.layui-tab-close", function () {
  236. //删除tab后重置session中的menu和curmenu
  237. liIndex = $(this).parent("li").index();
  238. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  239. //获取被删除元素
  240. delMenu = menu[liIndex - 1];
  241. var curmenu = window.sessionStorage.getItem("curmenu") == "undefined" ? "undefined" : window.sessionStorage
  242. .getItem("curmenu") == "" ? '' : JSON.parse(window.sessionStorage.getItem("curmenu"));
  243. if (JSON.stringify(curmenu) != JSON.stringify(menu[liIndex - 1])) { //如果删除的不是当前选中的tab
  244. // window.sessionStorage.setItem("curmenu",JSON.stringify(curmenu));
  245. curNav = JSON.stringify(curmenu);
  246. } else {
  247. if ($(this).parent("li").length > liIndex) {
  248. window.sessionStorage.setItem("curmenu", curmenu);
  249. curNav = curmenu;
  250. } else {
  251. window.sessionStorage.setItem("curmenu", JSON.stringify(menu[liIndex - 1]));
  252. curNav = JSON.stringify(menu[liIndex - 1]);
  253. }
  254. }
  255. menu.splice((liIndex - 1), 1);
  256. window.sessionStorage.setItem("menu", JSON.stringify(menu));
  257. element.tabDelete("bodyTab", $(this).parent("li").attr("lay-id")).init();
  258. })
  259. var bodyTab = new Tab();
  260. exports("bodyTab", function (option) {
  261. return bodyTab.set(option);
  262. });
  263. })