Debian Codegraph @

demo/browser/xbCollapsibleLists.js

raw
1 /*
2 xbCollapsibleLists.js 2001-02-26
3
4 Contributor(s): Michael Bostock, Netscape Communications, Copyright 1997
5 Bob Clary, Netscape Communications, Copyright 2001
6 Seth Dillingham, Macrobyte Resources, Copyright 2001
7 Mark Filanowicz, Amdahl IT Services, Copyright 2002
8
9 Netscape grants you a royalty free license to use, modify or
10 distribute this software provided that this copyright notice
11 appears on all copies. This software is provided "AS IS,"
12 without a warranty of any kind.
13
14 See xbCollapsibleLists.js.changelog.html for details of changes.
15 */
16
17
18 var xbcl__id = 0;
19 var xbcl_item_id = 0;
20 var xbcl_mLists = new Array();
21 var xbcl_parentElement = null;
22
23 document.lists = xbcl_mLists;
24
25 function List(visible, width, height, bgColor, collapsedImageURL, expandedImageURL)
26 {
27 this.lists = new Array(); // sublists
28 this.items = new Array(); // layers
29 this.types = new Array(); // type
30 this.strs = new Array(); // content
31 this.visible = visible;
32 this.id = xbcl__id;
33 this.width = width || 350;
34 this.height = height || 22;
35
36 this.collapsedImageURL = collapsedImageURL || 'false.gif';
37 this.expandedImageURL = expandedImageURL || 'true.gif';
38
39 if (bgColor)
40 this.bgColor = bgColor;
41
42 xbcl_mLists[xbcl__id++] = this;
43 }
44
45 function xbcl_SetFont(i,j)
46 {
47 this.fontIntro = i;
48 this.fontOutro = j;
49 }
50
51 function xbcl_GetFont()
52 {
53 return [this.fontIntro, this.fontOutro];
54 }
55
56 function xbcl_setIndent(indent)
57 {
58 this.i = indent;
59 if (this.i < 0)
60 {
61 this.i = 0;
62 this.space = false;
63 }
64 else
65 this.space = true;
66 }
67
68 function xbcl_getIndent(indent)
69 {
70 return this.i;
71 }
72
73 function xbcl_writeItemDOMHTML( obj, s, flList, listObj )
74 {
75 var styleObj;
76 var outerDiv, innerLeft, innerRight;
77 var str;
78 var leftEdge = 0;
79
80 styleObj = new xbStyle(obj);
81 styleObj.setVisibility('hidden');
82 outerDiv = document.createElement( "DIV" );
83 outerDiv.id = "DIV_" + obj.id;
84 styleObj = new xbStyle( outerDiv );
85 styleObj.setWidth( this.width );
86
87 if ( flList )
88 {
89 innerLeft = document.createElement( "DIV" );
90 innerLeft.style.position = "absolute";
91 innerLeft.style.valign = "middle";
92 leftEdge = 15;
93
94 styleObj = new xbStyle( innerLeft );
95 styleObj.setWidth( 15 );
96 styleObj.setBackgroundColor( "transparent" );
97
98 if ( listObj.visible )
99 str = '<A TARGET="_self" HREF="javascript:xbcl_expand(' + listObj.id + ');"><IMG BORDER="0" SRC="' + this.expandedImageURL + '" ID="_img' + listObj.id + '" NAME="_img' + listObj.id + '"></A>';
100 else
101 str = '<A TARGET="_self" HREF="javascript:xbcl_expand(' + listObj.id + ');"><IMG BORDER="0" SRC="' + this.collapsedImageURL + '" ID="_img' + listObj.id + '" NAME="_img' + listObj.id + '"></A>';
102
103 innerLeft.innerHTML = str;
104 outerDiv.appendChild( innerLeft );
105 }
106 else if ( this.space )
107 leftEdge = 15;
108
109 innerRight = document.createElement( "DIV" );
110 innerRight.noWrap = true;
111 innerRight.style.position = "absolute";
112
113 styleObj = new xbStyle( innerRight );
114 styleObj.setLeft( leftEdge + ( this.l * this.i ) );
115 styleObj.setWidth( this.width - 15 - this.l * this.i );
116 styleObj.setBackgroundColor( "transparent" );
117
118 // start of change by Mark Filanowicz 02-22-2002
119 if ( flList )
120 {
121 s = this.fontIntro + '<A TARGET="_self" STYLE="text-decoration: none;" HREF="javascript:xbcl_expand(' + listObj.id + ');">' + s + this.fontOutro;
122 }
123 else
124 {
125 s = this.fontIntro + s + this.fontOutro;
126 }
127 // end of change by Mark Filanowicz 02-22-2002
128
129
130 innerRight.innerHTML = s;
131 outerDiv.appendChild( innerRight );
132
133 obj.appendChild( outerDiv );
134
135 return;
136 }
137
138 function xbcl_writeItem( obj, s, flList, listObj )
139 {
140 var cellStyle = '';
141 var str = '';
142 var styleObj = new xbStyle( obj );
143
144 styleObj.setVisibility( 'hidden' );
145
146 if ( document.body && document.body.style )
147 cellStyle = ' style="background-color: transparent;"';
148
149 str += '<TABLE WIDTH='+this.width+' NOWRAP BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR>';
150
151 if ( flList )
152 {
153 str += '<TD WIDTH="15" NOWRAP VALIGN="MIDDLE"' + cellStyle + '>';
154 str += '<A TARGET="_self" HREF="javascript:xbcl_expand(' + listObj.id + ');">';
155
156 if ( listObj.visible )
157 str += '<IMG BORDER="0" SRC="' + this.expandedImageURL + '" ID="_img' + listObj.id + '" NAME="_img' + listObj.id + '">';
158 else
159 str += '<IMG BORDER="0" SRC="' + this.collapsedImageURL + '" ID="_img' + listObj.id + '" NAME="_img' + listObj.id + '">';
160
161 str += '</A></TD>';
162 }
163 else if (this.space)
164 str += '<TD WIDTH="15" NOWRAP' + cellStyle + '>&nbsp;</TD>';
165
166 if (this.l>0 && this.i>0)
167 str += '<TD WIDTH="' + this.l*this.i+ '" NOWRAP' + cellStyle + '>&nbsp;</TD>';
168
169 str += '<TD HEIGHT="' + ( this.height - 3) + '" WIDTH="' + ( this.width - 15 - this.l * this.i ) + '" VALIGN="MIDDLE" ALIGN="LEFT"' + cellStyle + '>';
170
171 // start of change by Mark Filanowicz 02-22-2002
172 if ( flList )
173 {
174 str += this.fontIntro + '<A TARGET="_self" STYLE="text-decoration: none;" HREF="javascript:xbcl_expand(' + listObj.id + ');">' + s + this.fontOutro;
175 }
176 else
177 {
178 str += this.fontIntro + s + this.fontOutro;
179 }
180 // end of change by Mark Filanowicz 02-22-2002
181
182 str += '</TD></TR></TABLE>';
183
184 styleObj.setInnerHTML( str );
185
186 return;
187 }
188
189 function xbcl_writeList()
190 {
191 var item;
192 var i;
193 var flList;
194
195 for ( i = 0; i < this.types.length; i++ )
196 {
197 item = this.items[ i ];
198 flList = ( this.types[ i ] == 'list' );
199
200 this._writeItem( item, this.strs[ i ], flList, this.lists[ i ] );
201
202 if ( flList && this.lists[ i ].visible )
203 this.lists[ i ]._writeList();
204 }
205
206 this.built = true;
207 this.needsRewrite = false;
208 self.status = '';
209 }
210
211 function xbcl_showList()
212 {
213 var item;
214 var styleObj;
215 var i;
216
217 for (i = 0; i < this.types.length; i++)
218 {
219 item = this.items[i];
220 styleObj = new xbStyle(item);
221 styleObj.setClipLeft(0);
222 styleObj.setClipRight(this.width);
223 styleObj.setClipTop(0);
224 if (item.height)
225 {
226 styleObj.setClipBottom(item.height);
227 styleObj.setHeight(item.height);
228 }
229 else
230 {
231 styleObj.setClipBottom(this.height);
232 styleObj.setHeight(this.height);
233 }
234
235 if ( this.visible )
236 styleObj.setVisibility( 'visible' );
237
238 var bg = item.oBgColor || this.bgColor;
239 if ((bg == null) || (bg == 'null'))
240 bg = '';
241
242 styleObj.setBackgroundColor(bg);
243
244 if (this.types[i] == 'list' && this.lists[i].visible)
245 this.lists[i]._showList();
246 }
247 this.shown = true;
248 this.needsUpdate = false;
249 }
250
251 function xbcl_setImage(list, item, file)
252 {
253 var id = '_img' + list.id;
254 var img = null;
255
256 // for DOMHTML or IE4 use cross browser getElementById from xbStyle
257 // can't use it for NN4 since it only works for layers in NN4
258 if (document.layers)
259 img = item.document.images[0];
260 else
261 img = xbGetElementById(id);
262
263 if (img)
264 img.src = file;
265 }
266
267 function xbcl_getHeight()
268 {
269 var totalHeight = 0;
270 var i;
271
272 if (!this.visible)
273 return 0;
274
275 for (i = 0; i < this.types.length; i++)
276 {
277 if (this.items[i].height)
278 totalHeight += this.items[i].height;
279 else
280 totalHeight += this.height;
281
282 if ((this.types[i] == 'list') && this.lists[i].visible)
283 {
284 totalHeight += this.lists[i].getHeight();
285 }
286 }
287
288 return totalHeight;
289 }
290
291 function xbcl_updateList(pVis, x, y)
292 {
293 var currTop = y;
294 var item;
295 var styleObj;
296 var i;
297
298 for (i = 0; i < this.types.length; i++)
299 {
300 item = this.items[i];
301 styleObj = new xbStyle(item);
302
303 if (this.visible && pVis)
304 {
305 styleObj.moveTo(x, currTop);
306 if (item.height) // allow custom heights for each item
307 currTop += item.height;
308 else
309 currTop += this.height;
310
311 styleObj.setVisibility('visible');
312 }
313 else
314 {
315 styleObj.setVisibility('hidden');
316 }
317
318 if (this.types[i] == 'list')
319 {
320 if (this.lists[i].visible)
321 {
322 if (!this.lists[i].built || this.lists[i].needsRewrite)
323 this.lists[i]._writeList();
324
325 if (!this.lists[i].shown || this.lists[i].needsUpdate)
326 this.lists[i]._showList();
327
328 xbcl_setImage(this.lists[i], item, this.expandedImageURL );
329 }
330 else
331 xbcl_setImage(this.lists[i], item, this.collapsedImageURL );
332
333 if (this.lists[i].built)
334 currTop = this.lists[i]._updateList(this.visible && pVis, x, currTop);
335 }
336 }
337 return currTop;
338 }
339
340 function xbcl_updateParent( pid, l )
341 {
342 var i;
343
344 if ( !l )
345 l = 0;
346
347 this.pid = pid;
348 this.l = l;
349
350 for ( i = 0; i < this.types.length; i++ )
351 {
352 if ( this.types[ i ] == 'list' )
353 {
354 this.lists[ i ]._updateParent( pid, l + 1 );
355 }
356 }
357 }
358
359 function xbcl_expand(i)
360 {
361 xbcl_mLists[i].visible = !xbcl_mLists[i].visible;
362
363 if (xbcl_mLists[i].onexpand != null)
364 xbcl_mLists[i].onexpand(xbcl_mLists[i].id);
365
366 xbcl_mLists[xbcl_mLists[i].pid].rebuild();
367
368 if (xbcl_mLists[i].postexpand != null)
369 xbcl_mLists[i].postexpand(xbcl_mLists[i].id);
370 }
371
372 function xbcl_build(x, y)
373 {
374 this._updateParent(this.id);
375 this._writeList();
376 this._showList();
377 this._updateList(true, x, y);
378 this.x = x;
379 this.y = y;
380 }
381
382 function xbcl_rebuild()
383 {
384 this._updateList(true, this.x, this.y);
385 }
386
387 function xbcl_getNewItem()
388 {
389 var newItem = null;
390
391 newItem = xbGetElementById('lItem' + xbcl_item_id);
392
393 if (!newItem)
394 {
395 if (document.all && !document.getElementById)
396 {
397 var parentElement = this.parentElement;
398 if (!parentElement)
399 parentElement = document.body;
400
401 parentElement.insertAdjacentHTML('beforeEnd', '<div id="lItem' + xbcl_item_id + '" style="position:absolute;"></div>');
402 newItem = xbGetElementById('lItem' + xbcl_item_id);
403 }
404 else if (document.layers)
405 {
406 if (this.parentElement)
407 newItem = new Layer(this.width, this.parentElement);
408 else
409 newItem = new Layer(this.width);
410 }
411 else if (document.createElement)
412 {
413 newItem = document.createElement('div');
414 newItem.id= 'lItem' + xbcl_item_id;
415 newItem.style.position = 'absolute';
416
417 if (this.parentElement)
418 this.parentElement.appendChild(newItem);
419 else
420 document.body.appendChild(newItem);
421 }
422 }
423
424 return newItem;
425 }
426
427 function xbcl_addItem(str, bgColor, item)
428 {
429 if (!item)
430 item = this._getNewItem();
431
432 if (!item)
433 return;
434
435 if (bgColor)
436 item.oBgColor = bgColor;
437
438 this.items[this.items.length] = item;
439 this.types[this.types.length] = 'item';
440 this.strs[this.strs.length] = str;
441 ++xbcl_item_id;
442
443 if ( this.built )
444 {
445 this._writeItem( item, str, false );
446 xbcl_mLists[this.pid].rebuild();
447 if ( this.visible )
448 this._showList();
449 else
450 this.needsUpdate = true;
451 }
452
453 return item;
454 }
455
456 function xbcl_addList(list, str, bgColor, item)
457 {
458 if (!item)
459 item = this._getNewItem();
460
461 if (!item)
462 return;
463
464 if (bgColor)
465 item.oBgColor = bgColor;
466
467 this.lists[this.items.length] = list;
468 this.items[this.items.length] = item;
469 this.types[this.types.length] = 'list';
470 this.strs[this.strs.length] = str;
471 ++xbcl_item_id;
472
473 list.parentList = this;
474
475 list.pid = this.pid;
476 list.l = this.l + 1;
477
478 if ( this.built )
479 {
480 this._writeItem( item, str, true, list );
481 xbcl_mLists[ this.pid ].rebuild();
482 if ( this.visible )
483 this._showList();
484 else
485 this.needsUpdate = true;
486 }
487
488 return item;
489 }
490
491 List.prototype.setIndent = xbcl_setIndent;
492 List.prototype.getIndent = xbcl_getIndent;
493 List.prototype.addItem = xbcl_addItem;
494 List.prototype.addList = xbcl_addList;
495 List.prototype.build = xbcl_build;
496 List.prototype.rebuild = xbcl_rebuild;
497 List.prototype.setFont = xbcl_SetFont;
498 List.prototype.getFont = xbcl_GetFont;
499 List.prototype.getHeight = xbcl_getHeight;
500
501 List.prototype._writeList = xbcl_writeList;
502 List.prototype._getNewItem = xbcl_getNewItem;
503
504 if ( document.getElementById && document.createElement )
505 List.prototype._writeItem = xbcl_writeItemDOMHTML;
506 else
507 List.prototype._writeItem = xbcl_writeItem;
508
509 List.prototype._showList = xbcl_showList;
510 List.prototype._updateList = xbcl_updateList;
511 List.prototype._updateParent = xbcl_updateParent;
512
513 List.prototype.onexpand = null;
514 List.prototype.postexpand = null;
515 List.prototype.lists = null; // sublists
516 List.prototype.items = null; // layers
517 List.prototype.types = null; // type
518 List.prototype.strs = null; // content
519 List.prototype.x = 0;
520 List.prototype.y = 0;
521 List.prototype.visible = false;
522 List.prototype.id = -1;
523 List.prototype.i = 18;
524 List.prototype.space = true;
525 List.prototype.pid = 0;
526 List.prototype.fontIntro = '';
527 List.prototype.fontOutro = '';
528 List.prototype.width = 350;
529 List.prototype.height = 22;
530 List.prototype.built = false;
531 List.prototype.shown = false;
532 List.prototype.needsUpdate = false;
533 List.prototype.needsRewrite = false;
534 List.prototype.l = 0;
535 List.prototype.bgColor = null;
536 List.prototype.parentList = null;
537 List.prototype.parentElement = null;