summaryrefslogtreecommitdiff
path: root/javascript/libs/tabpane.js
blob: e4e14566099f0e40e1f658246205eefcced8cdb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// $Header: /cvsroot/bitweaver/_bit_util/javascript/libs/tabpane.js,v 1.22 2010/05/13 23:38:40 spiderr Exp $

//-------------------------------------------------------------------------
//  						   Tab Pane 1.02
//-------------------------------------------------------------------------
//  					 Created by Erik Arvidsson
//  			  (http://webfx.eae.net/contact.html#erik)
//  				  For WebFX (http://webfx.eae.net/)
//-------------------------------------------------------------------------
//  			  Copyright (c) 1998 - 2003 Erik Arvidsson
//-------------------------------------------------------------------------

// please modify this file and leave plenty of comments. This file will be
// compressed automatically. Please make sure you only use comments beginning
// with '//' and put comments on separate lines otherwise the packer will choke

// Called to cause an FCKEditor to show. This fixes an incompatibility with
// tab pane and FCKEditor in Gecko browsers where the FCKEditor is created
// in a hidden tab. The editor fails to get focused without this.

// see http://webfx.eae.net/dhtml/tabpane/tabpane.html for details

function switchEditors(oNode) {
	var i=0;
	// We use this to avoid the error when this runs BEFORE
	// FKCEditor has created the API object.
	if (document.FCKEditorLoaded) {
		for (i=0;i<oNode.childNodes.length;i++) {
			childNode = oNode.childNodes.item(i);
			editor = FCKeditorAPI.GetInstance(childNode.name);
			if (editor && editor.EditorDocument && editor.EditMode == FCK_EDITMODE_WYSIWYG) {
				editor.SwitchEditMode();
				editor.SwitchEditMode();
			}
			switchEditors(childNode);
		}
	}
}

// This function is used to define if the browser supports the needed
// features
function hasSupport() {

	if (typeof hasSupport.support != "undefined") {
		return hasSupport.support;
	}

	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );

	hasSupport.support = ( typeof document.implementation != "undefined" && document.implementation.hasFeature( "html", "1.0" ) || ie55 );

	// IE55 has a serious DOM1 bug... Patch it!
	if ( ie55 ) {
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName = function ( sTagName ) {
			if ( sTagName == "*" ) {
				return document.all;
			} else {
				return document._getElementsByTagName( sTagName );
			}
		};
	}

	return hasSupport.support;
}


// The constructor for tab panes
// el : HTMLElement		The html element used to represent the tab pane
// bUseCookie : Boolean	Optional. Default is true. Used to determine whether to us
//						persistance using cookies or not

