forked from thenikso/angular-flexslider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-flexslider.js
203 lines (197 loc) · 8.38 KB
/
angular-flexslider.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
// Generated by CoffeeScript 1.7.1
(function() {
'use strict';
angular.module('angular-flexslider', []).directive('flexSlider', [
'$parse', '$timeout', function($parse, $timeout) {
return {
restrict: 'AE',
scope: false,
replace: true,
transclude: true,
template: '<div class="flexslider-container"></div>',
compile: function(element, attr, linker) {
return function($scope, $element) {
var addSlide, collectionString, flexsliderDiv, getTrackFromItem, indexString, match, removeSlide, slidesItems, trackBy;
match = (attr.slide || attr.flexSlide).match(/^\s*(.+)\s+in\s+(.*?)(?:\s+track\s+by\s+(.+?))?\s*$/);
indexString = match[1];
collectionString = match[2];
trackBy = angular.isDefined(match[3]) ? $parse(match[3]) : $parse("" + indexString);
flexsliderDiv = null;
slidesItems = {};
getTrackFromItem = function(collectionItem, index) {
var locals;
locals = {};
locals[indexString] = collectionItem;
locals['$index'] = index;
return trackBy($scope, locals);
};
addSlide = function(collectionItem, index, callback) {
var childScope, track;
track = getTrackFromItem(collectionItem, index);
if (slidesItems[track] != null) {
throw "Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.";
}
childScope = $scope.$new();
childScope[indexString] = collectionItem;
childScope['$index'] = index;
return linker(childScope, function(clone) {
var slideItem;
slideItem = {
collectionItem: collectionItem,
childScope: childScope,
element: clone
};
slidesItems[track] = slideItem;
return typeof callback === "function" ? callback(slideItem) : void 0;
});
};
removeSlide = function(collectionItem, index) {
var slideItem, track;
track = getTrackFromItem(collectionItem, index);
slideItem = slidesItems[track];
if (slideItem == null) {
return;
}
delete slidesItems[track];
slideItem.childScope.$destroy();
return slideItem;
};
return $scope.$watchCollection(collectionString, function(collection) {
var attrKey, attrVal, c, currentSlidesLength, e, i, idx, n, options, slider, slides, t, toAdd, toRemove, trackCollection, _i, _j, _k, _l, _len, _len1, _len2, _len3;
if (!(collection != null ? collection.length : void 0)) {
return;
}
if (flexsliderDiv != null) {
slider = flexsliderDiv.data('flexslider');
currentSlidesLength = Object.keys(slidesItems).length;
if (collection == null) {
collection = [];
}
trackCollection = {};
for (i = _i = 0, _len = collection.length; _i < _len; i = ++_i) {
c = collection[i];
trackCollection[getTrackFromItem(c, i)] = c;
}
toAdd = (function() {
var _j, _len1, _results;
_results = [];
for (i = _j = 0, _len1 = collection.length; _j < _len1; i = ++_j) {
c = collection[i];
if (slidesItems[getTrackFromItem(c, i)] == null) {
_results.push({
value: c,
index: i
});
}
}
return _results;
})();
toRemove = (function() {
var _results;
_results = [];
for (t in slidesItems) {
i = slidesItems[t];
if (trackCollection[t] == null) {
_results.push(i.collectionItem);
}
}
return _results;
})();
if ((toAdd.length === 1 && toRemove.length === 0) || toAdd.length === 0) {
for (_j = 0, _len1 = toRemove.length; _j < _len1; _j++) {
e = toRemove[_j];
e = removeSlide(e, collection.indexOf(e));
slider.removeSlide(e.element);
}
for (_k = 0, _len2 = toAdd.length; _k < _len2; _k++) {
e = toAdd[_k];
idx = e.index;
addSlide(e.value, idx, function(item) {
if (idx === currentSlidesLength) {
idx = void 0;
}
return $scope.$evalAsync(function() {
return slider.addSlide(item.element, idx);
});
});
}
return;
}
}
options = {};
for (attrKey in attr) {
attrVal = attr[attrKey];
if (attrKey.indexOf('$') === 0) {
continue;
}
// check if sliderId attr exists and create the flexslider div with the slides
// attaching sliderId to flexsliderDiv
if (attrKey === 'sliderId') {
slidesItems = {};
if (flexsliderDiv != null) {
flexsliderDiv.remove();
}
slides = angular.element('<ul class="slides"></ul>');
var slider = '<div id="' + attrVal + '" class="flexslider"></div>'
flexsliderDiv = angular.element(slider);
flexsliderDiv.append(slides);
$element.append(flexsliderDiv);
for (i = _l = 0, _len3 = collection.length; _l < _len3; i = ++_l) {
c = collection[i];
addSlide(c, i, function(item) {
return slides.append(item.element);
});
}
continue;
}
if (!isNaN(n = parseInt(attrVal))) {
options[attrKey] = n;
continue;
}
if (attrVal === 'false' || attrVal === 'true') {
options[attrKey] = attrVal === 'true';
continue;
}
if (attrKey === 'start' || attrKey === 'before' || attrKey === 'after' || attrKey === 'end' || attrKey === 'added' || attrKey === 'removed') {
options[attrKey] = (function(attrVal) {
var f;
f = $parse(attrVal);
return function(slider) {
return $scope.$apply(function() {
return f($scope, {
'$slider': {
element: slider
}
});
});
};
})(attrVal);
continue;
}
options[attrKey] = attrVal;
}
// check if flexsliderDiv is null. If attr sliderId doesnt exists, this is the case
// so create this div and add the slides to it.
if (flexsliderDiv === null) {
slidesItems = {};
slides = angular.element('<ul class="slides"></ul>');
flexsliderDiv = angular.element('<div class="flexslider"></div>');
flexsliderDiv.append(slides);
$element.append(flexsliderDiv);
for (i = _l = 0, _len3 = collection.length; _l < _len3; i = ++_l) {
c = collection[i];
addSlide(c, i, function(item) {
return slides.append(item.element);
});
}
}
return $timeout((function() {
return flexsliderDiv.flexslider(options);
}), 0);
});
};
}
};
}
]);
}).call(this);