Advertisement
Guest User

haccordion.js adapted for 100% width

a guest
Aug 13th, 2011
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Horizontal Accordion script
  2. * Created: Oct 27th, 2009. This notice must stay intact for usage
  3. * Author: Dynamic Drive at http://www.dynamicdrive.com/
  4. * Visit http://www.dynamicdrive.com/ for full source code
  5. */
  6.  
  7.  
  8. var haccordion={
  9.     //customize loading message if accordion markup is fetched via Ajax:
  10.     ajaxloadingmsg: '<div style="margin: 1em; font-weight: bold"><img src="ajaxloadr.gif" style="vertical-align: middle" /></div>',
  11.  
  12.     accordioninfo: {}, //class that holds config information of each haccordion instance
  13.  
  14.     expandli:function(accordionid, targetli){
  15.         var config=haccordion.accordioninfo[accordionid]
  16.         var $targetli=(typeof targetli=="number")? config.$targetlis.eq(targetli) : (typeof targetli=="string")? jQuery('#'+targetli) : jQuery(targetli)
  17.         if (typeof config.$lastexpanded!="undefined") //targetli may be an index, ID string, or DOM reference to LI
  18.             config.$lastexpanded.stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract last opened content
  19.         $targetli.stop().animate({width:$targetli.data('hpaneloffsetw')}, config.speed) //expand current content
  20.         config.$lastexpanded=$targetli
  21.     },
  22.  
  23.  
  24.     urlparamselect:function(accordionid){
  25.         var result=window.location.search.match(new RegExp(accordionid+"=(\\d+)", "i")) //check for "?accordionid=index" in URL
  26.         if (result!=null)
  27.             result=parseInt(RegExp.$1)+"" //return value as string so 0 doesn't test for false
  28.         return result //returns null or index, where index is the desired selected hcontent index
  29.     },
  30.  
  31.     getCookie:function(Name){
  32.         var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair
  33.         if (document.cookie.match(re)) //if cookie found
  34.             return document.cookie.match(re)[0].split("=")[1] //return its value
  35.         return null
  36.     },
  37.  
  38.     setCookie:function(name, value){
  39.         document.cookie = name + "=" + value + "; path=/"
  40.     },
  41.  
  42.  
  43.     loadexternal:function($, config){ //function to fetch external page containing the entire accordion content markup
  44.         var $hcontainer=$('#'+config.ajaxsource.container).html(this.ajaxloadingmsg)
  45.         $.ajax({
  46.             url: config.ajaxsource.path, //path to external content
  47.             async: true,
  48.             error:function(ajaxrequest){
  49.                 $hcontainer.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
  50.             },
  51.             success:function(content){
  52.                 $hcontainer.html(content)
  53.                 haccordion.init($, config)
  54.             }
  55.         })
  56.     },
  57.  
  58.  
  59.     init:function($, config){
  60.             haccordion.accordioninfo[config.accordionid]=config //cache config info for this accordion
  61.             var $targetlis=$('#'+config.accordionid).find('ul:eq(0) > li') //find top level LIs
  62.             config.$targetlis=$targetlis
  63.             config.selectedli=config.selectedli || [] //set default selectedli option
  64.             config.speed=config.speed || "normal" //set default speed
  65.  
  66. //KEY_CHANGE_BEGIN
  67.             var maxWidth    = $targetlis.parent ().width ();
  68.             $targetlis.each ( function () { maxWidth -= $(this).outerWidth (true); } );
  69.  
  70.             $targetlis.each(function(i){
  71.                 var $target=$(this).data('pos', i) //give each li an index #
  72.  
  73.                 var lclMaxWidth     = maxWidth + $target.find ('.hpanel:eq(0)').outerWidth (true);
  74.                 $target.css ('width', config.paneldimensions.fullw);
  75.  
  76.                 //get offset width of each .hpanel DIV (config.dimensions.fullw + any DIV padding)
  77.                 var hpaneloffsetw   = $target.find ('.hpanel:eq(0)').outerWidth (true);
  78.                 if (hpaneloffsetw > lclMaxWidth)
  79.                     hpaneloffsetw   = lclMaxWidth;
  80.  
  81.                 $target.data('hpaneloffsetw', hpaneloffsetw);
  82.                 $target.css ('width', '');
  83. //KEY_CHANGE_END
  84.  
  85.                 $target.mouseenter(function(){
  86.                         haccordion.expandli(config.accordionid, this)
  87.                     config.$lastexpanded=$(this)
  88.                 })
  89.                 if (config.collapsecurrent){ //if previous content should be contracted when expanding current
  90.                     $target.mouseleave(function(){
  91.                         $(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
  92.                     })
  93.                 }
  94.             }) //end $targetlis.each
  95.             var selectedli=haccordion.urlparamselect(config.accordionid) || ((config.selectedli[1] && haccordion.getCookie(config.accordionid))? parseInt(haccordion.getCookie(config.accordionid)) : config.selectedli[0])
  96.             selectedli=parseInt(selectedli)
  97.             if (selectedli>=0 && selectedli<config.$targetlis.length){ //if selectedli index is within range
  98.                 config.$lastexpanded=$targetlis.eq(selectedli)
  99.                 config.$lastexpanded.css('width', config.$lastexpanded.data('hpaneloffsetw')) //expand selected li
  100.             }
  101.             $(window).bind('unload', function(){ //clean up and persist on page unload
  102.                 haccordion.uninit($, config)
  103.             }) //end window.onunload
  104.     },
  105.  
  106.     uninit:function($, config){
  107.         var $targetlis=config.$targetlis
  108.         var expandedliindex=-1 //index of expanded content to remember (-1 indicates non)
  109.         $targetlis.each(function(){
  110.             var $target=$(this)
  111.             $target.unbind()
  112.             if ($target.width()==$target.data('hpaneloffsetw'))
  113.                 expandedliindex=$target.data('pos')
  114.         })
  115.         if (config.selectedli[1]==true) //enable persistence?
  116.             haccordion.setCookie(config.accordionid, expandedliindex)
  117.     },
  118.  
  119.     setup:function(config){
  120.         //Use JS to write out CSS that sets up initial dimensions of each LI, for JS enabled browsers only
  121.         document.write('<style type="text/css">\n')
  122.         document.write('#'+config.accordionid+' li{width: '+config.paneldimensions.peekw+';\nheight: '+config.paneldimensions.h+';\n}\n')
  123.         document.write('#'+config.accordionid+' li .hpanel{width: '+config.paneldimensions.fullw+';\nheight: '+config.paneldimensions.h+';\n}\n')
  124.         document.write('<\/style>')
  125.         jQuery(document).ready(function($){ //on Dom load
  126.             if (config.ajaxsource) //if config.ajaxsource option defined
  127.                 haccordion.loadexternal($, config)
  128.             else
  129.                 haccordion.init($, config)
  130.         }) //end DOM load
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement