jquery.addtabs.js 9.9 KB

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