jquery.addtabs.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * http://git.oschina.net/hbbcs/bootStrap-addTabs
  3. * Created by joe on 2015-12-19.
  4. */
  5. $.fn.addtabs = function (options) {
  6. var obj = $(this);
  7. options = $.extend({
  8. content: '', //直接指定所有页面TABS内容
  9. close: true, //是否可以关闭
  10. monitor: 'body', //监视的区域
  11. nav: '.nav-addtabs',
  12. tab: '.tab-addtabs',
  13. iframeUse: true, //使用iframe还是ajax
  14. iframeHeight: $(window).height() - 50, //固定TAB中IFRAME高度,根据需要自己修改
  15. iframeForceRefresh: false, //点击后强制刷新对应的iframe
  16. callback: function () {
  17. //关闭后回调函数
  18. }
  19. }, options || {});
  20. var navobj = $(options.nav);
  21. var tabobj = $(options.tab);
  22. if (history.pushState) {
  23. //浏览器前进后退事件
  24. $(window).on("popstate", function (e) {
  25. var state = e.originalEvent.state;
  26. if (state) {
  27. $("a[addtabs=" + state.id + "]", options.monitor).data("pushstate", true).trigger("click");
  28. }
  29. });
  30. }
  31. $(options.monitor).on('click', '[addtabs]', function (e) {
  32. if ($(this).attr('url').indexOf("javascript:") !== 0) {
  33. if ($(this).is("a")) {
  34. e.preventDefault();
  35. }
  36. var id = $(this).attr('addtabs');
  37. var title = $(this).attr('title') ? $(this).attr('title') : $.trim($(this).text());
  38. var url = $(this).attr('url');
  39. var content = options.content ? options.content : $(this).attr('content');
  40. var ajax = $(this).attr('ajax') ? true : false;
  41. var state = ({
  42. url: url, title: title, id: id, content: content, ajax: ajax
  43. });
  44. document.title = title;
  45. if (history.pushState && !$(this).data("pushstate")) {
  46. var pushurl = url.indexOf("ref=addtabs") == -1 ? (url + (url.indexOf("?") > -1 ? "&" : "?") + "ref=addtabs") : url;
  47. window.history.pushState(state, title, pushurl);
  48. }
  49. $(this).data("pushstate", null);
  50. _add.call(this, {
  51. id: id,
  52. title: $(this).attr('title') ? $(this).attr('title') : $(this).html(),
  53. content: content,
  54. url: url,
  55. ajax: ajax
  56. });
  57. }
  58. });
  59. navobj.on('click', '.close-tab', function (e) {
  60. id = $(this).prev("a").attr("aria-controls");
  61. _close(id);
  62. return false;
  63. });
  64. navobj.on('dblclick', 'li[role=presentation]', function (e) {
  65. $(this).find(".close-tab").trigger("click");
  66. });
  67. navobj.on('click', 'li[role=presentation]', function (e) {
  68. $("a[addtabs=" + $("a", this).attr("node-id") + "]").trigger("click");
  69. });
  70. $(window).resize(function () {
  71. $("#nav").width($("#header > .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20);
  72. _drop();
  73. });
  74. _add = function (opts) {
  75. var id, tabid, conid, url;
  76. id = opts.id;
  77. tabid = 'tab_' + opts.id;
  78. conid = 'con_' + opts.id;
  79. url = opts.url;
  80. url += (opts.url.indexOf("?") > -1 ? "&addtabs=1" : "?addtabs=1");
  81. navobj.find("[role='presentation']").removeClass('active');
  82. tabobj.find("[role='tabpanel']").removeClass('active');
  83. //如果TAB不存在,创建一个新的TAB
  84. if ($("#" + tabid).size() == 0) {
  85. //创建新TAB的title
  86. title = $('<li role="presentation" id="' + tabid + '"><a href="#' + conid + '" node-id="' + opts.id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + opts.title + '</a></li>');
  87. //是否允许关闭
  88. if (options.close && $("li", navobj).size() > 0) {
  89. title.append(' <i class="close-tab fa fa-remove"></i>');
  90. }
  91. //创建新TAB的内容
  92. content = $('<div role="tabpanel" class="tab-pane" id="' + conid + '"></div>');
  93. //是否指定TAB内容
  94. if (opts.content) {
  95. content.append(opts.content);
  96. } else if (options.iframeUse && !opts.ajax) {//没有内容,使用IFRAME打开链接
  97. var height = options.iframeHeight;
  98. content.append('<iframe src="' + url + '" width="100%" height="' + height + '" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling-x="no" scrolling-y="auto" allowtransparency="yes"></iframe></div>');
  99. } else {
  100. $.get(url, function (data) {
  101. content.append(data);
  102. });
  103. }
  104. //加入TABS
  105. if ($('.tabdrop li').size() > 0) {
  106. $('.tabdrop ul').append(title);
  107. } else {
  108. navobj.append(title);
  109. }
  110. tabobj.append(content);
  111. } else {
  112. //强制刷新iframe
  113. if (options.iframeForceRefresh) {
  114. $("#" + conid + " iframe").attr('src', function (i, val) {
  115. return val;
  116. });
  117. }
  118. }
  119. localStorage.setItem("addtabs", $(this).prop('outerHTML'));
  120. //激活TAB
  121. $("#" + tabid).addClass('active');
  122. $("#" + conid).addClass("active");
  123. _drop();
  124. };
  125. _close = function (id) {
  126. var tabid = 'tab_' + id;
  127. var conid = 'con_' + id;
  128. //如果关闭的是当前激活的TAB,激活他的前一个TAB
  129. if (obj.find("li.active").attr('id') == tabid) {
  130. if ($("#" + tabid).prev().not(".tabdrop").size() > 0) {
  131. $("#" + tabid).prev().not(".tabdrop").find("a").trigger("click");
  132. } else if ($("#" + tabid).next().size() > 0) {
  133. $("#" + tabid).next().trigger("click");
  134. }
  135. }
  136. //关闭TAB
  137. $("#" + tabid).remove();
  138. $("#" + conid).remove();
  139. _drop();
  140. options.callback();
  141. };
  142. _drop = function () {
  143. //创建下拉标签
  144. var dropdown = $('<li class="dropdown pull-right hide tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">' +
  145. '<i class="glyphicon glyphicon-align-justify"></i>' +
  146. ' <b class="caret"></b></a><ul class="dropdown-menu"></ul></li>');
  147. //检测是否已增加
  148. if (!$('.tabdrop').html()) {
  149. dropdown.prependTo(navobj);
  150. } else {
  151. dropdown = navobj.find('.tabdrop');
  152. }
  153. //检测是否有下拉样式
  154. if (navobj.parent().is('.tabs-below')) {
  155. dropdown.addClass('dropup');
  156. }
  157. var collection = 0;
  158. var maxwidth = navobj.width() - 60;
  159. var liwidth = 0;
  160. //检查超过一行的标签页
  161. var litabs = navobj.append(dropdown.find('li')).find('>li').not('.tabdrop');
  162. var lisize = litabs.size();
  163. litabs.each(function (i, j) {
  164. liwidth += $(this).width();
  165. if (collection == 0 && i == lisize - 1 && liwidth <= navobj.width()) {
  166. return true;
  167. }
  168. if (liwidth > maxwidth) {
  169. dropdown.find('ul').append($(this));
  170. collection++;
  171. }
  172. });
  173. //如果有超出的,显示下拉标签
  174. if (collection > 0) {
  175. dropdown.removeClass('hide');
  176. if (dropdown.find('.active').length == 1) {
  177. dropdown.addClass('active');
  178. } else {
  179. dropdown.removeClass('active');
  180. }
  181. } else {
  182. dropdown.addClass('hide');
  183. }
  184. };
  185. };