-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngBackStretchCarousel.js
More file actions
115 lines (114 loc) · 4.08 KB
/
Copy pathngBackStretchCarousel.js
File metadata and controls
115 lines (114 loc) · 4.08 KB
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
/*! ngBackStretchCarousel - an AngularJS directive
* Useses jQuery backstretch with an image selector, swipe effects and
* ajax reader for the images
* Depends on jQuery and AngularJs.. Lazy to make and dependency file
*
* Copyright (c) 2014 Antti Stenvall; Licensed MIT
*
* Example use
* - in template
* <div ng-controller='temp' ng-back-stretch-carousel></div>
*
* - in controller
* app.controller('temp', ['$scope', '$http', function($scope, $http) {
* $scope.images = [];
* $http({
* method: 'get',
* url: 'rest/gimmeMyImageData'
* }).success(function(data) {
* $scope.images = data.images;
* });
* }]);
*
* */
app.directive('ngBackStretchCarousel', function($rootScope, $swipe) {
var directive = {}; // init directive
directive.restrict = 'A'; // one just uses ng-back-stretch-carousel attribute to utilize this
directive.compile = function(element, attributes) {
var linkFunction = function($scope, element, atttributes) {
// change the height of the element to the parent's height
var height = element.parent()[0].clientHeight;
$(element).css('height', height);
// follow changes in $scope.images and when it changes, init backstretch
// this allows initiation with $http ajax request that asks for the files
$scope.$watch('images', function(images) {
if (images.length === 0) { // do nothing is images is empty array
return;
}
// remove old if it exists
if ($(element).children() !== null) {
$($(element).children()).remove();
}
// add backstretch
$(element).backstretch(images, {duration: 3000, fade: 700});
var index = 0; // set which one is running
// bind swipes left and right
(function() {
// introduce swipe local variables
var endAction, startX, deltaX;
$swipe.bind(element, {
start: function(coords) {
endAction = null;
startX = coords.x;
},
cancel: function(e) {
endAction = null;
e.stopPropagation();
},
end: function(coords, e) {
if (endAction === 'prev') {
index = index === 0 ? $scope.images.length - 1 : index - 1;
} else if (endAction === 'next') {
index = index === ($scope.images.length - 1) ? 0 : index + 1;
}
if (endAction !== null) {
$(element).backstretch('show', index);
}
e.stopPropagation();
},
move: function(coords) {
deltaX = coords.x - startX;
var deltaXRatio = deltaX / element[0].clientWidth;
// swipe thresholds are determined here
if (deltaXRatio > 0.2) {
endAction = 'next';
} else if (deltaXRatio < -0.2) {
endAction = 'prev';
} else {
endAction = null;
}
}
});
})();
// Add carousel action list here
var div = document.createElement('div');
// bind event to backstretch.before (could be as well after, but this gives faster response to click)
$(window).on('backstretch.before', function(e, instance, i) {
var elements = $(div).children();
$(elements).removeClass('active');
$(elements[i]).addClass('active');
index = i;
});
$(div).addClass('ngBackStretchCarouselList')
.appendTo(element);
var clicker = function(el, ind) {
$(el).bind('click touch', function() {
$(element).backstretch('show', ind);
});
}
for (var i = 0; i < images.length; i++) {
var a = document.createElement('a');
if (i === 0) {
$(a).addClass('active');
}
$(a).appendTo(div);
(function(element, ind) {
clicker(element, ind);
})(a, i);
}
});
}
return linkFunction;
}
return directive;
});