-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-carousel.js
113 lines (95 loc) · 2.7 KB
/
angular-carousel.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
'use strict';
var ngApp = angular.module('jmp.carousel', []);
(function (app) {
CarouselController.prototype.start = start;
CarouselController.prototype.Carousel = Carousel;
// Directive default options
var defaultOptions = {
rtl: false,
loop: true,
margin: 10,
nav: true
};
/* @ngInject */
app.directive('owlCarousel', ['$timeout', function($timeout) {
return {
restrict: 'EA',
controller: CarouselController,
scope: {
owlCarousel: '=',
owlInstance: '&'
},
transclude: true,
template: '<div class="owl-carousel owl-theme" ng-transclude></div>',
link: function (scope, element, attrs, controller) {
// Owl carousel options holder
scope.options = {};
// Owl Carousel
scope.carousel = null;
// Start owl carousel
$timeout(function () { init(); });
scope.$watch('owlCarousel', function (nval) {
destroy();
if (nval) {
scope.options = setOptions(nval);
}
});
scope.$on('$destroy', destroy);
function init() {
scope.$apply(function () {
scope.carousel = element.find('.owl-carousel');
scope.carousel.owlCarousel(scope.options);
controller.start(scope.carousel);
if (scope.owlInstance) {
scope.owlInstance({ $owl: controller.Carousel() });
}
});
}
function setOptions(o) {
var options = angular.extend({}, defaultOptions, 0);
if (o) {
for (var key in o) {
if (o.hasOwnProperty(key)) {
options[key] = o[key];
}
}
}
return options;
}
function destroy() {
var stage = element.find('.owl-stage');
controller.Carousel().trigger('destroy.owl.carousel');
if (stage.length) {
stage.remove();
}
}
}
};
}]);
function CarouselController() {
this.instance = {};
}
function start() {
this.instance = arguments[0];
}
function Carousel() {
var self = this;
return {
trigger: trigger
};
/**
* @param selector
* @param {[*]} speed
*/
function trigger(selector, speed) {
// Allow to just input a number instead of an array
if (!angular.isArray(speed)) {
speed = [speed];
arguments[1] = speed;
}
if (self.instance.hasOwnProperty('trigger')) {
self.instance.trigger.apply(self.instance, arguments);
}
}
}
}(ngApp));