1 from django import template
2 from django.template import resolve_variable
3 register = template.Library()
4
5 @register.tag(name="home_link")
7 """Provides a back button link to the home panel.
8 """
9 tokens = token.split_contents()
10 print tokens
11 if len(tokens) == 1:
12 href = "#"
13 text = "Back Home"
14 elif len(tokens) == 2:
15 href = "#"
16 text = tokens[1]
17 elif len(tokens) == 3:
18 text = tokens[1]
19 href = '#' + tokens[2].replace(' ', '')
20 else:
21 raise template.TemplateSyntaxError, "%r tag accepts no more than 2 arguments." % token.contents.split()[0]
22 return HomeLinkNode(href, text)
23
26 self.href = href
27 self.text = text
28
30 return """
31 <a class="back_link" href="%s">%s</a>
32 """ % (self.href, self.text)
33
34 @register.tag(name="back_link")
36 """Provides a back button linking to the given url, displaying the specified title.
37 """
38 tokens = token.split_contents()
39 if len(tokens) < 3:
40 raise template.TemplateSyntaxError, "%r tag requires at least 2 arguments, a title, url and optionally a quoted tab target." % token.contents.split()[0]
41
42 title = tokens[1]
43 urlvar = tokens[2]
44
45 if not (title[0] == title[-1] and title[0] in ('"', "'")):
46 raise template.TemplateSyntaxError, "%r tag requires at least 2 arguments, a title, url and optionally a quoted tab target." % token.contents.split()[0]
47
48 if len(tokens) == 4:
49 tabid = tokens[3]
50 if not (tabid[0] == tabid[-1] and tabid[0] in ('"', "'")):
51 raise template.TemplateSyntaxError, "Tab name must be in quotes."
52 tab_id = tab_id.replace('"', '').replace("'", "")
53 return BackLinkNode(title, urlvar, tab=tab_id)
54 else:
55 return BackLinkNode(title[1:-1], urlvar)
56
58 - def __init__(self, title, urlvar, tab=None):
59 self.title = title
60 self.urlvar = urlvar
61 self.tab = tab
62
64 url = resolve_variable(self.urlvar, context)
65 if self.tab != None:
66 url = url + '#' + self.tab
67 return """
68 <a class="back_link" href="%s">%s</a>
69 """ % (url, self.title)
70
71
72 @register.tag(name="footer")
79
88
89
90 @register.tag(name="panel")
92 """Creates markup for a panel displayed using the panelManager widget.
93 """
94 try:
95
96 tag_name, title = token.split_contents()
97 except ValueError:
98 raise template.TemplateSyntaxError, "%r tag requires a single argument. You must give your panel a title." % token.contents.split()[0]
99
100 nodelist = parser.parse(('endpanel',))
101 parser.delete_first_token()
102 return PanelNode(nodelist, title)
103
106 self.nodelist = nodelist
107 self.title = title
108
110 if not (self.title[0] == self.title[-1] and self.title[0] in ('"', "'")):
111 self.title = resolve_variable(self.title, context)
112 else:
113 self.title = self.title[1:-1]
114 output = self.nodelist.render(context)
115 return """<div class="sidebar-panel">
116 <div class="sidebar-header">
117 <h1>%s</h1>
118 </div>
119 <div class="sidebar-body">
120 <div class="sidebar-wrapper">
121 %s
122 </div>
123 </div>
124 </div>
125 """ % (self.title, output)
126
127 @register.tag(name="printable")
129 """Use this tag to specify that the current panel should be accessible via
130 its url as a printable/static page in addition to the dynamic sidebar
131 form.
132 """
133 return PrintableNode()
134
136
138 return """
139 <a class="printable" href="#" target="_blank">Print View</a>
140 """
141
142 @register.tag(name="tabpanel")
144 """Creates markup for a panel that contains many tabs using the tab tag.
145 """
146 try:
147
148 tag_name, title = token.split_contents()
149 except ValueError:
150 raise template.TemplateSyntaxError, "%r tag requires a single argument. You must give your tabpanel a title." % token.contents.split()[0]
151 nodelist = parser.parse(('endtabpanel',))
152 parser.delete_first_token()
153 return TabPanelNode(nodelist, title)
154
157 self.nodelist = nodelist
158 self.title = title
159
161
162 if not (self.title[0] == self.title[-1] and self.title[0] in ('"', "'")):
163 self.title = resolve_variable(self.title, context)
164 else:
165 self.title = self.title[1:-1]
166 output = self.nodelist.render(context)
167 list_items = ''
168 for node in self.nodelist:
169 if node.__class__.__name__ is 'TabNode':
170 list_items += """
171 <li><a href="#%s">%s</a></li>
172 """ % (node.tab_id, node.title)
173 return """<div class="sidebar-panel">
174 <div class="sidebar-header">
175 <h1>%s</h1>
176 </div>
177 <ul class="tabs">
178 %s
179 </ul>
180 <div class="sidebar-body">
181 <div class="sidebar-wrapper">
182 %s
183 </div>
184 </div>
185 </div>
186 """ % (self.title, list_items, output)
187
188 @register.tag(name="tab")
190 """Create a tab within a tab_panel.
191 """
192 try:
193
194 tag_name, title = token.split_contents()
195 except ValueError:
196 raise template.TemplateSyntaxError, "%r tag requires a single argument. You must give your tab a title." % token.contents.split()[0]
197
198 nodelist = parser.parse(('endtab',))
199 parser.delete_first_token()
200 return TabNode(nodelist, title)
201
204 self.nodelist = nodelist
205 self.title = title
206 self.tab_id = title.replace("'", "").replace('"', '').replace(' ', '')
207
209 if not (self.title[0] == self.title[-1] and self.title[0] in ('"', "'")):
210 self.title = resolve_variable(self.title, context)
211 else:
212 self.title = self.title[1:-1]
213
214 output = self.nodelist.render(context)
215 return """<div id="%s" class="sidebar-body">
216 <div class="sidebar-wrapper">
217 %s
218 </div>
219 </div>
220 """ % (self.tab_id, output)
221