Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/iostreams/doc/tree/tree.js @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

  • Property svn:executable set to *
File size: 12.8 KB
Line 
1// (C) Copyright Jonathan Turkanis 2003.
2// Distributed under the Boost Software License, Version 1.0. (See accompanying
3// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4
5//--------------Event-handlers------------------------------------------------//
6
7function toggle(id) { get_tree().find(id).toggle(); }
8function blur_tree() 
9{ 
10    if (  window.event && 
11          window.event.srcElement && 
12          window.event.srcElement.blur &&
13          window.event.srcElement != document.body  )
14        window.event.srcElement.blur();
15    else if (target_frame()) 
16        target_frame().window.focus(); 
17}
18document.onclick = blur_tree;
19
20//--------------Netscape 4.x-specific-----------------------------------------//
21
22window.saved_width = window.outerWidth;
23function reload_tree() 
24{   
25    if (window.outerWidth != window.saved_width) 
26        window.location.reload();
27}
28if (window.Event && Event.RESIZE) {
29    window.captureEvents(Event.RESIZE);
30    window.onResize = reload_tree;
31}
32
33//--------------Functions for browser-sniffing--------------------------------//
34
35function major_version(app)
36{
37    var index = navigator.userAgent.indexOf(app);
38    if (index == -1)
39        return -1;
40    return parseInt(navigator.userAgent.charAt(index + app.length + 1));
41}
42function dom_support()
43{
44    if (dom_support.cache == null)
45        dom_support.cache = dom_support_impl();
46    return dom_support.cache;
47}
48function dom_support_impl()
49{
50    var version;
51    if ( (version = major_version("Mozilla")) != -1 &&
52         navigator.userAgent.indexOf("compatible") == -1 ) 
53        return version > 4;
54    if ((version = major_version("Opera")) != -1) 
55        return version > 6;
56    if ((version = major_version("Konqueror")) != -1) 
57        return version > 2;
58    if ((version = major_version("Links")) != -1) 
59        return false;
60    return document.getElementById || document.all;
61}
62
63//--------------Utility functions---------------------------------------------//
64
65function target_frame() 
66{ 
67    return get_tree() ? top.frames[get_tree().target] : null; 
68}
69function get_tree() { return document.tree_control; }
70function static_display() { return !dom_support() || get_tree().dump_html; }
71function elt_by_id(id)
72{
73    return document.all ?
74        document.all[id] :
75        document.getElementById ?
76            document.getElementById(id) :
77            null;
78}
79function replace_query(url, query)
80{
81    var pos;
82    if ((pos = url.indexOf("?")) != -1)
83        url = url.substring(0, pos);
84    return url + "?" + query;
85}
86
87//--------------Functions for HTML-generation---------------------------------//
88
89function icon_char(state)
90{
91    return state == tree_node.expanded ?
92                "-" :
93                state == tree_node.collapsed ?
94                    "+" :
95                    " ";
96}
97function html_list(id, display, margin)
98{ 
99    return "<div id='" + id + "' style='white-space:nowrap;display:" + 
100           display + "'>";
101}
102function html_list_item(content)
103{ 
104    return "\n<div class='tree-item'>" + content + "</div>"; 
105}
106function html_anchor(content, cl, href, target)
107{ 
108    return "<A class='" + cl + "' onfocus='blur_tree()" + 
109           "' href='" +  href + "'" + 
110           (target ? " target='" + target + "'" : "") +
111           ">" + content + "</A>"; 
112}
113
114//--------------Definition of class tree_node---------------------------------//
115
116function tree_node__add(text_or_node, link) 
117{     
118    if (this.state == tree_node.neutral)
119        this.state = tree_node.collapsed;
120    var k = text_or_node.length != null ?
121        new tree_node(text_or_node, link) :
122        text_or_node;
123    k.mom = this;
124    if (this.kids == null)
125        this.kids = new Array();
126    this.kids[this.kids.length] = k; 
127    return k;
128}
129function tree_node__level() 
130{ 
131    var level;
132    var node;
133    for (node = this.mom, level = -1; node; node = node.mom, ++level)
134        ;
135    return level;
136}
137function tree_node__parent() { return this.mom; }
138function tree_node__print() 
139{
140    var icon = 
141        !static_display() ? 
142            "<span style='font-family:monospace' class='tree-icon' id='icon" + 
143                 this.id + "'>" + icon_char(this.state) + "</span>&nbsp;&nbsp;" :
144            "";
145    var handler = 
146        !static_display() && this.kids ?
147            "javascript:toggle(\"id" + this.id + "\")" :
148            "";
149    var text = "<span class='tree-text'>" + this.text + "</span>"
150    var tree = get_tree();
151    var indent = tree.indent * this.level();
152    return html_list_item(
153               "<div style='margin-left:" + (2 * indent) + 
154               ";text-indent:-" + indent + "'>" +
155               ( !tree.dump_html ?
156                     this.kids ?
157                         html_anchor(icon, "tree-icon", handler) :
158                         icon :
159                     "" ) +
160               ( tree.numbered ? 
161                     "" + "<span class='tree-label'>" + 
162                         this.id.substring(1) + "</span>" : 
163                     "" ) +
164               "&nbsp;&nbsp;" +
165               ( this.link ?
166                     html_anchor( text, "tree-text", this.link, 
167                                  tree.target ) :
168                     text ) + 
169               "</div>" + 
170               this.print_kids()
171           );
172}
173function tree_node__print_kids(margin) 
174{
175    var result = "";
176    if (this.kids) {
177        if (margin == null)
178            margin = get_tree().indent;
179        result += html_list( "list" + this.id,
180                             this.state == tree_node.collapsed && 
181                             !static_display() 
182                                ? "none" : "",
183                             margin );
184        for (var z = 0; z < this.kids.length; ++z) {
185            var k = this.kids[z];
186            k.id = this.id + "." + (z + 1);
187            result += k.print();
188        }
189        result += "</div>";
190    }
191    return result;
192}
193function tree_node__toggle(expand) 
194{
195    if ( static_display() ||
196         this.kids == null || 
197         expand != null && expand == 
198            (this.state == tree_node.expanded) )
199    {
200        return;
201    }
202    this.state = 
203        this.state == tree_node.expanded ?
204            tree_node.collapsed :
205            tree_node.expanded;
206    elt_by_id("icon" + this.id).innerHTML = 
207        icon_char(this.state);
208    elt_by_id("list" + this.id).style.display = 
209        this.state == tree_node.expanded ?
210            "" : 
211            "none";
212}
213function add_methods(obj)
214{
215    obj.add = tree_node__add;
216    obj.level = tree_node__level;
217    obj.parent = tree_node__parent;
218    obj.print = tree_node__print;
219    obj.print_kids = tree_node__print_kids;
220    obj.toggle = tree_node__toggle;
221}
222function tree_node(text, link) 
223{
224    // Member data
225    this.text = text;
226    this.link = link;
227    this.mom = null;
228    this.kids = null;
229    this.id = null;
230    this.state = 0; // Neutral.
231
232    if (!add_methods.prototype)
233        add_methods(this);
234}
235tree_node.neutral = 0;
236tree_node.expanded = 1;
237tree_node.collapsed = 2;
238if (tree_node.prototype)
239    add_methods(tree_node.prototype);
240               
241//--------------Definition of class tree_control------------------------------//
242
243function tree_control__add(text, link) 
244{ 
245    return this.root.add(text, link); 
246}
247function tree_control__draw() 
248{ 
249    var tree    = get_tree();
250    var dom     = dom_support();
251    var element = dom ? elt_by_id('tree_control') : null;
252    if (element || !dom || tree.drawn) {
253        var html = tree.html();
254        if (tree.dump_html) {
255            var pat = new RegExp("<", "g");
256            html = "<pre>" + html.replace(pat, "&lt;") + "</pre>";
257            if (document.body.innerHTML)
258                document.body.innerHTML = html;
259            else 
260                document.write(html);
261        } else if (dom) {
262            element.innerHTML = html;
263        } else {
264            document.open();
265            document.write( 
266                "<body>" + html +
267                ( major_version("MSIE") == 3 ?
268                     "<noscript>" :
269                     document.layers ?
270                         "<layer visibility='hide'>" :
271                         "<table width=0 height=0 style='" + 
272                         "visibility:hidden;display:none'><tr><td>" ) 
273            );
274            document.close();
275        } 
276        tree.drawn = true;
277        tree.load();
278    } else { 
279        var t = navigator.userAgent.indexOf("Clue") != -1 ? 500 : 100;
280        setTimeout("tree_control__draw()", t); 
281    }
282}
283function tree_control__find(id) 
284{ 
285    var indices = id.split(".");
286    var node = this.root;
287    for (var z = 1; z < indices.length; ++z) 
288        node = node.kids[indices[z] - 1];
289    return node;
290}
291function tree_control__html()
292{
293    return  "<table><tr><td align='left'><table width=150><tr><td>" +
294            "<h1 class=tree-caption>" + this.caption + "</h1></td></tr>" +
295            ( !static_display() ? 
296                  "<tr><td><p class='tree-sync'><a title='reload current " + 
297                      "page with a url suitable for bookmarking' class=" +
298                      "'tree-sync' href='javascript:get_tree().sync()'>" + 
299                      "[link to this page]</a></p></td></tr>" :
300                  "" ) + 
301            "</table></td></tr><tr><td>" + this.root.print_kids(0) + 
302            "</td></tr></table>";
303}
304function load_target(url)
305{
306    var target;
307    if ((target = target_frame()) && target.location.href != url)
308        target.location.replace(url);
309    else {
310        setTimeout("load_target('" + url + "')", 100);
311    }
312}
313function tree_control__load() 
314{ 
315    var query;
316    if ((query = top.location.search).length == 0) 
317        return;
318    query = query.substring(1);
319    var eq;
320    if ((eq = query.indexOf("=")) != -1) {
321        if (query.substring(0, 4) == "page") {
322            load_target(unescape(query.substring(eq + 1)));
323            return;
324        }
325        query = query.substring(eq + 1);
326    }
327    var indices = query.split(".");
328    if (!indices.length)
329        return;
330    this.reset();
331    var node = this.root;
332    for (var z = 0; z < indices.length; ++z) {
333        var i = parseInt(indices[z]) - 1;
334        if (!node.kids || i < 0 || node.kids.length <= i)
335            break;
336        node = node.kids[i];
337        node.toggle(/*z != indices.length - 1*/);
338    }
339    if (node.link)
340        load_target(node.link);
341}
342function tree_control__recurse(op) 
343{
344    var stack = new Array();
345    stack[stack.length] = this.root;
346    while (stack.length) {
347        var node = stack[stack.length - 1];
348        stack.length -=1 ; // Konqueror 2.
349        op(node);
350        if (node.kids)
351            for (var z = 0; z < node.kids.length; ++z)
352                stack[stack.length] = node.kids[z];
353    }
354}
355function tree_control__reset() 
356{ 
357    if (!dom_support())
358        return;
359    this.recurse(new Function("x", "if (x.parent()) x.toggle(false);"));
360}
361function sync_node(node)
362{
363    if (!node.link)
364        return;
365    var tgt = target_frame().location.href;
366    var pos;
367    if ((pos = tgt.indexOf("?")) != -1)
368        tgt = tgt.substring(0, pos);
369    if (node.link.indexOf("://") != -1) {
370        if (node.link != tgt) 
371            return;
372    } else {
373        var base = window.location.href;
374        if ((pos = base.lastIndexOf("/")) != -1)
375            base = base.substring(0, pos + 1);
376        if (base + node.link != tgt)
377            return;
378    }
379    window.success = true;
380    var href = replace_query( get_tree().top_url,
381                              "path=" + node.id.substring(1) );
382    top.location.replace(href);
383}
384function tree_control__sync() 
385{ 
386    if (!dom_support() || self == top)
387        return;
388    window.success = false;
389    get_tree().recurse(sync_node);
390    if (!window.success)
391        top.location.replace( 
392            replace_query( get_tree().top_url,
393                           "page=" + escape(target_frame().location.href) )
394        );
395}
396function tree_control(target) 
397{
398    // Member data
399    this.root = new tree_node("");
400    this.target = target ? target : "_self";
401    this.dump_html = false;
402    this.caption = "Contents";
403    this.numbered = true;
404    this.indent = 15;
405    this.drawn = false;
406    this.top_url = top.location.href; // For Opera.
407
408    this.root.state = tree_node.expanded;
409    this.root.id = "";
410
411    // Member functions
412    this.add = tree_control__add;
413    this.draw = tree_control__draw;
414    this.find = tree_control__find;
415    this.html = tree_control__html;
416    this.load = tree_control__load;
417    this.recurse = tree_control__recurse;
418    this.reset = tree_control__reset;
419    this.sync = tree_control__sync;
420    document.tree_control = this;
421}
422tree_control.sync = tree_control__sync;
Note: See TracBrowser for help on using the repository browser.