PageSourceSearch

https://www.aifsfoundation.org/assets/js/jquery.accordion.js

js aifsfoundation.org collected 2026-09-29 16:06:05 UTC 6,749 bytes, 246 lines download raw bytes

1(function( window, $, undefined ) {
2	
3	/*
4	* smartresize: debounced resize event for jQuery
5	*
6	* latest version and complete README available on Github:
7	* https://github.com/louisremi/jquery.smartresize.js
8	*
9	* Copyright 2011 @louis_remi
10	* Licensed under the MIT license.
11	*/
12
13	var $event = $.event, resizeTimeout;
14
15	$event.special.smartresize 	= {
16		setup: function() {
17			$(this).bind( "resize", $event.special.smartresize.handler );
18		},
19		teardown: function() {
20			$(this).unbind( "resize", $event.special.smartresize.handler );
21		},
22		handler: function( event, execAsap ) {
23			// Save the context
24			var context = this,
25				args 	= arguments;
26
27			// set correct event type
28			event.type = "smartresize";
29
30			if ( resizeTimeout ) { clearTimeout( resizeTimeout ); }
31			resizeTimeout = setTimeout(function() {
32				jQuery.event.handle.apply( context, args );
33			}, execAsap === "execAsap"? 0 : 100 );
34		}
35	};
36
37	$.fn.smartresize 			= function( fn ) {
38		return fn ? this.bind( "smartresize", fn ) : this.trigger( "smartresize", ["execAsap"] );
39	};
40	
41	$.Accordion 				= function( options, element ) {
42	
43		this.$el			= $( element );
44		// list items
45		this.$items			= this.$el.children('ul').children('li');
46		// total number of items
47		this.itemsCount		= this.$items.length;
48		
49		// initialize accordion
50		this._init( options );
51		
52	};
53	
54	$.Accordion.defaults 		= {
55		// index of opened item. -1 means all are closed by default.
56		open			: -1,
57		// if set to true, only one item can be opened. Once one item is opened, any other that is opened will be closed first
58		oneOpenedItem	: false,
59		// speed of the open / close item animation
60		speed			: 300,
61		// easing of the open / close item animation
62		easing			: 'easeInOutExpo',
63		// speed of the scroll to action animation
64		scrollSpeed		: 900,
65		// easing of the scroll to action animation
66		scrollEasing	: 'easeInOutExpo'
67    };
68	
69	$.Accordion.prototype 		= {
70		_init 				: function( options ) {
71			
72			this.options 		= $.extend( true, {}, $.Accordion.defaults, options );
73			
74			// validate options
75			this._validate();
76			
77			// current is the index of the opened item
78			this.current		= this.options.open;
79			
80			// hide the contents so we can fade it in afterwards
81			this.$items.find('div.st-content').hide();
82			
83			// save original height and top of each item	
84			this._saveDimValues();
85			
86			// if we want a default opened item...
87			if( this.current != -1 )
88				this._toggleItem( this.$items.eq( this.current ) );
89			
90			// initialize the events
91			this._initEvents();
92			
93		},
94		_saveDimValues		: function() {
95		
96			this.$items.each( function() {
97				
98				var $item		= $(this);
99				
100				$item.data({
101					originalHeight 	: $item.find('a:first').height(),
102					offsetTop		: $item.offset().top
103				});
104				
105			});
106			
107		},
108		// validate options
109		_validate			: function() {
110		
111			// open must be between -1 and total number of items, otherwise we set it to -1
112			if( this.options.open < -1 || this.options.open > this.itemsCount - 1 )
113				this.options.open = -1;
114	 	
115		},
116		_initEvents			: function() {
117			
118			var instance	= this;
119			
120			// open / close item
121			this.$items.find('a:first').bind('click.accordion', function( event ) {
122				
123				var $item			= $(this).parent();
124				
125				// close any opened item if oneOpenedItem is true
126				if( instance.options.oneOpenedItem && instance._isOpened() && instance.current!== $item.index() ) {
127					
128					instance._toggleItem( instance.$items.eq( instance.current ) );
129				
130				}
131				
132				// open / close item
133				instance._toggleItem( $item );
134				
135				return false;
136			
137			});
138			
139			$(window).bind('smartresize.accordion', function( event ) {
140				
141				// reset orinal item values
142				instance._saveDimValues();
143			
144				// reset the content's height of any item that is currently opened
145				instance.$el.find('li.st-open').each( function() {
146					
147					var $this	= $(this);
148					$this.css( 'height', $this.data( 'originalHeight' ) + $this.find('div.st-content').outerHeight( true ) );
149				
150				});
151				
152				// scroll to current
153				if( instance._isOpened() )
154				instance._scroll();
155				
156			});
157			
158		},
159		// checks if there is any opened item
160		_isOpened			: function() {
161		
162			return ( this.$el.find('li.st-open').length > 0 );
163		
164		},
165		// open / close item
166		_toggleItem			: function( $item ) {
167			
168			var $content = $item.find('div.st-content');
169			
170			( $item.hasClass( 'st-open' ) ) 
171					
172				? ( this.current = -1, $content.stop(true, true).fadeOut( this.options.speed ), $item.removeClass( 'st-open' ).stop().animate({
173					height	: $item.data( 'originalHeight' )
174				}, this.options.speed, this.options.easing ) )
175				
176				: ( this.current = $item.index(), $content.stop(true, true).fadeIn( this.options.speed ), $item.addClass( 'st-open' ).stop().animate({
177					height	: $item.data( 'originalHeight' ) + $content.outerHeight( true )
178				}, this.options.speed, this.options.easing ), this._scroll( this ) )
179		
180		},
181		// scrolls to current item or last opened item if current is -1
182		_scroll				: function( instance ) {
183			
184			var instance	= instance || this, current;
185			
186			( instance.current !== -1 ) ? current = instance.current : current = instance.$el.find('li.st-open:last').index();
187			
188			//$('html, body').stop().animate({
189//				scrollTop	: ( instance.options.oneOpenedItem ) ? instance.$items.eq( current ).data( 'offsetTop' ) : instance.$items.eq( current ).offset().top
190//			}, instance.options.scrollSpeed, instance.options.scrollEasing );
191		
192		}
193	};
194	
195	var logError 				= function( message ) {
196		
197		if ( this.console ) {
198			
199			console.error( message );
200			
201		}
202		
203	};
204	
205	$.fn.accordion 				= function( options ) {
206	
207		if ( typeof options === 'string' ) {
208		
209			var args = Array.prototype.slice.call( arguments, 1 );
210
211			this.each(function() {
212			
213				var instance = $.data( this, 'accordion' );
214				
215				if ( !instance ) {
216					logError( "cannot call methods on accordion prior to initialization; " +
217					"attempted to call method '" + options + "'" );
218					return;
219				}
220				
221				if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
222					logError( "no such method '" + options + "' for accordion instance" );
223					return;
224				}
225				
226				instance[ options ].apply( instance, args );
227			
228			});
229		
230		} 
231		else {
232		
233			this.each(function() {
234				var instance = $.data( this, 'accordion' );
235				if ( !instance ) {
236					$.data( this, 'accordion', new $.Accordion( options, this ) );
237				}
238			});
239		
240		}
241		
242		return this;
243		
244	};
245	
246})( 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.