Blame view

bower_components/Materialize/js/slider.js 10.4 KB
74249687   Luigi Serra   Cross browser con...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  (function ($) {
  
    var methods = {
  
      init : function(options) {
        var defaults = {
          indicators: true,
          height: 400,
          transition: 500,
          interval: 6000
        };
        options = $.extend(defaults, options);
  
        return this.each(function() {
  
          // For each slider, we want to keep track of
          // which slide is active and its associated content
          var $this = $(this);
          var $slider = $this.find('ul.slides').first();
          var $slides = $slider.find('li');
          var $active_index = $slider.find('.active').index();
c5169e0e   Renato De Donato   a new hope
22
          var $active;
74249687   Luigi Serra   Cross browser con...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
          if ($active_index != -1) { $active = $slides.eq($active_index); }
  
          // Transitions the caption depending on alignment
          function captionTransition(caption, duration) {
            if (caption.hasClass("center-align")) {
              caption.velocity({opacity: 0, translateY: -100}, {duration: duration, queue: false});
            }
            else if (caption.hasClass("right-align")) {
              caption.velocity({opacity: 0, translateX: 100}, {duration: duration, queue: false});
            }
            else if (caption.hasClass("left-align")) {
              caption.velocity({opacity: 0, translateX: -100}, {duration: duration, queue: false});
            }
          }
  
          // This function will transition the slide to any index of the next slide
          function moveToSlide(index) {
74249687   Luigi Serra   Cross browser con...
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
            if (index >= $slides.length) index = 0;
            else if (index < 0) index = $slides.length -1;
  
            $active_index = $slider.find('.active').index();
  
            // Only do if index changes
            if ($active_index != index) {
              $active = $slides.eq($active_index);
              $caption = $active.find('.caption');
  
              $active.removeClass('active');
              $active.velocity({opacity: 0}, {duration: options.transition, queue: false, easing: 'easeOutQuad',
                                complete: function() {
                                  $slides.not('.active').velocity({opacity: 0, translateX: 0, translateY: 0}, {duration: 0, queue: false});
                                } });
              captionTransition($caption, options.transition);
  
  
              // Update indicators
              if (options.indicators) {
                $indicators.eq($active_index).removeClass('active');
              }
  
              $slides.eq(index).velocity({opacity: 1}, {duration: options.transition, queue: false, easing: 'easeOutQuad'});
              $slides.eq(index).find('.caption').velocity({opacity: 1, translateX: 0, translateY: 0}, {duration: options.transition, delay: options.transition, queue: false, easing: 'easeOutQuad'});
              $slides.eq(index).addClass('active');
  
  
              // Update indicators
              if (options.indicators) {
                $indicators.eq(index).addClass('active');
              }
            }
          }
  
          // Set height of slider
          // If fullscreen, do nothing
          if (!$this.hasClass('fullscreen')) {
            if (options.indicators) {
              // Add height if indicators are present
              $this.height(options.height + 40);
            }
            else {
              $this.height(options.height);
            }
            $slider.height(options.height);
          }
  
  
          // Set initial positions of captions
          $slides.find('.caption').each(function () {
            captionTransition($(this), 0);
          });
  
          // Move img src into background-image
          $slides.find('img').each(function () {
c5169e0e   Renato De Donato   a new hope
96
97
            $(this).css('background-image', 'url(' + $(this).attr('src') + ')' );
            $(this).attr('src', 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');
74249687   Luigi Serra   Cross browser con...
98
99
100
101
          });
  
          // dynamically add indicators
          if (options.indicators) {
c5169e0e   Renato De Donato   a new hope
102
            var $indicators = $('<ul class="indicators"></ul>');
74249687   Luigi Serra   Cross browser con...
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
            $slides.each(function( index ) {
              var $indicator = $('<li class="indicator-item"></li>');
  
              // Handle clicks on indicators
              $indicator.click(function () {
                var $parent = $slider.parent();
                var curr_index = $parent.find($(this)).index();
                moveToSlide(curr_index);
  
                // reset interval
                clearInterval($interval);
                $interval = setInterval(
                  function(){
                    $active_index = $slider.find('.active').index();
                    if ($slides.length == $active_index + 1) $active_index = 0; // loop to start
                    else $active_index += 1;
  
                    moveToSlide($active_index);
  
                  }, options.transition + options.interval
                );
              });
              $indicators.append($indicator);
            });
            $this.append($indicators);
            $indicators = $this.find('ul.indicators').find('li.indicator-item');
          }
  
          if ($active) {
            $active.show();
          }
          else {
            $slides.first().addClass('active').velocity({opacity: 1}, {duration: options.transition, queue: false, easing: 'easeOutQuad'});
  
            $active_index = 0;
            $active = $slides.eq($active_index);
  
            // Update indicators
            if (options.indicators) {
              $indicators.eq($active_index).addClass('active');
            }
          }
  
          // Adjust height to current slide
          $active.find('img').each(function() {
            $active.find('.caption').velocity({opacity: 1, translateX: 0, translateY: 0}, {duration: options.transition, queue: false, easing: 'easeOutQuad'});
          });
  
          // auto scroll
          $interval = setInterval(
            function(){
              $active_index = $slider.find('.active').index();
              moveToSlide($active_index + 1);
  
            }, options.transition + options.interval
          );
  
  
          // HammerJS, Swipe navigation
  
          // Touch Event
          var panning = false;
          var swipeLeft = false;
          var swipeRight = false;
  
          $this.hammer({
              prevent_default: false
          }).bind('pan', function(e) {
            if (e.gesture.pointerType === "touch") {
  
              // reset interval
              clearInterval($interval);
  
              var direction = e.gesture.direction;
              var x = e.gesture.deltaX;
              var velocityX = e.gesture.velocityX;
  
              $curr_slide = $slider.find('.active');
              $curr_slide.velocity({ translateX: x
                  }, {duration: 50, queue: false, easing: 'easeOutQuad'});
  
              // Swipe Left
              if (direction === 4 && (x > ($this.innerWidth() / 2) || velocityX < -0.65)) {
                swipeRight = true;
              }
              // Swipe Right
              else if (direction === 2 && (x < (-1 * $this.innerWidth() / 2) || velocityX > 0.65)) {
                swipeLeft = true;
              }
  
              // Make Slide Behind active slide visible
              var next_slide;
              if (swipeLeft) {
                next_slide = $curr_slide.next();
                if (next_slide.length === 0) {
                  next_slide = $slides.first();
                }
                next_slide.velocity({ opacity: 1
                    }, {duration: 300, queue: false, easing: 'easeOutQuad'});
              }
              if (swipeRight) {
                next_slide = $curr_slide.prev();
                if (next_slide.length === 0) {
                  next_slide = $slides.last();
                }
                next_slide.velocity({ opacity: 1
                    }, {duration: 300, queue: false, easing: 'easeOutQuad'});
              }
  
  
            }
  
          }).bind('panend', function(e) {
            if (e.gesture.pointerType === "touch") {
  
              $curr_slide = $slider.find('.active');
              panning = false;
              curr_index = $slider.find('.active').index();
  
              if (!swipeRight && !swipeLeft) {
                // Return to original spot
                $curr_slide.velocity({ translateX: 0
                    }, {duration: 300, queue: false, easing: 'easeOutQuad'});
              }
              else if (swipeLeft) {
                moveToSlide(curr_index + 1);
                $curr_slide.velocity({translateX: -1 * $this.innerWidth() }, {duration: 300, queue: false, easing: 'easeOutQuad',
                                      complete: function() {
                                        $curr_slide.velocity({opacity: 0, translateX: 0}, {duration: 0, queue: false});
                                      } });
              }
              else if (swipeRight) {
                moveToSlide(curr_index - 1);
                $curr_slide.velocity({translateX: $this.innerWidth() }, {duration: 300, queue: false, easing: 'easeOutQuad',
                                      complete: function() {
                                        $curr_slide.velocity({opacity: 0, translateX: 0}, {duration: 0, queue: false});
                                      } });
              }
              swipeLeft = false;
              swipeRight = false;
  
              // Restart interval
              clearInterval($interval);
              $interval = setInterval(
                function(){
                  $active_index = $slider.find('.active').index();
                  if ($slides.length == $active_index + 1) $active_index = 0; // loop to start
                  else $active_index += 1;
  
                  moveToSlide($active_index);
  
                }, options.transition + options.interval
              );
            }
          });
  
          $this.on('sliderPause', function() {
            clearInterval($interval);
          });
  
          $this.on('sliderStart', function() {
            clearInterval($interval);
            $interval = setInterval(
              function(){
                $active_index = $slider.find('.active').index();
                if ($slides.length == $active_index + 1) $active_index = 0; // loop to start
                else $active_index += 1;
  
                moveToSlide($active_index);
  
              }, options.transition + options.interval
            );
          });
  
74249687   Luigi Serra   Cross browser con...
277
278
279
280
281
282
283
284
285
286
        });
  
  
  
      },
      pause : function() {
        $(this).trigger('sliderPause');
      },
      start : function() {
        $(this).trigger('sliderStart');
74249687   Luigi Serra   Cross browser con...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
      }
    };
  
  
      $.fn.slider = function(methodOrOptions) {
        if ( methods[methodOrOptions] ) {
          return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
          // Default to "init"
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  methodOrOptions + ' does not exist on jQuery.tooltip' );
        }
      }; // Plugin end
  }( jQuery ));