[2] | 1 | /** |
---|
| 2 | * Tabs class for handling HTML/CSS tabs |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2003 Dipl.-Inform. Kai Hofmann and probusiness AG |
---|
| 5 | * |
---|
| 6 | * This library is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 19 | * |
---|
| 20 | * Contact information: |
---|
| 21 | * Dipl.-Inform. Kai Hofmann |
---|
| 22 | * Arberger Heerstr. 92 |
---|
| 23 | * 28307 Bremen |
---|
| 24 | * Germany |
---|
| 25 | * |
---|
| 26 | * |
---|
| 27 | * probusiness AG |
---|
| 28 | * Expo-Plaza-Nr. 1 |
---|
| 29 | * 30539 Hannover |
---|
| 30 | * Germany |
---|
| 31 | * |
---|
| 32 | * |
---|
| 33 | * @version 1.0 |
---|
| 34 | * @author hofmann@hofmann-int.de |
---|
| 35 | * |
---|
| 36 | * @argument nrTabs Number of Tabs to handle |
---|
| 37 | * @argument activeCSSclass CSS class name for active tabs (display:inline) |
---|
| 38 | * @argument inactiveCSSclass CSS class name for inactive tabs (display:none) |
---|
| 39 | * @argument HTMLtabID HTML ID name prefix that would be used with the tab number as tab name. |
---|
| 40 | * @argument HTMLtabcontentID HTML ID prefix for the tab content used with the tab number |
---|
| 41 | * @argument HTMLtabselectorID HTML ID prefix for a selectbox used to switch between the tabs |
---|
| 42 | * @argument HTMLtabradioID HTML ID prefix for radio button input fields used to switch between the tabs |
---|
| 43 | * @argument tabPageKey URL parameter name to use for setting/getting the actual tab |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | function Tabs(nrTabs,activeCSSclass,inactiveCSSclass,HTMLtabID,HTMLtabcontentID,HTMLtabselectorID,HTMLtabradioID,tabPageKey) |
---|
| 47 | { |
---|
| 48 | this.nrTabs = nrTabs; |
---|
| 49 | this.activeCSSclass = activeCSSclass; |
---|
| 50 | this.inactiveCSSclass = inactiveCSSclass; |
---|
| 51 | this.HTMLtabID = HTMLtabID; |
---|
| 52 | this.HTMLtabcontentID = HTMLtabcontentID; |
---|
| 53 | this.HTMLtabselectorID = HTMLtabselectorID; |
---|
| 54 | this.HTMLtabradioID = HTMLtabradioID; |
---|
| 55 | this.tabPageKey = tabPageKey; |
---|
| 56 | |
---|
| 57 | if (typeof(_tabs_prototype_called) == 'undefined') |
---|
| 58 | { |
---|
| 59 | _tabs_prototype_called = true; |
---|
| 60 | Tabs.prototype.setActive = setActive; |
---|
| 61 | Tabs.prototype.setInactive = setInactive; |
---|
| 62 | Tabs.prototype.isActive = isActive; |
---|
| 63 | Tabs.prototype.getActive = getActive; |
---|
| 64 | Tabs.prototype.disableAll = disableAll; |
---|
| 65 | Tabs.prototype.display = display; |
---|
| 66 | Tabs.prototype.changeToActive = changeToActive; |
---|
| 67 | Tabs.prototype.init = init; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Set tab as active |
---|
| 73 | * |
---|
| 74 | * @argument tabnr The tab number (1-nrTabs) of the tab that should be active |
---|
| 75 | */ |
---|
| 76 | function setActive(tabnr) |
---|
| 77 | { |
---|
| 78 | if ((tabnr > 0) && (tabnr <= this.nrTabs)) |
---|
| 79 | { |
---|
| 80 | if(document.getElementById(this.HTMLtabID + tabnr)) |
---|
| 81 | document.getElementById(this.HTMLtabID + tabnr).className = this.activeCSSclass; |
---|
| 82 | if(document.getElementById(this.HTMLtabcontentID + tabnr)) |
---|
| 83 | document.getElementById(this.HTMLtabcontentID + tabnr).className = this.activeCSSclass; |
---|
| 84 | |
---|
[309] | 85 | |
---|
[2] | 86 | if(document.getElementById(this.HTMLtabselectorID)) |
---|
| 87 | document.getElementById(this.HTMLtabselectorID).selectedIndex = tabnr-1; |
---|
| 88 | if(document.getElementById(this.HTMLtabradioID + tabnr)) |
---|
| 89 | document.getElementById(this.HTMLtabradioID + tabnr).checked = true; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Set tab as inactive |
---|
| 97 | * |
---|
| 98 | * @argument tabnr The tab number (1-nrTabs) of the tab that should be inactive |
---|
| 99 | */ |
---|
| 100 | function setInactive(tabnr) |
---|
| 101 | { |
---|
| 102 | if ((tabnr > 0) && (tabnr <= this.nrTabs)) |
---|
| 103 | { |
---|
| 104 | if(document.getElementById(this.HTMLtabID + tabnr)) |
---|
| 105 | document.getElementById(this.HTMLtabID + tabnr).className = this.inactiveCSSclass; |
---|
| 106 | if(document.getElementById(this.HTMLtabcontentID + tabnr)) |
---|
| 107 | document.getElementById(this.HTMLtabcontentID + tabnr).className = this.inactiveCSSclass; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Test if tab is active |
---|
| 114 | * |
---|
| 115 | * @argument tabnr The tab number (1-nrTabs) of the tab that should be tested |
---|
| 116 | * @returns boolean - true if tab is active, false otherwise |
---|
| 117 | */ |
---|
| 118 | function isActive(tabnr) |
---|
| 119 | { |
---|
| 120 | return(document.getElementById(HTMLtabID + tabnr).className == this.activeCSSclass); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Get the active tab number |
---|
| 126 | * |
---|
| 127 | * @returns Tab (1-nrTabs) that is currently active or 0 if non is active. |
---|
| 128 | */ |
---|
| 129 | function getActive() |
---|
| 130 | { |
---|
| 131 | for (i = 1; i <= this.nrTabs; ++i) |
---|
| 132 | { |
---|
| 133 | if (this.isActive(i)) |
---|
| 134 | { |
---|
| 135 | return(i); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | return(0); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Disable all tabs |
---|
| 144 | */ |
---|
| 145 | function disableAll() |
---|
| 146 | { |
---|
| 147 | for (i = 1; i <= this.nrTabs; ++i) |
---|
| 148 | { |
---|
| 149 | this.setInactive(i); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Disable all tabs and then display the tab number given |
---|
| 156 | * |
---|
| 157 | * @argument tabnr Tab number to display |
---|
| 158 | */ |
---|
| 159 | function display(tabnr) |
---|
| 160 | { |
---|
| 161 | this.disableAll(this.nrTabs); |
---|
| 162 | this.setActive(tabnr); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Loop over all tabs - switch off currently active tabs and display the new tab |
---|
| 168 | * |
---|
| 169 | * @argument tabnr Tab number to display |
---|
| 170 | */ |
---|
| 171 | function changeToActive(tabnr) |
---|
| 172 | { |
---|
| 173 | for (i = 1; i <= this.nrTabs; ++i) |
---|
| 174 | { |
---|
| 175 | if (i == tabnr) |
---|
| 176 | { |
---|
| 177 | if (!this.isActive(i)) |
---|
| 178 | { |
---|
| 179 | this.setActive(i); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | if (this.isActive(i)) |
---|
| 185 | { |
---|
| 186 | this.setInactive(i); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * Get url parameter for first tab and display it. |
---|
| 195 | */ |
---|
| 196 | function init() |
---|
| 197 | { |
---|
| 198 | var tab = 0; |
---|
| 199 | var url = document.URL; |
---|
| 200 | var pos = url.indexOf("?"); |
---|
| 201 | if (pos > -1) |
---|
| 202 | { |
---|
| 203 | var urlparams = url.substr(pos + 1,url.length - (pos + 1)); |
---|
| 204 | var regexp = new RegExp('(^|&)' + this.tabPageKey + '=[0-9]{1,2}'); |
---|
| 205 | var urlparamstart = urlparams.search(regexp); |
---|
| 206 | if (urlparamstart > -1) |
---|
| 207 | { |
---|
| 208 | urlparamstart = urlparamstart + ((urlparams[urlparamstart] == '&') ? 1 : 0); |
---|
| 209 | var urlparam = urlparams.substr(urlparamstart,urlparams.length - urlparamstart); |
---|
| 210 | pos = urlparam.indexOf("&"); |
---|
| 211 | if (pos > -1) |
---|
| 212 | { |
---|
| 213 | urlparam = urlparam.substr(0,pos); |
---|
| 214 | } |
---|
| 215 | pos = urlparam.indexOf("="); |
---|
| 216 | if (pos > -1) |
---|
| 217 | { |
---|
| 218 | var urlparamvalue = urlparam.substr(pos + 1,urlparam.length - (pos + 1)); |
---|
| 219 | tab = urlparamvalue; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | else |
---|
| 223 | { |
---|
| 224 | tab = 1; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | else |
---|
| 228 | { |
---|
| 229 | tab = 1; |
---|
| 230 | } |
---|
| 231 | if ((tab <= 0) || (tab > this.nrTabs)) |
---|
| 232 | { |
---|
| 233 | tab = 1; |
---|
| 234 | } |
---|
| 235 | this.display(tab); |
---|
| 236 | } |
---|
| 237 | } |
---|