Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Formatter/HTML/TreeFormatterJS.py
    1| # $Id: TreeFormatterJS.py,v 1.3 2001/06/26 04:32:16 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: TreeFormatterJS.py,v $
   23| # Revision 1.3  2001/06/26 04:32:16  stefan
   24| # A whole slew of changes mostly to fix the HTML formatter's output generation,
   25| # i.e. to make the output more robust towards changes in the layout of files.
   26| #
   27| # the rpm script now works, i.e. it generates source and binary packages.
   28| #
   29| # Revision 1.2  2001/06/21 01:17:27  chalky
   30| # Fixed some paths for the new dir structure
   31| #
   32| # Revision 1.1  2001/06/05 10:43:07  chalky
   33| # Initial TreeFormatter and a JS version
   34| #
   35| # Revision 1.3  2001/04/17 13:36:10  chalky
   36| # Slight enhancement to JSTree and derivatives, to use a dot graphic for leaves
   37| #
   38| # Revision 1.2  2001/02/06 18:06:35  chalky
   39| # Added untested compatability with IE4 and Navigator 4
   40| #
   41| # Revision 1.1  2001/02/06 05:12:46  chalky
   42| # Added JSTree class and FileTreeJS and modified ModuleListingJS to use JSTree
   43| #
   44| #
   45| 
   46| import os
   47| 
   48| from Synopsis.Core import AST
   49| import Page
   50| from core import config
   51| from Tags import *
   52| 
   53| 
   54| from TreeFormatter import TreeFormatter
   55| 
   56| #The javascript that goes up the top
   57| top_js = """<script language="JavaScript1.2"><!--
   58| var isNav4 = false, isIE4 = false;
   59| if (parseInt(navigator.appVersion.charAt(0)) == 4) {
   60|     isNav4 = (navigator.appName == "Netscape") ? true : false;
   61| } else if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
   62|     isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
   63| }
   64| var isMoz = (isNav4 || isIE4) ? false : true;
   65| 
   66| showImage = new Image(); hideImage = new Image();
   67| function init_tree(show_src, hide_src) {
   68|     showImage.src = show_src; hideImage.src = hide_src;
   69| }
   70| function toggle(id) {
   71|     if (isMoz) {
   72|         section = document.getElementById(id);
   73|         image = document.getElementById(id+"_img");
   74|         if (section.style.display == "none") {
   75|             section.style.display = "";
   76|             image.src = showImage.src;
   77|         } else {
   78|             section.style.display = "none";
   79|             image.src = hideImage.src;
   80|         }
   81|     } else if (isIE4) {
   82|         section = document.items[id];
   83|         image = document.images[id+"_img"];
   84|         if (section.style.display == "none") {
   85|             section.style.display = "";
   86|             image.src = showImage.src;
   87|         } else {
   88|             section.style.display = "none";
   89|             image.src = hideImage.src;
   90|         }
   91|     } else if (isNav4) {
   92|         section = document.items[id];
   93|         image = document.images[id+"_img"];
   94|         if (section.display == "none") {
   95|             section.style.display = "";
   96|             image.src = showImage.src;
   97|         } else {
   98|             section.display = "none";
   99|             image.src = hideImage.src;
  100|         }
  101|     }
  102| }
  103| var tree_max_node = 0;
  104| function openAll() {
  105|     for (i = 1; i <= tree_max_node; i++) {
  106|         id = "tree"+i;
  107|         section = document.getElementById(id);
  108|         image = document.getElementById(id+"_img");
  109|         section.style.display = "";
  110|         image.src = showImage.src;
  111|     }
  112| }
  113| function closeAll() {
  114|     for (i = 1; i <= tree_max_node; i++) {
  115|         id = "tree"+i;
  116|         section = document.getElementById(id);
  117|         image = document.getElementById(id+"_img");
  118|         section.style.display = "none";
  119|         image.src = hideImage.src;
  120|     }
  121| }
  122| 
  123| init_tree("%s", "%s");
  124| --></script>
  125| """
  126| # The HTML for one image. %s's are 2x the same id string and the image
  127| img_html = """<a href="javascript:toggle('%s');"
  128| >%s</a>"""
  129| 
  130| class TreeFormatterJS (TreeFormatter):
  131|     """Javascript trees. The trees have expanding and
  132|     collapsing nodes. call js_init() with the button images and default
  133|     open/close policy during process"""
  134| 
  135|     def __init__(self, page):
  136|         TreeFormatter.__init__(self, page)
  137|         self.__id = 0
  138|         self.__open_img = ''
  139|         self.__close_img = ''
  140|         self.__leaf_img = ''
  141| 
  142|         share = config.datadir
  143|         self.js_init(os.path.join(share, 'syn-down.png'),
  144|                      os.path.join(share, 'syn-right.png'),
  145|                      os.path.join(share, 'syn-dot.png'),
  146|                      'tree_%s.png', 0)
  147|         
  148|     def getId(self):
  149|         self.__id = self.__id + 1
  150|         return "tree%d"%self.__id
  151| 
  152|     def js_init(self, open_img, close_img, leaf_img, base, default_open=1):
  153|         """Initialise the JSTree page. This method copies the files to the
  154|         output directory and stores the values given.
  155|         @param open_img             filename of original op
  156|         @param close_img     filename of original close image
  157|         @param base filename with a %s for open/close images, eg "tree_%s.png"
  158|         @param default_open  true if sections are open by default
  159|         """
  160|         self.__open_img = open_img
  161|         self.__close_img = close_img
  162|         self.__leaf_img = leaf_img
  163|         self.__def_open = default_open
  164|         self.__base_open = base%'open'
  165|         self.__base_close = base%'close'
  166|         self.__base_leaf = base%'leaf'
  167|         # Copy images across
  168|         config.files.copyFile(open_img, config.basename + '/' + self.__base_open)
  169|         config.files.copyFile(close_img, config.basename + '/' + self.__base_close)
  170|         config.files.copyFile(leaf_img, config.basename + '/' + self.__base_leaf)
  171| 
  172|     def startTree(self):
  173|         """Writes the javascript"""
  174|         self.write(top_js%(
  175|             self.__base_open, self.__base_close
  176|         ))
  177| 
  178|     def formatImage(self, id, filename, alt_text=""):
  179|         """Returns the image element for the given image"""
  180|         # todo: resolve directory path
  181|         id = id and 'id="%s" '%id or ''
  182|         return '<img %sborder=0 src="%s" alt="%s">'%(id, filename, alt_text)
  183| 
  184|     def writeLeaf(self, item_text):
  185|         """Write a leaf node to the output at the current tree level."""
  186|         img = self.formatImage(None, self.__base_leaf, "leaf")
  187|         self.write(div('module-section', img+item_text))
  188| 
  189|     def writeNodeStart(self, item_text):
  190|         """Write a non-leaf node to the output at the current tree level, and
  191|         start a new level."""
  192|         # Get a unique id for this node
  193|         id = self.getId()
  194|         # Get the image for this node
  195|         if self.__def_open: img = self.formatImage(id, self.__base_open, 'node')
  196|         else: img = self.formatImage(id+"_img", self.__base_close, 'node')
  197|         # Get the scripted link for the image
  198|         img_link = img_html%(id, img)
  199|         # Write the item
  200|         self.write('<div class="module-section">%s%s'%(img_link, item_text))
  201|         # Start the (collapsible) section for the child nodes
  202|         if self.__def_open:
  203|             self.write('<div id="%s">'%id)
  204|         else:
  205|             self.write('<div id="%s" style="display:none;">'%id)
  206| 
  207|     def writeNodeEnd(self):
  208|         """Finish a non-leaf node, and close the current tree level."""
  209|         # Close the collapsible div, and the node's div
  210|         self.write('</div></div>')
  211| 
  212|     def endTree(self):
  213|         """Writes the end of the tree."""
  214|         self.write(self.js_end%self.__id)
  215|         self.write(self.html_buttons)
  216| 
  217|     js_end = """<script language="JavaScript1.2"><!--
  218|     tree_max_node = %d; // --></script>"""
  219|     html_buttons = """
  220|     <div><a href="javascript:openAll()">Open All</a> | 
  221|     <a href="javascript:closeAll()">Close All</a></div>
  222|     """
  223|