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)
|
---|
9 |
|
---|
10 | ////NO NEED TO EDIT BELOW////////////////////////
|
---|
11 |
|
---|
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")
|
---|
21 | }
|
---|
22 |
|
---|
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 ""
|
---|
28 | }
|
---|
29 |
|
---|
30 | ddtabcontent.setCookie=function(name, value){
|
---|
31 | document.cookie = name+"="+value+";path=/" //cookie value is domain wide (path=/)
|
---|
32 | }
|
---|
33 |
|
---|
34 | ddtabcontent.prototype={
|
---|
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 | },
|
---|
49 |
|
---|
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
|
---|
158 |
|
---|
159 | } //END Prototype assignment |
---|