PDA

View Full Version : Switch Content Script - persist problem



psycho
01-20-2007, 07:06 AM
1) Script Title: Switch Content Script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/switchcontent.htm

3) Describe problem: This is a great script but I would like have the persistance feature enabled (so that it remembers the header states) AND if it finds no cookie (default state when starting a session) it is possible to specify which content to expand by default.
By default when
xxxxxx.setPersist(true)
all of my content blocks are contracted.

Is it possible to modify the following block of code:

switchcontent.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

so that it if it DOESN'T find a cookie then it expands specific content blocks?

All help and support greatly appreciated.
Thanks

jscheuer1
01-20-2007, 08:08 AM
You can feed it the numbers of the contents you want open:


switchcontent.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return "0,1"
}

Since things are usually numbered in javascript from 0 to whatever, the above (red) would represent the first two content blocks.

If you have only one instance of the script on a page, that is all you would need.

If you have more than one, you have to test for its class name:


switchcontent.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
if(Name=='switchgroup2')
return "0,1"
return ""
}

This would select switchgroup2 to open its first two blocks but, only if there were no cookie data for switchgroup2. You can use as many of these as you need, each with its own default block state settings:


switchcontent.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
if(Name=='switchgroup1')
return "1,2"
if(Name=='switchgroup2')
return "0,1"
return ""
}

This will only work with persistence true and these groups probably also would need collapse previous false if they were set to have more than one block open using the above method. If persist is false, you can use the default expanded property (as documented on the demo page):


<script type="text/javascript">

var joeexample=new switchcontent("switchgroup2", "p") //Limit scanning of switch contents to just "p" elements
joeexample.setStatus('[open] ', '[closed] ')
joeexample.setColor('green', 'red')
joeexample.collapsePrevious(false) //Allow more than 1 content to be open simultanously
joeexample.setPersist(false)
joeexample.defaultExpanded(0,1)
joeexample.init()
</script>

psycho
01-20-2007, 09:07 AM
Thanks John

That did the trick!

ddadmin
01-20-2007, 10:09 AM
Maybe I'll missing something in your question here, but why not just use the defaultExpanded() feature that's built in? For example:


joeexample.defaultExpanded(0, 1)

Would cause the first and 2nd contents to be expanded by default if no cookie is found (ie: Persist is enabled but this is the 1st page load).

Edit: Ah nevermind, I didn't realize the script behaved as such that if cookie is enabled, the defaultExpanded() function is not run altogether. Looks like a design flaw that I should probably fix...

jscheuer1
01-20-2007, 03:44 PM
Edit: Ah nevermind, I didn't realize the script behaved as such that if cookie is enabled, the defaultExpanded() function is not run altogether. Looks like a design flaw that I should probably fix...

How about:


function switchcontent(className){
if(!switchcontent.ar)
switchcontent.ar=[]; //array to cache instances of switchcontent on page
switchcontent.ar[switchcontent.ar.length]=this;
this.className=className
this.collapsePrev=false //Default: Collapse previous content each time
this.enablePersist=false //Default: Disable session only persistence
//Limit type of element to scan for on page for switch contents if 2nd function parameter is defined, for efficiency sake (ie: "div")
this.filter_content_tag=(arguments.length==2)? arguments[1].toLowerCase() : ""
}

and:


switchcontent.getCookie=function(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
for (var i_tem = 0; i_tem < switchcontent.ar.length; i_tem++)
if(Name=switchcontent.ar[i_tem].className&&switchcontent.ar[i_tem].expandedindices)
return switchcontent.ar[i_tem].expandedindices;
return ""
}

ddadmin
01-20-2007, 10:40 PM
Hmm there's actually no need to tamper with the cookies function. Basically where I went wrong with the original code was to completely ignore the defaultExpanded() function if persistence is set, which is too presumptuous. It should only be ignored if persistence is set AND the cookie actually contains a value, indicating this isn't the first time the page is loaded and to use that value.

So the only line in the script that needs to be changed is:


var opencontents_ids=(this.enablePersist && switchcontent.getCookie(this.className)!="")? ','+switchcontent.getCookie(this.className)+',' : (this.expandedindices)? ','+this.expandedindices+',' : ""

I'll probably do some more testing before updating the script with the above fix. I know, looking at someone else's code can be like looking for a needle in the haystack . :)

jscheuer1
01-20-2007, 11:15 PM
Makes sense if it works (looks to). I was already focused on the particular part of the code that I chose to modify from the way the original question in this thread was posed.