Modules |
Files |
Inheritance Tree |
Inheritance Graph |
Name Index |
Config
File: Synopsis/Formatter/HTML/JSTree.py
1| # $Id: JSTree.py,v 1.4 2001/07/05 05:39:58 stefan Exp $
2| #
3| # This file is a part of Synopsis.
4| # Copyright (C) 2000, 2001 Stephen Davies
5| # Copyright (C) 2000, 2001 Stefan Seefeld
6| #
7| # Synopsis is free software; you can redistribute it and/or modify it
8| # under the terms of the GNU General Public License as published by
9| # the Free Software Foundation; either version 2 of the License, or
10| # (at your option) any later version.
11| #
12| # This program is distributed in the hope that it will be useful,
13| # but WITHOUT ANY WARRANTY; without even the implied warranty of
14| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15| # General Public License for more details.
16| #
17| # You should have received a copy of the GNU General Public License
18| # along with this program; if not, write to the Free Software
19| # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20| # 02111-1307, USA.
21| #
22| # $Log: JSTree.py,v $
23| # Revision 1.4 2001/07/05 05:39:58 stefan
24| # advanced a lot in the refactoring of the HTML module.
25| # Page now is a truely polymorphic (abstract) class. Some derived classes
26| # implement the 'filename()' method as a constant, some return a variable
27| # dependent on what the current scope is...
28| #
29| # Revision 1.3 2001/04/17 13:36:10 chalky
30| # Slight enhancement to JSTree and derivatives, to use a dot graphic for leaves
31| #
32| # Revision 1.2 2001/02/06 18:06:35 chalky
33| # Added untested compatability with IE4 and Navigator 4
34| #
35| # Revision 1.1 2001/02/06 05:12:46 chalky
36| # Added JSTree class and FileTreeJS and modified ModuleListingJS to use JSTree
37| #
38| #
39|
40| import Page
41| from core import config
42| from Tags import *
43|
44| #The javascript that goes up the top
45| top_js = """<script language="JavaScript1.2"><!--
46| var isNav4 = false, isIE4 = false;
47| if (parseInt(navigator.appVersion.charAt(0)) == 4) {
48| isNav4 = (navigator.appName == "Netscape") ? true : false;
49| } else if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
50| isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
51| }
52| var isMoz = (isNav4 || isIE4) ? false : true;
53|
54| showImage = new Image(); hideImage = new Image();
55| function init_tree(show_src, hide_src) {
56| showImage.src = show_src; hideImage.src = hide_src;
57| }
58| function toggle(id) {
59| if (isMoz) {
60| section = document.getElementById(id);
61| image = document.getElementById(id+"_img");
62| if (section.style.display == "none") {
63| section.style.display = "";
64| image.src = showImage.src;
65| } else {
66| section.style.display = "none";
67| image.src = hideImage.src;
68| }
69| } else if (isIE4) {
70| section = document.items[id];
71| image = document.images[id+"_img"];
72| if (section.style.display == "none") {
73| section.style.display = "";
74| image.src = showImage.src;
75| } else {
76| section.style.display = "none";
77| image.src = hideImage.src;
78| }
79| } else if (isNav4) {
80| section = document.items[id];
81| image = document.images[id+"_img"];
82| if (section.display == "none") {
83| section.style.display = "";
84| image.src = showImage.src;
85| } else {
86| section.display = "none";
87| image.src = hideImage.src;
88| }
89| }
90| }
91| init_tree("%s", "%s");
92| --></script>
93| """
94| # The HTML for one image. %s's are 2x the same id string and the image
95| img_html = """<a href="javascript:toggle('%s');"
96| >%s</a>"""
97|
98| class JSTree (Page.Page):
99| """Page that makes Javascript trees. The trees have expanding and
100| collapsing nodes. call js_init() with the button images and default
101| open/close policy during process"""
102|
103| def __init__(self, manager):
104| Page.Page.__init__(self, manager)
105| self.__id = 0
106| self.__open_img = ''
107| self.__close_img = ''
108| self.__leaf_img = ''
109|
110| def getId(self):
111| self.__id = self.__id + 1
112| return "tree%d"%self.__id
113|
114| def js_init(self, open_img, close_img, leaf_img, base, default_open=1):
115| """Initialise the JSTree page. This method copies the files to the
116| output directory and stores the values given.
117| @param open_img filename of original op
118| @param close_img filename of original close image
119| @param base filename with a %s for open/close images, eg "tree_%s.png"
120| @param default_open true if sections are open by default
121| """
122| self.__open_img = open_img
123| self.__close_img = close_img
124| self.__leaf_img = leaf_img
125| self.__def_open = default_open
126| self.__base_open = base%'open'
127| self.__base_close = base%'close'
128| self.__base_leaf = base%'leaf'
129| # Copy images across
130| config.files.copyFile(open_img, self.__base_open)
131| config.files.copyFile(close_img, self.__base_close)
132| config.files.copyFile(leaf_img, self.__base_leaf)
133|
134| def start_file(self):
135| """Overrides start_file to add the javascript"""
136| Page.Page.start_file(self, headextra=top_js%(self.__base_open, self.__base_close))
137|
138| def formatImage(self, id, filename, alt_text=""):
139| """Returns the image element for the given image"""
140| # todo: resolve directory path
141| id = id and 'id="%s" '%id or ''
142| return '<img %sborder=0 src="%s" alt="%s">'%(id, filename, alt_text)
143|
144| def writeLeaf(self, item_text):
145| """Write a leaf node to the output at the current tree level."""
146| img = self.formatImage(None, self.__base_leaf, "leaf")
147| self.write(div('module-section', img+item_text))
148|
149| def writeNodeStart(self, item_text):
150| """Write a non-leaf node to the output at the current tree level, and
151| start a new level."""
152| # Get a unique id for this node
153| id = self.getId()
154| # Get the image for this node
155| if self.__def_open: img = self.formatImage(id, self.__base_open, 'node')
156| else: img = self.formatImage(id+"_img", self.__base_close, 'node')
157| # Get the scripted link for the image
158| img_link = img_html%(id, img)
159| # Write the item
160| self.write('<div class="module-section">%s%s'%(img_link, item_text))
161| # Start the (collapsible) section for the child nodes
162| if self.__def_open:
163| self.write('<div id="%s">'%id)
164| else:
165| self.write('<div id="%s" style="display:none;">'%id)
166|
167| def writeNodeEnd(self):
168| """Finish a non-leaf node, and close the current tree level."""
169| # Close the collapsible div, and the node's div
170| self.write('</div></div>')
171|
172|