123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- !function ($) {
- "use strict";
-
- var defaults = {
-
- 'loginUrl' : '/',
-
- 'className' : undefined,
-
- 'sortable' : true,
-
- 'resize' : undefined
- };
-
-
- var constants = {
- closeBtnTemplate : '<button type="button" class="navTabsCloseBtn" title="关闭" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>',
-
- noCloseClass : 'noclose',
-
- noSortClass : 'noSort',
- prefixKey : 'bTabs_'
- };
-
- var bTabs = function(box,p){
- this.$container = box;
- this.openTabs = new Array();
- this.p = p;
- };
-
- bTabs.version = '1.0';
-
- bTabs.prototype.init = function(){
- var self = this,c = constants, $tabs = this.$container, openTabs = this.openTabs, p = this.p;
-
- $($tabs).addClass('bTabs');
- if(p.className) $($tabs).addClass(p.className);
-
- $('ul.nav-tabs a',$($tabs)).each(function(i,row){
- var li = $(this).closest('li');
- if(li && !$(li).hasClass(c.noCloseClass)) $(row).append(c.closeBtnTemplate);
- });
- $('div.tab-content div.tab-pane',$tabs).each(function(i,row){
- openTabs.push($(this).attr('id'));
- });
-
- $('ul.nav-tabs',$tabs).on('click','button',function(e){
- var id = $(this).parent().attr('href').replace('#', '');
- self.closeTab(id);
- });
-
- if(p.sortable && $.fn.sortable){
- $('ul.nav-tabs',$tabs).sortable({
- items : "li:not(."+c.noSortClass+")",
- cancel : "li:not(.active)",
- axis : "x",
- placeholder : 'bTabsPlaceHolder',
- forcePlaceholderSize : true,
- stop : function(e,ui){}
- }).disableSelection();
- }
- if(p && p.resize && $.isFunction(p.resize)){
- p.resize();
- self.innerResize();
- $(window).off('resize.bTabs').on('resize.bTabs',function(e){
- p.resize();
- self.innerResize();
- });
- }
- };
-
- bTabs.prototype.innerResize = function(){
- var $tabs = this.$container;
-
- var mainHeight = $($tabs).innerHeight();
- var tabBarHeight = $('ul.nav-tabs',$tabs).outerHeight(true);
- $('div.tab-content',$tabs).height(mainHeight - tabBarHeight);
- };
-
- bTabs.prototype.addTab = function(id,title,url,loginCheck){
- if(!id || !title || !url) console.error('新增tab时,id,title,url参数为必须传递参数!');
- var c = constants, $tabs = this.$container, openTabs = this.openTabs, p = this.p;
- var tabId = c.prefixKey + id;
- if(openTabs && $.isArray(openTabs) && openTabs.length>0){
- var exist = false;
- $.each(openTabs,function(i,row){
- if(row == tabId){
- exist = true;
- return false;
- }
- });
-
- if(exist){
- $('ul.nav-tabs a[href="#'+tabId+'"]',$tabs).tab('show');
- return;
- }
- }else openTabs = new Array();
- $('ul.nav-tabs',$tabs).append('<li><a href="#'+tabId+'" title="'+title+'" data-toggle="tab">'+title+c.closeBtnTemplate+'</a></li>');
- var content = $('<div class="tab-pane" id="'+tabId+'"></div>');
- $('div.tab-content',$tabs).append(content);
-
- $('ul.nav-tabs li:last a',$tabs).tab('show');
- openTabs.push(tabId);
-
- var openIframe = function(){
- $(content).append('<iframe frameborder="0" scrolling="yes" style="width:100%;height:100%;min-width:1150px;border:0px;" src="'+url+'"></iframe>');
- };
-
- if(loginCheck && $.isFunction(loginCheck)){
- if(loginCheck()) openIframe();
- else if(p && p.loginUrl) window.top.location.replace(p.loginUrl);
- }else openIframe();
- };
-
- bTabs.prototype.closeTab = function(id,toId){
- var c = constants, $tabs = this.$container, openTabs = this.openTabs;
- var thisTab = $('#' + id);
-
- if($('iframe',$(thisTab)).size() > 0) $('iframe',$(thisTab)).remove();
-
- $(thisTab).remove();
- var a = $('ul.nav-tabs a[href="#'+id+'"]',$tabs);
- var li = $(a).closest('li');
-
- var prevLi = $(li).prev();
-
- li.remove();
- if(openTabs && $.isArray(openTabs) && openTabs.length>0){
- var index = -1;
- $.each(openTabs,function(i,d){
- if(d == id){
- index = i;
- return false;
- }
- });
- if(index != -1) openTabs.splice(index,1);
- }
-
- if(toId != "" && toId !=undefined) {
- toId = c.prefixKey + toId;
- $('ul.nav-tabs a[href="#' + toId + '"]', $tabs).tab('show');
- $("#"+toId).find("iframe")[0].contentWindow.location.reload();
- }else {
- if (prevLi.size() > 0) $('a', $(prevLi)).tab('show');
- }
- };
-
-
- function Plugin(p){
- return this.each(function(){
-
- var $this = $(this),
- data = $this.data('bTabs'),
- params = $.extend({}, defaults, $this.data(), typeof p == 'object' && p);
- if(!data) $this.data('bTabs', (data = new bTabs(this,params)));
- data.init();
- });
- }
-
- function bTabsAdd(id,title,url,loginCheck){
- return this.each(function(){
- if(!id || !title || !url) return;
- var $this = $(this),data = $this.data('bTabs');
- if(data) data.addTab(id,title,url,loginCheck);
- });
- }
-
- function bTabsClose(id){
- return this.each(function(){
- if(!id || !title || !url) return;
- var $this = $(this),data = $this.data('bTabs');
- if(data) data.closeTab(id);
- });
- }
-
- function CloseTabs(id,toId){
- var c = constants;
- id = c.prefixKey+id;
- return this.each(function(){
- if(!id) return;
- var $this = $(this),data = $this.data('bTabs');
- if(data) data.closeTab(id,toId);
- });
- }
-
- var old = $.fn.bTabs;
- $.fn.bTabs = Plugin;
- $.fn.bTabs.Constructor = bTabs;
- $.fn.bTabsAdd = bTabsAdd;
- $.fn.bTabsClose = bTabsClose;
- $.fn.CloseTabs = CloseTabs;
-
-
- $.fn.bTabs.noConflict = function () {
- $.fn.bTabs = old;
- return this;
- };
- }(window.jQuery);
|