1/** 2 * jquery.dropdown.js v1.0.0 3 * http://www.codrops.com 4 * 5 * Licensed under the MIT license. 6 * http://www.opensource.org/licenses/mit-license.php 7 * 8 * Copyright 2012, Codrops 9 * http://www.codrops.com 10 */ 11;( function( $, window, undefined ) { 12 13 'use strict'; 14 15 $.DropDown = function( options, element ) { 16 this.$el = $( element ); 17 this._init( options ); 18 }; 19 20 // the options 21 $.DropDown.defaults = { 22 speed : 300, 23 easing : 'ease', 24 gutter : 0, 25 // initial stack effect 26 stack : false, 27 // delay between each option animation 28 delay : 0, 29 // random angle and positions for the options 30 random : false, 31 // rotated [right||left||false] : the options will be rotated to thr right side or left side. 32 // make sure to tune the transform origin in the stylesheet 33 rotated : false, 34 // effect to slide in the options. value is the margin to start with 35 slidingIn : false, 36 //Hoppy 37 onActivate : function(opt) { return false; }, 38 onOptionSelect : function(opt) { return false; } 39 }; 40 41 $.DropDown.prototype = { 42 43 _init : function( options ) { 44 45 // options 46 this.options = $.extend( true, {}, $.DropDown.defaults, options ); 47 this._layout(); 48 this._initEvents(); 49 50 }, 51 _layout : function() { 52 53 var self = this; 54 this.minZIndex = 1000; 55 var value = this._transformSelect(); 56 this.opts = this.listopts.children( 'li' ); 57 this.optsCount = this.opts.length; 58 this.size = { width : this.dd.width(), height : this.dd.height() }; 59 60 var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ), 61 inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime(); 62 63 this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel ); 64 65 this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount ); 66 this._positionOpts(); 67 if( Modernizr.csstransitions ) { 68 setTimeout( function() { self.opts.css( 'transition', 'all ' + self.options.speed + 'ms ' + self.options.easing ); }, 25 ); 69 } 70 71 }, 72 _transformSelect : function() { 73 74 var optshtml = '', selectlabel = '', value = -1; 75 this.$el.children( 'option' ).each( function() { 76 77 var $this = $( this ), 78 val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) , 79 classes = $this.attr( 'class' ), 80 selected = $this.attr( 'selected' ), 81 label = $this.text(); 82 83 if( val !== -1 ) { 84 optshtml += 85 classes !== undefined ? 86 '<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' : 87 '<li data-value="' + val + '"><span>' + label + '</span></li>'; 88 } 89 90 if( selected ) { 91 selectlabel = label; 92 value = val; 93 } 94 95 } ); 96 97 this.listopts = $( '<ul/>' ).append( optshtml ); 98 this.selectlabel = $( '<span/>' ).append( selectlabel ); 99 this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el ); 100 this.$el.remove(); 101 102 return value; 103 104 }, 105 _positionOpts : function( anim ) { 106 107 var self = this; 108 109 this.listopts.css( 'height', 'auto' ); 110 this.opts 111 .each( function( i ) { 112 $( this ).css( { 113 zIndex : self.minZIndex + self.optsCount - 1 - i, 114 top : self.options.slidingIn ? ( i + 1 ) * ( self.size.height + self.options.gutter ) : 0, 115 left : 0, 116 marginLeft : self.options.slidingIn ? i % 2 === 0 ? self.options.slidingIn : - self.options.slidingIn : 0, 117 opacity : self.options.slidingIn ? 0 : 1, 118 transform : 'none' 119 } ); 120 } ); 121 122 if( !this.options.slidingIn ) { 123 this.opts 124 .eq( this.optsCount - 1 ) 125 .css( { top : this.options.stack ? 9 : 0, left : this.options.stack ? 4 : 0, width : this.options.stack ? this.size.width - 8 : this.size.width, transform : 'none' } ) 126 .end() 127 .eq( this.optsCount - 2 ) 128 .css( { top : this.options.stack ? 6 : 0, left : this.options.stack ? 2 : 0, width : this.options.stack ? this.size.width - 4 : this.size.width, transform : 'none' } ) 129 .end() 130 .eq( this.optsCount - 3 ) 131 .css( { top : this.options.stack ? 3 : 0, left : 0, transform : 'none' } ); 132 } 133 134 }, 135 _initEvents : function() { 136 137 var self = this; 138 139 this.selectlabel.on( 'mousedown.dropdown', function( event ) { 140 //Hoppy 141 var opt = $(this); 142 self.options.onActivate( opt ); 143 self.opened ? self.close() : self.open(); 144 return false;
145 146 } ); 147 148 this.opts.on( 'click.dropdown', function() { 149 if( self.opened ) { 150 var opt = $( this ); 151 self.options.onOptionSelect( opt ); 152 self.inputEl.val( opt.data( 'value' ) ); 153 self.selectlabel.html( opt.html() ); 154 self.close(); 155 } 156 } ); 157 158 }, 159 open : function() { 160 var self = this; 161 this.dd.toggleClass( 'cd-active' ); 162 this.listopts.css( 'height', ( this.optsCount + 1 ) * ( this.size.height + this.options.gutter ) ); 163 this.opts.each( function( i ) { 164 165 $( this ).css( { 166 opacity : 1, 167 top : self.options.rotated ? self.size.height + self.options.gutter : ( i + 1 ) * ( self.size.height + self.options.gutter ), 168 left : self.options.random ? Math.floor( Math.random() * 11 - 5 ) : 0, 169 width : self.size.width, 170 marginLeft : 0, 171 transform : self.options.random ? 172 'rotate(' + Math.floor( Math.random() * 11 - 5 ) + 'deg)' : 173 self.options.rotated ? 174 self.options.rotated === 'right' ? 175 'rotate(-' + ( i * 5 ) + 'deg)' : 176 'rotate(' + ( i * 5 ) + 'deg)' 177 : 'none', 178 transitionDelay : self.options.delay && Modernizr.csstransitions ? self.options.slidingIn ? ( i * self.options.delay ) + 'ms' : ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : 0 179 } ); 180 181 } ); 182 this.opened = true; 183 184 }, 185 close : function() { 186 187 var self = this; 188 this.dd.toggleClass( 'cd-active' ); 189 if( this.options.delay && Modernizr.csstransitions ) { 190 this.opts.each( function( i ) { 191 $( this ).css( { 'transition-delay' : self.options.slidingIn ? ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : ( i * self.options.delay ) + 'ms' } ); 192 } ); 193 } 194 this._positionOpts( true ); 195 this.opened = false; 196 197 } 198 199 } 200 201 $.fn.dropdown = function( options ) { 202 var instance = $.data( this, 'dropdown' ); 203 if ( typeof options === 'string' ) { 204 var args = Array.prototype.slice.call( arguments, 1 ); 205 this.each(function() { 206 instance[ options ].apply( instance, args ); 207 }); 208 } 209 else { 210 this.each(function() { 211 instance ? instance._init() : instance = $.data( this, 'dropdown', new $.DropDown( options, this ) ); 212 }); 213 } 214 return instance; 215 }; 216 217} )( jQuery, window );
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.