File: Synopsis/Formatters/HTML/Views/Tree.py
 1#
 2# Copyright (C) 2000 Stephen Davies
 3# Copyright (C) 2000 Stefan Seefeld
 4# All rights reserved.
 5# Licensed to the public under the terms of the GNU LGPL (>= 2),
 6# see the file COPYING for details.
 7#
 8
 9from Synopsis import config
10from Synopsis.Processor import Parameter
11from Synopsis.Formatters.HTML.View import View
12from Synopsis.Formatters.HTML.Tags import *
13import os
14
15class Tree(View):
16    """View that makes Javascript trees. The trees have expanding and
17    collapsing nodes. call js_init() with the button images and default
18    open/close policy during process"""
19
20    def register(self, frame):
21
22        super(Tree, self).register(frame)
23        self.__id = 0
24        share = config.datadir
25        open_img = os.path.join(share, 'syn-down.png')
26        close_img = os.path.join(share, 'syn-right.png')
27        leaf_img = os.path.join(share, 'syn-dot.png')
28        self.tree_open = 'tree_open.png'
29        self.tree_close = 'tree_close.png'
30        self.tree_leaf = 'tree_leaf.png'
31        # Copy resources across
32        self.directory_layout.copy_file(open_img, self.tree_open)
33        self.directory_layout.copy_file(open_img, self.tree_open)
34        self.directory_layout.copy_file(close_img, self.tree_close)
35        self.directory_layout.copy_file(leaf_img, self.tree_leaf)
36
37    def get_id(self):
38
39        self.__id += 1
40        return 'tree%d'%self.__id
41
42    def write_leaf(self, text):
43        """Write a leaf node to the output at the current tree level."""
44        i = img(src=self.tree_leaf, alt='leaf')
45        self.write(div(i + text, class_='module-section') + '\n')
46
47    def write_node_start(self, text):
48        """Write a non-leaf node to the output at the current tree level, and
49        start a new level."""
50        # Get a unique id for this node
51        id = self.get_id()
52        # Get the image for this node
53        node = img(id='%s_img'%id, src=self.tree_close, alt='node', border='0')
54        # Get the scripted link for the image
55        link = href("javascript:tree_node_toggle('%s');"%id, node)
56        # Write the item
57        self.write('<div class="module-section">%s%s'%(link, text))
58        # Start the (collapsible) section for the child nodes
59        self.write('<div id="%s" style="display:none;">\n'%id)
60
61    def write_node_end(self):
62        """Finish a non-leaf node, and close the current tree level."""
63
64        self.write('</div>\n</div>\n')
65
66    def end_tree(self):
67        """Writes the end of the tree."""
68
69        js_end = """<script type="text/javascript" language="JavaScript1.2"><!--
70        tree_max_node = %d; // --></script>"""
71
72        self.write(js_end%self.__id)
73
74        self.write(div(href('javascript:tree_open_all()', 'Open All') +
75                       href('javascript:tree_close_all()', 'Close All'),
76                       class_='tree-navigation'))
77
78