1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/**
* Light jQuery tab plugin
*
* the html structure is:
*
* <div id="generalid">
* <ul class="tabbar">
* <li><a href="#tab-1">Title 1</a></li>
* </ul>
* <div id="tab-1">
* Content 1
* </div>
* </div>
*/
(function( $ ){
var methods = {
/**
* Initialise the general tab area
* @return this (to preserve chainability)
*/
init: function() {
var tabs = this;
this.find('ul.tabbar li').bind('click.tabs', function(event){
event.preventDefault();
tabs.tabs('select',$(this).find('a').attr('href'));
});
this.data('tabs',{});
return this;
},
/**
* Select a tab
* @param {String} id The tab id with #
* @return this
*/
select: function(id) {
var selectedId = this.data('tabs').selected;
if ( selectedId == id) {
return this;
}
this.find('ul.tabbar li a[href="' + selectedId + '"]').parent().removeClass('selected');
this.find('ul.tabbar li a[href="' + id + '"]').parent().addClass('selected');
$(selectedId).hide();
$(id).show();
this.data('tabs').selected = id;
return this;
},
/**
* Add a tab
* @param {String} name The tab Title
* @param {String} id The tab id without #
* @pram {Bool} remove Wether the tab should be closable
* return this
*/
add: function(name, id, remove) {
var tabs = this;
var li = $('<li><a href="#' + id + '">' + name +'</a></li>');
if ( remove ) {
li.append('<span class="tab-close"/>');
li.find('.tab-close').bind('click', function() {
tabs.tabs('remove', '#'+id);
});
}
li.bind('click.tabs',function(event){
event.preventDefault();
tabs.tabs('select',$(this).find('a').attr('href'));
});
$(this).find('ul.tabbar').append(li);
$(this).append('<div class="tab" id="' + id + '"></div>');
return this;
},
/**
* Test if a tab exists
* @param {String} id The tab id with #
* @return {Bool}
*/
exist: function(id) {
return (this.find('ul.tabbar li a[href="' + id + '"]').length != 0);
},
/**
* Remove a tab
* @param {String} id The tab id with #
* @return this
*/
remove: function(id) {
this.find('ul.tabbar li a[href="' + id + '"]').parent().remove();
$(id).remove();
if (this.data('tabs').selected == id) {
var first = this.find('ul.tabbar li:first a').attr('href');
this.tabs('select', first);
}
}
};
/*
* Register the 'tabs' method to the jQuery objects
* the first argument of this method is the submethod
* you want to call
*/
$.fn.tabs = function(method) {
if ( methods[method] ) {
return methods[method].apply(this, Array.prototype.slice
.call(arguments, 1));
} else if ( typeof method === 'object' || !method ) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.tabs');
}
};
})(jQuery);
|