This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-slideshow.js
More file actions
422 lines (362 loc) · 13 KB
/
angular-slideshow.js
File metadata and controls
422 lines (362 loc) · 13 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
'use strict';
angular.module('iln-slideshow', [])
.factory('$IlnSlideshow', [
'$rootScope',
function( $rootScope ) {
var slides = [];
var currentSlide = 0;
var nextSlide = 0;
var maxSlides = 0;
var minSlides = 0;
// Slideshow factory functions
/**
* Set the current slide
* @name setCurrentSlide
* @param { Number } _slide - the current slide
*
*/
function setCurrentSlide( _slide ){
currentSlide = _slide;
}
/**
* Set the next slide
* @name setNextSlide
* @param { Number } _slide - the next slide
*
*/
function setNextSlide( _slide ){
nextSlide = _slide;
}
/**
* Set the data of the slides and the max slides
* @name setSlideData
* @param {array} _data - the array of slides
* @return {function} callback - when all done
*
*/
function setSlideData( _data, callback ){
slides = _data;
maxSlides = ( slides.length ) - 1;
return callback();
}
/**
* Return the data of a single slide
* @name getSlideData
* @param {number} _slide - the number of the slide
* @return {object} slides - the single slide
*
*/
function getSlideData( _slide ){
return slides[ _slide ];
}
/**
* Return the data of all the slides
* @name getSlides
* @param {number} _slide - the number of the slide
* @return {object} slides - the single slide
*
*/
function getSlideshowData(){
return slides;
}
/**
* Get the current slide number
* @name getCurrentSlide
* @return {number} currentSlide
*
*/
function getCurrentSlide(){
return currentSlide;
}
/**
* Get the current next slide number
* @name getNextSlide
* @return {number} nextSlide
*
*/
function getNextSlide(){
return nextSlide;
}
/**
* Call the next slide
* @name callNextSlide
*
*/
function callNextSlide(){
if( currentSlide !== maxSlides ){
nextSlide = currentSlide + 1;
goToSlide( nextSlide );
}
if( currentSlide === maxSlides ){
nextSlide = 0;
goToSlide( nextSlide );
}
}
/**
* Call the previous slide
* @name callPreviousSlide
*
*/
function callPreviousSlide(){
if( currentSlide !== minSlides ){
nextSlide = currentSlide - 1;
goToSlide( nextSlide );
}
if( currentSlide === minSlides ){
nextSlide = maxSlides;
goToSlide( nextSlide );
}
}
/**
* Jump to a specific slide
* @name goToSlide
* @param {number} _slide
*
*/
function goToSlide( _slide ){
$rootScope.$broadcast('IlnSlideshowGoToSlide', _slide);
}
var service = {
setSlideData : setSlideData,
getSlideshowData : getSlideshowData,
getSlideData : getSlideData,
setCurrentSlide : setCurrentSlide,
setNextSlide : setNextSlide,
getCurrentSlide : getCurrentSlide,
getNextSlide : getNextSlide,
callNextSlide : callNextSlide,
callPreviousSlide : callPreviousSlide,
goToSlide : goToSlide
};
return service;
}
])
/**
* The slideshow directive controller
*/
.controller( 'IlnSlideshowCtrl', [
'$scope',
'$compile',
'$timeout',
'$rootScope',
'$IlnSlideshow',
function( $scope, $compile, $timeout, $rootScope, $IlnSlideshow ) {
var slide_transitioning = false;
var slide_init = true;
$scope.animateSlideCss = '';
$scope.init = function(){
$IlnSlideshow.setSlideData( $scope.SLIDES_JSON().slides, function(){
$IlnSlideshow.goToSlide( 0 );
});
};
/**
* Call the next slide
* @name $scope.nextSlide
*
*/
$scope.nextSlide = function(){
$IlnSlideshow.callNextSlide();
};
/**
* Call the previous slide
* @name $scope.previousSlide
*
*/
$scope.previousSlide = function(){
$IlnSlideshow.callPreviousSlide();
};
/**
* Animatie out the current slde
* @name $scope.animateOutCurrentSlide
*
*/
$scope.animateOutCurrentSlide = function(){
// css animate out the slide
$scope.animateSlideCss = 'iln-slide-animate-out';
$timeout(function(){
// remove the css
$scope.animateSlideCss = '';
// slide has animated out remove current slide
$scope.removeSlide();
}, 1000);
};
/**
* Fully remove the current slide
* @name $scope.removeSlide
*
*/
$scope.removeSlide = function(){
// remove and clear current slide
angular.element(
document.getElementById('iln-slide-wrap-' + String( $IlnSlideshow.getCurrentSlide() ))
).remove();
// give the dom some time to click over and add in the next slide
$timeout(function(){
// slide has animated out remove current slide
$scope.animateInNextSlide( $IlnSlideshow.getNextSlide() );
}, 10);
};
/**
* Animate in the next slide
* @name $scope.animateInNextSlide
* @param {number} _next - the next slide
*
*/
$scope.animateInNextSlide = function( _next ){
// set the new current slide
$IlnSlideshow.setCurrentSlide( _next );
angular.element(
document.getElementById('iln-slide-container')
).append(
$compile(
'<iln-slideshow-slide id="iln-slide-wrap-' +
String( $IlnSlideshow.getCurrentSlide() ) +
'" template-url="' +
// this might not work check
$IlnSlideshow.getSlideData( $IlnSlideshow.getCurrentSlide() ).template +
'"></iln-slideshow-slide>')($scope));
// some time to click over and add in the animation
$timeout(function(){
slide_transitioning = false;
// set the animation
$scope.animateSlideCss = 'iln-slide-animate-in';
// broadcast global slide complete
$rootScope.$broadcast('IlnSlideshowTransitionComplete', $IlnSlideshow.getCurrentSlide() );
}, 10);
};
/**
* Jump to a specific slide
* @name IlnSlideshowGoToSlide
*
*/
$scope.$on( 'IlnSlideshowGoToSlide', function( _s, _data ){
if( !slide_transitioning ){
slide_transitioning = true;
if( slide_init ){
slide_init = false;
$scope.animateInNextSlide( _data );
}else{
$scope.animateOutCurrentSlide();
}
}
});
// init the directive
$scope.init();
}
])
/**
* The controller for the pagination navigation
* TODO update the pagination
*/
.controller( 'IlnSlideshowPaginationCtrl', [
'$scope',
'$rootScope',
'$IlnSlideshow',
function( $scope, $rootScope, $IlnSlideshow ) {
$scope.slide_data = [];
$scope.current_slide = 0;
$scope.init = function(){
$scope.slide_data = $IlnSlideshow.getSlideshowData();
};
$scope.goToSlide = function( _index ){
if( $scope.current_slide !== _index ){
$scope.current_slide = _index;
$IlnSlideshow.setNextSlide( _index );
$IlnSlideshow.goToSlide( _index );
}
};
$scope.isActive = function( _index ){
if( _index <= $scope.current_slide ){
return 'active';
}else{
return '';
}
};
$scope.$on( 'IlnSlideshowTransitionComplete', function( _s, _data ){
$scope.current_slide = _data;
});
$scope.init();
}
])
/**
* The controller for the individual slide
*/
.controller( 'IlnSlideshowSlideCtrl', [
'$scope',
function( $scope ) {
$scope.$on('$destroy', function() {
console.log('removed');
});
}
])
/**
* The directive for the slideshow wrapper
*/
.directive('ilnSlideshow', function() {
return {
restrict: 'E',
controller: 'IlnSlideshowCtrl',
template: '<section id="iln-slideshow"><div id="iln-slide-container" ng-class="animateSlideCss"></div><button id="iln-slide-next" class="nav-arrow" ng-click="nextSlide()"></button><button id="iln-slide-previous" class="nav-arrow" ng-click="previousSlide()"></button><nav id="iln-pagination"><iln-slideshow-pagination></iln-slideshow-pagination></nav></section>',
scope: { SLIDES_JSON: '&slideJson' }
};
})
/**
* The directive for the pagination button
*/
.directive('ilnSlideshowPagination', function() {
return {
restrict: 'E',
controller: 'IlnSlideshowPaginationCtrl',
template: '<ul><li ng-repeat="slide in slide_data" ng-click="goToSlide( $index )" ng-class="isActive( $index )"></li></ul>'
};
})
/**
* The directive for the individual slide
*/
.directive('ilnSlideshowSlide', function() {
return {
restrict: 'E',
controller: 'IlnSlideshowSlideCtrl',
templateUrl: function( elem, attrs ) {
return attrs.templateUrl || 'slides/slide.html';
}
};
})
/**
* image preloader
*/
.directive('ilnImagePreloader', function() {
return {
restrict: 'E',
link: function ( scope, element, attrs ) {
/**
* Preload images from the slide images array
* @name preloadImages
* @param {Array} _data - the images to preaload
*
*/
function preloadImages( _images ){
// add a holder for the preloader
// if no image holder add it
angular.element(
document.getElementsByTagName('iln-image-preloader')
).append('<div id="iln-img-preload-holder" style="display:none;"></div>');
for( var i = 0; i < _images.length; i++ ){
angular.element(
document.getElementById('iln-img-preload-holder')
).append('<img src="' + _images[i] + '" width="1" height="1"></img>');
// remove container on last image
if( i === _images.length - 1 ){
angular.element(
document.getElementsByTagName('iln-image-preloader')
).remove();
}
}
}
scope.$watch( attrs.images, function ( _images ) {
preloadImages(_images.images);
});
}
};
})
;