Attachment 'jQuery_0_1.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - jQuery macro
   4 
   5     This macro output proper jquery.js javascript include tag.
   6     
   7     @copyright: 2008 Jiang Xin <worldhello.net@gmail.com>
   8     @license: GNU GPL, see COPYING for details.
   9 """
  10 
  11 from MoinMoin import wikiutil
  12 from inspect import isfunction, isclass, ismethod
  13 
  14 _aliasname = {
  15     'demo': 'example',
  16     'toggle': 'animatedcollapse',
  17     }
  18 
  19 _sysmsg = '<p><strong class="%s">%s</strong></p>'
  20 
  21 def externalScript(request, name):
  22     """ Format external script html """
  23     src = '%s/common/js/%s.js' % (request.cfg.url_prefix_static, name)
  24     return '<script type="text/javascript" src="%s"></script>' % src
  25 
  26 
  27 class jQuery(object):
  28     def __init__(self, macro, args):
  29         self.macro = macro
  30         self.args = args
  31     
  32     def renderInPage(self):
  33         request = self.macro.request
  34         _ = request.getText
  35         args = self.args
  36         result = []
  37         if not args:
  38             result.append(externalScript(request, 'jquery'))
  39         else: # call with argument
  40             if ',' in args:
  41                 name, args = args.split(',',1)
  42             else:
  43                 name = args
  44                 args = u''
  45             name = name.strip().lower()
  46             aliasname = _aliasname.get(name, name)
  47             
  48             has_function = False
  49             for prefix in ['jquery_', 'fn_', 'class_']:
  50                 for item in [aliasname, name]:
  51                     try:
  52                         function = eval(prefix+item)
  53                     except:
  54                         continue
  55                     if isfunction(function) or ismethod(function) or isclass(function):
  56                         has_function = True
  57                         break
  58             if has_function:
  59                 result.append(wikiutil.invoke_extension_function(request, function, args, fixed_args=[request]))
  60             elif args:
  61                 return (_sysmsg % ('error', _('Invalid jQuery arguments "%s, %s"!')) % (name, args))
  62             else:
  63                 result.append(externalScript(request, 'jquery'))
  64                 result.append(externalScript(request, 'jquery_'+aliasname))
  65 
  66         result = '\n'.join(result)
  67         return result
  68         
  69 def execute(macro, args):
  70     return jQuery(macro, args).renderInPage()
  71 
  72 
  73 def jquery_animatedcollapse(request, action="init", id="", _trailing_args=[], _kwargs={}):
  74     """
  75     _trailing_args is a list of IDs...
  76     _kwargs is a dict of key/values...
  77     
  78     <<jQuery(toggle)>>, <<jQuery(toggle, init)>> : toggle initialized
  79         <script ... src='jquery.js'></script>
  80         <script ... src='jquery_animatedcollapse.js'></script>
  81         <script ...>animatedcollapse.init();</script>
  82     
  83     <<jQuery(toggle, set, #id|.class, fade=1,hide=1,group=demo,persist=1)>> : toggle initialized
  84         <script ...>animatedcollapse.addDiv('.demo1', 'fade=1,hide=1,group=demo,persist=1'); </script>
  85     
  86     <<jQuery(toggle, show, #id|.class, type=radio|button|link, name=name, text=text)>>
  87         <a href="javascript:animatedcollapse.show('.class3')">Slide Down</a>
  88     <<jQuery(toggle, hide, #id|.class, type=radio|button|link, name=name, text=text)>>
  89         <a href="javascript:animatedcollapse.hide('.class3')">Slide Up</a>
  90     <<jQuery(toggle, toggle, #id|.class, type=radio|button|link, name=name, text=text)>>
  91         <a href="javascript:animatedcollapse.toggle('.class3')"><img src="http://i25.tinypic.com/wa0img.jpg" border="0" /></a>
  92     """
  93     _ = request.getText
  94     result = []
  95     if id:
  96         if _trailing_args:
  97             id = """['%s',%s]""" % ( id, ','.join(["'%s'" % i for i in _trailing_args]) )
  98             _trailing_args = []
  99         else:
 100             id = "'%s'" % id
 101     if not action or action == "init":
 102         result.append(externalScript(request, 'jquery'))
 103         result.append(externalScript(request, 'jquery_animatedcollapse'))
 104         result.append("""<script type="text/javascript">animatedcollapse.init();</script>""")
 105     elif action == "set":
 106         arguments = ','.join(['%s=%s' % (k,_kwargs[k]) for k in _kwargs])
 107         result.append("""<script type="text/javascript">animatedcollapse.addDiv(%(id)s, '%(args)s'); </script>"""
 108                       % { 'id': id, 'args': arguments } )
 109     # display show, hide, toggle link/button/radio
 110     else:
 111         # type: link, button, radio
 112         if _kwargs.has_key('type'):
 113             type = _kwargs['type']
 114         else:
 115             type = 'link'
 116         
 117         # text: text for link, button or radio
 118         if _kwargs.has_key('text'):
 119             link_text = _kwargs['text']
 120         else:
 121             link_text = u''
 122         
 123         # name for radio/button
 124         if _kwargs.has_key('name'):
 125             link_name = 'name="%s"' % _kwargs['name']
 126         else:
 127             link_name = ""
 128     
 129         # 
 130         if type == 'button':
 131             output = u"""<span class="toggle"><input type="button" %(name)s value="%(text)s" onclick="javascript:animatedcollapse.%(action)s(%(id)s)"></span>"""
 132         elif type == 'radio':
 133             output = u"""<span class="toggle"><input type="radio"  %(name)s value="%(text)s" onclick="javascript:animatedcollapse.%(action)s(%(id)s)">%(text)s</span>"""
 134         else:
 135             output = u"""<span class="toggle"><a href="javascript:animatedcollapse.%(action)s(%(id)s)">%(text)s</a></span>"""
 136         
 137         if action == "hide":
 138             link_text = link_text or _('Hide')
 139             result.append(output % {'action': action, 'name': link_name, 'id':id, 'text':link_text})
 140         elif action == "show":
 141             link_text = link_text or _('Hide')
 142             result.append(output % {'action': action, 'name': link_name, 'id':id, 'text':link_text})
 143         elif action == "toggle":
 144             link_text = link_text or _('Hide')
 145             result.append(output % {'action': action, 'name': link_name, 'id':id, 'text':link_text})
 146         else:
 147             return (_sysmsg % ('error', _('Unknown action: %s!') % (action)))
 148     return u'\n'.join(result)
 149     
 150 # vim:ts=4:sw=4:et

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] (2010-06-04 08:50:57, 32.6 KB) [[attachment:jQuery-20100323.patch.gz]]
  • [get | view] (2008-10-16 16:18:48, 5.9 KB) [[attachment:jQuery_0_1.py]]
  • [get | view] (2008-10-16 16:20:23, 7.1 KB) [[attachment:jquery_animatedcollapse_2.01_p1.js]]
  • [get | view] (2008-10-16 16:21:07, 2.3 KB) [[attachment:jquery_example_0.1.js]]
  • [get | view] (2008-10-16 16:20:45, 0.2 KB) [[attachment:jquery_mark_0.1.js]]
 All files | Selected Files: delete move to page copy to page

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