Attachment 'CollapsibleSection-1.1.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 
  15     @license: GNU GPL, see COPYING for details.
  16     @author: Andy Doan <andy.doan@linaro.org>
  17 """
  18 
  19 from MoinMoin import wikiutil
  20 from MoinMoin.macro import Include
  21 
  22 section_js = """
  23 <style type="text/css">
  24 .CollapsibleSection {cursor: pointer;}
  25 </style>
  26 <script type='text/javascript'>
  27     function toggle_viz(id) {
  28         div = document.getElementById(id);
  29         h2 = document.getElementById('h2_'+id);
  30         if (div.style.display == 'none' ) {
  31             div.style.display = 'block';
  32             //strip the - from the old text
  33             old = h2.innerHTML.substring(2);
  34             h2.innerHTML = "- " + old;
  35         }
  36         else {
  37             div.style.display = 'none';
  38             //strip the "- " from the old text
  39             old = h2.innerHTML.substring(2)
  40             h2.innerHTML = "+ " + old;
  41         }
  42     }
  43 </script>
  44 """
  45 section_template = """
  46 <%s class='CollapsibleSection' id='h2_%s' onclick='toggle_viz("%s")'>%s</%s>
  47 <div id='%s'>
  48 %s
  49 </div>
  50 """
  51 
  52 section_hide_template = """
  53 <script type='text/javascript'>
  54 toggle_viz('%s');
  55 </script>
  56 """
  57 
  58 section_base = 'CollapsibleSection'
  59 
  60 def macro_CollapsibleSection(macro, title, page, visible=False, header='h2'):
  61     request = macro.request
  62     idx = request.uid_generator(section_base)
  63     html = ''
  64     if idx == section_base:
  65         #this is the first call to this macro, include the main JS code
  66         html = section_js
  67 
  68     if header is None:
  69         header = 'h2'
  70 
  71     title = "- %s" % title
  72     page_html = Include.execute(macro, page)
  73     html += section_template % (header, idx, idx, title, header, idx, page_html)
  74     if not visible:
  75         hide_html = section_hide_template % idx
  76         html += hide_html
  77     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.