3 * $Revision: 1.3 $ $Date: 2003/07/14 06:02:50 $
6 /* ***** BEGIN LICENSE BLOCK *****
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is Netscape code.
21 * The Initial Developer of the Original Code is
22 * Netscape Corporation.
23 * Portions created by the Initial Developer are Copyright (C) 2003
24 * the Initial Developer. All Rights Reserved.
26 * Contributor(s): Bob Clary <bclary@netscape.com>
28 * ***** END LICENSE BLOCK ***** */
30 function CTOCWidget(domTOCModel, target)
32 if (domTOCModel.documentElement.nodeName != 'toc')
34 throw 'CTOCWidget called on non toc Document: ' + domTOCModel.nodeName;
37 this.model = domTOCModel;
39 this.view = document.createElement('div');
40 this.view.setAttribute('class', CTOCWidget._classprefix + '_view');
42 var modelItems = domTOCModel.documentElement.childNodes;
43 for (var i = 0; i < modelItems.length; i++)
45 var modelItem = modelItems.item(i);
46 if (modelItem.nodeType == Node.ELEMENT_NODE)
48 var viewItem = CTOCWidget.createItemView(modelItem, target);
49 this.view.appendChild(viewItem);
54 CTOCWidget._handleImages = { open: '/toolbox/examples/2003/CTOCWidget/minus.gif', closed: '/toolbox/examples/2003/CTOCWidget/plus.gif', height: '12px', width: '16px'};
55 CTOCWidget._classprefix = 'CTOCWidget';
57 CTOCWidget.createItemView = function (modelItem, target)
59 if (modelItem.nodeType != Node.ELEMENT_NODE)
61 throw 'CTOCWidget.createItemView called on non-Element: ' + modelItem.nodeName;
66 var viewItem = document.createElement('div');
67 viewItem.setAttribute('class', CTOCWidget._classprefix + '_item');
69 var viewItemHandle = document.createElement('div');
70 viewItemHandle.setAttribute('class', CTOCWidget._classprefix + '_itemhandle');
71 viewItemHandle.style.cursor = 'pointer';
73 var viewItemHandleImg = document.createElement('img');
74 viewItemHandleImg.style.height = CTOCWidget._handleImages.height;
75 viewItemHandleImg.style.width = CTOCWidget._handleImages.width;
76 viewItemHandleImg.addEventListener('click', CTOCWidget.toggleHandle, false);
78 var viewItemHandleLink;
79 if (!modelItem.getAttribute('url'))
81 viewItemHandleLink = document.createElement('span');
85 viewItemHandleLink = document.createElement('a');
86 viewItemHandleLink.setAttribute('href', modelItem.getAttribute('url'));
87 viewItemHandleLink.setAttribute('target', target);
89 viewItemHandleLink.appendChild(document.createTextNode(modelItem.getAttribute('title')));
91 viewItemHandle.appendChild(viewItemHandleImg);
92 viewItemHandle.appendChild(viewItemHandleLink);
93 viewItem.appendChild(viewItemHandle);
95 if (modelItem.childNodes.length == 0)
97 viewItemHandleImg.setAttribute('src', CTOCWidget._handleImages.open);
101 viewItemHandleImg.setAttribute('src', CTOCWidget._handleImages.closed);
103 var viewItemChildren = document.createElement('div');
104 viewItemChildren.setAttribute('class', CTOCWidget._classprefix + '_itemchildren');
105 viewItemChildren.style.display = 'none';
106 viewItemChildren.style.position = 'relative';
107 viewItemChildren.style.left = '1em';
109 for (i = 0; i < modelItem.childNodes.length; i++)
111 var modelItemChild = modelItem.childNodes.item(i);
112 if (modelItemChild.nodeType == Node.ELEMENT_NODE)
114 viewItemChildren.appendChild(CTOCWidget.createItemView(modelItemChild, target));
118 viewItem.appendChild(viewItemChildren);
124 // fires on img part of the handle
125 CTOCWidget.toggleHandle = function(e)
127 switch (e.eventPhase)
129 case Event.CAPTURING_PHASE:
130 case Event.BUBBLING_PHASE:
133 case Event.AT_TARGET:
137 var domHandle = e.target.parentNode;
138 var domChildren = domHandle.nextSibling;
145 switch(domChildren.style.display)
149 domChildren.style.display = 'none';
150 e.target.setAttribute('src', CTOCWidget._handleImages.closed);
153 domChildren.style.display = 'block';
154 e.target.setAttribute('src', CTOCWidget._handleImages.open);
163 dump('Unknown Event Phase ' + e.eventPhase);