summaryrefslogtreecommitdiff
path: root/slides/browser/CTOCWidget.js
blob: a411ea9959f8bb9d8c61db9771dcb65fe1ee189a (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
/*
 * CTOCWidget.js
 * $Revision: 1.3 $ $Date: 2003/07/14 06:02:50 $
 */

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Bob Clary <bclary@netscape.com>
 *
 * ***** END LICENSE BLOCK ***** */

function CTOCWidget(domTOCModel, target)
{
  if (domTOCModel.documentElement.nodeName != 'toc')
  {
    throw 'CTOCWidget called on non toc Document: ' + domTOCModel.nodeName;
  }

  this.model = domTOCModel;
  this.target = target;
  this.view = document.createElement('div');
  this.view.setAttribute('class', CTOCWidget._classprefix + '_view');

  var modelItems = domTOCModel.documentElement.childNodes;
  for (var i = 0; i < modelItems.length; i++)
  {
    var modelItem = modelItems.item(i);
    if (modelItem.nodeType == Node.ELEMENT_NODE)
    {
      var viewItem  = CTOCWidget.createItemView(modelItem, target);
      this.view.appendChild(viewItem);
    }
  }
}

CTOCWidget._handleImages  = { open: '/toolbox/examples/2003/CTOCWidget/minus.gif', closed: '/toolbox/examples/2003/CTOCWidget/plus.gif', height: '12px', width: '16px'};
CTOCWidget._classprefix  = 'CTOCWidget';

CTOCWidget.createItemView = function (modelItem, target)
{
  if (modelItem.nodeType != Node.ELEMENT_NODE)
  {
    throw 'CTOCWidget.createItemView called on non-Element: ' + modelItem.nodeName;
  }

  var i;

  var viewItem = document.createElement('div');
  viewItem.setAttribute('class', CTOCWidget._classprefix + '_item');

  var viewItemHandle = document.createElement('div');
  viewItemHandle.setAttribute('class', CTOCWidget._classprefix + '_itemhandle');
  viewItemHandle.style.cursor = 'pointer';

  var viewItemHandleImg = document.createElement('img');
  viewItemHandleImg.style.height = CTOCWidget._handleImages.height;
  viewItemHandleImg.style.width = CTOCWidget._handleImages.width;
  viewItemHandleImg.addEventListener('click', CTOCWidget.toggleHandle, false);

  var viewItemHandleLink;
  if (!modelItem.getAttribute('url'))
  {
    viewItemHandleLink = document.createElement('span');
  }
  else 
  {
    viewItemHandleLink = document.createElement('a');
    viewItemHandleLink.setAttribute('href', modelItem.getAttribute('url'));
    viewItemHandleLink.setAttribute('target', target);
  }
  viewItemHandleLink.appendChild(document.createTextNode(modelItem.getAttribute('title')));

  viewItemHandle.appendChild(viewItemHandleImg);
  viewItemHandle.appendChild(viewItemHandleLink);
  viewItem.appendChild(viewItemHandle);

  if (modelItem.childNodes.length == 0)
  {
    viewItemHandleImg.setAttribute('src', CTOCWidget._handleImages.open);
  }
  else
  {
    viewItemHandleImg.setAttribute('src', CTOCWidget._handleImages.closed);

    var viewItemChildren = document.createElement('div');
    viewItemChildren.setAttribute('class', CTOCWidget._classprefix + '_itemchildren');
    viewItemChildren.style.display = 'none';
    viewItemChildren.style.position = 'relative';
    viewItemChildren.style.left = '1em';

    for (i = 0; i < modelItem.childNodes.length; i++)
    {
      var modelItemChild = modelItem.childNodes.item(i);
      if (modelItemChild.nodeType == Node.ELEMENT_NODE)
      {
        viewItemChildren.appendChild(CTOCWidget.createItemView(modelItemChild, target));
      }
    }

    viewItem.appendChild(viewItemChildren);
  }

  return viewItem;
};

// fires on img part of the handle
CTOCWidget.toggleHandle = function(e)
{
  switch (e.eventPhase)
  {
    case Event.CAPTURING_PHASE:
    case Event.BUBBLING_PHASE:
      return true;
    
    case Event.AT_TARGET:
     
      e.preventBubble();

      var domHandle   = e.target.parentNode;
      var domChildren = domHandle.nextSibling;

      if (!domChildren)
      {
        return true;
      }

      switch(domChildren.style.display)
      {
        case '':
        case 'block':
          domChildren.style.display = 'none';
          e.target.setAttribute('src', CTOCWidget._handleImages.closed);
          break;
        case 'none':
          domChildren.style.display = 'block';
          e.target.setAttribute('src', CTOCWidget._handleImages.open);
          break;
        default:
          return false;
       }

       return true;

    default:
      dump('Unknown Event Phase ' + e.eventPhase);
      break;
  }

  return true;
}