jquery.addtabs.js 9.2 KB

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