Attachment 'CollapsibleSection-1.2.py'

Download

   1 """
   2     MoinMoin - Collapsible Section  macro
   3 
   4     This macro provides a way to display the contents of another page into
   5     an HTML div that can be collapsed and expanded. Its useful for including
   6     supplemental information in a page that normal users might not care about.
   7 
   8     Example Usage:
   9      collapsed by default:
  10         <<CollapsibleSection("Title for Section", "PageName")>>
  11      expanded by default:
  12         <<CollapsibleSection("Title for Section", "PageName", 1)>>
  13         <<CollapsibleSection("Title for Section", "PageName", 1, "h3")>>
  14      set custom plus/minus signs:
  15         <<CollapsibleSection("Title for Section", "PageName", 1, "h3", plus=++, minus=--)>>
  16 
  17     @license: GNU GPL, see COPYING for details.
  18     @author: Andy Doan <andy.doan@linaro.org>
  19 """
  20 
  21 from MoinMoin import wikiutil
  22 from MoinMoin.macro import Include
  23 
  24 section_js = """
  25 <style type="text/css">
  26 .CollapsibleSection {cursor: pointer;}
  27 </style>
  28 <script type='text/javascript'>
  29     function toggle_viz(id) {
  30         div = document.getElementById(id);
  31         h2 = document.getElementById('h2_'+id);
  32         if (div.style.display == 'none' ) {
  33             div.style.display = 'block';
  34             //strip the - from the old text
  35             old = h2.innerHTML.substring(2);
  36             h2.innerHTML = "%s" + " " + old;
  37         }
  38         else {
  39             div.style.display = 'none';
  40             //strip the "- " from the old text
  41             old = h2.innerHTML.substring(2)
  42             h2.innerHTML = "%s" + " " + old;
  43         }
  44     }
  45 </script>
  46 """
  47 section_template = """
  48 <%s class='CollapsibleSection' id='h2_%s' onclick='toggle_viz("%s")'>%s</%s>
  49 <div id='%s'>
  50 %s
  51 </div>
  52 """
  53 
  54 section_hide_template = """
  55 <script type='text/javascript'>
  56 toggle_viz('%s');
  57 </script>
  58 """
  59 
  60 section_base = 'CollapsibleSection'
  61 
  62 def macro_CollapsibleSection(macro, title, page, visible=False, header='h2', plus='+', minus='-'):
  63     request = macro.request
  64     idx = request.uid_generator(section_base)
  65     html = ''
  66     if idx == section_base:
  67         #this is the first call to this macro, include the main JS code
  68         html = section_js % (minus, plus)
  69 
  70     if header is None:
  71         header = 'h2'
  72 
  73     title = "%s %s" % (minus, title)
  74     page_html = Include.execute(macro, page)
  75     html += section_template % (header, idx, idx, title, header, idx, page_html)
  76     if not visible:
  77         hide_html = section_hide_template % idx
  78         html += hide_html
  79     return macro.formatter.rawHTML(html)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-09-26 15:36:37, 2.2 KB) [[attachment:CollapsibleSection-1.1.py]]
  • [get | view] (2011-10-15 15:57:23, 2.4 KB) [[attachment:CollapsibleSection-1.2.py]]
  • [get | view] (2011-06-15 20:25:56, 2.1 KB) [[attachment:CollapsibleSection.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.