Skip to content

Commit 7b14fb0

Browse files
author
Stephen Hoogendijk
committed
Fixed issue with bootstrap 2 accordions, fixed hash-set functionality, minor fixed
1 parent fd34b28 commit 7b14fb0

File tree

3 files changed

+62
-47
lines changed

3 files changed

+62
-47
lines changed

demo/index.html

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,26 @@ <h4 class="panel-title">
6363
<div class="panel panel-default">
6464
<div class="panel-heading">
6565
<h4 class="panel-title">
66-
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
67-
Collapsible Group Item #2
66+
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" data-tab-url="remote/delay.html" data-tab-delay="3000">
67+
Simple remote data (with 3 second delay)
6868
</a>
6969
</h4>
7070
</div>
7171
<div id="collapseTwo" class="panel-collapse collapse">
72-
<div class="panel-body">
73-
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
72+
<div class="panel-body">&nbsp;
7473
</div>
7574
</div>
7675
</div>
7776
<div class="panel panel-default">
7877
<div class="panel-heading">
7978
<h4 class="panel-title">
80-
<a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
81-
Collapsible Group Item #3
79+
<a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" data-toggle="tab" data-tab-url="remote/callback.html" data-tab-callback="sampleCallback">
80+
Remote data with callback
8281
</a>
8382
</h4>
8483
</div>
8584
<div id="collapseThree" class="panel-collapse collapse">
86-
<div class="panel-body">
87-
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
85+
<div class="panel-body">&nbsp;
8886
</div>
8987
</div>
9088
</div>

js/bootstrap-remote-tabs.js

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ var tabShowEvent = (bootstrapVersion2 ? 'show' : 'show.bs.tab');
1919
var accordionShowEvent = (bootstrapVersion2 ? 'show' : 'show.bs.collapse');
2020

