summaryrefslogtreecommitdiff
path: root/buildscripts/jGrouseDoc/skins/common/js/navTree.js
blob: 5a71ec51c4b5f2ff85216c408a8f77ef296a55e8 (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
jgdoc = {}; 
jgdoc.TreeItem = function(nodeName, item)
{
    this._nodeName = nodeName;
    this._data = item;
    this._children = [];
    
}

jgdoc.Searcher = 
{
    setData : function(data) {
      this._data = data;
      this.processItems();
      this.render();
    },

    sorter: function(o1, o2) {
      var l1 = o1.localName;
      var l2 = o2.localName;
      return l1 < l2? -1 : (l1 > l2 ? 1 : 0);
    },
    
    processItems : function() {
      var root;

      // Pass 1: Build index by fullName, and locate the root element
      this._byName = {};
      for (var i = 0; i < this._data.length; i++) {
        var d = this._data[i];
        if (d.fullName == "GLOBAL") {
          root = this._root = d;
        }
        this._byName[d.fullName] = d;
      }
    
      // Pass 2: Populate _children arrays
      for (var i = 0; i < this._data.length; i++) {
        var item = this._data[i];
        if (item.elementType == "logical_container" && item != this._root) {
          var parent = this._byName[item.parent];
          parent._children = parent._children || [];
          parent._children.push(item);
        }
      }
    },
    
    addClass : function(element, className)
    {
        var s = element.className;
        var a = s.split(' ');
        for (var i = 0; i < a.length; i++)
        {
            if (a[i] == className)
            {
                return;
            }
        }
        a.push(className);
        element.className = a.join(' ');
    },
    
    removeClass : function(element, className)
    {
        var s = element.className;
        var a = s.split(' ');
        var found = false;
        for (var i = 0; i < a.length; i++)
        {
            if (a[i] == className)
            {
                a.splice(i, 1);
                found = true;
                break;
            }
        }
        element.className = a.join(' ');
        return found;
    },    
    
    clicked : function(event)
    {
        event = window.event? window.event : event;
        var target = event.target || event.srcElement;
        var span = target.parentNode;
        var li = span.parentNode;
        var wasOpen = jgdoc.Searcher.removeClass(li, 'open');
        if (wasOpen)
        {
            jgdoc.Searcher.addClass(li, 'closed');
        } 
        else
        {
            jgdoc.Searcher.removeClass(li, 'closed');
            jgdoc.Searcher.addClass(li, 'open');
        }
        span.title = "Click to " + (wasOpen? "expand" : "collapse");
                
    },
    
    addListener : function(element, eventName, handler)
    {
        if (element.addEventListener)
        {
            element.addEventListener(eventName, handler, false);
        }
        else
        {
            element.attachEvent('on' + eventName, handler);
        }
    },
    
    removeListener : function(element, eventName, handler)
    {
        if (element.removeEventListener)
        {
            element.removeEventListener(eventName, handler, false);
        }
        else
        {
            element.detachEvent('on' + eventName, handler);
        }
    },
    
    
    render : function()
    {
        var d = document.getElementById('content');
        d.innerHTML = '';
        var athis = this;
        function renderNode(item)
        {
            var node = document.createElement('li');
            node.className = item.type;
            node.innerHTML = "<span class='node'><span class='markerSpace'>&nbsp;</span></span><a href='" + item.ref + "' target='classFrame' title='" + item.summary + "'>" + item.localName + "</a>";
            var span = node.firstChild;
            var img = span.firstChild;
            athis.addListener(img, 'mousedown', athis.clicked);
            if (item._children)
            {
                item._children.sort(jgdoc.Searcher.sorter);
                node.className += (item == athis._root)? ' open' : ' closed';
                span.title = "Click to " + (item != athis._root? 'expand' : 'collapse');
                var subnode = document.createElement("ul");
                subnode.className = 'contents';
                for (var i = 0; i < item._children.length; i++)
                {
                    var child = renderNode(item._children[i]);
                    subnode.appendChild(child);
                }
                node.appendChild(subnode);
            }
            else
            {
                node.className += ' leaf';
            }
            item._node = node;
            return node;
        }
        var root = renderNode(this._root);
        d.appendChild(root);
    },
    
    cancelEvent : function(event)
    {
        if (event.preventDefault)
        {
            event.preventDefault();
            event.stopPropagation();
        }
        else
        {
            event.preventDefault();
            event.stopPropagation();
        }
    },
    
    switchAll : function(doOpen)
    {
        var ac = doOpen? 'open' : 'closed';
        var rc = doOpen? 'closed' : 'open';
        
        var athis = this;
        
        function doSwitchNode(anode)
        {
            if (anode._children)
            {
                if (doOpen || anode != athis._root)
                {
                    athis.removeClass(anode._node, rc);
                    athis.addClass(anode._node, ac);
                }
                for (var i = 0; i < anode._children.length; i++)
                {
                    doSwitchNode(anode._children[i]);
                }
            }
        }
        doSwitchNode(this._root);
    },
    
    onOpenAll : function()
    {
        jgdoc.Searcher.switchAll(true);
    },
    
    onCloseAll : function()
    {
        jgdoc.Searcher.switchAll(false);
    }

};