This repository was archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathslides.js
204 lines (185 loc) · 5.48 KB
/
slides.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
204
/**
* @ngdoc directive
* @name ionSlides
* @module ionic
* @restrict E
* @description
* The Slides component is a powerful multi-page container where each page can be swiped or dragged between.
*
* Note: this is a new version of the Ionic Slide Box based on the [Swiper](http://www.idangero.us/swiper/#.Vmc1J-ODFBc) widget from
* [idangerous](http://www.idangero.us/).
*
* 
*
* @usage
* ```html
* <ion-content scroll="false">
* <ion-slides options="options" slider="data.slider">
* <ion-slide-page>
* <div class="box blue"><h1>BLUE</h1></div>
* </ion-slide-page>
* <ion-slide-page>
* <div class="box yellow"><h1>YELLOW</h1></div>
* </ion-slide-page>
* <ion-slide-page>
* <div class="box pink"><h1>PINK</h1></div>
* </ion-slide-page>
* </ion-slides>
* </ion-content>
* ```
*
* ```js
* $scope.options = {
* loop: false,
* effect: 'fade',
* speed: 500,
* }
*
* $scope.$on("$ionicSlides.sliderInitialized", function(event, data){
* // data.slider is the instance of Swiper
* $scope.slider = data.slider;
* });
*
* $scope.$on("$ionicSlides.slideChangeStart", function(event, data){
* console.log('Slide change is beginning');
* });
*
* $scope.$on("$ionicSlides.slideChangeEnd", function(event, data){
* // note: the indexes are 0-based
* $scope.activeIndex = data.slider.activeIndex;
* $scope.previousIndex = data.slider.previousIndex;
* });
*
* ```
*
* ## Slide Events
*
* The slides component dispatches events when the active slide changes
*
* <table class="table">
* <tr>
* <td><code>$ionicSlides.slideChangeStart</code></td>
* <td>This event is emitted when a slide change begins</td>
* </tr>
* <tr>
* <td><code>$ionicSlides.slideChangeEnd</code></td>
* <td>This event is emitted when a slide change completes</td>
* </tr>
* <tr>
* <td><code>$ionicSlides.sliderInitialized</code></td>
* <td>This event is emitted when the slider is initialized. It provides access to an instance of the slider.</td>
* </tr>
* </table>
*
*
* ## Updating Slides Dynamically
* When applying data to the slider at runtime, typically everything will work as expected.
*
* In the event that the slides are looped, use the `updateLoop` method on the slider to ensure the slides update correctly.
*
* ```
* $scope.$on("$ionicSlides.sliderInitialized", function(event, data){
* // grab an instance of the slider
* $scope.slider = data.slider;
* });
*
* function dataChangeHandler(){
* // call this function when data changes, such as an HTTP request, etc
* if ( $scope.slider ){
* $scope.slider.updateLoop();
* }
* }
* ```
*
*/
IonicModule
.directive('ionSlides', [
'$animate',
'$timeout',
'$compile',
function($animate, $timeout, $compile) {
return {
restrict: 'E',
transclude: true,
scope: {
options: '=',
slider: '='
},
template: '<div class="swiper-container">' +
'<div class="swiper-wrapper" ng-transclude>' +
'</div>' +
'<div ng-hide="!showPager" class="swiper-pagination"></div>' +
'</div>',
controller: ['$scope', '$element', function($scope, $element) {
var _this = this;
this.update = function() {
$timeout(function() {
if (!_this.__slider) {
return;
}
_this.__slider.update();
if (_this._options.loop) {
_this.__slider.createLoop();
}
var slidesLength = _this.__slider.slides.length;
// Don't allow pager to show with > 10 slides
if (slidesLength > 10) {
$scope.showPager = false;
}
// When slide index is greater than total then slide to last index
if (_this.__slider.activeIndex > slidesLength - 1) {
_this.__slider.slideTo(slidesLength - 1);
}
});
};
this.rapidUpdate = ionic.debounce(function() {
_this.update();
}, 50);
this.getSlider = function() {
return _this.__slider;
};
$timeout(function() {
var options = $scope.options || {};
var newOptions = angular.extend({
pagination: $element.children().children()[1],
paginationClickable: true,
lazyLoading: true,
preloadImages: false
}, options);
this._options = newOptions;
var slider = new ionic.views.Swiper($element.children()[0], newOptions, $scope, $compile);
$scope.$emit("$ionicSlides.sliderInitialized", { slider: slider });
_this.__slider = slider;
$scope.slider = _this.__slider;
$scope.$on('$destroy', function() {
slider.destroy();
_this.__slider = null;
});
});
$timeout(function() {
// if it's a loop, render the slides again just incase
_this.rapidUpdate();
}, 200);
}],
link: function($scope) {
$scope.showPager = true;
// Disable ngAnimate for slidebox and its children
//$animate.enabled(false, $element);
}
};
}])
.directive('ionSlidePage', [function() {
return {
restrict: 'E',
require: '?^ionSlides',
transclude: true,
replace: true,
template: '<div class="swiper-slide" ng-transclude></div>',
link: function($scope, $element, $attr, ionSlidesCtrl) {
ionSlidesCtrl.rapidUpdate();
$scope.$on('$destroy', function() {
ionSlidesCtrl.rapidUpdate();
});
}
};
}]);