2121
$(function() {
22+
// try to navigate to the tab/accordion last given in the URL
2223
var hash = document.location.hash;
2324
if (hash) {
24-
var hasTab = $('.nav-tabs a[href*='+hash+']');
25+
var hasTab = $('[data-toggle=tab][href='+hash+']');
2526
if (hasTab) {
26-
hasTab.tab(tabShowEvent);
27+
hasTab.tab('show');
2728
}
2829

29-
var hasAccordion = $('.accordion-heading a[href*='+hash+']');
30-
if (hasAccordion) {
31-
hasAccordion.collapse(accordionShowEvent);
30+
var hasAccordion = $('[data-toggle=collapse][href='+hash+']');
31+
if (hasAccordion) {
32+
// for some reason we cannot execute the 'show' event for an accordion properly, so here's a workaround
33+
if (hasAccordion[0] != $('[data-toggle=collapse]:first')[0]) {
34+
hasAccordion.click();
35+
}
3236
}
3337
}
3438
});
@@ -50,30 +54,32 @@ var RemoteTabs = function() {
5054

5155
// enable all remote data tabs
5256
$('[data-toggle=tab], [data-toggle=collapse]').each(function(k, tab) {
53-
var tabObj = $(tab),
54-
tabDiv,
55-
tabData,
56-
tabCallback,
57+
var bsObj = $(tab),
58+
bsDiv,
59+
bsData,
60+
bsCallback,
5761
url,
5862
simulateDelay,
5963
alwaysRefresh,
60-
hasOpenPanel = false;
64+
hasOpenPanel = false,
65+
originalObj;
6166

6267
// check if the tab has a data-url property
63-
if(tabObj.is('[data-tab-url]')) {
64-
url = tabObj.attr('data-tab-url');
65-
tabDiv = $( '#' + tabObj.attr('href').split('#')[1]);
66-
tabData = tabObj.attr('data-tab-json') || [];
67-
tabCallback = tabObj.attr('data-tab-callback') || null;
68-
simulateDelay = tabObj.attr('data-tab-delay') || null;
69-
alwaysRefresh = (tabObj.is('[data-tab-always-refresh]')
70-
&& tabObj.attr('data-tab-always-refresh') == 'true') || null,
71-
showEvent = (tabObj.attr('data-toggle') == 'tab' ? tabShowEvent : accordionShowEvent);
72-
73-
if(tabData.length > 0) {
68+
if(bsObj.is('[data-tab-url]')) {
69+
url = bsObj.attr('data-tab-url');
70+
bsDiv = $( '#' + bsObj.attr('href').split('#')[1]);
71+
bsData = bsObj.attr('data-tab-json') || [];
72+
bsCallback = bsObj.attr('data-tab-callback') || null;
73+
simulateDelay = bsObj.attr('data-tab-delay') || null;
74+
alwaysRefresh = (bsObj.is('[data-tab-always-refresh]')
75+
&& bsObj.attr('data-tab-always-refresh') == 'true') || null,
76+
originalObj = bsObj,
77+
showEvent = (bsObj.attr('data-toggle') == 'tab' ? tabShowEvent : accordionShowEvent);
78+
79+
if(bsData.length > 0) {
7480
try
7581
{
76-
tabData = $.parseJSON(tabData);
82+
bsData = $.parseJSON(bsData);
7783
} catch (exc) {
7884
console.log('Invalid json passed to data-tab-json');
7985
console.log(exc);
@@ -82,22 +88,22 @@ var RemoteTabs = function() {
8288
}
8389

8490
if (showEvent == accordionShowEvent) {
85-
hasOpenPanel = tabDiv.hasClass('in');
91+
hasOpenPanel = bsDiv.hasClass('in');
8692
// when an accordion is triggered, make the div the triggered object instead of the link
8793
if (bootstrapVersion2) {
88-
tabObj = tabObj.parent();
94+
bsObj = bsObj.parent();
8995
} else {
90-
tabObj = tabObj.parents('.panel');
96+
bsObj = bsObj.parents('.panel');
9197
}
9298

9399
// If there is a panel already opened, make sure the data url is fetched
94100
if (hasOpenPanel) {
95-
me._triggerChange(null, url, tabData, tabCallback, tabObj, tabDiv, simulateDelay, alwaysRefresh);
101+
me._triggerChange(null, url, bsData, bsCallback, bsObj, bsDiv, simulateDelay, alwaysRefresh, originalObj);
96102
}
97103
}
98104

99-
tabObj.on(showEvent, function(e) {
100-
me._triggerChange(e, url, tabData, tabCallback, tabObj, tabDiv, simulateDelay, alwaysRefresh);
105+
bsObj.on(showEvent, function(e) {
106+
me._triggerChange(e, url, bsData, bsCallback, bsObj, bsDiv, simulateDelay, alwaysRefresh, originalObj);
101107
});
102108

103109
}
@@ -108,32 +114,43 @@ var RemoteTabs = function() {
108114
* Trigger the change
109115
*
110116
* @param e
111-
* @param objDetails
117+
* @param url
118+
* @param bsData
119+
* @param bsCallback
120+
* @param bsObj
121+
* @param bsDiv
122+
* @param simulateDelay
123+
* @param alwaysRefresh
124+
* @param originalObj
112125
*/
113-
_triggerChange: function(e, url, tabData, tabCallback, tabObj, tabDiv, simulateDelay, alwaysRefresh) {
126+
_triggerChange: function(e, url, bsData, bsCallback, bsObj, bsDiv, simulateDelay, alwaysRefresh, originalObj) {
114127
var me = this;
115128

116129
// change the hash of the location
117-
if (e && e.target.hash != 'undefined') {
118-
window.location.hash = e.target.hash;
130+
if (e) {
131+
if (typeof e.target.hash != 'undefined') {
132+
window.location.hash = e.target.hash;
133+
} else {
134+
window.location.hash = originalObj.prop('hash');
135+
}
119136
}
120137

121-
if ((!tabObj.hasClass("loaded") || alwaysRefresh) &&
122-
!tabObj.hasClass('loading')) {
138+
if ((!bsObj.hasClass("loaded") || alwaysRefresh) &&
139+
!bsObj.hasClass('loading')) {
123140

124141
if(me.hasLoadingMask) {
125-
tabDiv.mask('Loading...');
142+
bsDiv.mask('Loading...');
126143
}
127-
tabObj.addClass('loading');
144+
bsObj.addClass('loading');
128145

129146
// delay the json call if it has been given a value
130147
if(simulateDelay) {
131148
clearTimeout(window.timer);
132149
window.timer=setTimeout(function(){
133-
me._executeRemoteCall(url, tabData, tabCallback, tabObj, tabDiv);
150+
me._executeRemoteCall(url, bsData, bsCallback, bsObj, bsDiv);
134151
}, simulateDelay);
135152
} else {
136-
me._executeRemoteCall(url, tabData, tabCallback, tabObj, tabDiv);
153+
me._executeRemoteCall(url, bsData, bsCallback, bsObj, bsDiv);
137154
}
138155

139156

js/bootstrap-remote-tabs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)