jquery.addtabs.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. try {
  48. window.history.pushState(state, title, pushurl);
  49. }catch(e){
  50. }
  51. }
  52. $(this).data("pushstate", null);
  53. _add.call(this, {
  54. id: id,
  55. title: $(this).attr('title') ? $(this).attr('title') : $(this).html(),
  56. content: content,
  57. url: url,
  58. ajax: ajax
  59. });
  60. }
  61. });
  62. navobj.on('click', '.close-tab', function (e) {
  63. id = $(this).prev("a").attr("aria-controls");
  64. _close(id);
  65. return false;
  66. });
  67. navobj.on('dblclick', 'li[role=presentation]', function (e) {
  68. $(this).find(".close-tab").trigger("click");
  69. });
  70. navobj.on('click', 'li[role=presentation]', function (e) {
  71. $("a[addtabs=" + $("a", this).attr("node-id") + "]").trigger("click");
  72. });
  73. $(window).resize(function () {
  74. $("#nav").width($("#header > .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20);
  75. _drop();
  76. });
  77. _add = function (opts) {
  78. var id, tabid, conid, url;
  79. id = opts.id;
  80. tabid = 'tab_' + opts.id;
  81. conid = 'con_' + opts.id;
  82. url = opts.url;
  83. url += (opts.url.indexOf("?") > -1 ? "&addtabs=1" : "?addtabs=1");
  84. navobj.find("[role='presentation']").removeClass('active');
  85. tabobj.find("[role='tabpanel']").removeClass('active');
  86. //如果TAB不存在,创建一个新的TAB
  87. if ($("#" + tabid).size() == 0) {
  88. //创建新TAB的title
  89. 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>');
  90. //是否允许关闭
  91. if (options.close && $("li", navobj).size() > 0) {
  92. title.append(' <i class="close-tab fa fa-remove"></i>');
  93. }
  94. //创建新TAB的内容
  95. content = $('<div role="tabpanel" class="tab-pane" id="' + conid + '"></div>');
  96. //是否指定TAB内容
  97. if (opts.content) {
  98. content.append(opts.content);
  99. } else if (options.iframeUse && !opts.ajax) {//没有内容,使用IFRAME打开链接
  100. var height = options.iframeHeight;
  101. 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>');
  102. } else {
  103. $.get(url, function (data) {
  104. content.append(data);
  105. });
  106. }
  107. //加入TABS
  108. if ($('.tabdrop li').size() > 0) {
  109. $('.tabdrop ul').append(title);
  110. } else {
  111. navobj.append(title);
  112. }
  113. tabobj.append(content);
  114. } else {
  115. //强制刷新iframe
  116. if (options.iframeForceRefresh) {
  117. $("#" + conid + " iframe").attr('src', function (i, val) {
  118. return val;
  119. });
  120. }
  121. }
  122. localStorage.setItem("addtabs", $(this).prop('outerHTML'));
  123. //激活TAB
  124. $("#" + tabid).addClass('active');
  125. $("#" + conid).addClass("active");
  126. _drop();
  127. };
  128. _close = function (id) {
  129. var tabid = 'tab_' + id;
  130. var conid = 'con_' + id;
  131. //如果关闭的是当前激活的TAB,激活他的前一个TAB
  132. if (obj.find("li.active").not('.tabdrop').attr('id') == tabid) {
  133. if ($("#" + tabid).prev().not(".tabdrop").size() > 0) {
  134. $("#" + tabid).prev().not(".tabdrop").find("a").trigger("click");
  135. } else if ($("#" + tabid).next().size() > 0) {
  136. $("#" + tabid).next().trigger("click");
  137. } else {
  138. $(">li:last > a", navobj).trigger('click');
  139. }
  140. }
  141. //关闭TAB
  142. $("#" + tabid).remove();
  143. $("#" + conid).remove();
  144. _drop();
  145. options.callback();
  146. };
  147. _drop = function () {
  148. //创建下拉标签
  149. var dropdown = $('<li class="dropdown pull-right hide tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">' +
  150. '<i class="glyphicon glyphicon-align-justify"></i>' +
  151. ' <b class="caret"></b></a><ul class="dropdown-menu"></ul></li>');
  152. //检测是否已增加
  153. if (!$('.tabdrop').html()) {
  154. dropdown.prependTo(navobj);
  155. } else {
  156. dropdown = navobj.find('.tabdrop');
  157. }
  158. //检测是否有下拉样式
  159. if (navobj.parent().is('.tabs-below')) {
  160. dropdown.addClass('dropup');
  161. }
  162. var collection = 0;
  163. var maxwidth = navobj.width() - 60;
  164. var liwidth = 0;
  165. //检查超过一行的标签页
  166. var litabs = navobj.append(dropdown.find('li')).find('>li').not('.tabdrop');
  167. var lisize = litabs.size();
  168. litabs.each(function (i, j) {
  169. liwidth += $(this).width();
  170. if (collection == 0 && i == lisize - 1 && liwidth <= navobj.width()) {
  171. return true;
  172. }
  173. if (liwidth > maxwidth) {
  174. dropdown.find('ul').append($(this));
  175. collection++;
  176. }
  177. });
  178. //如果有超出的,显示下拉标签
  179. if (collection > 0) {
  180. dropdown.removeClass('hide');
  181. if (dropdown.find('.active').length == 1) {
  182. dropdown.addClass('active');
  183. } else {
  184. dropdown.removeClass('active');
  185. }
  186. } else {
  187. dropdown.addClass('hide');
  188. }
  189. };
  190. };