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 : 0, // image margin right 86 border : 0, // 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 //return false; 269 }); 270 271 // touch events 272 instance.$slider.touchwipe({ 273 wipeLeft : function() { 274 instance._slide('right'); 275 }, 276 wipeRight : function() { 277 instance._slide('left'); 278 } 279 }); 280 281 }, 282 _slide : function( dir, val, anim, callback ) { 283 284 // if animating return 285 if( this.$slider.is(':animated') ) 286 return false; 287 288 // current margin left 289 var ml = parseFloat( this.$slider.css('margin-left') ); 290 291 // val is just passed when we want an exact value for the margin left (used in the _slideToCurrent function) 292 if( val === undefined ) { 293 294 // how much to slide? 295 var amount = this.fitCount * this.itemW, val; 296 297 if( amount < 0 ) return false; 298 299 // make sure not to leave a space between the last item / first item and the end / beggining of the slider available width 300 if( dir === 'right' && this.sliderW - ( Math.abs( ml ) + amount ) < this.visibleWidth ) { 301 amount = this.sliderW - ( Math.abs( ml ) + this.visibleWidth ) - this.options.margin; // decrease the margin left 302 // show / hide navigation buttons 303 this._toggleControls( 'right', -1 ); 304 this._toggleControls( 'left', 1 ); 305 } 306 else if( dir === 'left' && Math.abs( ml ) - amount < 0 ) { 307 amount = Math.abs( ml ); 308 // show / hide navigation buttons 309 this._toggleControls( 'left', -1 ); 310 this._toggleControls( 'right', 1 ); 311 } 312 else { 313 var fml; // future margin left 314 ( dir === 'right' ) 315 ? fml = Math.abs( ml ) + this.options.margin + Math.abs( amount ) 316 : fml = Math.abs( ml ) - this.options.margin - Math.abs( amount ); 317 318 // show / hide navigation buttons 319 if( fml > 0 ) 320 this._toggleControls( 'left', 1 ); 321 else 322 this._toggleControls( 'left', -1 ); 323 324 if( fml < this.sliderW - this.visibleWidth ) 325 this._toggleControls( 'right', 1 ); 326 else 327 this._toggleControls( 'right', -1 ); 328 329 } 330 331 ( dir === 'right' ) ? val = '-=' + amount : val = '+=' + amount 332 333 } 334 else { 335 var fml = Math.abs( val ); // future margin left 336 337 if( Math.max( this.sliderW, this.visibleWidth ) - fml < this.visibleWidth ) { 338 val = - ( Math.max( this.sliderW, this.visibleWidth ) - this.visibleWidth ); 339 if( val !== 0 ) 340 val += this.options.margin; // decrease the margin left if not on the first position 341 342 // show / hide navigation buttons 343 this._toggleControls( 'right', -1 ); 344 fml = Math.abs( val ); 345 } 346 347 // show / hide navigation buttons 348 if( fml > 0 ) 349 this._toggleControls( 'left', 1 ); 350 else 351 this._toggleControls( 'left', -1 ); 352 353 if( Math.max( this.sliderW, this.visibleWidth ) - this.visibleWidth > fml + this.options.margin ) 354 this._toggleControls( 'right', 1 ); 355 else 356 this._toggleControls( 'right', -1 ); 357 358 } 359 360 $.fn.applyStyle = ( anim === undefined ) ? $.fn.animate : $.fn.css; 361 362 var sliderCSS = { marginLeft : val }; 363 364 var instance = this; 365 366 this.$slider.applyStyle( sliderCSS, $.extend( true, [], { duration : this.options.speed, easing : this.options.easing, complete : function() { 367 if( callback ) callback.call(); 368 } } ) ); 369 370 }, 371 _slideToCurrent : function( anim ) { 372 373 // how much to slide? 374 var amount = this.current * this.itemW; 375 this._slide('', -amount, anim ); 376 377 },
378 add : function( $newelems, callback ) { 379 380 // adds new items to the carousel 381 this.$items = this.$items.add( $newelems ); 382 this.itemsCount = this.$items.length; 383 this._setDim(); 384 this._setCurrentValues(); 385 this.$slider.css({ 386 width : this.sliderW 387 }); 388 this._slideToCurrent(); 389 390 if ( callback ) callback.call( $newelems ); 391 392 }, 393 destroy : function( callback ) { 394 this._destroy( callback ); 395 }, 396 _destroy : function( callback ) { 397 this.$el.unbind('.elastislide').removeData('elastislide'); 398 $(window).unbind('.elastislide'); 399 if ( callback ) callback.call(); 400 } 401 }; 402 403 var logError = function( message ) { 404 if ( this.console ) { 405 console.error( message ); 406 } 407 }; 408 409 $.fn.elastislide = function( options ) { 410 if ( typeof options === 'string' ) { 411 var args = Array.prototype.slice.call( arguments, 1 ); 412 413 this.each(function() { 414 var instance = $.data( this, 'elastislide' ); 415 if ( !instance ) { 416 logError( "cannot call methods on elastislide prior to initialization; " + 417 "attempted to call method '" + options + "'" ); 418 return; 419 } 420 if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) { 421 logError( "no such method '" + options + "' for elastislide instance" ); 422 return; 423 } 424 instance[ options ].apply( instance, args ); 425 }); 426 } 427 else { 428 this.each(function() { 429 var instance = $.data( this, 'elastislide' ); 430 if ( !instance ) { 431 $.data( this, 'elastislide', new $.elastislide( options, this ) ); 432 } 433 }); 434 } 435 return this; 436 }; 437 438})( 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.