Funktion "Als Entwurf speichern" für Dialogbuchungen implementiert.
[kivitendo-erp.git] / js / tabcontent.js
index 243be26..62f842d 100644 (file)
-//** Tab Content script- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)\r
-//** Last updated: June 29th, 06\r
-\r
-var enabletabpersistence=1 //enable tab persistence via session only cookies, so selected tab is remembered?\r
+//** Tab Content script v2.0- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)\r
+//** Updated Oct 7th, 07 to version 2.0. Contains numerous improvements:\r
+//   -Added Auto Mode: Script auto rotates the tabs based on an interval, until a tab is explicitly selected\r
+//   -Ability to expand/contract arbitrary DIVs on the page as the tabbed content is expanded/ contracted\r
+//   -Ability to dynamically select a tab either based on its position within its peers, or its ID attribute (give the target tab one 1st)\r
+//   -Ability to set where the CSS classname "selected" get assigned- either to the target tab's link ("A"), or its parent container\r
+//** Updated Feb 18th, 08 to version 2.1: Adds a "tabinstance.cycleit(dir)" method to cycle forward or backward between tabs dynamically\r
+//** 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) \r
 \r
 ////NO NEED TO EDIT BELOW////////////////////////\r
-var tabcontentIDs=new Object()\r
-\r
-function expandcontent(linkobj){\r
-var ulid=linkobj.parentNode.parentNode.id //id of UL element\r
-var ullist=document.getElementById(ulid).getElementsByTagName("li") //get list of LIs corresponding to the tab contents\r
-for (var i=0; i<ullist.length; i++){\r
-ullist[i].className=""  //deselect all tabs\r
-if (typeof tabcontentIDs[ulid][i]!="undefined") //if tab content within this array index exists (exception: More tabs than there are tab contents)\r
-document.getElementById(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents\r
-}\r
-linkobj.parentNode.className="selected"  //highlight currently clicked on tab\r
-document.getElementById(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content\r
-saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"))\r
-}\r
 \r
-function savetabcontentids(ulid, relattribute){// save ids of tab content divs\r
-if (typeof tabcontentIDs[ulid]=="undefined") //if this array doesn't exist yet\r
-tabcontentIDs[ulid]=new Array()\r
-tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute\r
+function ddtabcontent(tabinterfaceid){\r
+       this.tabinterfaceid=tabinterfaceid //ID of Tab Menu main container\r
+       this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") //Get all tab links within container\r
+       this.enabletabpersistence=true\r
+       this.hottabspositions=[] //Array to store position of tabs that have a "rel" attr defined, relative to all tab links, within container\r
+       this.currentTabIndex=0 //Index of currently selected hot tab (tab with sub content) within hottabspositions[] array\r
+       this.subcontentids=[] //Array to store ids of the sub contents ("rel" attr values)\r
+       this.revcontentids=[] //Array to store ids of arbitrary contents to expand/contact as well ("rev" attr values)\r
+       this.selectedClassTarget="link" //keyword to indicate which target element to assign "selected" CSS class ("linkparent" or "link")\r
 }\r
 \r
-function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie\r
-if (enabletabpersistence==1) //if persistence feature turned on\r
-setCookie(ulid, selectedtabid)\r
+ddtabcontent.getCookie=function(Name){ \r
+       var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair\r
+       if (document.cookie.match(re)) //if cookie found\r
+               return document.cookie.match(re)[0].split("=")[1] //return its value\r
+       return ""\r
 }\r
 \r
-function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content\r
-var ullist=document.getElementById(ulid).getElementsByTagName("li")\r
-for (var i=0; i<ullist.length; i++){\r
-if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){\r
-return ullist[i].getElementsByTagName("a")[0]\r
-break\r
-}\r
-}\r
+ddtabcontent.setCookie=function(name, value){\r
+       document.cookie = name+"="+value+";path=/" //cookie value is domain wide (path=/)\r
 }\r
 \r
-function initializetabcontent(){\r
-for (var i=0; i<arguments.length; i++){ //loop through passed UL ids\r
-if (enabletabpersistence==0 && getCookie(arguments[i])!="") //clean up cookie if persist=off\r
-setCookie(arguments[i], "")\r
-var clickedontab=getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any\r
-var ulobj=document.getElementById(arguments[i])\r
-var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL\r
-for (var x=0; x<ulist.length; x++){ //loop through each LI element\r
-var ulistlink=ulist[x].getElementsByTagName("a")[0]\r
-if (ulistlink.getAttribute("rel")){\r
-savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs\r
-ulistlink.onclick=function(){\r
-expandcontent(this)\r
-return false\r
-}\r
-if (ulist[x].className=="selected" && clickedontab=="") //if a tab is set to be selected by default\r
-expandcontent(ulistlink) //auto load currenly selected tab content\r
-}\r
-} //end inner for loop\r
-if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value\r
-var culistlink=getullistlinkbyId(arguments[i], clickedontab)\r
-if (typeof culistlink!="undefined") //if match found between tabcontent id and rel attribute value\r
-expandcontent(culistlink) //auto load currenly selected tab content\r
-else //else if no match found between tabcontent id and rel attribute value (cookie mis-association)\r
-expandcontent(ulist[0].getElementsByTagName("a")[0]) //just auto load first tab instead\r
-}\r
-} //end outer for loop\r
-}\r
+ddtabcontent.prototype={\r
 \r
+       expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers\r
+               this.cancelautorun() //stop auto cycling of tabs (if running)\r
+               var tabref=""\r
+               try{\r
+                       if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr\r
+                               tabref=document.getElementById(tabid_or_position)\r
+                       else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr\r
+                               tabref=this.tabs[tabid_or_position]\r
+               }\r
+               catch(err){alert("Invalid Tab ID or position entered!")}\r
+               if (tabref!="") //if a valid tab is found based on function parameter\r
+                       this.expandtab(tabref) //expand this tab\r
+       },\r
 \r
-function getCookie(Name){ \r
-var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair\r
-if (document.cookie.match(re)) //if cookie found\r
-return document.cookie.match(re)[0].split("=")[1] //return its value\r
-return ""\r
-}\r
+       cycleit:function(dir, autorun){ //PUBLIC function to move foward or backwards through each hot tab (tabinstance.cycleit('foward/back') )\r
+               if (dir=="next"){\r
+                       var currentTabIndex=(this.currentTabIndex<this.hottabspositions.length-1)? this.currentTabIndex+1 : 0\r
+               }\r
+               else if (dir=="prev"){\r
+                       var currentTabIndex=(this.currentTabIndex>0)? this.currentTabIndex-1 : this.hottabspositions.length-1\r
+               }\r
+               if (typeof autorun=="undefined") //if cycleit() is being called by user, versus autorun() function\r
+                       this.cancelautorun() //stop auto cycling of tabs (if running)\r
+               this.expandtab(this.tabs[this.hottabspositions[currentTabIndex]])\r
+       },\r
+\r
+       setpersist:function(bool){ //PUBLIC function to toggle persistence feature\r
+                       this.enabletabpersistence=bool\r
+       },\r
+\r
+       setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link")\r
+               this.selectedClassTarget=objstr || "link"\r
+       },\r
+\r
+       getselectedClassTarget:function(tabref){ //Returns target element to assign "selected" CSS class to\r
+               return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref\r
+       },\r
+\r
+       urlparamselect:function(tabinterfaceid){\r
+               var result=window.location.search.match(new RegExp(tabinterfaceid+"=(\\d+)", "i")) //check for "?tabinterfaceid=2" in URL\r
+               return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index\r
+       },\r
+\r
+       expandtab:function(tabref){\r
+               var subcontentid=tabref.getAttribute("rel") //Get id of subcontent to expand\r
+               //Get "rev" attr as a string of IDs in the format ",john,george,trey,etc," to easily search through\r
+               var associatedrevids=(tabref.getAttribute("rev"))? ","+tabref.getAttribute("rev").replace(/\s+/, "")+"," : ""\r
+               this.expandsubcontent(subcontentid)\r
+               this.expandrevcontent(associatedrevids)\r
+               for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected"\r
+                       this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("rel")==subcontentid)? "selected" : ""\r
+               }\r
+               if (this.enabletabpersistence) //if persistence enabled, save selected tab position(int) relative to its peers\r
+                       ddtabcontent.setCookie(this.tabinterfaceid, tabref.tabposition)\r
+               this.setcurrenttabindex(tabref.tabposition) //remember position of selected tab within hottabspositions[] array\r
+       },\r
+\r
+       expandsubcontent:function(subcontentid){\r
+               for (var i=0; i<this.subcontentids.length; i++){\r
+                       var subcontent=document.getElementById(this.subcontentids[i]) //cache current subcontent obj (in for loop)\r
+                       subcontent.style.display=(subcontent.id==subcontentid)? "block" : "none" //"show" or hide sub content based on matching id attr value\r
+               }\r
+       },\r
+\r
+       expandrevcontent:function(associatedrevids){\r
+               var allrevids=this.revcontentids\r
+               for (var i=0; i<allrevids.length; i++){ //Loop through rev attributes for all tabs in this tab interface\r
+                       //if any values stored within associatedrevids matches one within allrevids, expand that DIV, otherwise, contract it\r
+                       document.getElementById(allrevids[i]).style.display=(associatedrevids.indexOf(","+allrevids[i]+",")!=-1)? "block" : "none"\r
+               }\r
+       },\r
+\r
+       setcurrenttabindex:function(tabposition){ //store current position of tab (within hottabspositions[] array)\r
+               for (var i=0; i<this.hottabspositions.length; i++){\r
+                       if (tabposition==this.hottabspositions[i]){\r
+                               this.currentTabIndex=i\r
+                               break\r
+                       }\r
+               }\r
+       },\r
+\r
+       autorun:function(){ //function to auto cycle through and select tabs based on a set interval\r
+               this.cycleit('next', true)\r
+       },\r
+\r
+       cancelautorun:function(){\r
+               if (typeof this.autoruntimer!="undefined")\r
+                       clearInterval(this.autoruntimer)\r
+       },\r
+\r
+       init:function(automodeperiod){\r
+               var persistedtab=ddtabcontent.getCookie(this.tabinterfaceid) //get position of persisted tab (applicable if persistence is enabled)\r
+               var selectedtab=-1 //Currently selected tab index (-1 meaning none)\r
+               var selectedtabfromurl=this.urlparamselect(this.tabinterfaceid) //returns null or index from: tabcontent.htm?tabinterfaceid=index\r
+               this.automodeperiod=automodeperiod || 0\r
+               for (var i=0; i<this.tabs.length; i++){\r
+                       this.tabs[i].tabposition=i //remember position of tab relative to its peers\r
+                       if (this.tabs[i].getAttribute("rel")){\r
+                               var tabinstance=this\r
+                               this.hottabspositions[this.hottabspositions.length]=i //store position of "hot" tab ("rel" attr defined) relative to its peers\r
+                               this.subcontentids[this.subcontentids.length]=this.tabs[i].getAttribute("rel") //store id of sub content ("rel" attr value)\r
+                               this.tabs[i].onclick=function(){\r
+                                       tabinstance.expandtab(this)\r
+                                       tabinstance.cancelautorun() //stop auto cycling of tabs (if running)\r
+                                       return false\r
+                               }\r
+                               if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element\r
+                                       this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/))\r
+                               }\r
+                               if (selectedtabfromurl==i || this.enabletabpersistence && selectedtab==-1 && parseInt(persistedtab)==i || !this.enabletabpersistence && selectedtab==-1 && this.getselectedClassTarget(this.tabs[i]).className=="selected"){\r
+                                       selectedtab=i //Selected tab index, if found\r
+                               }\r
+                       }\r
+               } //END for loop\r
+               if (selectedtab!=-1) //if a valid default selected tab index is found\r
+                       this.expandtab(this.tabs[selectedtab]) //expand selected tab (either from URL parameter, persistent feature, or class="selected" class)\r
+               else //if no valid default selected index found\r
+                       this.expandtab(this.tabs[this.hottabspositions[0]]) //Just select first tab that contains a "rel" attr\r
+               if (parseInt(this.automodeperiod)>500 && this.hottabspositions.length>1){\r
+                       this.autoruntimer=setInterval(function(){tabinstance.autorun()}, this.automodeperiod)\r
+               }\r
+       } //END int() function\r
 \r
-function setCookie(name, value){\r
-document.cookie = name+"="+value //cookie value is domain wide (path=/)\r
-}
\ No newline at end of file
+} //END Prototype assignment
\ No newline at end of file