jquery.addtabs.js 10 KB

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