/** * http://git.oschina.net/hbbcs/bootStrap-addTabs * Created by joe on 2015-12-19. * Modified by Karson */ (function ($) { $.fn.addtabs = function (options) { var obj = $(this); options = $.extend({ content: '', //直接指定所有页面TABS内容 close: true, //是否可以关闭 monitor: 'body', //监视的区域 nav: '.nav-addtabs', tab: '.tab-addtabs', iframeUse: true, //使用iframe还是ajax iframeHeight: $(window).height() - 50, //固定TAB中IFRAME高度,根据需要自己修改 iframeForceRefresh: false, //点击后强制加载对应的iframe iframeForceRefreshTable: false, //点击后强制刷新对应的iframe中的table callback: function () { //关闭后回调函数 } }, options || {}); var navobj = $(options.nav); var tabobj = $(options.tab); if (history.pushState) { //浏览器前进后退事件 $(window).on("popstate", function (e) { var state = e.originalEvent.state; if (state) { $("a[addtabs=" + state.id + "]", options.monitor).data("pushstate", true).trigger("click"); } }); } $(options.monitor).on('click', '[addtabs]', function (e) { if ($(this).attr('url').indexOf("javascript:") !== 0) { if ($(this).is("a")) { e.preventDefault(); } var id = $(this).attr('addtabs'); var title = $(this).attr('title') ? $(this).attr('title') : $.trim($(this).text()); var url = $(this).attr('url'); var content = options.content ? options.content : $(this).attr('content'); var ajax = $(this).attr('ajax') === '1' || $(this).attr('ajax') === 'true'; var state = ({ url: url, title: title, id: id, content: content, ajax: ajax }); document.title = title; if (history.pushState && !$(this).data("pushstate")) { var pushurl = url.indexOf("ref=addtabs") === -1 ? (url + (url.indexOf("?") > -1 ? "&" : "?") + "ref=addtabs") : url; try { window.history.pushState(state, title, pushurl); } catch (e) { } } $(this).data("pushstate", null); _add.call(this, { id: id, title: $(this).attr('title') ? $(this).attr('title') : $(this).html(), content: content, url: url, ajax: ajax }); } }); navobj.on('click', '.close-tab', function () { var id = $(this).prev("a").attr("aria-controls"); _close(id); return false; }); navobj.on('dblclick', 'li[role=presentation]', function () { $(this).find(".close-tab").trigger("click"); }); navobj.on('click', 'li[role=presentation]', function () { $("a[addtabs=" + $("a", this).attr("node-id") + "]").trigger("click"); }); $(window).resize(function () { if (typeof options.nav === 'object') { var siblingsWidth = 0; navobj.siblings().each(function () { siblingsWidth += $(this).outerWidth(); }); navobj.width(navobj.parent().width() - siblingsWidth); } else { $("#nav").width($("#header").find("> .navbar").width() - $(".sidebar-toggle").outerWidth() - $(".navbar-custom-menu").outerWidth() - 20); } _drop(); }); var _add = function (opts) { var id, tabid, conid, url; id = opts.id; tabid = 'tab_' + opts.id; conid = 'con_' + opts.id; url = opts.url; url += (opts.url.indexOf("?") > -1 ? "&addtabs=1" : "?addtabs=1"); var tabitem = $('#' + tabid, navobj); var conitem = $('#' + conid, tabobj); navobj.find("[role='presentation']").removeClass('active'); tabobj.find("[role='tabpanel']").removeClass('active'); //如果TAB不存在,创建一个新的TAB if (tabitem.size() === 0) { //创建新TAB的title tabitem = $('