bootstrap-tab.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. var addTabs = function (options) {
  2. //var rand = Math.random().toString();
  3. //var id = rand.substring(rand.indexOf('.') + 1);
  4. //var url = window.location.protocol + '//' + window.location.host;
  5. //options.url = url + options.url;
  6. id = "tab_" + options.id;
  7. $(".active").removeClass("active");
  8. //如果TAB不存在,创建一个新的TAB
  9. if (!$("#" + id)[0]) {
  10. //固定TAB中IFRAME高度
  11. //update-begin--Author:dangzhenghui Date:20170614 for:ace自适应高度--------------------
  12. mainHeight = screen.height;
  13. mainHeight = mainHeight*0.72;//Ace 右侧高度默认
  14. //update-end--Author:dangzhenghui Date:20170614 for:ace自适应高度--------------------
  15. //
  16. //创建新TAB的title
  17. title = '<li role="presentation" id="tab_' + id + '"><a href="#' + id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + options.title;
  18. //是否允许关闭
  19. if (options.close) {
  20. title += ' <i class="icon-remove" tabclose="' + id + '"></i>';
  21. }
  22. title += '</a></li>';
  23. //是否指定TAB内容
  24. if (options.content) {
  25. content = '<div role="tabpanel" class="tab-pane" id="' + id + '">' + options.content + '</div>';
  26. } else {//没有内容,使用IFRAME打开链接
  27. content = '<div role="tabpanel" class="tab-pane" id="' + id + '"><iframe src="' + options.url + '" width="100%" height="' + mainHeight +
  28. '" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="yes" allowtransparency="yes"></iframe></div>';
  29. }
  30. //加入TABS
  31. $(".nav-tabs").append(title);
  32. $(".tab-content").append(content);
  33. }else{
  34. //切换后重新加载页面
  35. $("#" + id).find("iframe").attr("src",options.url);
  36. }
  37. //激活TAB
  38. $("#tab_" + id).addClass('active');
  39. $("#" + id).addClass("active");
  40. var last = $("#tabs>ul>li:last");
  41. /*$(".contextMenuPlugin").mouseout(function(){
  42. $(".contextMenuPlugin").remove();
  43. })
  44. $(".contextMenuPlugin").mouseup(function(){
  45. alert("aaa");
  46. })*/
  47. last.contextPopup({
  48. title: '菜单',
  49. items: [
  50. {
  51. // add-begin--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑--------------------
  52. label:'刷新缓存',icon:'plug-in/diy/icons/refresh.png',action:function(){
  53. // add-end--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑----------------------
  54. //last就是当前选中的元素
  55. var tab = last.children("a").attr("aria-controls").toString();
  56. //$("#tabs").find("li[aria-controls='"+tab+"']").remove();
  57. var div = $("#tabs").find("div[id='"+tab+"']");
  58. div.find("iframe").attr("src",options.url);
  59. //tabs.tabs("refresh");
  60. }
  61. },
  62. {
  63. // add-begin--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑--------------------
  64. label:'关闭',icon:'plug-in/diy/icons/closeone.png',action:function(){
  65. // add-end--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑----------------------
  66. //last就是当前选中的元素
  67. var closeText = last.children("a").text().trim();
  68. var nowText = $("#tabs").find("li[class='active']").children("a").text().trim();
  69. if(closeText==nowText){
  70. //关闭的是当前页的时候,显示前一页,如果没有前一页了,就提示
  71. var prevCount = last.prevAll().size();
  72. if(prevCount==0){
  73. var tab = last.children("a").attr("aria-controls").toString();
  74. last.remove();
  75. $("#tabs").find("div[id='"+tab+"']").remove();
  76. }else{
  77. //显示前一个tab
  78. var tab = last.children("a").attr("aria-controls").toString();
  79. var prev = last.prevAll().first();
  80. last.remove();
  81. $("#tabs").find("div[id='"+tab+"']").remove();
  82. prev.addClass("active");
  83. var id = prev.children("a").attr("aria-controls").toString();
  84. $("#tabs").find("div[id='"+id+"']").addClass("active");
  85. }
  86. }else{
  87. //关闭的不是当前页,关闭就好了╮(╯_╰)╭
  88. var tab = last.children("a").attr("aria-controls").toString();
  89. last.remove();
  90. $("#tabs").find("div[id='"+tab+"']").remove();
  91. }
  92. }
  93. },
  94. {
  95. // add-begin--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑--------------------
  96. label:'全部关闭',icon:'plug-in/diy/icons/closeall.png',action:function(){
  97. // add-end--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑----------------------
  98. $("#tabs>ul>li").remove();
  99. $("#tabs>div>div").remove();
  100. //tabs.tabs("refresh");
  101. }
  102. },
  103. {
  104. // add-begin--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑--------------------
  105. label:'除此之外全部关闭',icon:'plug-in/diy/icons/closeother.png',action:function(){
  106. // add-end--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑----------------------
  107. var closeText = last.children("a").text().trim();
  108. var nowText = $("#tabs").find("li[class='active']").children("a").text().trim();
  109. //此是当前页则关闭,如果不是当前页面,要激活选择页面
  110. if(closeText==nowText){
  111. //此是当前页面
  112. var tab = last.children("a").attr("aria-controls").toString();
  113. $("#tabs>ul>li").not(last).remove();
  114. $("#tabs>div>div").not($("#tabs").find("div[id='"+tab+"']")).remove();
  115. }else{
  116. var tab = last.children("a").attr("aria-controls").toString();
  117. $("#tabs>ul>li").not(last).remove();
  118. $("#tabs>div>div").not($("#tabs").find("div[id='"+tab+"']")).remove();
  119. last.addClass("active");
  120. var id = last.children("a").attr("aria-controls").toString();
  121. $("#tabs").find("div[id='"+id+"']").addClass("active");
  122. }
  123. //tabs.tabs("refresh");
  124. }
  125. },
  126. null,
  127. {
  128. // add-begin--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑--------------------
  129. label:'当前页右侧全部关闭',icon:'plug-in/diy/icons/closeright.png',action:function(){
  130. // add-end--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑----------------------
  131. var closeText = last.children("a").text().trim();
  132. var nowText = $("#tabs").find("li[class='active']").children("a").text().trim();
  133. if(closeText==nowText){
  134. //当前页面
  135. var nextAll = last.nextAll();
  136. if(nextAll.length!=0){
  137. nextAll.remove();
  138. var tab = last.children("a").attr("aria-controls").toString();
  139. //$("#tabs>ul>li").not(shouye).remove();
  140. $("#tabs>div").find("div[id='"+tab+"']").nextAll().remove();
  141. //tabs.tabs("refresh");
  142. }else{
  143. layer.msg('<b>右侧没有啦</b>');
  144. }
  145. }else{
  146. //不是当前页,当前页的active去掉
  147. var now = $("#tabs").find("li[class='active']");
  148. var nowid = now.children("a").attr("aria-controls").toString();
  149. now.removeClass("active");
  150. $("#tabs").find("div[id='"+nowid+"']").removeClass("active");
  151. var nextAll = last.nextAll();
  152. if(nextAll.length!=0){
  153. nextAll.remove();
  154. var tab = last.children("a").attr("aria-controls").toString();
  155. //$("#tabs>ul>li").not(shouye).remove();
  156. $("#tabs>div").find("div[id='"+tab+"']").nextAll().remove();
  157. last.addClass("active");
  158. var id = last.children("a").attr("aria-controls").toString();
  159. $("#tabs").find("div[id='"+id+"']").addClass("active");
  160. //tabs.tabs("refresh");
  161. }else{
  162. layer.msg('<b>右侧没有啦</b>');
  163. }
  164. }
  165. }
  166. },
  167. {
  168. // add-begin--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑--------------------
  169. label:'当前页左侧全部关闭',icon:'plug-in/diy/icons/closeleft.png',action:function(){
  170. // add-end--Author:weict Date:20170614 for:TASK #2108 【ace样式】Ace 风格右键操作太丑----------------------
  171. var closeText = last.children("a").text().trim();
  172. var nowText = $("#tabs").find("li[class='active']").children("a").text().trim();
  173. if(closeText==nowText){
  174. //当前页面
  175. var prevAll = last.prevAll();
  176. if(prevAll.length!=0){
  177. prevAll.remove();
  178. var tab = last.children("a").attr("aria-controls").toString();
  179. //$("#tabs>ul>li").not(shouye).remove();
  180. $("#tabs>div").find("div[id='"+tab+"']").prevAll().remove();
  181. //tabs.tabs("refresh");
  182. }else{
  183. layer.msg('<b>左侧没有啦</b>');
  184. }
  185. }else{
  186. //不是当前页,当前页的active去掉
  187. var now = $("#tabs").find("li[class='active']");
  188. var nowid = now.children("a").attr("aria-controls").toString();
  189. now.removeClass("active");
  190. $("#tabs").find("div[id='"+nowid+"']").removeClass("active");
  191. var prevAll = last.prevAll();
  192. if(prevAll.length!=0){
  193. prevAll.remove();
  194. var tab = last.children("a").attr("aria-controls").toString();
  195. //$("#tabs>ul>li").not(shouye).remove();
  196. $("#tabs>div").find("div[id='"+tab+"']").prevAll().remove();
  197. last.addClass("active");
  198. var id = last.children("a").attr("aria-controls").toString();
  199. $("#tabs").find("div[id='"+id+"']").addClass("active");
  200. //tabs.tabs("refresh");
  201. }else{
  202. layer.msg('<b>左侧没有啦</b>');
  203. }
  204. }
  205. /*var prevAll = last.prevAll();
  206. if(prevAll.length!=0){
  207. prevAll.remove();
  208. }else{
  209. layer.msg('<b>左侧没有啦</b>');
  210. }
  211. var tab = last.attr("aria-controls").toString();
  212. //$("#tabs>ul>li").not(shouye).remove();
  213. $("#tabs>div").find("div[id='"+tab+"']").prevAll().remove();*/
  214. //tabs.tabs("refresh");
  215. }
  216. }
  217. ]
  218. });
  219. };
  220. var closeTab = function (id) {
  221. //如果关闭的是当前激活的TAB,激活他的前一个TAB
  222. if ($("li.active").attr('id') == "tab_" + id) {
  223. $("#tab_" + id).prev().addClass('active');
  224. $("#" + id).prev().addClass('active');
  225. }
  226. //关闭TAB
  227. $("#tab_" + id).remove();
  228. $("#" + id).remove();
  229. };
  230. $(function () {
  231. mainHeight = $(document.body).height();
  232. $('.main-left,.main-right').height(mainHeight);
  233. $("[addtabs]").click(function () {
  234. addTabs({ id: $(this).attr("id"), title: $(this).attr('title'), close: true });
  235. });
  236. $(".nav-tabs").on("click", "[tabclose]", function (e) {
  237. id = $(this).attr("tabclose");
  238. closeTab(id);
  239. });
  240. });