function WebFXTabPane( el, bUseCookie ) {
	if ( !hasSupport() || el == null ) { return; }

	this.element = el;
	this.element.tabPane = this;
	this.pages = [];
	this.selectedIndex = null;

	this.useCookie = bUseCookie != null ? bUseCookie : true;

	// <--- quick hack to set persistence only on pages where referrer == location
	var ref = document.referrer.split( /[#\?]/ );
	var loc = document.location.href.split( /[#\?]/ );
	if( loc[0] != ref[0] ) {
		this.useCookie = false;
	}
	// end quick hack - xing --->

	// add class name tag to class name
	this.element.className = this.classNameTag + " " + this.element.className;

	// add tab row
	this.tabRow = document.createElement( "div" );
	this.tabRow.className = "tabcontainer";
	el.insertBefore( this.tabRow, el.firstChild );

	// loop through child nodes and add them
	var cs = el.childNodes;
	var n;
	for (var i = 0; i < cs.length; i++) {
		if (cs[i].nodeType == 1 && ( cs[i].className == "tabpage" || cs[i].className.substr(0,8) == "tabpage ") ) {
			this.addTabPage( cs[i] );
		}
	}

	var tabIndex = 0;
	if ( this.useCookie ) {
		tabIndex = Number( WebFXTabPane.getCookie( "webfxtab_" + this.element.id ) );
		if ( isNaN( tabIndex ) || !this.pages[ tabIndex ] ) {
			tabIndex = 0;
		}
	}
	this.setSelectedIndex( tabIndex );
}

WebFXTabPane.prototype.classNameTag = "tabsystem";

WebFXTabPane.prototype.setSelectedIndex = function ( n ) {
	if (this.selectedIndex != n) {
		if (this.selectedIndex != null && this.pages[ this.selectedIndex ] != null ) {
			this.pages[ this.selectedIndex ].hide();
		}
		this.selectedIndex = n;
		if( this.pages[ this.selectedIndex ] != undefined ) {
			this.pages[ this.selectedIndex ].show();
		}

		if ( this.useCookie ) {
			WebFXTabPane.setCookie( "webfxtab_" + this.element.id, n, 1 );
		}
	}
};

WebFXTabPane.prototype.getSelectedIndex = function () {
	return this.selectedIndex;
};

WebFXTabPane.prototype.addTabPage = function ( oElement ) {
	if ( !hasSupport() ) { return; }

	if ( oElement.tabPage == this )	{
		return oElement.tabPage;
	}

	var n = this.pages.length;
	var tp = this.pages[n] = new WebFXTabPage( oElement, this, n );
	tp.tabPane = this;

	// move the tab out of the box
	this.tabRow.appendChild( tp.tab );

	if ( n == this.selectedIndex ) {
		tp.show();
	} else {
		tp.hide();
	}

	return tp;
};

WebFXTabPane.prototype.dispose = function () {
	this.element.tabPane = null;
	this.element = null;
	this.tabRow = null;

	for (var i = 0; i < this.pages.length; i++) {
		this.pages[i].dispose();
		this.pages[i] = null;
	}
	this.pages = null;
};



// Cookie handling
WebFXTabPane.setCookie = function ( sName, sValue, nDays ) {
	var expires = "";
	if ( nDays ) {
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}

	document.cookie = sName + "=" + sValue + expires + "; path="+bitCookiePath;
};

WebFXTabPane.getCookie = function (sName) {
	var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? res[3] : null;
};

WebFXTabPane.removeCookie = function ( name ) {
	setCookie( name, "", -1 );
};


// The constructor for tab pages. This one should not be used.
// Use WebFXTabPage.addTabPage instead
// el : HTMLElement			The html element used to represent the tab pane
// tabPane : WebFXTabPane	The parent tab pane
// nindex :	Number			The index of the page in the parent pane page array

function WebFXTabPage( el, tabPane, nIndex ) {
	if ( !hasSupport() || el == null ) { return; }

	this.element = el;
	this.element.tabPage = this;
	this.index = nIndex;

	var cs = el.childNodes;
	for (var i = 0; i < cs.length; i++) {
		if (cs[i].nodeType == 1 && ( cs[i].className == "tab" || cs[i].className.substr(0,4) == "tab ") ) {
			this.tab = cs[i];
			break;
		}
	}

	// insert a tag around content to support keyboard navigation


	var a = document.createElement( "A" );
	this.aElement = a;
	a.href = "#";
	a.onclick = function () { return false; };
	while ( this.tab.hasChildNodes() ) {
		a.appendChild( this.tab.firstChild );
	}
	this.tab.appendChild( a );


	// hook up events, using DOM0
	var oThis = this;
	// we can bind a custom onclick event to the tab by passing one in - this lets us associate additional events with the click of a tab -wjames5
	var oldOnclick = this.tab.onclick != null? this.tab.onclick:function(){};
	this.tab.onclick = function () { oThis.select(); oldOnclick(); };
	this.tab.onmouseover = function () { WebFXTabPage.tabOver( oThis ); };
	this.tab.onmouseout = function () { WebFXTabPage.tabOut( oThis ); };
}

WebFXTabPage.prototype.show = function () {
	var el = this.tab;
	var s = el.className + " tab-active";
	s = s.replace(/ +/g, " ");
	el.className = s;

	// Fix for FCKEditor focus bug
	switchEditors(this.element);
	this.element.style.display = "block";
};

WebFXTabPage.prototype.hide = function () {
	var el = this.tab;
	var s = el.className;
	// Packer doesn't like \- in regexp for some reason
	s = s.replace(/ tab.active/g, "");
	el.className = s;

	this.element.style.display = "none";
};

WebFXTabPage.prototype.select = function () {
	this.tabPane.setSelectedIndex( this.index );
};

WebFXTabPage.prototype.dispose = function () {
	// Safari only submits inputs which are not display:none
	// I suspect this is for "security" but that is stupid since we
	// can still hide it off screen like this.
	var safari = /^Apple/;
	// note the crazy 'unknown' check here. this is for IE (of course) whoes xmlhtml is not part of the js engine and willy nilly throws in crap outside the spec.
	if (this.element.style.display == "none" && typeof( navigator.vendor ) != "unknown" && navigator.vendor == safari) {
		this.element.style.position = "absolute";
		this.element.style.left = "-10000px";
		this.element.style.display = "block";
	}
	this.aElement.onclick = null;
	this.aElement = null;
	this.element.tabPage = null;
	this.tab.onclick = null;
	this.tab.onmouseover = null;
	this.tab.onmouseout = null;
	this.tab = null;
	this.tabPane = null;
	this.element = null;
};

WebFXTabPage.tabOver = function ( tabpage ) {
	var el = tabpage.tab;
	var s = el.className + " tab-hover";
	s = s.replace(/ +/g, " ");
	el.className = s;
};

WebFXTabPage.tabOut = function ( tabpage ) {
	var el = tabpage.tab;
	var s = el.className;
	// Packer doesn't like \- in regexp for some reason
	s = s.replace(/ tab.hover/g, "");
	el.className = s;
};


// This function initializes all uninitialized tab panes and tab pages
function setupAllTabs() {
	if ( !hasSupport() ) { return; }

	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tabPaneRe = /tabpane/;
	var tabPageRe = /tabpage/;
	var cn, el;
	var parentTabPane;

	for ( var i = 0; i < l; i++ ) {
		el = all[i];
		cn = el.className;

		// no className
		if ( cn == "" ) { continue; }

		// uninitiated tab pane
		if ( tabPaneRe.test( cn ) && !el.tabPane ) {
			new WebFXTabPane( el );
		}

		// unitiated tab page wit a valid tab pane parent
		else if ( tabPageRe.test( cn ) && !el.tabPage && tabPaneRe.test( el.parentNode.className ) ) {
			el.parentNode.tabPane.addTabPage( el );
		}
	}
}

function disposeAllTabs() {
	if ( !hasSupport() ) { return; }

	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tabPaneRe = /tabpane/;
	var cn, el;
	var tabPanes = [];

	for ( var i = 0; i < l; i++ ) {
		el = all[i];
		cn = el.className;

		// no className
		if ( cn == "" ) { continue; }

		// tab pane
		if ( tabPaneRe.test( cn ) && el.tabPane ) {
			tabPanes[tabPanes.length] = el.tabPane;
		}
	}

	for (var i = tabPanes.length - 1; i >= 0; i--) {
		tabPanes[i].dispose();
		tabPanes[i] = null;
	}
}


// initialization hook up

// DOM2
if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", setupAllTabs, false );
} else if ( typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", setupAllTabs );
	window.attachEvent( "onunload", disposeAllTabs );
} else {
	if ( window.onload != null ) {
		var oldOnload = window.onload;
		window.onload = function ( e ) {
			oldOnload( e );
			setupAllTabs();
		};
	} else {
		window.onload = setupAllTabs;
	}
}