forked from mikejacobson/angular-bootstrap-scrolling-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrolling-tabs.js
626 lines (480 loc) · 20.2 KB
/
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
;(function () {
'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_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_IS_MOUSEDOWN: 'ismousedown'
},
scrollingTabsModule = angular.module('mj.scrollingTabs', []),
/* *************************************************************
* scrolling-tabs element directive template
* *************************************************************/
// plunk: http://plnkr.co/edit/YhKiIhuAPkpAyacu6tuk
scrollingTabsTemplate = [
'<div class="scrtabs-tab-container">',
' <div class="scrtabs-tab-scroll-arrow scrtabs-js-tab-scroll-arrow-left"><span class="glyphicon glyphicon-chevron-left"></span></div>',
' <div class="scrtabs-tabs-fixed-container">',
' <div class="scrtabs-tabs-movable-container">',
' <ul class="nav nav-tabs" role="tablist">',
' <li ng-class="{ \'active\': tab[propActive || \'active\'], ',
' \'disabled\': tab[propDisabled || \'disabled\'] }" ',
' data-tab="{{tab}}" data-index="{{$index}}" ng-repeat="tab in tabsArr">',
' <a ng-href="{{\'#\' + tab[propPaneId || \'paneId\']}}" role="tab"',
' data-toggle="{{tab[propDisabled || \'disabled\'] ? \'\' : \'tab\'}}" ',
' ng-bind-html="sanitize(tab[propTitle || \'title\']);">',
' </a>',
' </li>',
' </ul>',
' </div>',
' </div>',
' <div class="scrtabs-tab-scroll-arrow scrtabs-js-tab-scroll-arrow-right"><span class="glyphicon glyphicon-chevron-right"></span></div>',
'</div>'
].join(''),
/* *************************************************************
* scrolling-tabs-wrapper element directive template
* *************************************************************/
// plunk: http://plnkr.co/edit/lWeQxxecKPudK7xlQxS3
scrollingTabsWrapperTemplate = [
'<div class="scrtabs-tab-container">',
' <div class="scrtabs-tab-scroll-arrow scrtabs-js-tab-scroll-arrow-left"><span class="glyphicon glyphicon-chevron-left"></span></div>',
' <div class="scrtabs-tabs-fixed-container">',
' <div class="scrtabs-tabs-movable-container" ng-transclude></div>',
' </div>',
' <div class="scrtabs-tab-scroll-arrow scrtabs-js-tab-scroll-arrow-right"><span class="glyphicon glyphicon-chevron-right"></span></div>',
'</div>'
].join('');
// 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);
};
};
jQuery.fn[sr] = function (fn) { return fn ? this.bind('resize.scrtabs', debounce(fn)) : this.trigger(sr); };
})(jQuery, 'smartresize');
/* ***********************************************************************************
* EventHandlers - Class that each instance of ScrollingTabsControl will instantiate
* **********************************************************************************/
function EventHandlers(scrollingTabsControl) {
var evh = this;
evh.stc = scrollingTabsControl;
}
// prototype methods
(function (p){
p.handleClickOnLeftScrollArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.incrementScrollLeft();
};
p.handleClickOnRightScrollArrow = function (e) {
var evh = this,
stc = evh.stc,
scrollMovement = stc.scrollMovement;
scrollMovement.incrementScrollRight(scrollMovement.getMinPos());
};
p.handleMousedownOnLeftScrollArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.startScrollLeft();
};
p.handleMousedownOnRightScrollArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.startScrollRight();
};
p.handleMouseupOnLeftScrollArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.stopScrollLeft();
};
p.handleMouseupOnRightScrollArrow = function (e) {
var evh = this,
stc = evh.stc;
stc.scrollMovement.stopScrollRight();
};
p.handleWindowResize = function (e) {
var evh = this,
stc = evh.stc,
newWinWidth = stc.$win.width();
if (newWinWidth === stc.winWidth) {
return false; // false alarm
}
stc.winWidth = newWinWidth;
stc.elementsHandler.refreshAllElementSizes(true); // true -> check for scroll arrows not being necessary anymore
};
}(EventHandlers.prototype));
/* ***********************************************************************************
* ElementsHandler - Class that each instance of ScrollingTabsControl will instantiate
* **********************************************************************************/
function ElementsHandler(scrollingTabsControl) {
var ehd = this;
ehd.stc = scrollingTabsControl;
}
// prototype methods
(function (p) {
p.initElements = function (isWrapperDirective) {
var ehd = this;
ehd.setElementReferences();
if (isWrapperDirective) {
ehd.moveTabContentOutsideScrollContainer();
}
ehd.setEventListeners();
};
p.moveTabContentOutsideScrollContainer = function () {
var ehd = this,
stc = ehd.stc,
$tabsContainer = stc.$tabsContainer;
$tabsContainer.find('.tab-content').appendTo($tabsContainer);
};
p.refreshAllElementSizes = function (isPossibleArrowVisibilityChange) {
var ehd = this,
stc = ehd.stc,
smv = stc.scrollMovement,
scrollArrowsWereVisible = stc.scrollArrowsVisible,
minPos;
ehd.setElementWidths();
ehd.setScrollArrowVisibility();
if (stc.scrollArrowsVisible) {
ehd.setFixedContainerWidthForJustVisibleScrollArrows();
}
// if this was a window resize, 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 (isPossibleArrowVisibilityChange && (stc.scrollArrowsVisible || scrollArrowsWereVisible)) {
if (stc.scrollArrowsVisible) {
// make sure container not too far left
minPos = smv.getMinPos();
if (stc.movableContainerLeftPos < minPos) {
smv.incrementScrollRight(minPos);
} else {
smv.scrollToActiveTab(true); // true -> isOnWindowResize
}
} else {
// scroll arrows went away after resize, so position movable container at 0
stc.movableContainerLeftPos = 0;
smv.slideMovableContainerToLeftPos();
}
}
};
p.setElementReferences = function () {
var ehd = this,
stc = ehd.stc,
$tabsContainer = stc.$tabsContainer;
stc.$fixedContainer = $tabsContainer.find('.scrtabs-tabs-fixed-container');
stc.$movableContainer = $tabsContainer.find('.scrtabs-tabs-movable-container');
stc.$tabsUl = $tabsContainer.find('.nav-tabs');
stc.$leftScrollArrow = $tabsContainer.find('.scrtabs-js-tab-scroll-arrow-left');
stc.$rightScrollArrow = $tabsContainer.find('.scrtabs-js-tab-scroll-arrow-right');
stc.$scrollArrows = stc.$leftScrollArrow.add(stc.$rightScrollArrow);
stc.$win = $(window);
};
p.setElementWidths = function () {
var ehd = this,
stc = ehd.stc;
stc.containerWidth = stc.$tabsContainer.outerWidth();
stc.winWidth = stc.$win.width();
stc.scrollArrowsCombinedWidth = stc.$leftScrollArrow.outerWidth() + stc.$rightScrollArrow.outerWidth();
ehd.setFixedContainerWidth();
ehd.setMovableContainerWidth();
};
p.setEventListeners = function () {
var ehd = this,
stc = ehd.stc,
evh = stc.eventHandlers; // eventHandlers
stc.$leftScrollArrow.on({
'mousedown.scrtabs': function (e) { evh.handleMousedownOnLeftScrollArrow.call(evh, e); },
'mouseup.scrtabs': function (e) { evh.handleMouseupOnLeftScrollArrow.call(evh, e); },
'click.scrtabs': function (e) { evh.handleClickOnLeftScrollArrow.call(evh, e); }
});
stc.$rightScrollArrow.on({
'mousedown.scrtabs': function (e) { evh.handleMousedownOnRightScrollArrow.call(evh, e); },
'mouseup.scrtabs': function (e) { evh.handleMouseupOnRightScrollArrow.call(evh, e); },
'click.scrtabs': function (e) { evh.handleClickOnRightScrollArrow.call(evh, e); }
});
stc.$win.smartresize(function (e) { evh.handleWindowResize.call(evh, e); });
};
p.setFixedContainerWidth = function () {
var ehd = this,
stc = ehd.stc;
stc.$fixedContainer.width(stc.fixedContainerWidth = stc.$tabsContainer.outerWidth());
};
p.setFixedContainerWidthForJustHiddenScrollArrows = function () {
var ehd = this,
stc = ehd.stc;
stc.$fixedContainer.width(stc.fixedContainerWidth);
};
p.setFixedContainerWidthForJustVisibleScrollArrows = function () {
var ehd = this,
stc = ehd.stc;
stc.$fixedContainer.width(stc.fixedContainerWidth - stc.scrollArrowsCombinedWidth);
};
p.setMovableContainerWidth = function () {
var ehd = this,
stc = ehd.stc;
stc.movableContainerWidth = 0;
stc.$tabsUl.find('li').each(function __getLiWidth() {
var $li = $(this);
stc.movableContainerWidth += $li.outerWidth();
});
stc.$movableContainer.width(stc.movableContainerWidth += 1);
};
p.setScrollArrowVisibility = function () {
var ehd = this,
stc = ehd.stc,
shouldBeVisible = stc.movableContainerWidth > stc.fixedContainerWidth;
if (shouldBeVisible && !stc.scrollArrowsVisible) {
stc.$scrollArrows.show();
stc.scrollArrowsVisible = true;
ehd.setFixedContainerWidthForJustVisibleScrollArrows();
} else if (!shouldBeVisible && stc.scrollArrowsVisible) {
stc.$scrollArrows.hide();
stc.scrollArrowsVisible = false;
ehd.setFixedContainerWidthForJustHiddenScrollArrows();
}
};
}(ElementsHandler.prototype));
/* ***********************************************************************************
* ScrollMovement - Class that each instance of ScrollingTabsControl will instantiate
* **********************************************************************************/
function ScrollMovement(scrollingTabsControl) {
var smv = this;
smv.stc = scrollingTabsControl;
}
// prototype methods
(function (p) {
p.continueScrollLeft = function () {
var smv = this,
stc = smv.stc;
stc.$timeout(function() {
if (stc.$leftScrollArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN) && (stc.movableContainerLeftPos < 0)) {
if (!smv.incrementScrollLeft()) { // scroll limit not reached, so keep scrolling
smv.continueScrollLeft();
}
}
}, CONSTANTS.CONTINUOUS_SCROLLING_TIMEOUT_INTERVAL);
};
p.continueScrollRight = function (minPos) {
var smv = this,
stc = smv.stc;
stc.$timeout(function() {
if (stc.$rightScrollArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN) && (stc.movableContainerLeftPos > minPos)) {
// slide tabs LEFT -> decrease movable container's left position
// min value is (movableContainerWidth - $tabHeader width)
if (!smv.incrementScrollRight(minPos)) {
smv.continueScrollRight(minPos);
}
}
}, 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;
}
};
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.incrementScrollLeft = function () {
var smv = this,
stc = smv.stc;
stc.movableContainerLeftPos += (stc.fixedContainerWidth / CONSTANTS.SCROLL_OFFSET_FRACTION);
if (stc.movableContainerLeftPos > 0) {
stc.movableContainerLeftPos = 0;
}
smv.slideMovableContainerToLeftPos();
return (stc.movableContainerLeftPos === 0); // indicates scroll limit reached
};
p.incrementScrollRight = function (minPos) {
var smv = this,
stc = smv.stc;
smv.decrementMovableContainerLeftPos(minPos);
smv.slideMovableContainerToLeftPos();
return (stc.movableContainerLeftPos === minPos);
};
p.scrollToActiveTab = function (isOnWindowResize) {
var smv = this,
stc = smv.stc,
$activeTab,
activeTabWidth,
activeTabLeftPos,
rightArrowLeftPos,
overlap;
// if the active tab is not fully visible, scroll till it is
if (!stc.scrollArrowsVisible) {
return;
}
$activeTab = stc.$tabsUl.find('li.active');
if (!$activeTab.length) {
return;
}
activeTabWidth = $activeTab.outerWidth();
activeTabLeftPos = $activeTab.offset().left;
rightArrowLeftPos = stc.$rightScrollArrow.offset().left;
overlap = activeTabLeftPos + activeTabWidth - rightArrowLeftPos;
if (overlap > 0) {
stc.movableContainerLeftPos = isOnWindowResize ? (stc.movableContainerLeftPos - overlap) : -overlap;
smv.slideMovableContainerToLeftPos();
}
};
p.slideMovableContainerToLeftPos = function () {
var smv = this,
stc = smv.stc,
leftVal;
stc.movableContainerLeftPos = stc.movableContainerLeftPos / 1;
leftVal = smv.getMovableContainerCssLeftVal();
stc.$movableContainer.stop().animate({ left: leftVal }, 'slow', function __slideAnimComplete() {
var newMinPos = smv.getMinPos();
// 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');
}
});
};
p.startScrollLeft = function () {
var smv = this,
stc = smv.stc;
stc.$leftScrollArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, true);
smv.continueScrollLeft();
};
p.startScrollRight = function () {
var smv = this,
stc = smv.stc;
stc.$rightScrollArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, true);
smv.continueScrollRight(smv.getMinPos());
};
p.stopScrollLeft = function () {
var smv = this,
stc = smv.stc;
stc.$leftScrollArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, false);
};
p.stopScrollRight = function () {
var smv = this,
stc = smv.stc;
stc.$rightScrollArrow.data(CONSTANTS.DATA_KEY_IS_MOUSEDOWN, false);
};
}(ScrollMovement.prototype));
/* **********************************************************************
* ScrollingTabsControl - Class that each directive will instantiate
* **********************************************************************/
function ScrollingTabsControl($tabsContainer, $timeout) {
var stc = this;
stc.$tabsContainer = $tabsContainer;
stc.$timeout = $timeout,
stc.movableContainerLeftPos = 0;
stc.scrollArrowsVisible = true;
stc.scrollMovement = new ScrollMovement(stc);
stc.eventHandlers = new EventHandlers(stc);
stc.elementsHandler = new ElementsHandler(stc);
}
// prototype methods
(function (p) {
p.initTabs = function (isWrapperDirective) {
var stc = this,
elementsHandler = stc.elementsHandler,
scrollMovement = stc.scrollMovement;
stc.$timeout(function __initTabsAfterTimeout() {
elementsHandler.initElements(isWrapperDirective);
elementsHandler.refreshAllElementSizes();
scrollMovement.scrollToActiveTab();
}, 100);
return elementsHandler;
};
}(ScrollingTabsControl.prototype));
/* ********************************************************
* scrolling-tabs Directive
* ********************************************************/
function scrollingTabsDirective($timeout, $sce) {
function sanitize (html) {
return $sce.trustAsHtml(html);
}
// ------------ Directive Object ---------------------------
return {
restrict: 'E',
template: scrollingTabsTemplate,
transclude: false,
replace: true,
scope: {
tabs: '@',
propPaneId: '@',
propTitle: '@',
propActive: '@',
propDisabled: '@',
localTabClick: '&tabClick'
},
link: function(scope, element, attrs) {
var scrollingTabsControl = new ScrollingTabsControl(element, $timeout);
scope.tabsArr = scope.$eval(scope.tabs);
scope.propPaneId = scope.propPaneId || 'paneId';
scope.propTitle = scope.propTitle || 'title';
scope.propActive = scope.propActive || 'active';
scope.propDisabled = scope.propDisabled || 'disabled';
scope.sanitize = sanitize;
element.on('click.scrollingTabs', '.nav-tabs > li', function __handleClickOnTab(e) {
var clickedTabElData = $(this).data();
scope.localTabClick({
$event: e,
$index: clickedTabElData.index,
tab: clickedTabElData.tab
});
});
scrollingTabsControl.initTabs(false); // false -> not the wrapper directive
}
};
}
/* ********************************************************
* scrolling-tabs-wrapper Directive
* ********************************************************/
function scrollingTabsWrapperDirective($timeout, $window) {
// ------------ Directive Object ---------------------------
return {
restrict: 'A',
template: scrollingTabsWrapperTemplate,
transclude: true,
replace: true,
scope: {
tabWatch: '='
},
link: function(scope, element, attrs) {
var scrollingTabsControl = new ScrollingTabsControl(element, $timeout);
var elementsHandler = scrollingTabsControl.initTabs(true); // true -> wrapper directive
scope.$watch('tabWatch', function(n,o) {
if (n!==o) {
$timeout(function() {
elementsHandler.refreshAllElementSizes(true);
}, 100);
}
});
}
};
}
scrollingTabsDirective.$inject = ['$timeout', '$sce'];
scrollingTabsWrapperDirective.$inject = ['$timeout', '$window'];
scrollingTabsModule.directive('scrollingTabs', scrollingTabsDirective);
scrollingTabsModule.directive('scrollingTabsWrapper', scrollingTabsWrapperDirective);
}());