comparison pylons_app/lib/helpers.py @ 288:cd2ee462fc2c

implemented yui tooltip, and added it into annotation and main page.
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 13 Jun 2010 23:14:04 +0200
parents ed7abf925696
children 237470e64bb8
comparison
equal deleted inserted replaced
287:373ddb868bd6 288:cd2ee462fc2c
7 from pygments import highlight as code_highlight 7 from pygments import highlight as code_highlight
8 from pylons import url, app_globals as g 8 from pylons import url, app_globals as g
9 from pylons.i18n.translation import _, ungettext 9 from pylons.i18n.translation import _, ungettext
10 from vcs.utils.annotate import annotate_highlight 10 from vcs.utils.annotate import annotate_highlight
11 from webhelpers.html import literal, HTML, escape 11 from webhelpers.html import literal, HTML, escape
12 from webhelpers.html.tools import *
12 from webhelpers.html.builder import make_tag 13 from webhelpers.html.builder import make_tag
13 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \ 14 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \
14 end_form, file, form, hidden, image, javascript_link, link_to, link_to_if, \ 15 end_form, file, form, hidden, image, javascript_link, link_to, link_to_if, \
15 link_to_unless, ol, required_legend, select, stylesheet_link, submit, text, \ 16 link_to_unless, ol, required_legend, select, stylesheet_link, submit, text, \
16 password, textarea, title, ul, xml_declaration 17 password, textarea, title, ul, xml_declaration, radio
17 from webhelpers.html.tools import auto_link, button_to, highlight, js_obfuscate, \ 18 from webhelpers.html.tools import auto_link, button_to, highlight, js_obfuscate, \
18 mail_to, strip_links, strip_tags, tag_re 19 mail_to, strip_links, strip_tags, tag_re
19 from webhelpers.number import format_byte_size, format_bit_size 20 from webhelpers.number import format_byte_size, format_bit_size
20 from webhelpers.pylonslib import Flash as _Flash 21 from webhelpers.pylonslib import Flash as _Flash
21 from webhelpers.pylonslib.secure_form import secure_form 22 from webhelpers.pylonslib.secure_form import secure_form
22 from webhelpers.text import chop_at, collapse, convert_accented_entities, \ 23 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
23 convert_misc_entities, lchop, plural, rchop, remove_formatting, \ 24 convert_misc_entities, lchop, plural, rchop, remove_formatting, \
24 replace_whitespace, urlify, truncate 25 replace_whitespace, urlify, truncate, wrap_paragraphs
25 26
26 27
27 #Custom helper here :) 28 #Custom helpers here :)
28 class _Link(object): 29 class _Link(object):
29 ''' 30 '''
30 Make a url based on label and url with help of url_for 31 Make a url based on label and url with help of url_for
31 @param label:name of link if not defined url is used 32 @param label:name of link if not defined url is used
32 @param url: the url for link 33 @param url: the url for link
36 if label is None or '': 37 if label is None or '':
37 label = url 38 label = url
38 link_fn = link_to(label, url(*url_, **urlargs)) 39 link_fn = link_to(label, url(*url_, **urlargs))
39 return link_fn 40 return link_fn
40 41
42 link = _Link()
41 43
42 class _GetError(object): 44 class _GetError(object):
43 45
44 def __call__(self, field_name, form_errors): 46 def __call__(self, field_name, form_errors):
45 tmpl = """<span class="error_msg">%s</span>""" 47 tmpl = """<span class="error_msg">%s</span>"""
46 if form_errors and form_errors.has_key(field_name): 48 if form_errors and form_errors.has_key(field_name):
47 return literal(tmpl % form_errors.get(field_name)) 49 return literal(tmpl % form_errors.get(field_name))
48 50
51 get_error = _GetError()
52
53 def recursive_replace(str, replace=' '):
54 """
55 Recursive replace of given sign to just one instance
56 @param str: given string
57 @param replace:char to find and replace multiple instances
58
59 Examples::
60 >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-')
61 'Mighty-Mighty-Bo-sstones'
62 """
63
64 if str.find(replace * 2) == -1:
65 return str
66 else:
67 str = str.replace(replace * 2, replace)
68 return recursive_replace(str, replace)
69
70 class _ToolTip(object):
71
72 def __call__(self, tooltip_title, trim_at=50):
73 """
74 Special function just to wrap our text into nice formatted autowrapped
75 text
76 @param tooltip_title:
77 """
78
79 return literal(wrap_paragraphs(tooltip_title, trim_at)\
80 .replace('\n', '<br/>'))
81
82 def activate(self):
83 """
84 Adds tooltip mechanism to the given Html all tooltips have to have
85 set class tooltip and set attribute tooltip_title.
86 Then a tooltip will be generated based on that
87 All with yui js tooltip
88 """
89
90 js = '''
91 YAHOO.util.Event.onDOMReady(function(){
92 function toolTipsId(){
93 var ids = [];
94 var tts = YAHOO.util.Dom.getElementsByClassName('tooltip');
95
96 for (var i = 0; i < tts.length; i++) {
97 //if element doesn not have and id autgenerate one for tooltip
98
99 if (!tts[i].id){
100 tts[i].id='tt'+i*100;
101 }
102 ids.push(tts[i].id);
103 }
104 return ids
105 };
106 var myToolTips = new YAHOO.widget.Tooltip("tooltip", {
107 context: toolTipsId(),
108 monitorresize:false,
109 xyoffset :[0,0],
110 autodismissdelay:300000,
111 hidedelay:5,
112 showdelay:20,
113 });
114
115 //Mouse subscribe optional arguments
116 myToolTips.contextMouseOverEvent.subscribe(
117 function(type, args) {
118 var context = args[0];
119 return true;
120 });
121
122 // Set the text for the tooltip just before we display it. Lazy method
123 myToolTips.contextTriggerEvent.subscribe(
124 function(type, args) {
125 var context = args[0];
126 var txt = context.getAttribute('tooltip_title');
127 this.cfg.setProperty("text", txt);
128 });
129 });
130 '''
131 return literal(js)
132
133 tooltip = _ToolTip()
134
49 class _FilesBreadCrumbs(object): 135 class _FilesBreadCrumbs(object):
50 136
51 def __call__(self, repo_name, rev, paths): 137 def __call__(self, repo_name, rev, paths):
52 url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))] 138 url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))]
53 paths_l = paths.split('/') 139 paths_l = paths.split(' / ')
54 140
55 for cnt, p in enumerate(paths_l, 1): 141 for cnt, p in enumerate(paths_l, 1):
56 if p != '': 142 if p != '':
57 url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path='/'.join(paths_l[:cnt])))) 143 url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path=' / '.join(paths_l[:cnt]))))
58 144
59 return literal(' / '.join(url_l)) 145 return literal(' / '.join(url_l))
146
147 files_breadcrumbs = _FilesBreadCrumbs()
60 148
61 def pygmentize(filenode, **kwargs): 149 def pygmentize(filenode, **kwargs):
62 """ 150 """
63 pygmentize function using pygments 151 pygmentize function using pygments
64 @param filenode: 152 @param filenode:
79 if color_dict.has_key(cs): 167 if color_dict.has_key(cs):
80 col = color_dict[cs] 168 col = color_dict[cs]
81 else: 169 else:
82 color_dict[cs] = gen_color() 170 color_dict[cs] = gen_color()
83 col = color_dict[cs] 171 col = color_dict[cs]
84 return "color: rgb(%s) ! important;" % (','.join(col)) 172 return "color: rgb(%s) ! important;" % (', '.join(col))
85 173
86 def url_func(changeset): 174 def url_func(changeset):
87 return '%s\n' % (link_to('r%s:%s' % (changeset.revision, changeset.raw_id), 175 tooltip_html = "<div style='font-size:0.8em'><b>Author:</b> %s<br/><b>Date:</b> %s</b><br/><b>Message:</b> %s<br/></div>"
88 url('changeset_home', repo_name='test', revision=changeset.raw_id), 176
89 title=_('author') + ':%s - %s' % (changeset.author, changeset.message,), 177 tooltip_html = tooltip_html % (changeset.author,
90 style=get_color_string(changeset.raw_id))) 178 changeset.date,
91 179 tooltip(changeset.message))
180 lnk_format = 'r%s:%s' % (changeset.revision,
181 changeset.raw_id)
182 uri = link_to(
183 lnk_format,
184 url('changeset_home', repo_name='test',
185 revision=changeset.raw_id),
186 style=get_color_string(changeset.raw_id),
187 class_='tooltip',
188 tooltip_title=tooltip_html
189 )
190
191 uri += '\n'
192 return uri
92 return literal(annotate_highlight(filenode, url_func, **kwargs)) 193 return literal(annotate_highlight(filenode, url_func, **kwargs))
93
94 def recursive_replace(str, replace=' '):
95 """
96 Recursive replace of given sign to just one instance
97 @param str: given string
98 @param replace:char to find and replace multiple instances
99
100 Examples::
101 >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-')
102 'Mighty-Mighty-Bo-sstones'
103 """
104
105 if str.find(replace * 2) == -1:
106 return str
107 else:
108 str = str.replace(replace * 2, replace)
109 return recursive_replace(str, replace)
110 194
111 def repo_name_slug(value): 195 def repo_name_slug(value):
112 """ 196 """
113 Return slug of name of repository 197 Return slug of name of repository
114 """ 198 """
115 slug = urlify(value) 199 slug = urlify(value)
116 for c in """=[]\;'"<>,/~!@#$%^&*()+{}|:""": 200 for c in """=[]\;'"<>,/~!@#$%^&*()+{}|:""":
117 slug = slug.replace(c, '-') 201 slug = slug.replace(c, '-')
118 slug = recursive_replace(slug, '-') 202 slug = recursive_replace(slug, '-')
119 return slug 203 return slug
120 204
121 files_breadcrumbs = _FilesBreadCrumbs()
122 link = _Link()
123 flash = _Flash() 205 flash = _Flash()
124 get_error = _GetError()