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