1(function( window, $, undefined ) { 2 3 // http://www.netcu.de/jquery-touchwipe-iphone-ipad-library 4 $.fn.touchwipe = function(settings) { 5 6 var config = { 7 min_move_x: 20, 8 min_move_y: 20, 9 wipeLeft: function() { }, 10 wipeRight: function() { }, 11 wipeUp: function() { }, 12 wipeDown: function() { }, 13 preventDefaultEvents: true 14 }; 15 16 if (settings) $.extend(config, settings); 17 18 this.each(function() { 19 var startX; 20 var startY; 21 var isMoving = false; 22 23 function cancelTouch() { 24 this.removeEventListener('touchmove', onTouchMove); 25 startX = null; 26 isMoving = false; 27 } 28 29 function onTouchMove(e) { 30 if(config.preventDefaultEvents) { 31 e.preventDefault(); 32 } 33 if(isMoving) { 34 var x = e.touches[0].pageX; 35 var y = e.touches[0].pageY; 36 var dx = startX - x; 37 var dy = startY - y; 38 if(Math.abs(dx) >= config.min_move_x) { 39 cancelTouch(); 40 if(dx > 0) { 41 config.wipeLeft(); 42 } 43 else { 44 config.wipeRight(); 45 } 46 } 47 else if(Math.abs(dy) >= config.min_move_y) { 48 cancelTouch(); 49 if(dy > 0) { 50 config.wipeDown(); 51 } 52 else { 53 config.wipeUp(); 54 } 55 } 56 } 57 } 58 59 function onTouchStart(e) 60 { 61 if (e.touches.length == 1) { 62 startX = e.touches[0].pageX; 63 startY = e.touches[0].pageY; 64 isMoving = true; 65 this.addEventListener('touchmove', onTouchMove, false); 66 } 67 } 68 if ('ontouchstart' in document.documentElement) { 69 this.addEventListener('touchstart', onTouchStart, false); 70 } 71 }); 72 73 return this; 74 }; 75 76 $.elastislide = function( options, element ) { 77 this.$el = $( element ); 78 this._init( options ); 79 }; 80 81 $.elastislide.defaults = { 82 speed : 450, // animation speed 83 easing : '', // animation easing effect 84 imageW : 190, // the images width 85 margin : 3, // image margin right 86 border : 2, // image border 87 minItems : 1, // the minimum number of items to show. 88 // when we resize the window, this will make sure minItems are always shown 89 // (unless of course minItems is higher than the total number of elements) 90 current : 0, // index of the current item 91 // when we resize the window, the carousel will make sure this item is visible 92 onClick : function() { return false; } // click item callback 93 }; 94 95 $.elastislide.prototype = { 96 _init : function( options ) { 97 98 this.options = $.extend( true, {}, $.elastislide.defaults, options ); 99 100 // <ul> 101 this.$slider = this.$el.find('ul'); 102 103 // <li> 104 this.$items = this.$slider.children('li'); 105 106 // total number of elements / images 107 this.itemsCount = this.$items.length; 108 109 // cache the <ul>'s parent, since we will eventually need to recalculate its width on window resize 110 this.$esCarousel = this.$slider.parent(); 111 112 // validate options 113 this._validateOptions(); 114 115 // set sizes and initialize some vars... 116 this._configure(); 117 118 // add navigation buttons 119 this._addControls(); 120 121 // initialize the events 122 this._initEvents(); 123 124 // show the <ul> 125 this.$slider.show(); 126 127 // slide to current's position 128 this._slideToCurrent( false ); 129 130 }, 131 _validateOptions : function() { 132 133 if( this.options.speed < 0 ) 134 this.options.speed = 450; 135 if( this.options.margin < 0 ) 136 this.options.margin = 4; 137 if( this.options.border < 0 ) 138 this.options.border = 1; 139 if( this.options.minItems < 1 || this.options.minItems > this.itemsCount ) 140 this.options.minItems = 1; 141 if( this.options.current > this.itemsCount - 1 ) 142 this.options.current = 0; 143 144 }, 145 _configure : function() { 146 147 // current item's index 148 this.current = this.options.current; 149 150 // the ul's parent's (div.es-carousel) width is the "visible" width 151 this.visibleWidth = this.$esCarousel.width(); 152 153 // test to see if we need to initially resize the items 154 if( this.visibleWidth < this.options.minItems * ( this.options.imageW + 2 * this.options.border ) + ( this.options.minItems - 1 ) * this.options.margin ) { 155 this._setDim( ( this.visibleWidth - ( this.options.minItems - 1 ) * this.options.margin ) / this.options.minItems ); 156 this._setCurrentValues();
157 // how many items fit with the current width 158 this.fitCount = this.options.minItems; 159 } 160 else { 161 this._setDim(); 162 this._setCurrentValues(); 163 } 164 165 // set the <ul> width 166 this.$slider.css({ 167 width : this.sliderW 168 }); 169 170 }, 171 _setDim : function( elW ) { 172 173 // <li> style 174 this.$items.css({ 175 marginRight : this.options.margin, 176 width : ( elW ) ? elW : this.options.imageW + 2 * this.options.border 177 }).children('a').css({ // <a> style 178 borderWidth : this.options.border 179 }); 180 181 }, 182 _setCurrentValues : function() { 183 184 // the total space occupied by one item 185 this.itemW = this.$items.outerWidth(true); 186 187 // total width of the slider / <ul> 188 // this will eventually change on window resize 189 this.sliderW = this.itemW * this.itemsCount; 190 191 // the ul parent's (div.es-carousel) width is the "visible" width 192 this.visibleWidth = this.$esCarousel.width(); 193 194 // how many items fit with the current width 195 this.fitCount = Math.floor( this.visibleWidth / this.itemW ); 196 197 }, 198 _addControls : function() { 199 200 this.$navNext = $('<span class="es-nav-next">Next</span>'); 201 this.$navPrev = $('<span class="es-nav-prev">Previous</span>'); 202 $('<div class="es-nav"/>') 203 .append( this.$navPrev ) 204 .append( this.$navNext ) 205 .appendTo( this.$el ); 206 207 //this._toggleControls(); 208 209 }, 210 _toggleControls : function( dir, status ) { 211 212 // show / hide navigation buttons 213 if( dir && status ) { 214 if( status === 1 ) 215 ( dir === 'right' ) ? this.$navNext.show() : this.$navPrev.show(); 216 else 217 ( dir === 'right' ) ? this.$navNext.hide() : this.$navPrev.hide(); 218 } 219 else if( this.current === this.itemsCount - 1 || this.fitCount >= this.itemsCount ) 220 this.$navNext.hide(); 221 222 }, 223 _initEvents : function() { 224 225 var instance = this; 226 227 // window resize 228 $(window).bind('resize.elastislide', function( event ) { 229 230 // set values again 231 instance._setCurrentValues();
232 233 // need to resize items 234 if( instance.visibleWidth < instance.options.minItems * ( instance.options.imageW + 2 * instance.options.border ) + ( instance.options.minItems - 1 ) * instance.options.margin ) { 235 instance._setDim( ( instance.visibleWidth - ( instance.options.minItems - 1 ) * instance.options.margin ) / instance.options.minItems ); 236 instance._setCurrentValues(); 237 instance.fitCount = instance.options.minItems; 238 } 239 else{ 240 instance._setDim(); 241 instance._setCurrentValues(); 242 } 243 244 instance.$slider.css({ 245 width : instance.sliderW + 10 // TODO: +10px seems to solve a firefox "bug" :S 246 }); 247 248 // slide to the current element 249 clearTimeout( instance.resetTimeout ); 250 instance.resetTimeout = setTimeout(function() { 251 instance._slideToCurrent(); 252 }, 200); 253 254 }); 255 256 // navigation buttons events 257 this.$navNext.bind('click.elastislide', function( event ) { 258 instance._slide('right'); 259 }); 260 261 this.$navPrev.bind('click.elastislide', function( event ) { 262 instance._slide('left'); 263 }); 264 265 // item click event 266 this.$items.bind('click.elastislide', function( event ) { 267 instance.options.onClick( $(this) ); 268 }); 269 270 // touch events 271 instance.$slider.touchwipe({ 272 wipeLeft : function() { 273 instance._slide('right'); 274 }, 275 wipeRight : function() { 276 instance._slide('left'); 277 } 278 }); 279 280 }, 281 _slide : function( dir, val, anim, callback ) { 282 283 // if animating return 284 if( this.$slider.is(':animated') ) 285 return false; 286 287 // current margin left 288 var ml = parseFloat( this.$slider.css('margin-left') ); 289 290 // val is just passed when we want an exact value for the margin left (used in the _slideToCurrent function) 291 if( val === undefined ) { 292 293 // how much to slide? 294 var amount = this.fitCount * this.itemW, val; 295 296 if( amount < 0 ) return false; 297 298 // make sure not to leave a space between the last item / first item and the end / beggining of the slider available width 299 if( dir === 'right' && this.sliderW - ( Math.abs( ml ) + amount ) < this.visibleWidth ) { 300 amount = this.sliderW - ( Math.abs( ml ) + this.visibleWidth ) - this.options.margin; // decrease the margin left 301 // show / hide navigation buttons 302 this._toggleControls( 'right', -1 ); 303 this._toggleControls( 'left', 1 ); 304 } 305 else if( dir === 'left' && Math.abs( ml ) - amount < 0 ) { 306 amount = Math.abs( ml ); 307 // show / hide navigation buttons 308 this._toggleControls( 'left', -1 ); 309 this._toggleControls( 'right', 1 ); 310 } 311 else { 312 var fml; // future margin left 313 ( dir === 'right' ) 314 ? fml = Math.abs( ml ) + this.options.margin + Math.abs( amount ) 315 : fml = Math.abs( ml ) - this.options.margin - Math.abs( amount ); 316 317 // show / hide navigation buttons 318 if( fml > 0 ) 319 this._toggleControls( 'left', 1 ); 320 else 321 this._toggleControls( 'left', -1 ); 322 323 if( fml < this.sliderW - this.visibleWidth ) 324 this._toggleControls( 'right', 1 ); 325 else 326 this._toggleControls( 'right', -1 ); 327 328 } 329 330 ( dir === 'right' ) ? val = '-=' + amount : val = '+=' + amount 331 332 } 333 else { 334 var fml = Math.abs( val ); // future margin left 335 336 if( Math.max( this.sliderW, this.visibleWidth ) - fml < this.visibleWidth ) { 337 val = - ( Math.max( this.sliderW, this.visibleWidth ) - this.visibleWidth ); 338 if( val !== 0 ) 339 val += this.options.margin; // decrease the margin left if not on the first position 340 341 // show / hide navigation buttons 342 this._toggleControls( 'right', -1 ); 343 fml = Math.abs( val ); 344 } 345 346 // show / hide navigation buttons 347 if( fml > 0 ) 348 this._toggleControls( 'left', 1 ); 349 else 350 this._toggleControls( 'left', -1 ); 351 352 if( Math.max( this.sliderW, this.visibleWidth ) - this.visibleWidth > fml + this.options.margin ) 353 this._toggleControls( 'right', 1 ); 354 else 355 this._toggleControls( 'right', -1 ); 356 357 } 358 359 $.fn.applyStyle = ( anim === undefined ) ? $.fn.animate : $.fn.css; 360 361 var sliderCSS = { marginLeft : val }; 362 363 var instance = this; 364 365 this.$slider.applyStyle( sliderCSS, $.extend( true, [], { duration : this.options.speed, easing : this.options.easing, complete : function() { 366 if( callback ) callback.call(); 367 } } ) ); 368 369 }, 370 _slideToCurrent : function( anim ) { 371 372 // how much to slide? 373 var amount = this.current * this.itemW; 374 this._slide('', -amount, anim ); 375 376 },
377 add : function( $newelems, callback ) { 378 379 // adds new items to the carousel 380 this.$items = this.$items.add( $newelems ); 381 this.itemsCount = this.$items.length; 382 this._setDim(); 383 this._setCurrentValues(); 384 this.$slider.css({ 385 width : this.sliderW 386 }); 387 this._slideToCurrent(); 388 389 if ( callback ) callback.call( $newelems ); 390 391 }, 392 destroy : function( callback ) { 393 this._destroy( callback ); 394 }, 395 _destroy : function( callback ) { 396 this.$el.unbind('.elastislide').removeData('elastislide'); 397 $(window).unbind('.elastislide'); 398 if ( callback ) callback.call(); 399 } 400 }; 401 402 var logError = function( message ) { 403 if ( this.console ) { 404 console.error( message ); 405 } 406 }; 407 408 $.fn.elastislide = function( options ) { 409 if ( typeof options === 'string' ) { 410 var args = Array.prototype.slice.call( arguments, 1 ); 411 412 this.each(function() { 413 var instance = $.data( this, 'elastislide' ); 414 if ( !instance ) { 415 logError( "cannot call methods on elastislide prior to initialization; " + 416 "attempted to call method '" + options + "'" ); 417 return; 418 } 419 if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) { 420 logError( "no such method '" + options + "' for elastislide instance" ); 421 return; 422 } 423 instance[ options ].apply( instance, args ); 424 }); 425 } 426 else { 427 this.each(function() { 428 var instance = $.data( this, 'elastislide' ); 429 if ( !instance ) { 430 $.data( this, 'elastislide', new $.elastislide( options, this ) ); 431 } 432 }); 433 } 434 return this; 435 }; 436 437})( window, jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.