forked from mikejacobson/jquery-bootstrap-scrolling-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.scrolling-tabs.js
1668 lines (1339 loc) · 54.6 KB
/
jquery.scrolling-tabs.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* jquery-bootstrap-scrolling-tabs
* @version v0.7.0
* @link https://github.com/mikejacobson/jquery-bootstrap-scrolling-tabs
* @author Mike Jacobson <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
*
* jQuery plugin version of Angular directive angular-bootstrap-scrolling-tabs:
* https://github.com/mikejacobson/angular-bootstrap-scrolling-tabs
*
* Usage:
*
* Use case #1: HTML-defined tabs
* ------------------------------
* Demo: http://plnkr.co/edit/thyD0grCxIjyU4PoTt4x?p=preview
*
* Sample HTML:
*
* <!-- Nav tabs -->
* <ul class="nav nav-tabs" role="tablist">
* <li role="presentation" class="active"><a href="#tab1" role="tab" data-toggle="tab">Tab Number 1</a></li>
* <li role="presentation"><a href="#tab2" role="tab" data-toggle="tab">Tab Number 2</a></li>
* <li role="presentation"><a href="#tab3" role="tab" data-toggle="tab">Tab Number 3</a></li>
* <li role="presentation"><a href="#tab4" role="tab" data-toggle="tab">Tab Number 4</a></li>
* </ul>
*
* <!-- Tab panes -->
* <div class="tab-content">
* <div role="tabpanel" class="tab-pane active" id="tab1">Tab 1 content...</div>
* <div role="tabpanel" class="tab-pane" id="tab2">Tab 2 content...</div>
* <div role="tabpanel" class="tab-pane" id="tab3">Tab 3 content...</div>
* <div role="tabpanel" class="tab-pane" id="tab4">Tab 4 content...</div>
* </div>
*
*
* JavaScript:
*
* $('.nav-tabs').scrollingTabs();
*
*
* Use Case #2: Data-driven tabs
* -----------------------------
* Demo: http://plnkr.co/edit/MWBjLnTvJeetjU3NEimg?p=preview
*
* Sample HTML:
*
* <!-- build .nav-tabs and .tab-content in here -->
* <div id="tabs-inside-here"></div>
*
*
* JavaScript:
*
* $('#tabs-inside-here').scrollingTabs({
* tabs: tabs, // required
* propPaneId: 'paneId', // optional
* propTitle: 'title', // optional
* propActive: 'active', // optional
* propDisabled: 'disabled', // optional
* propContent: 'content', // optional
* ignoreTabPanes: false, // optional
* scrollToTabEdge: false, // optional
* disableScrollArrowsOnFullyScrolled: false, // optional
* reverseScroll: false // optional
* });
*
* Settings/Options:
*
* tabs: tabs data array
* prop*: name of your tab object's property name that
* corresponds to that required tab property if
* your property name is different than the
* standard name (paneId, title, etc.)
* ignoreTabPanes: relevant for data-driven tabs only--set to true if
* you want the plugin to only touch the tabs
* and to not generate the tab pane elements
* that go in .tab-content. By default, the plugin
* will generate the tab panes based on the content
* property in your tab data, if a content property
* is present.
* scrollToTabEdge: set to true if you want to force full-width tabs
* to display at the left scroll arrow. i.e., if the
* scrolling stops with only half a tab showing,
* it will snap the tab to its edge so the full tab
* shows.
* disableScrollArrowsOnFullyScrolled:
* set to true if you want the left scroll arrow to
* disable when the tabs are scrolled fully left,
* and the right scroll arrow to disable when the tabs
* are scrolled fully right.
* reverseScroll:
* set to true if you want the left scroll arrow to
* slide the tabs left instead of right, and the right
* scroll arrow to slide the tabs right.
* widthMultiplier:
* set to a value less than 1 if you want the tabs
* container to be less than the full width of its
* parent element. For example, set it to 0.5 if you
* want the tabs container to be half the width of
* its parent.
* tabClickHandler:
* a callback function to execute any time a tab is clicked.
* The function is simply passed as the event handler
* to jQuery's .on(), so the function will receive
* the jQuery event as an argument, and the 'this'
* inside the function will be the clicked tab's anchor
* element.
* cssClassLeftArrow, cssClassRightArrow:
* custom values for the class attributes for the
* left and right scroll arrows. The defaults are
* 'glyphicon glyphicon-chevron-left' and
* 'glyphicon glyphicon-chevron-right'.
* Using different icons might require you to add
* custom styling to the arrows to position the icons
* correctly; the arrows can be targeted with these
* selectors:
* .scrtabs-tab-scroll-arrow
* .scrtabs-tab-scroll-arrow-left
* .scrtabs-tab-scroll-arrow-right
*
*
* On tabs data change:
*
* $('#tabs-inside-here').scrollingTabs('refresh');
*
* On tabs data change, if you want the active tab to be set based on
* the updated tabs data (i.e., you want to override the current
* active tab setting selected by the user), for example, if you
* added a new tab and you want it to be the active tab:
*
* $('#tabs-inside-here').scrollingTabs('refresh', {
* forceActiveTab: true
* });
*
* Any options that can be passed into the plugin can be set on the
* plugin's 'defaults' object instead so you don't have to pass them in:
*
* $.fn.scrollingTabs.defaults.tabs = tabs;
* $.fn.scrollingTabs.defaults.forceActiveTab = true;
* $.fn.scrollingTabs.defaults.scrollToTabEdge = true;
* $.fn.scrollingTabs.defaults.disableScrollArrowsOnFullyScrolled = true;
* $.fn.scrollingTabs.defaults.reverseScroll = true;
* $.fn.scrollingTabs.defaults.widthMultiplier = 0.5;
* $.fn.scrollingTabs.defaults.tabClickHandler = function () { };
*
*
* Methods
* -----------------------------
* - refresh
* On window resize, the tabs should refresh themselves, but to force a refresh:
*
* $('.nav-tabs').scrollingTabs('refresh');
*
* - scrollToActiveTab
* On window resize, the active tab will automatically be scrolled to
* if it ends up offscreen, but you can also programmatically force a
* scroll to the active tab any time (if, for example, you're
* programmatically setting the active tab) by calling the
* 'scrollToActiveTab' method:
*
* $('.nav-tabs').scrollingTabs('scrollToActiveTab');
*
*
* Events
* -----------------------------
* The plugin triggers event 'ready.scrtabs' when the tabs have
* been wrapped in the scroller and are ready for viewing:
*
* $('.nav-tabs')
* .scrollingTabs()
* .on('ready.scrtabs', function() {
* // tabs ready, do my other stuff...
* });
*
* $('#tabs-inside-here')
* .scrollingTabs({ tabs: tabs })
* .on('ready.scrtabs', function() {
* // tabs ready, do my other stuff...
* });
*
*
* Destroying
* -----------------------------
* To destroy:
*
* $('.nav-tabs').scrollingTabs('destroy');
*
* $('#tabs-inside-here').scrollingTabs('destroy');
*
* If you were wrapping markup, the markup will be restored; if your tabs
* were data-driven, the tabs will be destroyed along with the plugin.
*
*/
;(function ($, window) {
'use strict';
var CONSTANTS = {
CONTINUOUS_SCROLLING_TIMEOUT_INTERVAL: 50, // timeout interval for repeatedly moving the tabs container
// by one increment while the mouse is held down--decrease to
// make mousedown continous scrolling faster
SCROLL_ARROW_WIDTH: 20,
SCROLL_OFFSET_FRACTION: 6, // each click moves the container this fraction of the fixed container--decrease
// to make the tabs scroll farther per click
DATA_KEY_DDMENU_MODIFIED: 'scrtabsddmenumodified',
DATA_KEY_IS_MOUSEDOWN: 'scrtabsismousedown',
CSS_CLASSES: {
SCROLL_ARROW_DISABLE: 'scrtabs-disable'
},
SLIDE_DIRECTION: {
LEFT: 1,
RIGHT: 2
},
EVENTS: {
CLICK: 'click.scrtabs',
DROPDOWN_MENU_HIDE: 'hide.bs.dropdown.scrtabs',
DROPDOWN_MENU_SHOW: 'show.bs.dropdown.scrtabs',
FORCE_REFRESH: 'forcerefresh.scrtabs',
MOUSEDOWN: 'mousedown.scrtabs touchstart.scrtabs',
MOUSEUP: 'mouseup.scrtabs touchend.scrtabs',
WINDOW_RESIZE: 'resize.scrtabs',
TABS_READY: 'ready.scrtabs'
}
};
// smartresize from Paul Irish (debounced window resize)
(function (sr) {
var debounce = function (func, threshold, execAsap) {
var timeout;
return function debounced() {
var obj = this, args = arguments;
function delayed() {
if (!execAsap) {
func.apply(obj, args);
}
timeout = null;
}
if (timeout) {
clearTimeout(timeout);
} else if (execAsap) {
func.apply(obj, args);
}
timeout = setTimeout(delayed, threshold || 100);
};
};
$.fn[sr] = function (fn) { return fn ? this.bind(CONSTANTS.EVENTS.WINDOW_RESIZE, debounce(fn)) : this.trigger(sr); };
})('smartresize');
/* ***********************************************************************************
* ElementsHandler - Class that each instance of ScrollingTabsControl will instantiate
* **********************************************************************************/
function ElementsHandler(scrollingTabsControl) {
var ehd = this;
ehd.stc = scrollingTabsControl;
}
// ElementsHandler prototype methods
(function (p) {
p.initElements = function (options) {
var ehd = this;
ehd.setElementReferences();
ehd.setEventListeners();
};
p.refreshAllElementSizes = function () {
var ehd = this,
stc = ehd.stc,
smv = stc.scrollMovement,
scrollArrowsWereVisible = stc.scrollArrowsVisible,
actionsTaken = {
didScrollToActiveTab: false
},
isPerformingSlideAnim = false,
minPos;
ehd.setElementWidths();
ehd.setScrollArrowVisibility();
// this could have been a window resize or the removal of a
// dynamic tab, so make sure the movable container is positioned
// correctly because, if it is far to the left and we increased the
// window width, it's possible that the tabs will be too far left,
// beyond the min pos.
if (stc.scrollArrowsVisible) {
// make sure container not too far left
minPos = smv.getMinPos();
isPerformingSlideAnim = smv.scrollToActiveTab({
isOnWindowResize: true
});
if (!isPerformingSlideAnim) {
smv.refreshScrollArrowsDisabledState();
if (stc.movableContainerLeftPos < minPos) {
smv.incrementMovableContainerRight(minPos);
}
}
actionsTaken.didScrollToActiveTab = true;
} else if (scrollArrowsWereVisible) {
// scroll arrows went away after resize, so position movable container at 0
stc.movableContainerLeftPos = 0;
smv.slideMovableContainerToLeftPos();
}
return actionsTaken;
};
p.setElementReferences = function () {
var ehd = this,
stc = ehd.stc,
$tabsContainer = stc.$tabsContainer,
$leftArrow = $tabsContainer.find('.scrtabs-tab-scroll-arrow-left'),
$rightArrow = $tabsContainer.find('.scrtabs-tab-scroll-arrow-right');
stc.isNavPills = false;
stc.$fixedContainer = $tabsContainer.find('.scrtabs-tabs-fixed-container');
stc.$movableContainer = $tabsContainer.find('.scrtabs-tabs-movable-container');
stc.$tabsUl = $tabsContainer.find('.nav-tabs');
// check for pills
if (!stc.$tabsUl.length) {
stc.$tabsUl = $tabsContainer.find('.nav-pills');
if (stc.$tabsUl.length) {
stc.isNavPills = true;
}
}
stc.$tabsLiCollection = stc.$tabsUl.find('> li');
stc.$slideLeftArrow = stc.reverseScroll ? $leftArrow : $rightArrow;
stc.$slideRightArrow = stc.reverseScroll ? $rightArrow : $leftArrow;
stc.$scrollArrows = stc.$slideLeftArrow.add(stc.$slideRightArrow);
stc.$win = $(window);
};
p.setElementWidths = function () {
var ehd = this,
stc = ehd.stc;
stc.winWidth = stc.$win.width();
stc.scrollArrowsCombinedWidth = stc.$slideLeftArrow.outerWidth() + stc.$slideRightArrow.outerWidth();
ehd.setFixedContainerWidth();
ehd.setMovableContainerWidth();
};
p.setEventListeners = function () {
var ehd = this,
stc = ehd.stc,
evh = stc.eventHandlers,
ev = CONSTANTS.EVENTS;
stc.$slideLeftArrow
.off('.scrtabs')
.on(ev.MOUSEDOWN, function (e) { evh.handleMousedownOnSlideMovContainerLeftArrow.call(evh, e); })
.on(ev.MOUSEUP, function (e) { evh.handleMouseupOnSlideMovContainerLeftArrow.call(evh, e); })
.on(ev.CLICK, function (e) { evh.handleClickOnSlideMovContainerLeftArrow.call(evh, e); });
stc.$slideRightArrow
.off('.scrtabs')
.on(ev.MOUSEDOWN, function (e) { evh.handleMousedownOnSlideMovContainerRightArrow.call(evh, e); })
.on(ev.MOUSEUP, function (e) { evh.handleMouseupOnSlideMovContainerRightArrow.call(evh, e); })
.on(ev.CLICK, function (e) { evh.handleClickOnSlideMovContainerRightArrow.call(evh, e); });
if (stc.tabClickHandler) {
stc.$tabsLiCollection
.find('a[data-toggle="tab"]')
.off(ev.CLICK)
.on(ev.CLICK, stc.tabClickHandler);
}
stc.$win.smartresize(function (e) { evh.handleWindowResize.call(evh, e); });
$('body').on(CONSTANTS.EVENTS.FORCE_REFRESH, stc.elementsHandler.refreshAllElementSizes.bind(stc.elementsHandler));
};
p.setFixedContainerWidth = function () {
var ehd = this,
stc = ehd.stc,
tabsContainerRect = stc.$tabsContainer.get(0).getBoundingClientRect();
/**
* @author poletaew
* It solves problem with rounding by jQuery.outerWidth
* If we have real width 100.5 px, jQuery.outerWidth returns us 101 px and we get layout's fail
*/
stc.fixedContainerWidth = tabsContainerRect.width || (tabsContainerRect.right - tabsContainerRect.left);
stc.fixedContainerWidth = stc.fixedContainerWidth * stc.widthMultiplier;
stc.$fixedContainer.width(stc.fixedContainerWidth);
};
p.setFixedContainerWidthForHiddenScrollArrows = function () {
var ehd = this,
stc = ehd.stc;
stc.$fixedContainer.width(stc.fixedContainerWidth);
};
p.setFixedContainerWidthForVisibleScrollArrows = function () {
var ehd = this,
stc = ehd.stc;
stc.$fixedContainer.width(stc.fixedContainerWidth - stc.scrollArrowsCombinedWidth);
};
p.setMovableContainerWidth = function () {
var ehd = this,
stc = ehd.stc,
$tabLi = stc.$tabsUl.find('> li');
stc.movableContainerWidth = 0;
if ($tabLi.length) {
$tabLi.each(function __getLiWidth() {
var $li = $(this),
totalMargin = 0;
if (stc.isNavPills) { // pills have a margin-left, tabs have no margin
totalMargin = parseInt($li.css('margin-left'), 10) + parseInt($li.css('margin-right'), 10);
}
stc.movableContainerWidth += ($li.outerWidth() + totalMargin);
});
stc.movableContainerWidth += 1;
// if the tabs don't span the width of the page, force the
// movable container width to full page width so the bottom
// border spans the page width instead of just spanning the
// width of the tabs
if (stc.movableContainerWidth < stc.fixedContainerWidth) {
stc.movableContainerWidth = stc.fixedContainerWidth;
}
}
stc.$movableContainer.width(stc.movableContainerWidth);
};
p.setScrollArrowVisibility = function () {
var ehd = this,
stc = ehd.stc,
shouldBeVisible = stc.movableContainerWidth > stc.fixedContainerWidth;
if (shouldBeVisible && !stc.scrollArrowsVisible) {
stc.$scrollArrows.show();
stc.scrollArrowsVisible = true;
} else if (!shouldBeVisible && stc.scrollArrowsVisible) {
stc.$scrollArrows.hide();
stc.scrollArrowsVisible = false;
}
if (stc.scrollArrowsVisible) {
ehd.setFixedContainerWidthForVisibleScrollArrows();
} else {
ehd.setFixedContainerWidthForHiddenScrollArrows();
}
};
}(ElementsHandler.prototype));
/* ***********************************************************************************
* EventHandlers - Class that each instance of ScrollingTabsControl will instantiate
* **********************************************************************************/
function EventHandlers(scrollingTabsControl) {
var evh = this;
evh.stc = scrollingTabsControl;
}
// prototype methods
(function (p){
p.handleClickOnSlideMovContainerLeftArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.incrementMovableContainerLeft();
};
p.handleClickOnSlideMovContainerRightArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.incrementMovableContainerRight();
};
p.handleMousedownOnSlideMovContainerLeftArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.$slideLeftArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, true);
stc.scrollMovement.continueSlideMovableContainerLeft();
};
p.handleMousedownOnSlideMovContainerRightArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.$slideRightArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, true);
stc.scrollMovement.continueSlideMovableContainerRight();
};
p.handleMouseupOnSlideMovContainerLeftArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.$slideLeftArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, false);
};
p.handleMouseupOnSlideMovContainerRightArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.$slideRightArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, false);
};
p.handleWindowResize = function (e) {
var evh = this,
stc = evh.stc,
newWinWidth = stc.$win.width();
if (newWinWidth === stc.winWidth) {
return false;
}
stc.winWidth = newWinWidth;
stc.elementsHandler.refreshAllElementSizes();
};
}(EventHandlers.prototype));
/* ***********************************************************************************
* ScrollMovement - Class that each instance of ScrollingTabsControl will instantiate
* **********************************************************************************/
function ScrollMovement(scrollingTabsControl) {
var smv = this;
smv.stc = scrollingTabsControl;
}
// prototype methods
(function (p) {
p.continueSlideMovableContainerLeft = function () {
var smv = this,
stc = smv.stc;
setTimeout(function() {
if (stc.movableContainerLeftPos <= smv.getMinPos() ||
!stc.$slideLeftArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN)) {
return;
}
if (!smv.incrementMovableContainerLeft()) { // haven't reached max left
smv.continueSlideMovableContainerLeft();
}
}, CONSTANTS.CONTINUOUS_SCROLLING_TIMEOUT_INTERVAL);
};
p.continueSlideMovableContainerRight = function () {
var smv = this,
stc = smv.stc;
setTimeout(function() {
if (stc.movableContainerLeftPos >= 0 ||
!stc.$slideRightArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN)) {
return;
}
if (!smv.incrementMovableContainerRight()) { // haven't reached max right
smv.continueSlideMovableContainerRight();
}
}, CONSTANTS.CONTINUOUS_SCROLLING_TIMEOUT_INTERVAL);
};
p.decrementMovableContainerLeftPos = function (minPos) {
var smv = this,
stc = smv.stc;
stc.movableContainerLeftPos -= (stc.fixedContainerWidth / CONSTANTS.SCROLL_OFFSET_FRACTION);
if (stc.movableContainerLeftPos < minPos) {
stc.movableContainerLeftPos = minPos;
} else if (stc.scrollToTabEdge) {
smv.setMovableContainerLeftPosToTabEdge(CONSTANTS.SLIDE_DIRECTION.LEFT);
if (stc.movableContainerLeftPos < minPos) {
stc.movableContainerLeftPos = minPos;
}
}
};
p.disableSlideLeftArrow = function () {
var smv = this,
stc = smv.stc;
if (!stc.disableScrollArrowsOnFullyScrolled || !stc.scrollArrowsVisible) {
return;
}
stc.$slideLeftArrow.addClass(CONSTANTS.CSS_CLASSES.SCROLL_ARROW_DISABLE);
};
p.disableSlideRightArrow = function () {
var smv = this,
stc = smv.stc;
if (!stc.disableScrollArrowsOnFullyScrolled || !stc.scrollArrowsVisible) {
return;
}
stc.$slideRightArrow.addClass(CONSTANTS.CSS_CLASSES.SCROLL_ARROW_DISABLE);
};
p.enableSlideLeftArrow = function () {
var smv = this,
stc = smv.stc;
if (!stc.disableScrollArrowsOnFullyScrolled || !stc.scrollArrowsVisible) {
return;
}
stc.$slideLeftArrow.removeClass(CONSTANTS.CSS_CLASSES.SCROLL_ARROW_DISABLE);
};
p.enableSlideRightArrow = function () {
var smv = this,
stc = smv.stc;
if (!stc.disableScrollArrowsOnFullyScrolled || !stc.scrollArrowsVisible) {
return;
}
stc.$slideRightArrow.removeClass(CONSTANTS.CSS_CLASSES.SCROLL_ARROW_DISABLE);
};
p.getMinPos = function () {
var smv = this,
stc = smv.stc;
return stc.scrollArrowsVisible ? (stc.fixedContainerWidth - stc.movableContainerWidth - stc.scrollArrowsCombinedWidth) : 0;
};
p.getMovableContainerCssLeftVal = function () {
var smv = this,
stc = smv.stc;
return (stc.movableContainerLeftPos === 0) ? '0' : stc.movableContainerLeftPos + 'px';
};
p.incrementMovableContainerLeft = function () {
var smv = this,
stc = smv.stc,
minPos = smv.getMinPos();
smv.decrementMovableContainerLeftPos(minPos);
smv.slideMovableContainerToLeftPos();
smv.enableSlideRightArrow();
// return true if we're fully left, false otherwise
return (stc.movableContainerLeftPos === minPos);
};
p.incrementMovableContainerRight = function (minPos) {
var smv = this,
stc = smv.stc;
// if minPos passed in, the movable container was beyond the minPos
if (minPos) {
stc.movableContainerLeftPos = minPos;
} else {
stc.movableContainerLeftPos += (stc.fixedContainerWidth / CONSTANTS.SCROLL_OFFSET_FRACTION);
if (stc.movableContainerLeftPos > 0) {
stc.movableContainerLeftPos = 0;
} else if (stc.scrollToTabEdge) {
smv.setMovableContainerLeftPosToTabEdge(CONSTANTS.SLIDE_DIRECTION.RIGHT);
}
}
smv.slideMovableContainerToLeftPos();
smv.enableSlideLeftArrow();
// return true if we're fully right, false otherwise
// left pos of 0 is the movable container's max position (farthest right)
return (stc.movableContainerLeftPos === 0);
};
p.refreshScrollArrowsDisabledState = function() {
var smv = this,
stc = smv.stc;
if (!stc.disableScrollArrowsOnFullyScrolled || !stc.scrollArrowsVisible) {
return;
}
if (stc.movableContainerLeftPos >= 0) { // movable container fully right
smv.disableSlideRightArrow();
smv.enableSlideLeftArrow();
return;
}
if (stc.movableContainerLeftPos <= smv.getMinPos()) { // fully left
smv.disableSlideLeftArrow();
smv.enableSlideRightArrow();
return;
}
smv.enableSlideLeftArrow();
smv.enableSlideRightArrow();
};
p.scrollToActiveTab = function (options) {
var smv = this,
stc = smv.stc,
RIGHT_OFFSET_BUFFER = 20,
$activeTab,
activeTabLeftPos,
activeTabRightPos,
rightArrowLeftPos;
if (!stc.scrollArrowsVisible) {
return;
}
$activeTab = stc.$tabsUl.find('li.active');
if (!$activeTab.length) {
return;
}
/**
* @author poletaew
* We need relative offset (depends on $fixedContainer), don't absolute
*/
activeTabLeftPos = $activeTab.offset().left - stc.$fixedContainer.offset().left;
activeTabRightPos = activeTabLeftPos + $activeTab.outerWidth();
rightArrowLeftPos = stc.fixedContainerWidth - RIGHT_OFFSET_BUFFER;
if (activeTabRightPos > rightArrowLeftPos) { // active tab off right side
stc.movableContainerLeftPos -= (activeTabRightPos - rightArrowLeftPos + CONSTANTS.SCROLL_ARROW_WIDTH);
smv.slideMovableContainerToLeftPos();
return true;
} else if (activeTabLeftPos < CONSTANTS.SCROLL_ARROW_WIDTH) { // active tab off left side
stc.movableContainerLeftPos += CONSTANTS.SCROLL_ARROW_WIDTH - activeTabLeftPos;
smv.slideMovableContainerToLeftPos();
return true;
}
return false;
};
p.setMovableContainerLeftPosToTabEdge = function (slideDirection) {
var smv = this,
stc = smv.stc,
offscreenWidth = -stc.movableContainerLeftPos,
totalTabWidth = 0;
// make sure LeftPos is set so that a tab edge will be against the
// left scroll arrow so we won't have a partial, cut-off tab
stc.$tabsLiCollection.each(function (index) {
var tabWidth = $(this).width();
totalTabWidth += tabWidth;
if (totalTabWidth > offscreenWidth) {
stc.movableContainerLeftPos = (slideDirection === CONSTANTS.SLIDE_DIRECTION.RIGHT) ? -(totalTabWidth - tabWidth) : -totalTabWidth;
return false; // exit .each() loop
}
});
};
p.slideMovableContainerToLeftPos = function () {
var smv = this,
stc = smv.stc,
minPos = smv.getMinPos(),
leftVal;
if (stc.movableContainerLeftPos > 0) {
stc.movableContainerLeftPos = 0;
} else if (stc.movableContainerLeftPos < minPos) {
stc.movableContainerLeftPos = minPos;
}
stc.movableContainerLeftPos = stc.movableContainerLeftPos / 1;
leftVal = smv.getMovableContainerCssLeftVal();
smv.performingSlideAnim = true;
stc.$movableContainer.stop().animate({ left: leftVal }, 'slow', function __slideAnimComplete() {
var newMinPos = smv.getMinPos();
smv.performingSlideAnim = false;
// if we slid past the min pos--which can happen if you resize the window
// quickly--move back into position
if (stc.movableContainerLeftPos < newMinPos) {
smv.decrementMovableContainerLeftPos(newMinPos);
stc.$movableContainer.stop().animate({ left: smv.getMovableContainerCssLeftVal() }, 'fast', function() {
smv.refreshScrollArrowsDisabledState();
});
} else {
smv.refreshScrollArrowsDisabledState();
}
});
};
}(ScrollMovement.prototype));
/* **********************************************************************
* ScrollingTabsControl - Class that each directive will instantiate
* **********************************************************************/
function ScrollingTabsControl($tabsContainer) {
var stc = this;
stc.$tabsContainer = $tabsContainer;
stc.movableContainerLeftPos = 0;
stc.scrollArrowsVisible = false;
stc.scrollToTabEdge = false;
stc.disableScrollArrowsOnFullyScrolled = false;
stc.reverseScroll = false;
stc.widthMultiplier = 1;
stc.scrollMovement = new ScrollMovement(stc);
stc.eventHandlers = new EventHandlers(stc);
stc.elementsHandler = new ElementsHandler(stc);
}
// prototype methods
(function (p) {
p.initTabs = function (options, $scroller, readyCallback, attachTabContentToDomCallback) {
var stc = this,
elementsHandler = stc.elementsHandler,
num;
if (options.scrollToTabEdge) {
stc.scrollToTabEdge = true;
}
if (options.disableScrollArrowsOnFullyScrolled) {
stc.disableScrollArrowsOnFullyScrolled = true;
}
if (options.reverseScroll) {
stc.reverseScroll = true;
}
if (options.widthMultiplier !== 1) {
num = Number(options.widthMultiplier); // handle string value
if (!isNaN(num)) {
stc.widthMultiplier = num;
}
}
setTimeout(initTabsAfterTimeout, 100);
function initTabsAfterTimeout() {
var actionsTaken;
// if we're just wrapping non-data-driven tabs, the user might
// have the .nav-tabs hidden to prevent the clunky flash of
// multi-line tabs on page refresh, so we need to make sure
// they're visible before trying to wrap them
$scroller.find('.nav-tabs').show();
elementsHandler.initElements(options);
actionsTaken = elementsHandler.refreshAllElementSizes();
$scroller.css('visibility', 'visible');
if (attachTabContentToDomCallback) {
attachTabContentToDomCallback();
}
if (readyCallback) {
readyCallback();
}
}
};
p.scrollToActiveTab = function(options) {
var stc = this,
smv = stc.scrollMovement;
smv.scrollToActiveTab(options);
};
}(ScrollingTabsControl.prototype));
////////////////////////////////////////////
//
// plugin-specific stuff
//
////////////////////////////////////////////
var tabElements = (function () {
return {
getElTabPaneForLi: getElTabPaneForLi,
getNewElNavTabs: getNewElNavTabs,
getNewElScrollerElementWrappingNavTabsInstance: getNewElScrollerElementWrappingNavTabsInstance,
getNewElTabAnchor: getNewElTabAnchor,
getNewElTabContent: getNewElTabContent,
getNewElTabLi: getNewElTabLi,
getNewElTabPane: getNewElTabPane
};
///////////////////
// ---- retrieve existing elements from the DOM ----------
function getElTabPaneForLi($li) {
return $($li.find('a').attr('href'));
}
// ---- create new elements ----------
function getNewElNavTabs() {
return $('<ul class="nav nav-tabs" role="tablist"></ul>');
}
function getNewElScrollerElementWrappingNavTabsInstance($navTabsInstance, settings) {
var $tabsContainer = $('<div class="scrtabs-tab-container"></div>'),
$leftArrow = $('<div class="scrtabs-tab-scroll-arrow scrtabs-tab-scroll-arrow-left"><span class="' + settings.cssClassLeftArrow + '"></span></div>'),
$rightArrow = $('<div class="scrtabs-tab-scroll-arrow scrtabs-tab-scroll-arrow-right"><span class="' + settings.cssClassRightArrow + '"></span></div>'),
$fixedContainer = $('<div class="scrtabs-tabs-fixed-container"></div>'),
$movableContainer = $('<div class="scrtabs-tabs-movable-container"></div>');
if (settings.disableScrollArrowsOnFullyScrolled) {
$leftArrow.add($rightArrow).addClass('scrtabs-disable');
}
return $tabsContainer
.append($leftArrow,
$fixedContainer.append($movableContainer.append($navTabsInstance)),
$rightArrow);
}
function getNewElTabAnchor(tab, propNames) {
return $('<a role="tab" data-toggle="tab"></a>')
.attr('href', '#' + tab[propNames.paneId])
.html(tab[propNames.title]);
}
function getNewElTabContent() {
return $('<div class="tab-content"></div>');
}
function getNewElTabLi(tab, propNames, forceActiveTab) {
var $li = $('<li role="presentation" class=""></li>'),
$a = getNewElTabAnchor(tab, propNames).appendTo($li);
if (tab[propNames.disabled]) {
$li.addClass('disabled');
$a.attr('data-toggle', '');
} else if (forceActiveTab && tab[propNames.active]) {
$li.addClass('active');
}
return $li;
}
function getNewElTabPane(tab, propNames, forceActiveTab) {
var $pane = $('<div role="tabpanel" class="tab-pane"></div>')
.attr('id', tab[propNames.paneId])
.html(tab[propNames.content]);
if (forceActiveTab && tab[propNames.active]) {
$pane.addClass('active');
}
return $pane;