1//** Accordion Content script: By Dynamic Drive, at http://www.dynamicdrive.com 2//** Created: Jan 7th, 08'. Last updated: Feb 16th, 2012 to v2.0 3 4//Version 1.9: June 7th, 2010': 5//**1) Ajax content support added, so a given header's content can be dynamically fetched from an external file and on demand. 6//**2) Optimized script performance by caching header and content container references 7 8//Version 2.0: Feb 16th, 2012': 9//** 1) Added option ("scrolltoheader") to scroll to the expanded header in question after it expands (useful if a header contains long content). 10 11 12var ddaccordion={ 13 ajaxloadingmsg: 'Loading Content...', //customize HTML to output while Ajax content is being fetched (if applicable) 14 15 headergroup: {}, //object to store corresponding header group based on headerclass value 16 contentgroup: {}, //object to store corresponding content group based on headerclass value 17 18 preloadimages:function($images){ 19 $images.each(function(){ 20 var preloadimage=new Image() 21 preloadimage.src=this.src 22 }) 23 }, 24 25 expandone:function(headerclass, selected, scrolltoheader){ //PUBLIC function to expand a particular header 26 this.toggleone(headerclass, selected, "expand", scrolltoheader) 27 }, 28 29 collapseone:function(headerclass, selected){ //PUBLIC function to collapse a particular header 30 this.toggleone(headerclass, selected, "collapse") 31 }, 32 33 expandall:function(headerclass){ //PUBLIC function to expand all headers based on their shared CSS classname 34 var $headers=this.headergroup[headerclass] 35 this.contentgroup[headerclass].filter(':hidden').each(function(){ 36 $headers.eq(parseInt($(this).attr('contentindex'))).trigger("evt_accordion") 37 }) 38 }, 39 40 collapseall:function(headerclass){ //PUBLIC function to collapse all headers based on their shared CSS classname 41 var $headers=this.headergroup[headerclass] 42 this.contentgroup[headerclass].filter(':visible').each(function(){ 43 $headers.eq(parseInt($(this).attr('contentindex'))).trigger("evt_accordion") 44 }) 45 }, 46 47 toggleone:function(headerclass, selected, optstate, scrolltoheader){ //PUBLIC function to expand/ collapse a particular header 48 var $targetHeader=this.headergroup[headerclass].eq(selected) 49 var $subcontent=this.contentgroup[headerclass].eq(selected) 50 if (typeof optstate=="undefined" || optstate=="expand" && $subcontent.is(":hidden") || optstate=="collapse" && $subcontent.is(":visible")) 51 $targetHeader.trigger("evt_accordion", [false, scrolltoheader]) 52 }, 53 54 ajaxloadcontent:function($targetHeader, $targetContent, config, callback){ 55 var ajaxinfo=$targetHeader.data('ajaxinfo') 56 57 function handlecontent(content){ //nested function 58 if (content){ //if ajax content has loaded
59 ajaxinfo.cacheddata=content //remember ajax content 60 ajaxinfo.status="cached" //set ajax status to cached 61 if ($targetContent.queue("fx").length==0){ //if this content isn't currently expanding or collapsing 62 $targetContent.hide().html(content) //hide loading message, then set sub content's HTML to ajax content 63 ajaxinfo.status="complete" //set ajax status to complete 64 callback() //execute callback function- expand this sub content 65 } 66 } 67 if (ajaxinfo.status!="complete"){ 68 setTimeout(function(){handlecontent(ajaxinfo.cacheddata)}, 100) //call handlecontent() again until ajax content has loaded (ajaxinfo.cacheddata contains data) 69 } 70 } //end nested function 71 72 if (ajaxinfo.status=="none"){ //ajax data hasn't been fetched yet 73 $targetContent.html(this.ajaxloadingmsg) 74 $targetContent.slideDown(config.animatespeed) 75 ajaxinfo.status="loading" //set ajax status to "loading" 76 $.ajax({ 77 url: ajaxinfo.url, //path to external menu file 78 error:function(ajaxrequest){ 79 handlecontent('Error fetching content. Server Response: '+ajaxrequest.responseText) 80 }, 81 success:function(content){ 82 content=(content=="")? " " : content //if returned content is empty, set it to "space" is content no longer returns false/empty (hasn't loaded yet) 83 handlecontent(content) 84 } 85 }) 86 } 87 else if (ajaxinfo.status=="loading") 88 handlecontent(ajaxinfo.cacheddata) 89 }, 90 91 expandit:function($targetHeader, $targetContent, config, useractivated, directclick, skipanimation, scrolltoheader){ 92 var ajaxinfo=$targetHeader.data('ajaxinfo') 93 if (ajaxinfo){ //if this content should be fetched via Ajax 94 if (ajaxinfo.status=="none" || ajaxinfo.status=="loading") 95 this.ajaxloadcontent($targetHeader, $targetContent, config, function(){ddaccordion.expandit($targetHeader, $targetContent, config, useractivated, directclick)}) 96 else if (ajaxinfo.status=="cached"){ 97 $targetContent.html(ajaxinfo.cacheddata) 98 ajaxinfo.cacheddata=null 99 ajaxinfo.status="complete" 100 } 101 } 102 this.transformHeader($targetHeader, config, "expand") 103 $targetContent.slideDown(skipanimation? 0 : config.animatespeed, function(){ 104 config.onopenclose($targetHeader.get(0), parseInt($targetHeader.attr('headerindex')), $targetContent.css('display'), useractivated) 105 if (scrolltoheader){ 106 var sthdelay=(config["collapseprev"])? 20 : 0 107 clearTimeout(config.sthtimer) 108 config.sthtimer=setTimeout(function(){ddaccordion.scrollToHeader($targetHeader)}, sthdelay) 109 } 110 if (config.postreveal=="gotourl" && directclick){ //if revealtype is "Go to Header URL upon click", and this is a direct click on the header 111 var targetLink=($targetHeader.is("a"))? $targetHeader.get(0) : $targetHeader.find('a:eq(0)').get(0) 112 if (targetLink) //if this header is a link 113 setTimeout(function(){location=targetLink.href}, 200 + (scrolltoheader? 400+sthdelay : 0) ) //ignore link target, as window.open(targetLink, targetLink.target) doesn't work in FF if popup blocker enabled 114 } 115 }) 116 }, 117 118 scrollToHeader:function($targetHeader){ 119 ddaccordion.$docbody.stop().animate({scrollTop: $targetHeader.offset().top}, 400) 120 }, 121 122 collapseit:function($targetHeader, $targetContent, config, isuseractivated){ 123 this.transformHeader($targetHeader, config, "collapse") 124 $targetContent.slideUp(config.animatespeed, function(){config.onopenclose($targetHeader.get(0), parseInt($targetHeader.attr('headerindex')), $targetContent.css('display'), isuseractivated)}) 125 }, 126 127 transformHeader:function($targetHeader, config, state){ 128 $targetHeader.addClass((state=="expand")? config.cssclass.expand : config.cssclass.collapse) //alternate btw "expand" and "collapse" CSS classes 129 .removeClass((state=="expand")? config.cssclass.collapse : config.cssclass.expand) 130 if (config.htmlsetting.location=='src'){ //Change header image (assuming header is an image)? 131 $targetHeader=($targetHeader.is("img"))? $targetHeader : $targetHeader.find('img').eq(0) //Set target to either header itself, or first image within header 132 $targetHeader.attr('src', (state=="expand")? config.htmlsetting.expand : config.htmlsetting.collapse) //change header image 133 } 134 else if (config.htmlsetting.location=="prefix") //if change "prefix" HTML, locate dynamically added ".accordprefix" span tag and change it 135 $targetHeader.find('.accordprefix').empty().append((state=="expand")? config.htmlsetting.expand : config.htmlsetting.collapse) 136 else if (config.htmlsetting.location=="suffix") 137 $targetHeader.find('.accordsuffix').empty().append((state=="expand")? config.htmlsetting.expand : config.htmlsetting.collapse) 138 }, 139 140 urlparamselect:function(headerclass){ 141 var result=window.location.search.match(new RegExp(headerclass+"=((\\d+)(,(\\d+))*)", "i")) //check for "?headerclass=2,3,4" in URL 142 if (result!=null)
143 result=RegExp.$1.split(',') 144 return result //returns null, [index], or [index1,index2,etc], where index are the desired selected header indices 145 }, 146 147 getCookie:function(Name){ 148 var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair 149 if (document.cookie.match(re)) //if cookie found 150 return document.cookie.match(re)[0].split("=")[1] //return its value 151 return null 152 }, 153 154 setCookie:function(name, value){ 155 document.cookie = name + "=" + value + "; path=/" 156 }, 157 158 init:function(config){ 159 document.write('<style type="text/css">\n') 160 document.write('.'+config.contentclass+'{display: none}\n') //generate CSS to hide contents 161 document.write('a.hiddenajaxlink{display: none}\n') //CSS class to hide ajax link 162 document.write('<\/style>') 163 jQuery(document).ready(function($){ 164 ddaccordion.urlparamselect(config.headerclass) 165 var persistedheaders=ddaccordion.getCookie(config.headerclass) 166 ddaccordion.headergroup[config.headerclass]=$('.'+config.headerclass) //remember header group for this accordion 167 ddaccordion.contentgroup[config.headerclass]=$('.'+config.contentclass) //remember content group for this accordion 168 ddaccordion.$docbody=(window.opera)? (document.compatMode=="CSS1Compat"? jQuery('html') : jQuery('body')) : jQuery('html,body') 169 var $headers=ddaccordion.headergroup[config.headerclass] 170 var $subcontents=ddaccordion.contentgroup[config.headerclass] 171 config.cssclass={collapse: config.toggleclass[0], expand: config.toggleclass[1]} //store expand and contract CSS classes as object properties 172 config.revealtype=config.revealtype || "click" 173 config.revealtype=config.revealtype.replace(/mouseover/i, "mouseenter") 174 if (config.revealtype=="clickgo"){ 175 config.postreveal="gotourl" //remember added action 176 config.revealtype="click" //overwrite revealtype to "click" keyword 177 } 178 if (typeof config.togglehtml=="undefined") 179 config.htmlsetting={location: "none"} 180 else 181 config.htmlsetting={location: config.togglehtml[0], collapse: config.togglehtml[1], expand: config.togglehtml[2]} //store HTML settings as object properties 182 config.oninit=(typeof config.oninit=="undefined")? function(){} : config.oninit //attach custom "oninit" event handler 183 config.onopenclose=(typeof config.onopenclose=="undefined")? function(){} : config.onopenclose //attach custom "onopenclose" event handler 184 var lastexpanded={} //object to hold reference to last expanded header and content (jquery objects) 185 var expandedindices=ddaccordion.urlparamselect(config.headerclass) || ((config.persiststate && persistedheaders!=null)? persistedheaders : config.defaultexpanded) 186 if (typeof expandedindices=='string') //test for string value (exception is config.defaultexpanded, which is an array) 187 expandedindices=expandedindices.replace(/c/ig, '').split(',') //transform string value to an array (ie: "c1,c2,c3" becomes [1,2,3] 188 if (expandedindices.length==1 && expandedindices[0]=="-1") //check for expandedindices value of [-1], indicating persistence is on and no content expanded 189 expandedindices=[] 190 if (config["collapseprev"] && expandedindices.length>1) //only allow one content open? 191 expandedindices=[expandedindices.pop()] //return last array element as an array (for sake of jQuery.inArray()) 192 if (config["onemustopen"] && expandedindices.length==0) //if at least one content should be open at all times and none are, open 1st header 193 expandedindices=[0] 194 $headers.each(function(index){ //loop through all headers 195 var $header=$(this) 196 if (/(prefix)|(suffix)/i.test(config.htmlsetting.location) && $header.html()!=""){ //add a SPAN element to header depending on user setting and if header is a container tag 197 $('<span class="accordprefix"></span>').prependTo(this) 198 $('<span class="accordsuffix"></span>').appendTo(this) 199 } 200 $header.attr('headerindex', index+'h') //store position of this header relative to its peers 201 $subcontents.eq(index).attr('contentindex', index+'c') //store position of this content relative to its peers 202 var $subcontent=$subcontents.eq(index) 203 var $hiddenajaxlink=$subcontent.find('a.hiddenajaxlink:eq(0)') //see if this content should be loaded via ajax 204 if ($hiddenajaxlink.length==1){ 205 $header.data('ajaxinfo', {url:$hiddenajaxlink.attr('href'), cacheddata:null, status:'none'}) //store info about this ajax content inside header 206 } 207 var needle=(typeof expandedindices[0]=="number")? index : index+'' //check for data type within expandedindices array- index should match that type 208 if (jQuery.inArray(needle, expandedindices)!=-1){ //check for headers that should be expanded automatically (convert index to string first) 209 ddaccordion.expandit($header, $subcontent, config, false, false, !config.animatedefault) //3rd last param sets 'isuseractivated' parameter, 2nd last sets isdirectclick, last sets skipanimation param 210 lastexpanded={$header:$header, $content:$subcontent} 211 } //end check 212 else{ 213 $subcontent.hide() 214 config.onopenclose($header.get(0), parseInt($header.attr('headerindex')), $subcontent.css('display'), false) //Last Boolean value sets 'isuseractivated' parameter 215 ddaccordion.transformHeader($header, config, "collapse") 216 } 217 }) 218 //if (config["scrolltoheader"] && lastexpanded.$header) 219 //ddaccordion.scrollToHeader(lastexpanded.$header) 220 $headers.bind("evt_accordion", function(e, isdirectclick, scrolltoheader){ //assign CUSTOM event handler that expands/ contacts a header 221 var $subcontent=$subcontents.eq(parseInt($(this).attr('headerindex'))) //get subcontent that should be expanded/collapsed 222 if ($subcontent.css('display')=="none"){ 223 ddaccordion.expandit($(this), $subcontent, config, true, isdirectclick, false, scrolltoheader) //2nd last param sets 'isuseractivated' parameter 224 if (config["collapseprev"] && lastexpanded.$header && $(this).get(0)!=lastexpanded.$header.get(0)){ //collapse previous content? 225 ddaccordion.collapseit(lastexpanded.$header, lastexpanded.$content, config, true) //Last Boolean value sets 'isuseractivated' parameter 226 } 227 lastexpanded={$header:$(this), $content:$subcontent} 228 } 229 else if (!config["onemustopen"] || config["onemustopen"] && lastexpanded.$header && $(this).get(0)!=lastexpanded.$header.get(0)){ 230 ddaccordion.collapseit($(this), $subcontent, config, true) //Last Boolean value sets 'isuseractivated' parameter 231 } 232 }) 233 $headers.bind(config.revealtype, function(){ 234 if (config.revealtype=="mouseenter"){ 235 clearTimeout(config.revealdelay) 236 var headerindex=parseInt($(this).attr("headerindex")) 237 config.revealdelay=setTimeout(function(){ddaccordion.expandone(config["headerclass"], headerindex, config.scrolltoheader)}, config.mouseoverdelay || 0) 238 } 239 else{ 240 $(this).trigger("evt_accordion", [true, config.scrolltoheader]) //last parameter indicates this is a direct click on the header 241 return false //cancel default click behavior 242 } 243 }) 244 $headers.bind("mouseleave", function(){ 245 clearTimeout(config.revealdelay) 246 }) 247 config.oninit($headers.get(), expandedindices) 248 $(window).bind('unload', function(){ //clean up and persist on page unload 249 $headers.unbind() 250 var expandedindices=[] 251 $subcontents.filter(':visible').each(function(index){ //get indices of expanded headers 252 expandedindices.push($(this).attr('contentindex')) 253 }) 254 if (config.persiststate==true && $headers.length>0){ //persist state? 255 expandedindices=(expandedindices.length==0)? '-1c' : expandedindices //No contents expanded, indicate that with dummy '-1c' value? 256 ddaccordion.setCookie(config.headerclass, expandedindices) 257 } 258 }) 259 }) 260 } 261} 262 263//preload any images defined inside ajaxloadingmsg variable 264//ddaccordion.preloadimages(jQuery(ddaccordion.ajaxloadingmsg).filter('img'))
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.