This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathresponsive-carousel.directive.js
81 lines (71 loc) · 2.47 KB
/
responsive-carousel.directive.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
import angular from 'angular'
(function() {
'use strict'
angular.module('tcUIComponents').directive('responsiveCarousel', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
template: require('./responsive-carousel')(),
scope: {
data: '=',
handle: '@'
},
controller: ['$scope', '$element', '$window',
function($scope, $element, $window) {
$scope.slideCounts = {}
activate()
function activate() {
$scope.$watchCollection('data', function(newValue, oldValue) {
init()
})
init()
var window = angular.element($window)
window.on('resize', function() {
init()
// don't forget manually trigger $digest()
$scope.$digest()
})
}
function init() {
var width = $window.innerWidth
if(width >= 1350) {
// desktop
buildCarouselSlide(7)
} else if(width >= 1180) {
// desktop
buildCarouselSlide(6)
} else if(width >= 1010) {
// desktop
buildCarouselSlide(5)
} else if(width < 1010) {
// tablet & mobile
buildCarouselSlide(4)
}
}
function buildCarouselSlide(numItemsPerSlide) {
var slidesCollection = []
var slide = []
// Might be able to change number of subtracks per slide based
// on screen size if the width of each subtrack is consistent:
// http://stackoverflow.com/questions/26252038/multi-item-responsive-carousel
numItemsPerSlide = numItemsPerSlide || 5
for(var i = 0; i < $scope.data.length; i++) {
if (slide.length === numItemsPerSlide) {
// When slide is full, push it to collection and make a new slide []
slidesCollection.push(slide)
// updates the slide count object
$scope.slideCounts[slidesCollection.length - 1] = slide.length
slide = []
}
slide.push($scope.data[i])
}
slidesCollection.push(slide)
// updates the slide count object
$scope.slideCounts[slidesCollection.length - 1] = slide.length
$scope.slidesCollection = slidesCollection
}
}]
}
})
})()