1
+ ( function ( ) {
2
+ 'use strict' ;
3
+ angular . module ( 'tcUIComponents' ) . directive ( 'responsiveCarousel' , function ( ) {
4
+ return {
5
+ restrict : 'E' ,
6
+ transclude : true ,
7
+ replace : true ,
8
+ templateUrl : 'directives/responsive-carousel/responsive-carousel.directive.html' ,
9
+ scope : {
10
+ data : '='
11
+ } ,
12
+ controller : [ '$log' , '$scope' , '$element' , '$window' ,
13
+ function ( $log , $scope , $element , $window ) {
14
+ $scope . slideCounts = { } ;
15
+
16
+ activate ( ) ;
17
+
18
+ function activate ( ) {
19
+ init ( ) ;
20
+
21
+ var window = angular . element ( $window ) ;
22
+ window . on ( 'resize' , function ( ) {
23
+ init ( ) ;
24
+ // don't forget manually trigger $digest()
25
+ $scope . $digest ( ) ;
26
+ } ) ;
27
+ }
28
+
29
+ function init ( ) {
30
+ var width = $window . innerWidth ;
31
+ console . log ( width ) ;
32
+ if ( width > 1070 ) {
33
+ // desktop
34
+ buildCarouselSlide ( 6 ) ;
35
+ } else if ( width > 900 ) {
36
+ // desktop
37
+ buildCarouselSlide ( 5 ) ;
38
+ } else if ( width <= 900 && width > 768 ) {
39
+ // tablet
40
+ buildCarouselSlide ( 4 ) ;
41
+ } else {
42
+ // phone
43
+ buildCarouselSlide ( 2 ) ;
44
+ }
45
+ }
46
+
47
+
48
+ function buildCarouselSlide ( numItemsPerSlide ) {
49
+ var slidesCollection = [ ] ;
50
+ var slide = [ ] ;
51
+ // Might be able to change number of subtracks per slide based
52
+ // on screen size if the width of each subtrack is consistent:
53
+ // http://stackoverflow.com/questions/26252038/multi-item-responsive-carousel
54
+ numItemsPerSlide = numItemsPerSlide || 5 ;
55
+
56
+ for ( var i = 0 ; i < $scope . data . length ; i ++ ) {
57
+ if ( slide . length === numItemsPerSlide ) {
58
+ // When slide is full, push it to collection and make a new slide []
59
+ slidesCollection . push ( slide ) ;
60
+ // updates the slide count object
61
+ $scope . slideCounts [ slidesCollection . length - 1 ] = slide . length ;
62
+ slide = [ ] ;
63
+ }
64
+ slide . push ( $scope . data [ i ] ) ;
65
+ }
66
+ slidesCollection . push ( slide ) ;
67
+ // updates the slide count object
68
+ $scope . slideCounts [ slidesCollection . length - 1 ] = slide . length ;
69
+ $scope . slidesCollection = slidesCollection ;
70
+ }
71
+
72
+ } ]
73
+ } ;
74
+ } ) ;
75
+ } ) ( ) ;
0 commit comments