| Home | Trees | Indices | Help |
|
|---|
|
|
1 # -*- coding: UTF-8 -*-
2 """TurboGears widgets for displaying fancy flash message boxes."""
3 __docformat__ = 'restructuredtext'
4
5 __all__ = ['FancyFlashWidget', 'FancyFlashDesc']
6
7 import pkg_resources
8
9 from turbogears.widgets import Widget, WidgetDescription, JSSource, JSLink, \
10 CSSLink, js_location, register_static_directory
11
12 from simplejson import loads
13
14 import jslibs
15
16 static_dir = pkg_resources.resource_filename("fancyflash.widgets", "static")
17 register_static_directory("fancyflash", static_dir)
18
19 fancyflash_css = [CSSLink("fancyflash", "css/fancyflash.css", media="screen")]
20 fancyflash_js = [
21 jslibs.mochikit,
22 jslibs.events,
23 JSLink("fancyflash", "javascript/fancyflash.js"),
24 JSSource("write_stylesheet();", js_location.head)
25 ]
26
28 # These substitutions are copied from cgi.escape().
29 s = s.replace("&", "&") # Must be done first!
30 s = s.replace("<", "<")
31 s = s.replace(">", ">")
32 if quote:
33 s = s.replace('"', """)
34 return s
35
37 """A message box with a status icon and background color based on status.
38
39 With JavaScript support enabled, can be positioned absolutely and on top
40 of the normal page content. The user can then click the message to make it
41 go away, or the message disappears after a cretain timeout. If JavaScript
42 is disabled. The message box will be display diretcly where the widget is
43 inserted in the template.
44 """
45
46 name = "statusmessage"
47 template = """
48 <div xmlns:py="http://purl.org/kid/ns#" id="statusmessage">
49 <!--[if gte ie 5.5000]>
50 <link rel="stylesheet" type="text/css"
51 href="/tg_widgets/fancyflash/css/ie.css">
52 <![endif]-->
53 <div py:if="message" class="${status}" py:content="XML(message)"></div>
54 <script py:if="timeout" py:replace="script()" />
55 </div>
56 """
57 params = ['message', 'status', 'timeout']
58 message = ''
59 status = 'info'
60 timeout = 0
61 css = fancyflash_css
62 javascript = fancyflash_js
63
64 params_doc = {
65 'message': 'The message test to display.',
66 'status': 'The status name, which will be used as the CSS class '
67 'of the inner DIV element.',
68 'timeout': 'The number of seconds after which the message will fade '
69 'out. Needs JavaScript enabled to work. Default is 0, i,e, the '
70 'message will stay until the user clicks on it.',
71 }
72
74 """Decode tg_flash string passed as value and set params from result.
75 """
76 super(FancyFlashWidget, self).update_params(params)
77 params.update(self._parse_tg_flash(params.get('value')))
78
80 """Try to decode given string as JSON and extract widget params."""
81 params = dict()
82 if tg_flash:
83 try:
84 tg_flash = loads(tg_flash)
85 except:
86 tg_flash = dict(msg=tg_flash)
87 else:
88 if not (isinstance(tg_flash, dict) and tg_flash.has_key('msg')):
89 if isinstance(tg_flash, basestring):
90 tg_flash = dict(msg=tg_flash)
91 else:
92 tg_flash = dict(msg=str(tg_flash))
93 msg = tg_flash.get('msg')
94 if msg:
95 if not tg_flash.get('allow_html', False):
96 msg = _escape(msg)
97 params['message'] = msg
98 params['status'] = tg_flash.get('status', 'info')
99 try:
100 params['timeout'] = int(tg_flash.get('timeout', 0))
101 except ValueError: pass
102 else:
103 if params['timeout'] > 0:
104 params['script'] = JSSource("setHideTimeout('%s', %s);" \
105 % (self.name, params['timeout']))
106 return params
107
109 name = "Fancy Flash Message"
110 for_widget = FancyFlashWidget()
111 template = """\
112 <div>
113 ${for_widget("This is a Fancy Flash Message!", status="info",
114 timeout="10")}
115 </div>
116 """
117
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Feb 4 18:13:32 2008 | http://epydoc.sourceforge.net |