PDA

View Full Version : Switch Content Script II: Need collapse previous option from ver 1



ethereal
03-20-2006, 10:18 AM
Hey all,

Script: Switch Content Script II
http://www.dynamicdrive.com/dynamicindex17/switchcontent2.htm

I love this script, but because using this on some webites (particularly free ones) real estate can be very limited. Since I prefer ver II of this script, I would like to see the collapseprevious option availiable in ver II as it was in ver I.

I tried and failed miserably (since I don't know js much at all) to do this myself, would it be possible to get this functionality in ver II?

Thanks for any assistance you can provide.

jscheuer1
03-20-2006, 02:51 PM
If using collapse previous in version II of this script, I think you would want to start out with all items collapsed and not offer the expand all/contract all options, the saving of the state of the contents should be based upon their being opened, not closed as is the case with the original script. If all that sounds OK to you, use this version of the script:


<script type="text/javascript">

/***********************************************
* Switch Content script II- Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use. Last updated April 2nd, 2005.
* Visit http://www.dynamicdrive.com/ for full source code
* Modified to start collapsed and collapse previous by
* jscheuer1 in http://www.dynamicdrive.com/forums
***********************************************/

var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var memoryduration="7" //persistence in # of days

var contractsymbol='minus.gif' //Path to image to represent contract state.
var expandsymbol='plus.gif' //Path to image to represent expand state.
var collapseprevious=1 //Set to 1 to collapse previous, 0 for previously opened items to remain open.

/////No need to edit beyond here //////////////////////////

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec, cid){
var inc=0
while (ccollect[inc]){
if (typeof cid=='undefined'||!(ccollect[inc].id==cid))
ccollect[inc].style.display=(ec=="contract")? "none" : ""
inc++
}
revivestatus()
}


function expandcontent(curobj, cid){
if (collapseprevious)
sweeptoggle('contract', cid)
if (ccollect.length>0){
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="none")? "none" : ""
curobj.src=(document.getElementById(cid).style.display=="none")? expandsymbol : contractsymbol
}
}

function revivecontent(){
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display=""
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="none")
statecollect[inc].src=expandsymbol
else
statecollect[inc].src=contractsymbol
inc++
}
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="")
selectedItem+=ccollect[inc].id+"|"
inc++
}
if (get_cookie(window.location.pathname)!=selectedItem){ //only update cookie if current states differ from cookie's
var expireDate = new Date()
expireDate.setDate(expireDate.getDate()+parseInt(memoryduration))
document.cookie = window.location.pathname+"="+selectedItem+";path=/;expires=" + expireDate.toGMTString()
}
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
sweeptoggle('contract')
if (enablepersist=="on" && get_cookie(window.location.pathname)!="" && ccollect.length>0)
revivecontent()
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

</script>

ethereal
03-23-2006, 07:38 AM
Thanks John, That worked perfectly!! :D

Just a thought, why isn't that option in the downloadable version of Switch Content Script II?

I think that others would want or could use this feature.

Again, just a thought.