1 /*==================================================
2 tabber.js by Patrick Fitzgerald pat@barelyfitz.com
4 Documentation can be found at the following URL:
5 http://www.barelyfitz.com/projects/tabber/
7 License (http://www.opensource.org/licenses/mit-license.php)
9 Copyright (c) 2006 Patrick Fitzgerald
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation files
13 (the "Software"), to deal in the Software without restriction,
14 including without limitation the rights to use, copy, modify, merge,
15 publish, distribute, sublicense, and/or sell copies of the Software,
16 and to permit persons to whom the Software is furnished to do so,
17 subject to the following conditions:
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 ==================================================*/
32 function tabberObj(argsObj)
34 var arg; /* name of an argument to override */
36 /* Element for the main tabber div. If you supply this in argsObj,
37 then the init() method will be called.
41 /* Class of the main tabber div */
42 this.classMain = "tabber";
44 /* Rename classMain to classMainLive after tabifying
45 (so a different style can be applied)
47 this.classMainLive = "tabberlive";
49 /* Class of each DIV that contains a tab */
50 this.classTab = "tabbertab";
52 /* Class to indicate which tab should be active on startup */
53 this.classTabDefault = "tabbertabdefault";
55 /* Class for the navigation UL */
56 this.classNav = "tabbernav";
58 /* When a tab is to be hidden, instead of setting display='none', we
59 set the class of the div to classTabHide. In your screen
60 stylesheet you should set classTabHide to display:none. In your
61 print stylesheet you should set display:block to ensure that all
62 the information is printed.
64 this.classTabHide = "tabbertabhide";
66 /* Class to set the navigation LI when the tab is active, so you can
67 use a different style on the active tab.
69 this.classNavActive = "tabberactive";
71 /* Elements that might contain the title for the tab, only used if a
72 title is not specified in the TITLE attribute of DIV classTab.
74 this.titleElements = ['h2','h3','h4','h5','h6'];
76 /* Should we strip out the HTML from the innerHTML of the title elements?
77 This should usually be true.
79 this.titleElementsStripHTML = true;
81 /* If the user specified the tab names using a TITLE attribute on
82 the DIV, then the browser will display a tooltip whenever the
83 mouse is over the DIV. To prevent this tooltip, we can remove the
84 TITLE attribute after getting the tab name.
86 this.removeTitle = true;
88 /* If you want to add an id to each link set this to true */
89 this.addLinkId = false;
91 /* If addIds==true, then you can set a format for the ids.
92 <tabberid> will be replaced with the id of the main tabber div.
93 <tabnumberzero> will be replaced with the tab number
94 (tab numbers starting at zero)
95 <tabnumberone> will be replaced with the tab number
96 (tab numbers starting at one)
97 <tabtitle> will be replaced by the tab title
98 (with all non-alphanumeric characters removed)
100 this.linkIdFormat = '<tabberid>nav<tabnumberone>';
102 /* You can override the defaults listed above by passing in an object:
103 var mytab = new tabber({property:value,property:value});
105 for (arg in argsObj) { this[arg] = argsObj[arg]; }
107 /* Create regular expressions for the class names; Note: if you
108 change the class names after a new object is created you must
109 also change these regular expressions.
111 this.REclassMain = new RegExp('\\b' + this.classMain + '\\b', 'gi');
112 this.REclassMainLive = new RegExp('\\b' + this.classMainLive + '\\b', 'gi');
113 this.REclassTab = new RegExp('\\b' + this.classTab + '\\b', 'gi');
114 this.REclassTabDefault = new RegExp('\\b' + this.classTabDefault + '\\b', 'gi');
115 this.REclassTabHide = new RegExp('\\b' + this.classTabHide + '\\b', 'gi');
117 /* Array of objects holding info about each tab */
118 this.tabs = new Array();
120 /* If the main tabber div was specified, call init() now */
125 /* We don't need the main div anymore, and to prevent a memory leak
126 in IE, we must remove the circular reference between the div
127 and the tabber object. */
133 /*--------------------------------------------------
134 Methods for tabberObj
135 --------------------------------------------------*/
138 tabberObj.prototype.init = function(e)
140 /* Set up the tabber interface.
142 e = element (the main containing div)
145 init(document.getElementById('mytabberdiv'))
149 childNodes, /* child nodes of the tabber div */
150 i, i2, /* loop indices */
151 t, /* object to store info about a single tab */
152 defaultTab=0, /* which tab to select by default */
153 DOM_ul, /* tabbernav list */
154 DOM_li, /* tabbernav list item */
155 DOM_a, /* tabbernav link */
156 aId, /* A unique id for DOM_a */
157 headingElement; /* searching for text to use in the tab */
159 /* Verify that the browser supports DOM scripting */
160 if (!document.getElementsByTagName) { return false; }
162 /* If the main DIV has an ID then save it. */
167 /* Clear the tabs array (but it should normally be empty) */
168 this.tabs.length = 0;
170 /* Loop through an array of all the child nodes within our tabber element. */
171 childNodes = e.childNodes;
172 for(i=0; i < childNodes.length; i++) {
174 /* Find the nodes where class="tabbertab" */
175 if(childNodes[i].className &&
176 childNodes[i].className.match(this.REclassTab)) {
178 /* Create a new object to save info about this tab */
181 /* Save a pointer to the div for this tab */
182 t.div = childNodes[i];
184 /* Add the new object to the array of tabs */
185 this.tabs[this.tabs.length] = t;
187 /* If the class name contains classTabDefault,
188 then select this tab by default.
190 if (childNodes[i].className.match(this.REclassTabDefault)) {
191 defaultTab = this.tabs.length-1;
196 /* Create a new UL list to hold the tab headings */
197 DOM_ul = document.createElement("ul");
198 DOM_ul.className = this.classNav;
200 /* Loop through each tab we found */
201 for (i=0; i < this.tabs.length; i++) {
205 /* Get the label to use for this tab:
206 From the title attribute on the DIV,
207 Or from one of the this.titleElements[] elements,
208 Or use an automatically generated number.
210 t.headingText = t.div.title;
212 /* Remove the title attribute to prevent a tooltip from appearing */
213 if (this.removeTitle) { t.div.title = ''; }
215 if (!t.headingText) {
217 /* Title was not defined in the title of the DIV,
218 So try to get the title from an element within the DIV.
219 Go through the list of elements in this.titleElements
220 (typically heading elements ['h2','h3','h4'])
222 for (i2=0; i2<this.titleElements.length; i2++) {
223 headingElement = t.div.getElementsByTagName(this.titleElements[i2])[0];
224 if (headingElement) {
225 t.headingText = headingElement.innerHTML;
226 if (this.titleElementsStripHTML) {
227 t.headingText.replace(/<br>/gi," ");
228 t.headingText = t.headingText.replace(/<[^>]+>/g,"");
235 if (!t.headingText) {
236 /* Title was not found (or is blank) so automatically generate a
239 t.headingText = i + 1;
242 /* Create a list element for the tab */
243 DOM_li = document.createElement("li");
245 /* Save a reference to this list item so we can later change it to
246 the "active" class */
249 /* Create a link to activate the tab */
250 DOM_a = document.createElement("a");
251 DOM_a.appendChild(document.createTextNode(t.headingText));
252 DOM_a.href = "javascript:void(null);";
253 DOM_a.title = t.headingText;
254 DOM_a.onclick = this.navClick;
256 /* Add some properties to the link so we can identify which tab
257 was clicked. Later the navClick method will need this.
260 DOM_a.tabberIndex = i;
262 /* Do we need to add an id to DOM_a? */
263 if (this.addLinkId && this.linkIdFormat) {
265 /* Determine the id name */
266 aId = this.linkIdFormat;
267 aId = aId.replace(/<tabberid>/gi, this.id);
268 aId = aId.replace(/<tabnumberzero>/gi, i);
269 aId = aId.replace(/<tabnumberone>/gi, i+1);
270 aId = aId.replace(/<tabtitle>/gi, t.headingText.replace(/[^a-zA-Z0-9\-]/gi, ''));
275 /* Add the link to the list element */
276 DOM_li.appendChild(DOM_a);
278 /* Add the list element to the list */
279 DOM_ul.appendChild(DOM_li);
282 /* Add the UL list to the beginning of the tabber div */
283 e.insertBefore(DOM_ul, e.firstChild);
285 /* Make the tabber div "live" so different CSS can be applied */
286 e.className = e.className.replace(this.REclassMain, this.classMainLive);
288 /* Activate the default tab, and do not call the onclick handler */
289 this.tabShow(defaultTab);
291 /* If the user specified an onLoad function, call it now. */
292 if (typeof this.onLoad == 'function') {
293 this.onLoad({tabber:this});
300 tabberObj.prototype.navClick = function(event)
302 /* This method should only be called by the onClick event of an <A>
303 element, in which case we will determine which tab was clicked by
304 examining a property that we previously attached to the <A>
307 Since this was triggered from an onClick event, the variable
308 "this" refers to the <A> element that triggered the onClick
309 event (and not to the tabberObj).
311 When tabberObj was initialized, we added some extra properties
312 to the <A> element, for the purpose of retrieving them now. Get
313 the tabberObj object, plus the tab number that was clicked.
317 rVal, /* Return value from the user onclick function */
318 a, /* element that triggered the onclick event */
319 self, /* the tabber object */
320 tabberIndex, /* index of the tab that triggered the event */
321 onClickArgs; /* args to send the onclick function */
324 if (!a.tabber) { return false; }
327 tabberIndex = a.tabberIndex;
329 /* Remove focus from the link because it looks ugly.
330 I don't know if this is a good idea...
334 /* If the user specified an onClick function, call it now.
335 If the function returns false then do not continue.
337 if (typeof self.onClick == 'function') {
339 onClickArgs = {'tabber':self, 'index':tabberIndex, 'event':event};
341 /* IE uses a different way to access the event object */
342 if (!event) { onClickArgs.event = window.event; }
344 rVal = self.onClick(onClickArgs);
345 if (rVal === false) { return false; }
348 self.tabShow(tabberIndex);
354 tabberObj.prototype.tabHideAll = function()
358 /* Hide all tabs and make all navigation links inactive */
359 for (i = 0; i < this.tabs.length; i++) {
365 tabberObj.prototype.tabHide = function(tabberIndex)
369 if (!this.tabs[tabberIndex]) { return false; }
371 /* Hide a single tab and make its navigation link inactive */
372 div = this.tabs[tabberIndex].div;
374 /* Hide the tab contents by adding classTabHide to the div */
375 if (!div.className.match(this.REclassTabHide)) {
376 div.className += ' ' + this.classTabHide;
378 this.navClearActive(tabberIndex);
384 tabberObj.prototype.tabShow = function(tabberIndex)
386 /* Show the tabberIndex tab and hide all the other tabs */
390 if (!this.tabs[tabberIndex]) { return false; }
392 /* Hide all the tabs first */
395 /* Get the div that holds this tab */
396 div = this.tabs[tabberIndex].div;
398 /* Remove classTabHide from the div */
399 div.className = div.className.replace(this.REclassTabHide, '');
401 /* Mark this tab navigation link as "active" */
402 this.navSetActive(tabberIndex);
404 /* If the user specified an onTabDisplay function, call it now. */
405 if (typeof this.onTabDisplay == 'function') {
406 this.onTabDisplay({'tabber':this, 'index':tabberIndex});
412 tabberObj.prototype.navSetActive = function(tabberIndex)
414 /* Note: this method does *not* enforce the rule
415 that only one nav item can be active at a time.
418 /* Set classNavActive for the navigation list item */
419 this.tabs[tabberIndex].li.className = this.classNavActive;
425 tabberObj.prototype.navClearActive = function(tabberIndex)
427 /* Note: this method does *not* enforce the rule
428 that one nav should always be active.
431 /* Remove classNavActive from the navigation list item */
432 this.tabs[tabberIndex].li.className = '';
438 /*==================================================*/
441 function tabberAutomatic(tabberArgs)
443 /* This function finds all DIV elements in the document where
444 class=tabber.classMain, then converts them to use the tabber
447 tabberArgs = an object to send to "new tabber()"
450 tempObj, /* Temporary tabber object */
451 divs, /* Array of all divs on the page */
454 if (!tabberArgs) { tabberArgs = {}; }
456 /* Create a tabber object so we can get the value of classMain */
457 tempObj = new tabberObj(tabberArgs);
459 /* Find all DIV elements in the document that have class=tabber */
461 /* First get an array of all DIV elements and loop through them */
462 divs = document.getElementsByTagName("div");
463 for (i=0; i < divs.length; i++) {
465 /* Is this DIV the correct class? */
466 if (divs[i].className &&
467 divs[i].className.match(tempObj.REclassMain)) {
469 /* Now tabify the DIV */
470 tabberArgs.div = divs[i];
471 divs[i].tabber = new tabberObj(tabberArgs);
479 /*==================================================*/
482 function tabberAutomaticOnLoad(tabberArgs)
484 /* This function adds tabberAutomatic to the window.onload event,
485 so it will run after the document has finished loading.
489 if (!tabberArgs) { tabberArgs = {}; }
491 /* Taken from: http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
493 oldOnLoad = window.onload;
494 if (typeof window.onload != 'function') {
495 window.onload = function() {
496 tabberAutomatic(tabberArgs);
499 window.onload = function() {
501 tabberAutomatic(tabberArgs);
507 /*==================================================*/
510 /* Run tabberAutomaticOnload() unless the "manualStartup" option was specified */
512 if (typeof tabberOptions == 'undefined') {
514 tabberAutomaticOnLoad();
518 if (!tabberOptions['manualStartup']) {
519 tabberAutomaticOnLoad(tabberOptions);