Package turbogears :: Package widgets :: Module links

Source Code for Module turbogears.widgets.links

  1  import warnings 
  2  from turbojson.jsonify import encode 
  3  from turbogears.widgets.base import CSSLink, JSLink, CSSSource, JSSource, \ 
  4                                      Widget, CoreWD, static, js_location 
  5  from turbogears.widgets.forms import SelectionField 
  6   
  7  try: 
  8      set 
  9  except NameError: # Python 2.3 
 10      from sets import Set as set 
 11   
 12  __all__ = ["Tabber", "SyntaxHighlighter", "JumpMenu"] 
 13   
14 -class Tabber(Widget):
15 """This widget includes the tabber js and css into your rendered 16 page so you can create tabbed divs by assigning them the 'tabber' 17 and 'tabbertab' classes. 18 """ 19 css = [CSSLink(static,"tabber/tabber.css", media="screen")]
20 - def __init__(self, tabber_options={}, use_cookie=False, hide_on_load=True, 21 *args, **kw):
22 super(Tabber, self).__init__(*args, **kw) 23 js = [] 24 # First some sanity-check 25 if use_cookie and (tabber_options.has_key('onLoad') or 26 tabber_options.has_key('onClick')): 27 warnings.warn("Cannot use cookies if overriden by " 28 "tabber_options['onClick'] or " 29 "tabber_options['onLoad']. Undefined behavior.") 30 # Build the js list in it's correct order 31 if use_cookie: 32 js.append(JSLink(static, "tabber/tabber_cookie.js")) 33 if tabber_options: 34 js.append(JSSource("var tabberOptions = %s;" % 35 encode(tabber_options))) 36 if use_cookie: 37 js.append(JSSource(""" 38 try { 39 tabberOptions 40 } catch(e){ 41 tabberOptions = {}; 42 } 43 tabberOptions['onLoad'] = tabber_onload; 44 tabberOptions['onClick'] = tabber_onclick; 45 tabberOptions['cookie'] = 'TGTabber';""")) 46 if hide_on_load: 47 js.append(JSSource("document.write('%s');" 48 % '<style type="text/css">.tabber{display:none;}</style>')) 49 js.append(JSLink(static, "tabber/tabber-minimized.js", 50 location=js_location.bodytop)) 51 self.javascript = js
52 53
54 -class TabberDesc(CoreWD):
55 name = "Tabber" 56 for_widget = Tabber() 57 template = """<div class="tabber"> 58 <div class="tabbertab"><h2>Tab 1</h2><p>This is page 1.</p></div> 59 <div class="tabbertab"><h2>Tab 2</h2><p>This is page 2.</p></div> 60 <div class="tabbertab"><h2>Tab 3</h2><p>This is page 3.</p></div> 61 </div>"""
62
63 -class SyntaxHighlighter(Widget):
64 """This widget includes the syntax highlighter js and css into your 65 rendered page to syntax-hightlight textareas named 'code'. The supported 66 languages can be listed at the 'languages' __init__ parameter. 67 """ 68 available_langs = set([ 69 'CSharp', 70 'Css', 71 'Delphi', 72 'Java', 73 'JScript', 74 'Php', 75 'Python', 76 'Ruby', 77 'Sql', 78 'Vb', 79 'Xml', 80 ]) 81 css = [CSSLink(static,"sh/SyntaxHighlighter.css")] 82
83 - def __init__(self, languages=['Python', 'Xml']):
84 javascript = [ 85 JSLink(static, 'sh/shCore.js', location=js_location.bodybottom) 86 ] 87 for lang in languages: 88 if lang not in self.available_langs: 89 raise ValueError, ("Unsupported language %s. Available " 90 "languages: '%s'" % 91 (lang, ', '.join(self.available_langs))) 92 source = "sh/shBrush%s.js" % lang 93 javascript.append( 94 JSLink(static, source, location=js_location.bodybottom) 95 ) 96 javascript.append( 97 JSSource( 98 "dp.SyntaxHighlighter.HighlightAll('code');", 99 location=js_location.bodybottom 100 ) 101 ) 102 self.javascript = javascript
103
104 -class SyntaxHighlighterDesc(CoreWD):
105 name = "Syntax Highlighter" 106 for_widget = SyntaxHighlighter() 107 template = """\ 108 <textarea name="code" class="py"> 109 def say_hello(): 110 print "Hello world!" 111 </textarea>"""
112
113 -class JumpMenu(SelectionField):
114 """ 115 Choose a link from the menu, 116 the page will be redirect to the selected link. 117 """ 118 js = JSSource(""" 119 <!-- 120 function TG_jumpMenu(targ,f,restore){ 121 eval(targ+".location='"+f.options[f.selectedIndex].value+"'"); 122 if (restore) f.selectedIndex=0; 123 } 124 //--> 125 """) 126 127 template = """ 128 <select xmlns:py="http://purl.org/kid/ns#" 129 name="${name}" 130 class="${field_class}" 131 id="${field_id}" 132 onchange="TG_jumpMenu('parent',this,0)" 133 py:attrs="attrs" 134 > 135 <optgroup py:for="group, options in grouped_options" 136 label="${group}" 137 py:strip="not group" 138 > 139 <option py:for="value, desc, attrs in options" 140 value="${value}" 141 py:attrs="attrs" 142 py:content="desc" 143 /> 144 </optgroup> 145 </select> 146 """ 147 javascript = [js] 148 _selected_verb = 'selected' 149 params = ["attrs"] 150 params_doc = {'attrs' : 'Dictionary containing extra (X)HTML attributes for' 151 ' the select tag'} 152 attrs = {}
153
154 -class JumpMenuDesc(CoreWD):
155 name = "Jump Menu" 156 for_widget = JumpMenu("your_jump_menu_field", 157 options=[('http://www.python.org', "Python"), 158 ('http://www.turbogears.org', "TurboGears"), 159 ('http://www.python.org/pypi', "Cheese Shop"), 160 ('http://www.pythonware.com/daily/', "Daily Python")], 161 #default='http://www.turbogears.org' 162 )
163