Package turbogears :: Package widgets :: Module links

Source Code for Module turbogears.widgets.links

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