bodyTab.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. @Author:
  3. @Time: 2017-10
  4. @Tittle: bodyTab
  5. @Description: 点击对应按钮添加新窗口
  6. */
  7. var tabFilter,menu=[],liIndex,curNav,delMenu,
  8. changeRefreshStr = window.sessionStorage.getItem("changeRefresh");
  9. layui.define(["element","jquery"],function(exports){
  10. var element = layui.element,
  11. $ = layui.$,
  12. layId,
  13. Tab = function(){
  14. this.tabConfig = {
  15. openTabNum : undefined, //最大可打开窗口数量
  16. tabFilter : "bodyTab", //添加窗口的filter
  17. url : undefined //获取菜单json地址
  18. }
  19. };
  20. //生成左侧菜单
  21. Tab.prototype.navBar = function(strData){
  22. var data;
  23. if(typeof(strData) == "string"){
  24. var data = JSON.parse(strData); //部分用户解析出来的是字符串,转换一下
  25. console.log(data)
  26. }else{
  27. data = strData;
  28. }
  29. var ulHtml = '';
  30. for(var i=0;i<data.length;i++){
  31. if(data[i].spread || data[i].spread == undefined){
  32. ulHtml += '<li class="layui-nav-item ">';
  33. }else{
  34. ulHtml += '<li class="layui-nav-item">';
  35. }
  36. if(data[i].children != undefined && data[i].children.length > 0){
  37. ulHtml += '<a>';
  38. if(data[i].icon != undefined && data[i].icon != ''){
  39. if(data[i].icon.indexOf("icon-") != -1){
  40. ulHtml += '<i class="seraph '+data[i].icon+'" data-icon="'+data[i].icon+'"></i>';
  41. }else{
  42. ulHtml += '<i class="layui-icon" data-icon="'+data[i].icon+'">'+data[i].icon+'</i>';
  43. }
  44. }
  45. ulHtml += '<cite>'+data[i].title+'</cite>';
  46. ulHtml += '<span class="layui-nav-more"></span>';
  47. ulHtml += '</a>';
  48. ulHtml += '<dl class="layui-nav-child">';
  49. for(var j=0;j<data[i].children.length;j++){
  50. if(data[i].children[j].target == "_blank"){
  51. ulHtml += '<dd><a data-url="'+data[i].children[j].href+'" target="'+data[i].children[j].target+'">';
  52. }else{
  53. ulHtml += '<dd><a data-url="'+data[i].children[j].href+'">';
  54. }
  55. if(data[i].children[j].icon != undefined && data[i].children[j].icon != ''){
  56. if(data[i].children[j].icon.indexOf("icon-") != -1){
  57. ulHtml += '<i class="seraph '+data[i].children[j].icon+'" data-icon="'+data[i].children[j].icon+'"></i>';
  58. }else{
  59. ulHtml += '<i class="layui-icon" data-icon="'+data[i].children[j].icon+'">'+data[i].children[j].icon+'</i>';
  60. }
  61. }
  62. ulHtml += '<cite>'+data[i].children[j].title+'</cite></a></dd>';
  63. }
  64. ulHtml += "</dl>";
  65. }else{
  66. if(data[i].target == "_blank"){
  67. ulHtml += '<a data-url="'+data[i].href+'" target="'+data[i].target+'">';
  68. }else{
  69. ulHtml += '<a data-url="'+data[i].href+'">';
  70. }
  71. if(data[i].icon != undefined && data[i].icon != ''){
  72. if(data[i].icon.indexOf("icon-") != -1){
  73. ulHtml += '<i class="seraph '+data[i].icon+'" data-icon="'+data[i].icon+'"></i>';
  74. }else{
  75. ulHtml += '<i class="layui-icon" data-icon="'+data[i].icon+'">'+data[i].icon+'</i>';
  76. }
  77. }
  78. ulHtml += '<cite>'+data[i].title+'</cite></a>';
  79. }
  80. ulHtml += '</li>';
  81. }
  82. return ulHtml;
  83. }
  84. //获取二级菜单数据
  85. Tab.prototype.render = function() {
  86. //显示左侧菜单
  87. var _this = this;
  88. $(".navBar ul").html('<li class="layui-nav-item layui-this"><a data-url="page/main.html"><i class="layui-icon" data-icon=""></i><cite>后台首页</cite></a></li>').append(_this.navBar(dataStr)).height($(window).height()-210);
  89. element.init(); //初始化页面元素
  90. $(window).resize(function(){
  91. $(".navBar").height($(window).height()-210);
  92. })
  93. }
  94. //是否点击窗口切换刷新页面
  95. Tab.prototype.changeRegresh = function(index){
  96. if(changeRefreshStr == "true"){
  97. $(".clildFrame .layui-tab-item").eq(index).find("iframe")[0].contentWindow.location.reload();
  98. }
  99. }
  100. //参数设置
  101. Tab.prototype.set = function(option) {
  102. var _this = this;
  103. $.extend(true, _this.tabConfig, option);
  104. return _this;
  105. };
  106. //通过title获取lay-id
  107. Tab.prototype.getLayId = function(title){
  108. $(".layui-tab-title.top_tab li").each(function(){
  109. if($(this).find("cite").text() == title){
  110. layId = $(this).attr("lay-id");
  111. }
  112. })
  113. return layId;
  114. }
  115. //通过title判断tab是否存在
  116. Tab.prototype.hasTab = function(title){
  117. var tabIndex = -1;
  118. $(".layui-tab-title.top_tab li").each(function(){
  119. if($(this).find("cite").text() == title){
  120. tabIndex = 1;
  121. }
  122. })
  123. return tabIndex;
  124. }
  125. //右侧内容tab操作
  126. var tabIdIndex = 0;
  127. Tab.prototype.tabAdd = function(_this){
  128. if(window.sessionStorage.getItem("menu")){
  129. menu = JSON.parse(window.sessionStorage.getItem("menu"));
  130. }
  131. var that = this;
  132. var openTabNum = that.tabConfig.openTabNum;
  133. tabFilter = that.tabConfig.tabFilter;
  134. if(_this.attr("target") == "_blank"){
  135. window.open(_this.attr("data-url"));
  136. }else if(_this.attr("data-url") != undefined){
  137. var title = '';
  138. if(_this.find("i.seraph,i.layui-icon").attr("data-icon") != undefined){
  139. if(_this.find("i.seraph").attr("data-icon") != undefined){
  140. title += '<i class="seraph '+_this.find("i.seraph").attr("data-icon")+'"></i>';
  141. }else{
  142. title += '<i class="layui-icon">'+_this.find("i.layui-icon").attr("data-icon")+'</i>';
  143. }
  144. }
  145. //已打开的窗口中不存在
  146. if(that.hasTab(_this.find("cite").text()) == -1 && _this.siblings("dl.layui-nav-child").length == 0){
  147. if($(".layui-tab-title.top_tab li").length == openTabNum){
  148. layer.msg('只能同时打开'+openTabNum+'个选项卡哦。不然系统会卡的!');
  149. return;
  150. }
  151. tabIdIndex++;
  152. title += '<cite>'+_this.find("cite").text()+'</cite>';
  153. title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="'+tabIdIndex+'">&#x1006;</i>';
  154. element.tabAdd(tabFilter, {
  155. title : title,
  156. content :"<iframe src='"+_this.attr("data-url")+"' data-id='"+tabIdIndex+"'></frame>",
  157. id : new Date().getTime()
  158. })
  159. //当前窗口内容
  160. var curmenu = {
  161. "icon" : _this.find("i.seraph").attr("data-icon")!=undefined ? _this.find("i.seraph").attr("data-icon") : _this.find("i.layui-icon").attr("data-icon"),
  162. "title" : _this.find("cite").text(),
  163. "href" : _this.attr("data-url"),
  164. "layId" : new Date().getTime()
  165. }
  166. menu.push(curmenu);
  167. window.sessionStorage.setItem("menu",JSON.stringify(menu)); //打开的窗口
  168. window.sessionStorage.setItem("curmenu",JSON.stringify(curmenu)); //当前的窗口
  169. element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
  170. that.tabMove(); //顶部窗口是否可滚动
  171. }else{
  172. //当前窗口内容
  173. var curmenu = {
  174. "icon" : _this.find("i.seraph").attr("data-icon")!=undefined ? _this.find("i.seraph").attr("data-icon") : _this.find("i.layui-icon").attr("data-icon"),
  175. "title" : _this.find("cite").text(),
  176. "href" : _this.attr("data-url")
  177. }
  178. that.changeRegresh(_this.parent('.layui-nav-item').index());
  179. window.sessionStorage.setItem("curmenu", JSON.stringify(curmenu)); //当前的窗口
  180. element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
  181. that.tabMove(); //顶部窗口是否可滚动
  182. }
  183. }
  184. }
  185. //顶部窗口移动
  186. Tab.prototype.tabMove = function(){
  187. $(window).on("resize",function(event){
  188. var topTabsBox = $("#top_tabs_box"),
  189. topTabsBoxWidth = $("#top_tabs_box").width(),
  190. topTabs = $("#top_tabs"),
  191. topTabsWidth = $("#top_tabs").width(),
  192. tabLi = topTabs.find("li.layui-this"),
  193. top_tabs = document.getElementById("top_tabs"),
  194. event = event || window.event;
  195. if(topTabsWidth > topTabsBoxWidth){
  196. if(tabLi.position().left > topTabsBoxWidth || tabLi.position().left+topTabsBoxWidth > topTabsWidth){
  197. topTabs.css("left",topTabsBoxWidth-topTabsWidth);
  198. }else{
  199. topTabs.css("left",-tabLi.position().left);
  200. }
  201. //拖动效果
  202. var flag = false;
  203. var cur = {
  204. x:0,
  205. y:0
  206. }
  207. var nx,dx,x ;
  208. function down(){
  209. flag = true;
  210. var touch ;
  211. if(event.touches){
  212. touch = event.touches[0];
  213. }else {
  214. touch = event;
  215. }
  216. cur.x = touch.clientX;
  217. dx = top_tabs.offsetLeft;
  218. }
  219. function move(){
  220. var self = this;
  221. if(flag){
  222. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  223. var touch ;
  224. if(event.touches){
  225. touch = event.touches[0];
  226. }else {
  227. touch = event;
  228. }
  229. nx = touch.clientX - cur.x;
  230. x = dx+nx;
  231. if(x > 0){
  232. x = 0;
  233. }else{
  234. if(x < topTabsBoxWidth-topTabsWidth){
  235. x = topTabsBoxWidth-topTabsWidth;
  236. }else{
  237. x = dx+nx;
  238. }
  239. }
  240. top_tabs.style.left = x +"px";
  241. //阻止页面的滑动默认事件
  242. document.addEventListener("touchmove",function(){
  243. event.preventDefault();
  244. },false);
  245. }
  246. }
  247. //鼠标释放时候的函数
  248. function end(){
  249. flag = false;
  250. }
  251. //pc端拖动效果
  252. topTabs.on("mousedown",down);
  253. topTabs.on("mousemove",move);
  254. $(document).on("mouseup",end);
  255. //移动端拖动效果
  256. topTabs.on("touchstart",down);
  257. topTabs.on("touchmove",move);
  258. topTabs.on("touchend",end);
  259. }else{
  260. //移除pc端拖动效果
  261. topTabs.off("mousedown",down);
  262. topTabs.off("mousemove",move);
  263. topTabs.off("mouseup",end);
  264. //移除移动端拖动效果
  265. topTabs.off("touchstart",down);
  266. topTabs.off("touchmove",move);
  267. topTabs.off("touchend",end);
  268. topTabs.removeAttr("style");
  269. return false;
  270. }
  271. }).resize();
  272. }
  273. //切换后获取当前窗口的内容
  274. $("body").on("click",".top_tab li",function(){
  275. var curmenu = '';
  276. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  277. if(window.sessionStorage.getItem("menu")) {
  278. curmenu = menu[$(this).index() - 1];
  279. }
  280. if($(this).index() == 0){
  281. window.sessionStorage.setItem("curmenu",'');
  282. }else{
  283. window.sessionStorage.setItem("curmenu",JSON.stringify(curmenu));
  284. if(window.sessionStorage.getItem("curmenu") == "undefined"){
  285. //如果删除的不是当前选中的tab,则将curmenu设置成当前选中的tab
  286. if(curNav != JSON.stringify(delMenu)){
  287. window.sessionStorage.setItem("curmenu",curNav);
  288. }else{
  289. window.sessionStorage.setItem("curmenu",JSON.stringify(menu[liIndex-1]));
  290. }
  291. }
  292. }
  293. element.tabChange(tabFilter,$(this).attr("lay-id")).init();
  294. bodyTab.changeRegresh($(this).index());
  295. setTimeout(function(){
  296. bodyTab.tabMove();
  297. },100);
  298. })
  299. //删除tab
  300. $("body").on("click",".top_tab li i.layui-tab-close",function(){
  301. //删除tab后重置session中的menu和curmenu
  302. liIndex = $(this).parent("li").index();
  303. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  304. if(menu != null) {
  305. //获取被删除元素
  306. delMenu = menu[liIndex - 1];
  307. var curmenu = window.sessionStorage.getItem("curmenu") == "undefined" ? undefined : window.sessionStorage.getItem("curmenu") == "" ? '' : JSON.parse(window.sessionStorage.getItem("curmenu"));
  308. if (JSON.stringify(curmenu) != JSON.stringify(menu[liIndex - 1])) { //如果删除的不是当前选中的tab
  309. // window.sessionStorage.setItem("curmenu",JSON.stringify(curmenu));
  310. curNav = JSON.stringify(curmenu);
  311. } else {
  312. if ($(this).parent("li").length > liIndex) {
  313. window.sessionStorage.setItem("curmenu", curmenu);
  314. curNav = curmenu;
  315. } else {
  316. window.sessionStorage.setItem("curmenu", JSON.stringify(menu[liIndex - 1]));
  317. curNav = JSON.stringify(menu[liIndex - 1]);
  318. }
  319. }
  320. menu.splice((liIndex - 1), 1);
  321. window.sessionStorage.setItem("menu", JSON.stringify(menu));
  322. }
  323. element.tabDelete("bodyTab",$(this).parent("li").attr("lay-id")).init();
  324. bodyTab.tabMove();
  325. })
  326. //刷新当前
  327. $(".refresh").on("click",function(){ //此处添加禁止连续点击刷新一是为了降低服务器压力,另外一个就是为了防止超快点击造成chrome本身的一些js文件的报错(不过貌似这个问题还是存在,不过概率小了很多)
  328. if($(this).hasClass("refreshThis")){
  329. $(this).removeClass("refreshThis");
  330. $(".clildFrame .layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload();
  331. setTimeout(function(){
  332. $(".refresh").addClass("refreshThis");
  333. },2000)
  334. }else{
  335. layer.msg("您点击的速度超过了服务器的响应速度,还是等两秒再刷新吧!");
  336. }
  337. })
  338. //关闭其他
  339. $(".closePageOther").on("click",function(){
  340. if($("#top_tabs li").length>2 && $("#top_tabs li.layui-this cite").text()!="后台首页"){
  341. var menu = JSON.parse(window.sessionStorage.getItem("menu"));
  342. $("#top_tabs li").each(function(){
  343. if($(this).attr("lay-id") != '' && !$(this).hasClass("layui-this")){
  344. element.tabDelete("bodyTab",$(this).attr("lay-id")).init();
  345. //此处将当前窗口重新获取放入session,避免一个个删除来回循环造成的不必要工作量
  346. for(var i=0;i<menu.length;i++){
  347. if($("#top_tabs li.layui-this cite").text() == menu[i].title){
  348. menu.splice(0,menu.length,menu[i]);
  349. window.sessionStorage.setItem("menu",JSON.stringify(menu));
  350. }
  351. }
  352. }
  353. })
  354. }else if($("#top_tabs li.layui-this cite").text()=="后台首页" && $("#top_tabs li").length>1){
  355. $("#top_tabs li").each(function(){
  356. if($(this).attr("lay-id") != '' && !$(this).hasClass("layui-this")){
  357. element.tabDelete("bodyTab",$(this).attr("lay-id")).init();
  358. window.sessionStorage.removeItem("menu");
  359. menu = [];
  360. window.sessionStorage.removeItem("curmenu");
  361. }
  362. })
  363. }else{
  364. layer.msg("没有可以关闭的窗口了@_@");
  365. }
  366. //渲染顶部窗口
  367. tab.tabMove();
  368. })
  369. //关闭全部
  370. $(".closePageAll").on("click",function(){
  371. if($("#top_tabs li").length > 1){
  372. $("#top_tabs li").each(function(){
  373. if($(this).attr("lay-id") != ''){
  374. element.tabDelete("bodyTab",$(this).attr("lay-id")).init();
  375. window.sessionStorage.removeItem("menu");
  376. menu = [];
  377. window.sessionStorage.removeItem("curmenu");
  378. }
  379. })
  380. }else{
  381. layer.msg("没有可以关闭的窗口了@_@");
  382. }
  383. //渲染顶部窗口
  384. tab.tabMove();
  385. })
  386. var bodyTab = new Tab();
  387. exports("bodyTab",function(option){
  388. return bodyTab.set(option);
  389. });
  390. })