Revision 256c879b
Von Moritz Bunkus vor etwa 16 Jahren hinzugefügt
js/tabcontent.js | ||
---|---|---|
1 |
//** Tab Content script- ? Dynamic Drive DHTML code library (http://www.dynamicdrive.com) |
|
2 |
//** Last updated: June 29th, 06 |
|
3 |
|
|
4 |
var enabletabpersistence=1 //enable tab persistence via session only cookies, so selected tab is remembered? |
|
1 |
//** Tab Content script v2.0- ? Dynamic Drive DHTML code library (http://www.dynamicdrive.com) |
|
2 |
//** Updated Oct 7th, 07 to version 2.0. Contains numerous improvements: |
|
3 |
// -Added Auto Mode: Script auto rotates the tabs based on an interval, until a tab is explicitly selected |
|
4 |
// -Ability to expand/contract arbitrary DIVs on the page as the tabbed content is expanded/ contracted |
|
5 |
// -Ability to dynamically select a tab either based on its position within its peers, or its ID attribute (give the target tab one 1st) |
|
6 |
// -Ability to set where the CSS classname "selected" get assigned- either to the target tab's link ("A"), or its parent container |
|
7 |
//** Updated Feb 18th, 08 to version 2.1: Adds a "tabinstance.cycleit(dir)" method to cycle forward or backward between tabs dynamically |
|
8 |
//** Updated April 8th, 08 to version 2.2: Adds support for expanding a tab using a URL parameter (ie: http://mysite.com/tabcontent.htm?tabinterfaceid=0) |
|
5 | 9 |
|
6 | 10 |
////NO NEED TO EDIT BELOW//////////////////////// |
7 |
var tabcontentIDs=new Object() |
|
8 |
|
|
9 |
function expandcontent(linkobj){ |
|
10 |
var ulid=linkobj.parentNode.parentNode.id //id of UL element |
|
11 |
var ullist=document.getElementById(ulid).getElementsByTagName("li") //get list of LIs corresponding to the tab contents |
|
12 |
for (var i=0; i<ullist.length; i++){ |
|
13 |
ullist[i].className="" //deselect all tabs |
|
14 |
if (typeof tabcontentIDs[ulid][i]!="undefined") //if tab content within this array index exists (exception: More tabs than there are tab contents) |
|
15 |
document.getElementById(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents |
|
16 |
} |
|
17 |
linkobj.parentNode.className="selected" //highlight currently clicked on tab |
|
18 |
document.getElementById(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content |
|
19 |
saveselectedtabcontentid(ulid, linkobj.getAttribute("rel")) |
|
20 |
} |
|
21 | 11 |
|
22 |
function savetabcontentids(ulid, relattribute){// save ids of tab content divs |
|
23 |
if (typeof tabcontentIDs[ulid]=="undefined") //if this array doesn't exist yet |
|
24 |
tabcontentIDs[ulid]=new Array() |
|
25 |
tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute |
|
12 |
function ddtabcontent(tabinterfaceid){ |
|
13 |
this.tabinterfaceid=tabinterfaceid //ID of Tab Menu main container |
|
14 |
this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container |
|
15 |
this.enabletabpersistence=true |
|
16 |
this.hottabspositions=[] //Array to store position of tabs that have a "rel" attr defined, relative to all tab links, within container |
|
17 |
this.currentTabIndex=0 //Index of currently selected hot tab (tab with sub content) within hottabspositions[] array |
|
18 |
this.subcontentids=[] //Array to store ids of the sub contents ("rel" attr values) |
|
19 |
this.revcontentids=[] //Array to store ids of arbitrary contents to expand/contact as well ("rev" attr values) |
|
20 |
this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link") |
|
26 | 21 |
} |
27 | 22 |
|
28 |
function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie |
|
29 |
if (enabletabpersistence==1) //if persistence feature turned on |
|
30 |
setCookie(ulid, selectedtabid) |
|
23 |
ddtabcontent.getCookie=function(Name){ |
|
24 |
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair |
|
25 |
if (document.cookie.match(re)) //if cookie found |
|
26 |
return document.cookie.match(re)[0].split("=")[1] //return its value |
|
27 |
return "" |
|
31 | 28 |
} |
32 | 29 |
|
33 |
function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content |
|
34 |
var ullist=document.getElementById(ulid).getElementsByTagName("li") |
|
35 |
for (var i=0; i<ullist.length; i++){ |
|
36 |
if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){ |
|
37 |
return ullist[i].getElementsByTagName("a")[0] |
|
38 |
break |
|
39 |
} |
|
40 |
} |
|
30 |
ddtabcontent.setCookie=function(name, value){ |
|
31 |
document.cookie = name+"="+value+";path=/" //cookie value is domain wide (path=/) |
|
41 | 32 |
} |
42 | 33 |
|
43 |
function initializetabcontent(){ |
|
44 |
for (var i=0; i<arguments.length; i++){ //loop through passed UL ids |
|
45 |
if (enabletabpersistence==0 && getCookie(arguments[i])!="") //clean up cookie if persist=off |
|
46 |
setCookie(arguments[i], "") |
|
47 |
var clickedontab=getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any |
|
48 |
var ulobj=document.getElementById(arguments[i]) |
|
49 |
var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL |
|
50 |
for (var x=0; x<ulist.length; x++){ //loop through each LI element |
|
51 |
var ulistlink=ulist[x].getElementsByTagName("a")[0] |
|
52 |
if (ulistlink.getAttribute("rel")){ |
|
53 |
savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs |
|
54 |
ulistlink.onclick=function(){ |
|
55 |
expandcontent(this) |
|
56 |
return false |
|
57 |
} |
|
58 |
if (ulist[x].className=="selected" && clickedontab=="") //if a tab is set to be selected by default |
|
59 |
expandcontent(ulistlink) //auto load currenly selected tab content |
|
60 |
} |
|
61 |
} //end inner for loop |
|
62 |
if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value |
|
63 |
var culistlink=getullistlinkbyId(arguments[i], clickedontab) |
|
64 |
if (typeof culistlink!="undefined") //if match found between tabcontent id and rel attribute value |
|
65 |
expandcontent(culistlink) //auto load currenly selected tab content |
|
66 |
else //else if no match found between tabcontent id and rel attribute value (cookie mis-association) |
|
67 |
expandcontent(ulist[0].getElementsByTagName("a")[0]) //just auto load first tab instead |
|
68 |
} |
|
69 |
} //end outer for loop |
|
70 |
} |
|
34 |
ddtabcontent.prototype={ |
|
71 | 35 |
|
36 |
expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers |
|
37 |
this.cancelautorun() //stop auto cycling of tabs (if running) |
|
38 |
var tabref="" |
|
39 |
try{ |
|
40 |
if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr |
|
41 |
tabref=document.getElementById(tabid_or_position) |
|
42 |
else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr |
|
43 |
tabref=this.tabs[tabid_or_position] |
|
44 |
} |
|
45 |
catch(err){alert("Invalid Tab ID or position entered!")} |
|
46 |
if (tabref!="") //if a valid tab is found based on function parameter |
|
47 |
this.expandtab(tabref) //expand this tab |
|
48 |
}, |
|
72 | 49 |
|
73 |
function getCookie(Name){ |
|
74 |
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair |
|
75 |
if (document.cookie.match(re)) //if cookie found |
|
76 |
return document.cookie.match(re)[0].split("=")[1] //return its value |
|
77 |
return "" |
|
78 |
} |
|
50 |
cycleit:function(dir, autorun){ //PUBLIC function to move foward or backwards through each hot tab (tabinstance.cycleit('foward/back') ) |
|
51 |
if (dir=="next"){ |
|
52 |
var currentTabIndex=(this.currentTabIndex<this.hottabspositions.length-1)? this.currentTabIndex+1 : 0 |
|
53 |
} |
|
54 |
else if (dir=="prev"){ |
|
55 |
var currentTabIndex=(this.currentTabIndex>0)? this.currentTabIndex-1 : this.hottabspositions.length-1 |
|
56 |
} |
|
57 |
if (typeof autorun=="undefined") //if cycleit() is being called by user, versus autorun() function |
|
58 |
this.cancelautorun() //stop auto cycling of tabs (if running) |
|
59 |
this.expandtab(this.tabs[this.hottabspositions[currentTabIndex]]) |
|
60 |
}, |
|
61 |
|
|
62 |
setpersist:function(bool){ //PUBLIC function to toggle persistence feature |
|
63 |
this.enabletabpersistence=bool |
|
64 |
}, |
|
65 |
|
|
66 |
setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link") |
|
67 |
this.selectedClassTarget=objstr || "link" |
|
68 |
}, |
|
69 |
|
|
70 |
getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to |
|
71 |
return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref |
|
72 |
}, |
|
73 |
|
|
74 |
urlparamselect:function(tabinterfaceid){ |
|
75 |
var result=window.location.search.match(new RegExp(tabinterfaceid+"=(\\d+)", "i")) //check for "?tabinterfaceid=2" in URL |
|
76 |
return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index |
|
77 |
}, |
|
78 |
|
|
79 |
expandtab:function(tabref){ |
|
80 |
var subcontentid=tabref.getAttribute("rel") //Get id of subcontent to expand |
|
81 |
//Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easily search through |
|
82 |
var associatedrevids=(tabref.getAttribute("rev"))? ","+tabref.getAttribute("rev").replace(/\s+/, "")+"," : "" |
|
83 |
this.expandsubcontent(subcontentid) |
|
84 |
this.expandrevcontent(associatedrevids) |
|
85 |
for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected" |
|
86 |
this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("rel")==subcontentid)? "selected" : "" |
|
87 |
} |
|
88 |
if (this.enabletabpersistence) //if persistence enabled, save selected tab position(int) relative to its peers |
|
89 |
ddtabcontent.setCookie(this.tabinterfaceid, tabref.tabposition) |
|
90 |
this.setcurrenttabindex(tabref.tabposition) //remember position of selected tab within hottabspositions[] array |
|
91 |
}, |
|
92 |
|
|
93 |
expandsubcontent:function(subcontentid){ |
|
94 |
for (var i=0; i<this.subcontentids.length; i++){ |
|
95 |
var subcontent=document.getElementById(this.subcontentids[i]) //cache current subcontent obj (in for loop) |
|
96 |
subcontent.style.display=(subcontent.id==subcontentid)? "block" : "none" //"show" or hide sub content based on matching id attr value |
|
97 |
} |
|
98 |
}, |
|
99 |
|
|
100 |
expandrevcontent:function(associatedrevids){ |
|
101 |
var allrevids=this.revcontentids |
|
102 |
for (var i=0; i<allrevids.length; i++){ //Loop through rev attributes for all tabs in this tab interface |
|
103 |
//if any values stored within associatedrevids matches one within allrevids, expand that DIV, otherwise, contract it |
|
104 |
document.getElementById(allrevids[i]).style.display=(associatedrevids.indexOf(","+allrevids[i]+",")!=-1)? "block" : "none" |
|
105 |
} |
|
106 |
}, |
|
107 |
|
|
108 |
setcurrenttabindex:function(tabposition){ //store current position of tab (within hottabspositions[] array) |
|
109 |
for (var i=0; i<this.hottabspositions.length; i++){ |
|
110 |
if (tabposition==this.hottabspositions[i]){ |
|
111 |
this.currentTabIndex=i |
|
112 |
break |
|
113 |
} |
|
114 |
} |
|
115 |
}, |
|
116 |
|
|
117 |
autorun:function(){ //function to auto cycle through and select tabs based on a set interval |
|
118 |
this.cycleit('next', true) |
|
119 |
}, |
|
120 |
|
|
121 |
cancelautorun:function(){ |
|
122 |
if (typeof this.autoruntimer!="undefined") |
|
123 |
clearInterval(this.autoruntimer) |
|
124 |
}, |
|
125 |
|
|
126 |
init:function(automodeperiod){ |
|
127 |
var persistedtab=ddtabcontent.getCookie(this.tabinterfaceid) //get position of persisted tab (applicable if persistence is enabled) |
|
128 |
var selectedtab=-1 //Currently selected tab index (-1 meaning none) |
|
129 |
var selectedtabfromurl=this.urlparamselect(this.tabinterfaceid) //returns null or index from: tabcontent.htm?tabinterfaceid=index |
|
130 |
this.automodeperiod=automodeperiod || 0 |
|
131 |
for (var i=0; i<this.tabs.length; i++){ |
|
132 |
this.tabs[i].tabposition=i //remember position of tab relative to its peers |
|
133 |
if (this.tabs[i].getAttribute("rel")){ |
|
134 |
var tabinstance=this |
|
135 |
this.hottabspositions[this.hottabspositions.length]=i //store position of "hot" tab ("rel" attr defined) relative to its peers |
|
136 |
this.subcontentids[this.subcontentids.length]=this.tabs[i].getAttribute("rel") //store id of sub content ("rel" attr value) |
|
137 |
this.tabs[i].onclick=function(){ |
|
138 |
tabinstance.expandtab(this) |
|
139 |
tabinstance.cancelautorun() //stop auto cycling of tabs (if running) |
|
140 |
return false |
|
141 |
} |
|
142 |
if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element |
|
143 |
this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/)) |
|
144 |
} |
|
145 |
if (selectedtabfromurl==i || this.enabletabpersistence && selectedtab==-1 && parseInt(persistedtab)==i || !this.enabletabpersistence && selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){ |
|
146 |
selectedtab=i //Selected tab index, if found |
|
147 |
} |
|
148 |
} |
|
149 |
} //END for loop |
|
150 |
if (selectedtab!=-1) //if a valid default selected tab index is found |
|
151 |
this.expandtab(this.tabs[selectedtab]) //expand selected tab (either from URL parameter, persistent feature, or class="selected" class) |
|
152 |
else //if no valid default selected index found |
|
153 |
this.expandtab(this.tabs[this.hottabspositions[0]]) //Just select first tab that contains a "rel" attr |
|
154 |
if (parseInt(this.automodeperiod)>500 && this.hottabspositions.length>1){ |
|
155 |
this.autoruntimer=setInterval(function(){tabinstance.autorun()}, this.automodeperiod) |
|
156 |
} |
|
157 |
} //END int() function |
|
79 | 158 |
|
80 |
function setCookie(name, value){ |
|
81 |
document.cookie = name+"="+value //cookie value is domain wide (path=/) |
|
82 |
} |
|
159 |
} //END Prototype assignment |
Auch abrufbar als: Unified diff
Update des DHTML-Tab-Scripts auf Version 2.2.