forked from kaspernj/bootstrap-tabs-dynamic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-tabs-dynamic.js
143 lines (114 loc) · 2.8 KB
/
bootstrap-tabs-dynamic.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
$.fn.addBSTab = function(id, title, content){
ele_with_id = $("#" + id)
if (ele_with_id.length > 0){
throw "An element with that ID already exist: '" + id + "'."
}
ul = $(this)
container = $($(".tab-content", ul.parent())[0])
li = $("<li />", {
"id": id + "-li",
"data": {
"toggle": "tab"
}
})
a = $("<a />", {
"href": "#" + id,
"text": title
})
a.click(function(){
// setTimeout-hack to make the event work (or the element wont look like its active, it will actually be active though) - kj
a_ele = $(this)
setTimeout(function(){
a_ele.tab("show")
}, 25)
})
li.append(a)
div = $("<div />", {
"id": id,
"class": ["tab-pane"],
"css": {
"display": "none"
}
})
if (typeof content == "string"){
div.html(content)
}else{
div.append(content)
}
ul.append(li)
container.append(div)
a.tab("show")
div.fadeIn("fast")
return {
"content": div,
"a": a
}
}
$.fn.getBSTabByID = function(id){
a = $("a[href=#" + id + "]")
if (a.length <= 0){
throw "Could not find a tab with that ID: '" + id + "'."
}
li = a.parent()
return li
}
$.fn.renameBSTab = function(title){
a = $("a", this)
if (a.length <= 0){
throw "No a element. Was that really a tab?"
}
a.text(title)
}
$.fn.removeBSTab = function(){
a = $("a", this)
if (a.length <= 0){
throw "No a-element could be found. Was that really a tab?"
}
content = $(a.attr("href"))
if (content.length <= 0){
throw "The content container could not be found."
}
content.slideUp("fast", function(){
$(this).remove()
})
a.fadeOut("fast")
$(this).remove()
}
$.fn.currentBSTab = function(){
pane = $(this).parents(".tab-pane")
if (pane.length <= 0){
throw "No parent tab was found. Are you sure there is one?"
}
pane = $(pane[0])
id = pane.attr("id")
if (!id){
throw "The pane found did not have an ID: '" + pane.html() + "'."
}
a = $("a[href=#" + id + "]")
if (a.length <= 0){
throw "The link from the pane could not be found. Did you make an invalid structure? The ID should have been: '" + id + "'."
}
li = a.parent()
if (li.length <= 0){
throw "The link-element did not have a parent: " + a.html()
}
return li
}
$.fn.currentBSTabContent = function(){
pane = $(this).parents(".tab-pane")
if (pane.length <= 0){
throw "No parent tab was found. Are you sure there is one?"
}
pane = $(pane[0])
return pane
}
$.fn.currentBSTabID = function(){
return $(this).currentBSTab().find("a").attr("href").substring(1, 999)
}
$.fn.addBSTabContent = function(id, content) {
ele_with_id = $("#" + id)
if (ele_with_id.length == 0) {
throw "An element with that ID not exist: '" + id + "'."
}
ele_with_id.append(content);
}