Skip to content

Commit 8ba93bf

Browse files
committed
Fix window resize when multiple instances on page
Fixes issue #33
1 parent 2700535 commit 8ba93bf

15 files changed

+471
-18
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-bootstrap-scrolling-tabs",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"main": [
55
"./dist/jquery.scrolling-tabs.js",
66
"./dist/jquery.scrolling-tabs.css"

dist/jquery.scrolling-tabs.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* jquery-bootstrap-scrolling-tabs
3-
* @version v2.0.0
3+
* @version v2.0.1
44
* @link https://github.com/mikejacobson/jquery-bootstrap-scrolling-tabs
55
* @author Mike Jacobson <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT

dist/jquery.scrolling-tabs.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* jquery-bootstrap-scrolling-tabs
3-
* @version v2.0.0
3+
* @version v2.0.1
44
* @link https://github.com/mikejacobson/jquery-bootstrap-scrolling-tabs
55
* @author Mike Jacobson <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -224,6 +224,7 @@
224224

225225
;(function ($, window) {
226226
'use strict';
227+
/* jshint unused:false */
227228

228229
/* exported CONSTANTS */
229230
var CONSTANTS = {
@@ -283,9 +284,12 @@
283284
timeout = setTimeout(delayed, threshold || 100);
284285
};
285286
};
286-
$.fn[sr] = function (fn) { return fn ? this.bind(CONSTANTS.EVENTS.WINDOW_RESIZE, debounce(fn)) : this.trigger(sr); };
287+
$.fn[sr] = function (fn, customEventName) {
288+
var eventName = customEventName || CONSTANTS.EVENTS.WINDOW_RESIZE;
289+
return fn ? this.bind(eventName, debounce(fn)) : this.trigger(sr);
290+
};
287291

288-
})('smartresize');
292+
})('smartresizeScrtabs');
289293

290294
/* ***********************************************************************************
291295
* ElementsHandler - Class that each instance of ScrollingTabsControl will instantiate
@@ -444,7 +448,8 @@
444448
var ehd = this,
445449
stc = ehd.stc,
446450
evh = stc.eventHandlers,
447-
ev = CONSTANTS.EVENTS;
451+
ev = CONSTANTS.EVENTS,
452+
resizeEventName = ev.WINDOW_RESIZE + stc.instanceId;
448453

449454
if (settings.enableSwiping) {
450455
ehd.listenForTouchEvents();
@@ -469,7 +474,9 @@
469474
.on(ev.CLICK, stc.tabClickHandler);
470475
}
471476

472-
stc.$win.off('.scrtabs').smartresize(function (e) { evh.handleWindowResize.call(evh, e); });
477+
stc.$win
478+
.off(resizeEventName)
479+
.smartresizeScrtabs(function (e) { evh.handleWindowResize.call(evh, e); }, resizeEventName);
473480

474481
$('body').on(CONSTANTS.EVENTS.FORCE_REFRESH, stc.elementsHandler.refreshAllElementSizes.bind(stc.elementsHandler));
475482
};
@@ -921,6 +928,7 @@
921928
var stc = this;
922929

923930
stc.$tabsContainer = $tabsContainer;
931+
stc.instanceId = $.fn.scrollingTabs.nextInstanceId++;
924932

925933
stc.movableContainerLeftPos = 0;
926934
stc.scrollArrowsVisible = false;
@@ -1520,6 +1528,7 @@
15201528
});
15211529

15221530
function handleClickOnDropdownMenuItem() {
1531+
/* jshint validthis: true */
15231532
var $selectedMenuItemAnc = $(this),
15241533
$selectedMenuItemLi = $selectedMenuItemAnc.parent('li'),
15251534
$selectedMenuItemDropdownMenu = $selectedMenuItemLi.parent('.dropdown-menu'),
@@ -1603,6 +1612,7 @@
16031612
}
16041613

16051614
function scrollToActiveTab() {
1615+
/* jshint validthis: true */
16061616
var $targetElInstance = $(this),
16071617
scrtabsData = $targetElInstance.data('scrtabs');
16081618

@@ -1669,6 +1679,7 @@
16691679
};
16701680

16711681
function destroyPlugin() {
1682+
/* jshint validthis: true */
16721683
var $targetElInstance = $(this),
16731684
scrtabsData = $targetElInstance.data('scrtabs'),
16741685
$tabsContainer;
@@ -1727,7 +1738,10 @@
17271738

17281739
$targetElInstance.removeData('scrtabs');
17291740

1730-
$(window).off(CONSTANTS.EVENTS.WINDOW_RESIZE);
1741+
while(--$.fn.scrollingTabs.nextInstanceId >= 0) {
1742+
$(window).off(CONSTANTS.EVENTS.WINDOW_RESIZE + $.fn.scrollingTabs.nextInstanceId);
1743+
}
1744+
17311745
$('body').off(CONSTANTS.EVENTS.FORCE_REFRESH);
17321746
}
17331747

@@ -1743,6 +1757,8 @@
17431757
}
17441758
};
17451759

