tabcontent.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ////NO NEED TO EDIT BELOW////////////////////////
  10. function ddtabcontent(tabinterfaceid){
  11. this.tabinterfaceid=tabinterfaceid //ID of Tab Menu main container
  12. this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container
  13. this.enabletabpersistence=true
  14. this.hottabspositions=[] //Array to store position of tabs that have a "rel" attr defined, relative to all tab links, within container
  15. this.currentTabIndex=0 //Index of currently selected hot tab (tab with sub content) within hottabspositions[] array
  16. this.subcontentids=[] //Array to store ids of the sub contents ("rel" attr values)
  17. this.revcontentids=[] //Array to store ids of arbitrary contents to expand/contact as well ("rev" attr values)
  18. this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link")
  19. }
  20. ddtabcontent.getCookie=function(Name){
  21. var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
  22. if (document.cookie.match(re)) //if cookie found
  23. return document.cookie.match(re)[0].split("=")[1] //return its value
  24. return ""
  25. }
  26. ddtabcontent.setCookie=function(name, value){
  27. document.cookie = name+"="+value+";path=/" //cookie value is domain wide (path=/)
  28. }
  29. ddtabcontent.prototype={
  30. expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers
  31. this.cancelautorun() //stop auto cycling of tabs (if running)
  32. var tabref=""
  33. try{
  34. if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr
  35. tabref=document.getElementById(tabid_or_position)
  36. else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr
  37. tabref=this.tabs[tabid_or_position]
  38. }
  39. catch(err){alert("Invalid Tab ID or position entered!")}
  40. if (tabref!="") //if a valid tab is found based on function parameter
  41. this.expandtab(tabref) //expand this tab
  42. },
  43. cycleit:function(dir, autorun){ //PUBLIC function to move foward or backwards through each hot tab (tabinstance.cycleit('foward/back') )
  44. if (dir=="next"){
  45. var currentTabIndex=(this.currentTabIndex<this.hottabspositions.length-1)? this.currentTabIndex+1 : 0
  46. }
  47. else if (dir=="prev"){
  48. var currentTabIndex=(this.currentTabIndex>0)? this.currentTabIndex-1 : this.hottabspositions.length-1
  49. }
  50. if (typeof autorun=="undefined") //if cycleit() is being called by user, versus autorun() function
  51. this.cancelautorun() //stop auto cycling of tabs (if running)
  52. this.expandtab(this.tabs[this.hottabspositions[currentTabIndex]])
  53. },
  54. setpersist:function(bool){ //PUBLIC function to toggle persistence feature
  55. this.enabletabpersistence=bool
  56. },
  57. setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link")
  58. this.selectedClassTarget=objstr || "link"
  59. },
  60. getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to
  61. return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref
  62. },
  63. urlparamselect:function(tabinterfaceid){
  64. var result=window.location.search.match(new RegExp(tabinterfaceid+"=(\\d+)", "i")) //check for "?tabinterfaceid=2" in URL
  65. return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index
  66. },
  67. expandtab:function(tabref){
  68. var subcontentid=tabref.getAttribute("rel") //Get id of subcontent to expand
  69. //Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easily search through
  70. var associatedrevids=(tabref.getAttribute("rev"))? ","+tabref.getAttribute("rev").replace(/\s+/, "")+"," : ""
  71. this.expandsubcontent(subcontentid)
  72. this.expandrevcontent(associatedrevids)
  73. for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected"
  74. this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("rel")==subcontentid)? "selected" : ""
  75. }
  76. if (this.enabletabpersistence) //if persistence enabled, save selected tab position(int) relative to its peers
  77. ddtabcontent.setCookie(this.tabinterfaceid, tabref.tabposition)
  78. this.setcurrenttabindex(tabref.tabposition) //remember position of selected tab within hottabspositions[] array
  79. },
  80. expandsubcontent:function(subcontentid){
  81. for (var i=0; i<this.subcontentids.length; i++){
  82. var subcontent=document.getElementById(this.subcontentids[i]) //cache current subcontent obj (in for loop)
  83. subcontent.style.display=(subcontent.id==subcontentid)? "block" : "none" //"show" or hide sub content based on matching id attr value
  84. }
  85. },
  86. expandrevcontent:function(associatedrevids){
  87. var allrevids=this.revcontentids
  88. for (var i=0; i<allrevids.length; i++){ //Loop through rev attributes for all tabs in this tab interface
  89. //if any values stored within associatedrevids matches one within allrevids, expand that DIV, otherwise, contract it
  90. document.getElementById(allrevids[i]).style.display=(associatedrevids.indexOf(","+allrevids[i]+",")!=-1)? "block" : "none"
  91. }
  92. },
  93. setcurrenttabindex:function(tabposition){ //store current position of tab (within hottabspositions[] array)
  94. for (var i=0; i<this.hottabspositions.length; i++){
  95. if (tabposition==this.hottabspositions[i]){
  96. this.currentTabIndex=i
  97. break
  98. }
  99. }
  100. },
  101. autorun:function(){ //function to auto cycle through and select tabs based on a set interval
  102. this.cycleit('next', true)
  103. },
  104. cancelautorun:function(){
  105. if (typeof this.autoruntimer!="undefined")
  106. clearInterval(this.autoruntimer)
  107. },
  108. init:function(automodeperiod){
  109. var persistedtab=ddtabcontent.getCookie(this.tabinterfaceid) //get position of persisted tab (applicable if persistence is enabled)
  110. var selectedtab=-1 //Currently selected tab index (-1 meaning none)
  111. var selectedtabfromurl=this.urlparamselect(this.tabinterfaceid) //returns null or index from: tabcontent.htm?tabinterfaceid=index
  112. this.automodeperiod=automodeperiod || 0
  113. for (var i=0; i<this.tabs.length; i++){
  114. this.tabs[i].tabposition=i //remember position of tab relative to its peers
  115. if (this.tabs[i].getAttribute("rel")){
  116. var tabinstance=this
  117. this.hottabspositions[this.hottabspositions.length]=i //store position of "hot" tab ("rel" attr defined) relative to its peers
  118. this.subcontentids[this.subcontentids.length]=this.tabs[i].getAttribute("rel") //store id of sub content ("rel" attr value)
  119. this.tabs[i].onclick=function(){
  120. tabinstance.expandtab(this)
  121. tabinstance.cancelautorun() //stop auto cycling of tabs (if running)
  122. return false
  123. }
  124. if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element
  125. this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/))
  126. }
  127. if (selectedtabfromurl==i || this.enabletabpersistence && selectedtab==-1 && parseInt(persistedtab)==i || !this.enabletabpersistence && selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){
  128. selectedtab=i //Selected tab index, if found
  129. }
  130. }
  131. } //END for loop
  132. if (selectedtab!=-1) //if a valid default selected tab index is found
  133. this.expandtab(this.tabs[selectedtab]) //expand selected tab (either from URL parameter, persistent feature, or class="selected" class)
  134. else //if no valid default selected index found
  135. this.expandtab(this.tabs[this.hottabspositions[0]]) //Just select first tab that contains a "rel" attr
  136. if (parseInt(this.automodeperiod)>500 && this.hottabspositions.length>1){
  137. this.autoruntimer=setInterval(function(){tabinstance.autorun()}, this.automodeperiod)
  138. }
  139. } //END int() function
  140. } //END Prototype assignment