1760+
$.fn.scrollingTabs.nextInstanceId = 0;
1761+
17461762
$.fn.scrollingTabs.defaults = {
17471763
tabs: null,
17481764
propPaneId: 'paneId',

dist/jquery.scrolling-tabs.min.css

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

dist/jquery.scrolling-tabs.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-bootstrap-scrolling-tabs",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "jQuery plugin for scrollable Bootstrap Tabs",
55
"homepage": "https://github.com/mikejacobson/jquery-bootstrap-scrolling-tabs",
66
"bugs": "https://github.com/mikejacobson/jquery-bootstrap-scrolling-tabs/issues",

run/data-driven-mult.html

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
7+
<link rel="stylesheet" href="../dist/jquery.scrolling-tabs.css">
8+
9+
<style>
10+
.left-side {
11+
width: 50%;
12+
}
13+
14+
.right-side {
15+
width: 50%;
16+
}
17+
18+
.st-demo-header {
19+
background-color: #666666;
20+
color: white;
21+
font-size: 24px;
22+
padding: 8px 24px;
23+
}
24+
25+
.st-demo-header button {
26+
color: black;
27+
font-size: 12px;
28+
}
29+
30+
.st-demo-subheader {
31+
background-color: #f0f0f0;
32+
color: #333;
33+
font-size: 16px;
34+
height: 65px;
35+
margin-top: 50px;
36+
padding: 8px 24px;
37+
}
38+
39+
.st-demo-subheader:first-child {
40+
margin-top: 0;
41+
}
42+
</style>
43+
</head>
44+
45+
<body>
46+
47+
<div class="st-demo-header">
48+
<div>jquery-bootstrap-scrolling-tabs Demo - Data Driven</div>
49+
<div>Using Tabs Array</div>
50+
<div>
51+
<button type="button" class="btn-add-tab">Add Tab</button>
52+
<button type="button" class="btn-remove-tab">Remove Tab</button>
53+
<button type="button" class="btn-update-tab">Update Tab</button>
54+
<button type="button" class="btn-move-tab">Move Tabs</button>
55+
<button type="button" class="btn-destroy">Destroy</button>
56+
</div>
57+
58+
</div>
59+
60+
<!-- build .nav-tabs and .tab-content in here -->
61+
<div id="tabs1" class=tabs-inside-here></div>
62+
<br /><br />
63+
<div id="tabs2" class="tabs-inside-here"></div>
64+
65+
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
66+
<script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
67+
68+
<script src="../dist/jquery.scrolling-tabs.js"></script>
69+
70+
<script>
71+
;(function() {
72+
'use strict';
73+
74+
75+
var tabs = [
76+
{ paneId: 'tab00', title: 'Tab Index 0', content: 'Tab Index 0 Content', active: true, disabled: false },
77+
{ paneId: 'tab01', title: 'Tab Index 1', content: 'Tab Index 1 Content', active: false, disabled: false },
78+
{ paneId: 'tab02', title: 'Tab Index 2', content: 'Tab Index 2 Content', active: false, disabled: false },
79+
{ paneId: 'tab03', title: 'Tab Index 3', content: 'Tab Index 3 Content', active: false, disabled: false },
80+
{ paneId: 'tab04', title: 'Tab Index 4', content: 'Tab Index 4 Content', active: false, disabled: true },
81+
{ paneId: 'tab05', title: 'Tab Index 5', content: 'Tab Index 5 Content', active: false, disabled: false },
82+
{ paneId: 'tab06', title: 'Tab Index 6', content: 'Tab Index 6 Content', active: false, disabled: false },
83+
{ paneId: 'tab07', title: 'Tab Index 7', content: 'Tab Index 7 Content', active: false, disabled: false },
84+
{ paneId: 'tab08', title: 'Tab Index 8', content: 'Tab Index 8 Content', active: false, disabled: false },
85+
{ paneId: 'tab09', title: 'Tab Index 9', content: 'Tab Index 9 Content', active: false, disabled: false },
86+
{ paneId: 'tab10', title: 'Tab Index 10', content: 'Tab Index 10 Content', active: false, disabled: false },
87+
{ paneId: 'tab11', title: 'Tab Index 11', content: 'Tab Index 11 Content', active: false, disabled: false }
88+
];
89+
90+
var tabs2 = [
91+
{ paneId: 'tab2_00', title: 'Tab2 Index 0', content: 'Tab2 Index 0 Content', active: true, disabled: false },
92+
{ paneId: 'tab2_01', title: 'Tab2 Index 1', content: 'Tab2 Index 1 Content', active: false, disabled: false },
93+
{ paneId: 'tab2_02', title: 'Tab2 Index 2', content: 'Tab2 Index 2 Content', active: false, disabled: false },
94+
{ paneId: 'tab2_03', title: 'Tab2 Index 3', content: 'Tab2 Index 3 Content', active: false, disabled: false },
95+
{ paneId: 'tab2_04', title: 'Tab2 Index 4', content: 'Tab2 Index 4 Content', active: false, disabled: true },
96+
{ paneId: 'tab2_05', title: 'Tab2 Index 5', content: 'Tab2 Index 5 Content', active: false, disabled: false },
97+
{ paneId: 'tab2_06', title: 'Tab2 Index 6', content: 'Tab2 Index 6 Content', active: false, disabled: false },
98+
{ paneId: 'tab2_07', title: 'Tab2 Index 7', content: 'Tab2 Index 7 Content', active: false, disabled: false },
99+
{ paneId: 'tab2_08', title: 'Tab2 Index 8', content: 'Tab2 Index 8 Content', active: false, disabled: false },
100+
{ paneId: 'tab2_09', title: 'Tab2 Index 9', content: 'Tab2 Index 9 Content', active: false, disabled: false },
101+
{ paneId: 'tab2_10', title: 'Tab2 Index 10', content: 'Tab2 Index 10 Content', active: false, disabled: false },
102+
{ paneId: 'tab2_11', title: 'Tab2 Index 11', content: 'Tab2 Index 11 Content', active: false, disabled: false }
103+
],
104+
lastTabId = 11;
105+
106+
107+
108+
$(activate);
109+
110+
111+
function activate() {
112+
113+
$('#tabs1').scrollingTabs({
114+
tabs: tabs, // required,
115+
propPaneId: 'paneId', // optional - pass in default value for demo purposes
116+
propTitle: 'title', // optional - pass in default value for demo purposes
117+
propActive: 'active', // optional - pass in default value for demo purposes
118+
propDisabled: 'disabled', // optional - pass in default value for demo purposes
119+
propContent: 'content', // optional - pass in default value for demo purposes
120+
scrollToTabEdge: false, // optional - pass in default value for demo purposes
121+
disableScrollArrowsOnFullyScrolled: true,
122+
enableSwiping: true,
123+
//widthMultiplier: 0.7,
124+
tabClickHandler: function () {
125+
console.log("click!! ", Date.now());
126+
}
127+
});
128+
129+
$('#tabs2').scrollingTabs({
130+
tabs: tabs2, // required,
131+
propPaneId: 'paneId', // optional - pass in default value for demo purposes
132+
propTitle: 'title', // optional - pass in default value for demo purposes
133+
propActive: 'active', // optional - pass in default value for demo purposes
134+
propDisabled: 'disabled', // optional - pass in default value for demo purposes
135+
propContent: 'content', // optional - pass in default value for demo purposes
136+
scrollToTabEdge: false, // optional - pass in default value for demo purposes
137+
disableScrollArrowsOnFullyScrolled: true,
138+
enableSwiping: true,
139+
//widthMultiplier: 0.7,
140+
tabClickHandler: function () {
141+
console.log("click!! ", Date.now());
142+
}
143+
});
144+
145+
$('.btn-add-tab').click(addTab);
146+
$('.btn-remove-tab').click(removeTab);
147+
$('.btn-update-tab').click(updateTab);
148+
$('.btn-move-tab').click(moveTab);
149+
$('.btn-destroy').click(destroy);
150+
}
151+
152+
function destroy() {
153+
$('.tabs-inside-here').scrollingTabs('destroy');
154+
}
155+
156+
function updateTab() {
157+
console.log("update " + tabs[1].title);
158+
159+
tabs[1].title = 'UPDATED ' + tabs[1].title;
160+
tabs[1].content = 'UPDATED ' + tabs[1].content;
161+
162+
$('.tabs-inside-here').scrollingTabs('refresh');
163+
}
164+
165+
function moveTab() {
166+
console.log("move " + tabs[1].title + " to after " + tabs[4].title +
167+
", move " + tabs[9].title + " to before " + tabs[6].title);
168+
169+
tabs.splice(4, 0, tabs.splice(1, 1)[0]); // move 1 to right after 4
170+
tabs.splice(6, 0, tabs.splice(9, 1)[0]); // move 9 to right before 6
171+
172+
$('.tabs-inside-here').scrollingTabs('refresh');
173+
}
174+
175+
function addTab() {
176+
var newTab = {
177+
paneId: 'tab' + (++lastTabId),
178+
title: 'Tab Index ' + lastTabId,
179+
content: 'Tab Index ' + lastTabId + ' Content',
180+
active: true,
181+
disabled: false
182+
};
183+
184+
console.log("append new tab ", newTab.title);
185+
186+
// deactivate currently active tab
187+
tabs.some(function (tab) {
188+
if (tab.active) {
189+
tab.active = false;
190+
return true; // exit loop
191+
}
192+
});
193+
194+
tabs.push(newTab);
195+
196+
$('#tabs1').scrollingTabs('refresh', {
197+
forceActiveTab: true // make our new tab active
198+
});
199+
}
200+
201+
function removeTab() {
202+
console.log("remove tab ", tabs[2].title);
203+
204+
tabs.splice(2, 1);
205+
206+
$('.tabs-inside-here').scrollingTabs('refresh');
207+
}
208+
209+
}());
210+
211+
</script>
212+
</body>
213+
214+
</html>

0 commit comments

Comments
 (0)