allow defaults for .define and allow barewords for pair list keys
[bse.git] / site / templates / preload.tmpl
CommitLineData
f04ca377 1<:# see preload.pod for documentation -:>
2de83e20
TC
2<:.set groups = 0 -:>
3<:.set grouplabel = "label" -:>
4<:.set groupid = "id" -:>
5<:.set itemgroupid = "groupid" -:>
5251d8e2 6<:.set dist_image_uri = cfg.entryIfVar("uri", "dist_images", "/images") -:>
b62cae00
TC
7<:# utility definitions :>
8<:-.define make_select-:>
19851056
TC
9 <:-.if !default.defined -:>
10 <:-.set default = "" -:>
11 <:.end if:>
411e6902 12 <select name="<:= name | html :>">
2de83e20
TC
13 <:- .if groups -:>
14 <:-.for i in list -:>
15 <:.if i.$itemgroupid eq "" -:>
411e6902 16 <option value="<:= i.$id | html :>"
2de83e20 17 <:- .if i.$id eq default :> selected="selected"<:.end if -:>
b62cae00 18 >
2de83e20
TC
19 <:-= i.$desc | html -:>
20 <:.end if -:>
21 <:.end for -:>
22 <:-.for g in groups -:>
23 <optgroup label="<:= g.$grouplabel | html -:>">
24 <:-.for i in list -:>
25 <:.if i.$itemgroupid == g.$groupid -:>
26 <option value="<:= i.$id | html :>"
27 <:- .if i.$id eq default :> selected="selected"<:.end if -:>
28 >
29 <:-= i.$desc | html -:>
30 <:.end if -:>
31 <:.end for -:>
32 </optgroup>
33 <:-.end for -:>
34 <:- .else -:>
35 <:-.for i in list -:>
36 <option value="<:= i.$id | html :>"
37 <:- .if i.$id eq default :> selected="selected"<:.end if -:>
38 >
39 <:-= i.$desc | html -:>
40 <:-.end for-:>
41 <:-.end if -:>
b62cae00
TC
42 </select>
43<:-.end define -:>
8f685a21 44<:#
f04ca377
TC
45make_multicheck expects:
46 values - list of hashes
47 default - a list of ids of checks to be checked
48 id - the name of the id field
49 desc - the name of the description field.
50 name - the name of the input elements
51 readonly - true to make it readonly
52-:>
53<:-.define make_multicheck-:>
54 <:.if !readonly -:>
55 <input type="hidden" name="_save_<:= name -:>" value="1">
56 <:.end if -:>
57 <:-.if !default.defined -:>
58 <:-.set default = [] -:>
59 <:.end if:>
60 <:.set checked = {} -:>
61 <:.for v in default -:>
62 <:% checked.set(v, 1) -:>
63 <:.end for -:>
64 <ul>
65 <:-.for i in values -:>
66 <li>
67 <:.set element_id = name _ "-" _ i.$id -:>
68 <input type="checkbox" name="<:= name :>" <:= checked[i.$id] ? 'checked ' : '' -:>
69 id="<:= element_id -:>" value="<:= i.$id :>"
70<:-# readonly attribute isn't valid for checkboxes -:>
71<:-= readonly ? " disabled" : "" -:>
72 >
73 <label for="<:= element_id :>"><:= i.$desc -:></label>
74 </li>
75 <:-.end for -:>
76 </ul>
77<:-.end define -:>
78<:#
8f685a21
TC
79page_list expects:
80base - base url for links
81pages - the result of the bse.pages call
82 :>
83<:-.define page_list-:>
84<:.set sep = base =~ /\?/ ? "&" : "?" -:>
85<:.set base = base _ sep :>
86<div class="pagelist">
87Page <:= pages.page :> of <:= pages.pagecount :>
88<:.if pages.previous_page -:>
89<a href="<:= base |html:><:= pages.pname | uri :>=1&amp;<:= pages.ppname | uri :>=<:= pages.pp | uri :>">&lt;&lt</a>
90<a href="<:= base |html:><:= pages.pname | uri :>=<:= pages.previous_page |uri:>&amp;<:= pages.ppname | uri :>=<:= pages.pp | uri :>">&lt;</a>
91<:.else -:>
92<span>&lt&lt</span>
93<span>&lt</span>
94<:.end if -:>
95<:.for p in pcats.pages :>
96<:.if p.link -:>
97<a href="<:= base |html :><:= pages.pname | uri:>=<:= p.page | uri :>&amp;<:=pages.ppname | uri :>=<:= pages.pp | uri :>"><:= p.page |html :></a>
98<:.else -:>
99<span><:= p.page |html:></span>
100<:.end if -:>
101<:.end for:>
102<:.if pages.next_page -:>
103<a href="<:= base |html:><:= pages.pname | uri :>=<:= pages.next_page |uri:>&amp;<:= pages.ppname | uri :>=<:= pages.pp | uri :>">&gt;</a>
104<a href="<:= base |html:><:= pages.pname | uri :>=<:= pages.pagecount |uri:>&amp;<:= pages.ppname | uri :>=<:= pages.pp | uri :>">&gt;&gt</a>
105<:.else -:>
106<span>&gt&gt</span>
107<span>&gt</span>
108<:.end if -:>
109</div>
59fca225
TC
110<:-.end define -:>
111
112<:.define old -:>
113<:# parameters: field, default -:>
114<: .if cgi.param(field).defined -:>
115<:= cgi.param(field) -:>
116<: .else -:>
117<:= default | html -:>
118<: .end if -:>
119<:.end define-:>
120
5251d8e2
TC
121<:.define error_img_n -:>
122<:# parameters:
123 field - name of field
124 index - index of field
125 errors - hash of error messages (should be set by code)
126-:>
127 <:.if errors.$field -:>
128 <:.set msg = errors.$field -:>
129 <:.set msg = msg.is_list ? msg[index] : msg -:>
c46eae4a
TC
130 <:.if msg.defined -:>
131 <:.set image = cfg.entry("error_img", "image", dist_image_uri _ "/admin/error.gif") -:>
132 <:.set width = cfg.entry("error_img", "width", 16) -:>
133 <:.set height = cfg.entry("error_img", "height", 16) -:>
5251d8e2 134 <img src="<:= image -:>" alt="<:= msg :>" title="<:= msg :>" width="<:= width :>" height="<:= height :>" class="error_img">
c46eae4a 135 <:.end if -:>
5251d8e2
TC
136 <:.end if -:>
137<:.end define -:>
138
59fca225 139<:.define error_img -:>
5251d8e2 140<:.call "error_img_n", "index":0 -:>
80e0459c 141<:.end define -:>
16fff1cd 142
b26e625c 143<:.define input -:>
c6369510 144<:# parameters:
b26e625c 145 name - field name
f04ca377 146 field - entry from fields
b26e625c
TC
147 object - source for defaults in edit mode
148:>
b26e625c 149<: .if object -:>
f04ca377
TC
150<: .set default = object.$name -:>
151<: .elsif field.default -:>
152<: .set default = field.default -:>
b26e625c
TC
153<: .else -:>
154<: .set default = "" -:>
155<: .end if -:>
f04ca377 156<: .if field.type and field.type eq "date" and default ne "" -:>
5522569e 157<: .set default = default.replace(/(\d+)\D+(\d+)\D+(\d+)/, "$3/$2/$1") -:>
f04ca377 158<: .elsif field.type and field.type eq "time" and default ne "" -:>
5522569e
TC
159<: .set default = bse.date(default =~ /:00$/ ? "%I:%M%p" : "%I:%M:%S%p", default).replace(/^0/, "").lower() -:>
160<: .end if -:>
161<: .if cgi.param(name).defined -:>
162<: .set default = cgi.param(name) -:>
163<: .end if -:>
f04ca377
TC
164<: .if field.htmltype eq "textarea" -:>
165<textarea id="<:= name | html :>" name="<:= name | html :>" rows="<:= field.height ? field.height : cfg.entry("forms", "textarea_rows", 10) :>" cols=<:= field.width ? field.width : cfg.entry("textarea_cols", 60) | html :>>
5522569e 166<:-= default | html -:>
b26e625c 167</textarea>
f04ca377
TC
168<: .elsif field.htmltype eq "checkbox" -:>
169<:.set is_checked = cgi.param("_save_" _ name) ? cgi.param(name).defined : default -:>
170<input type="hidden" name="_save_<:= name -:>" value="1">
171<input id="<:= name | html :>" type="checkbox" name="<:= name | html :>"<:= is_checked ? ' checked="checked"' : '' :> value="<:= field.value ? field.value : 1 | html :>" />
172<: .elsif field.htmltype eq "multicheck" -:>
173<:# we expect default to be a list of selected checks -:>
174<:.set values = field.select["values"] -:>
175<:.set values = values.is_code ? values() : values -:>
176<:.set default = cgi.param("_save_" _ name) ? [ cgi.param(name) ] : default -:>
177<:.call "make_multicheck",
178 "id":field.select.id,
179 "desc":field.select.label,
180 "readonly":0 -:>
181<: .elsif field.htmltype eq "select" -:>
182<:.set values = field.select["values"] -:>
411e6902
TC
183<:.set values = values.is_code ? values() : values -:>
184<:.call "make_select",
185 "name":name,
186 "default":default,
187 "list":values,
f04ca377
TC
188 "id":field.select.id,
189 "desc":field.select.label,
190 "groupid": (field.select.groupid or "id"),
191 "itemgroupid": (field.select.itemgroupid or "groupid"),
192 "groups": field.select.groups ? (field.select.groups.is_code ? (field.select.groups)() : field.select.groups ) : 0,
193 "grouplabel": (field.select.grouplabel or "label")
411e6902 194-:>
b26e625c 195<: .else -:>
5522569e 196<input id="<:= name | html :>" type="text" name="<:= name | html :>" value="<:= default | html :>"
f04ca377
TC
197<:-= field.maxlength ? ' maxlength="' _ field.maxlength _ '"' : '' |raw:>
198<:-= field.width ? ' size="' _ field.width _ '"' : '' | raw :> />
b26e625c
TC
199<: .end if -:>
200<:.end define -:>
201
202<:.define field -:>
5522569e 203<:# parameters:
b26e625c
TC
204 name - field name
205 fields - hash of fields (may be set by page instead)
206:>
f04ca377
TC
207 <:.if fields.is_hash -:>
208 <:.set f = fields[name] -:>
209 <:.if f -:>
210 <:.call "inlinefield", "field":f, "name":name -:>
211 <:.else -:>
212<p>Unknown field id '<b><:= name :></b>', available fields are '<:= fields.keys.join("', '") :>'</p>
213 <:.end if -:>
214 <:.else -:>
215<p>fields isn't a hash, something is wrong.</p>
216 <:.end if -:>
217<:.end define -:>
218
219<:.define inlinefield -:>
220<:# parameters:
221 name - the field name
222 field - a field, as an entry in fields
223-:>
224 <:.if field.is_hash -:>
b26e625c 225<div>
f04ca377 226 <label for="<:= name | html :>"><:= field.nolabel ? "" : field.description | html :>:</label>
2ced88e0
TC
227 <span>
228 <:-.if field.readonly -:>
229<:-.call "display", "name":name -:>
230 <:.else -:>
231<:-.call "input", "name":name -:><:.call "error_img", "field":name :>
232 <:-.end if -:>
f04ca377
TC
233 <:-.if field.units -:>
234 <:-= field.units | html -:>
235 <:-.end if -:>
f04ca377
TC
236</span>
237</div>
672a2e42 238 <:.end if -:>
f04ca377
TC
239<:.end define -:>
240
241<:.define fieldset -:>
242<:# like field, but wrap in a per-field field set.
243parameters:
244 name - field name
245 fields - hash of fields (may be set by page instead)
246:>
247 <:.set f = fields[name] -:>
248 <:.if f.is_hash -:>
249<fieldset>
250 <:.if !f.nolabel -:>
251 <legend><:= f.description :></legend>
252 <:.end if -:>
253 <:.call "input", "name":name, "field":f -:>
254</fieldset>
255 <:.elsif fields.is_hash -:>
256<p>Unknown field id '<b><:= name :></b>', available fields are '<:= fields.keys.join("', '") :>'</p>
257 <:.else -:>
258<p>fields isn't a hash, something is wrong.</p>
259 <:.end if -:>
260<:.end define -:>
261
262<:.define display -:>
263<:# paramaters:
264 name - field name
265 field - entry from fields
266 object - source for defaults in edit mode
267:>
268<: .if object -:>
269<: .set default = object.$name -:>
270<: .end if -:>
271<: .if field.type and field.type eq "date" and default ne "" -:>
272<: .set default = default.replace(/(\d+)\D+(\d+)\D+(\d+)/, "$3/$2/$1") -:>
273<: .elsif field.type and field.type eq "time" and default ne "" -:>
274<: .set default = bse.date(default =~ /:00$/ ? "%I:%M%p" : "%I:%M:%S%p", default).replace(/^0/, "").lower() -:>
275<: .end if -:>
276<: .if field.htmltype eq "textarea" -:>
277<textarea id="<:= name | html :>" name="<:= name | html :>" rows="<:= field.height ? field.height : cfg.entry("forms", "textarea_rows", 10) :>" cols=<:= field.width ? field.width : cfg.entry("textarea_cols", 60) | html :> readonly>
278<:-= default | html -:>
279</textarea>
280<: .elsif field.htmltype eq "checkbox" -:>
281<input id="<:= name | html :>" type="checkbox" name="<:= name | html :>"<:= is_checked ? ' checked="checked"' : '' :> value="<:= field.value ? field.value : 1 | html :>" readonly>
282<: .elsif field.htmltype eq "multicheck" -:>
283<:# we expect default to be a list of selected checks -:>
284<:.set values = field.select["values"] -:>
285<:.set values = values.is_code ? values() : values -:>
286<:.call "make_multicheck",
287 "id":field.select.id,
288 "desc":field.select.label,
289 "readonly":1 -:>
290<: .elsif field.htmltype eq "select" -:>
291<:.set values = field.select["values"] -:>
292<:.set values = values.is_code ? values() : values -:>
293<:# find the selected value and display it's label -:>
294<:.set sid = field.select.id -:>
295<:.set sdesc = field.select.label -:>
296<:.for value in values -:>
297 <:.if value.$id eq default -:>
298 <:= value.$desc -:>
299 <:.end if -:>
300<:.end for -:>
301<: .else -:>
302<input id="<:= name :>" type="text" name="<:= name :>" value="<:= default :>"
303<:-= field.width ? ' size="' _ field.width _ '"' : '' | raw :> readonly>
304<: .end if -:>
305<:.end define -:>
306
307<:.define inlinefieldro -:>
308<:# parameters:
309 name - the field name
310 field - a field, as an entry in fields
311-:>
312 <:.if field.is_hash -:>
313<div>
314 <label for="<:= name :>"><:= field.nolabel ? "" : field.description :>:</label>
315 <span><:.call "display", "name":name -:>
316 <:-.if field.units -:>
317 <:-= field.units -:>
318 <:-.end if -:>
319 <:.end if -:>
e9d454b4 320</span>
b26e625c
TC
321</div>
322<:.end define -:>
323
f04ca377
TC
324<:.define fieldro -:>
325<:# like field, but for display, not editing -:>
326 <:.if fields.is_hash -:>
327 <:.set f = fields[name] -:>
328 <:.if f -:>
329 <:.call "inlinefieldro", "field":f, "name":name -:>
330 <:.else -:>
331<p>Unknown field id '<b><:= name :></b>', available fields are '<:= fields.keys.join("', '") :>'</p>
332 <:.end if -:>
333 <:.else -:>
334<p>fields isn't a hash, something is wrong.</p>
335 <:.end if -:>
336<:.end define -:>
337
338<:.define fieldsetro -:>
339<:# like fieldro, but wrap in a per-field field set.
340parameters:
341 name - field name
342 fields - hash of fields (may be set by page instead)
343:>
344 <:.set f = fields[name] -:>
345 <:.if f.is_hash -:>
346<fieldset>
347 <:.if !f.nolabel -:>
348 <legend><:= f.description :></legend>
349 <:.end if -:>
350 <:.call "display", "name":name, "field":f -:>
351</fieldset>
352 <:.elsif fields.is_hash -:>
353<p>Unknown field id '<b><:= name :></b>', available fields are '<:= fields.keys.join("', '") :>'</p>
354 <:.else -:>
355<p>fields isn't a hash, something is wrong.</p>
356 <:.end if -:>
357<:.end define -:>
358
b26e625c
TC
359<:.define messages -:>
360<: .if request.messages.size -:>
361<div class="messages">
362<: .for m in request.messages -:>
6f186c3b 363 <div class="<:= m.class :>"><:= m.html |raw :></div>
b26e625c
TC
364<: .end for -:>
365</div>
366<: .end if -:>
367<:.end define -:>
368
491223ee 369<:.define mover -:>
f34ff248 370<:# expects loop, request, parent
491223ee
TC
371 which in most cases means you can just supply parent
372-:>
f34ff248
TC
373<:.if request.user_can("bse_edit_reorder_children", parent) -:>
374 <:.set width = cfg.entry("arrows", "width", 17) -:>
375 <:.set height = cfg.entry("arrows", "height", 13) -:>
376 <:.set extras = 'width="' _ width _ '" height="' _ height _ '"' -:>
377 <:.set blankim = '<img src="' _ (dist_image_uri _ "/trans_pixel.gif").escape("html") _ '" alt="" ' _ extras _ ' />' -:>
378 <:.if loop.prev -:>
379 <:.set upimg = cfg.entry("arrows", "upimg", dist_image_uri _ "/admin/move_up.gif") -:>
380 <a href="<:= cfg.admin_url("move", { "stepparent":parent.id, "d":"swap", "id":loop.current.id, "other":loop.prev.id, "r":top.admin }) :>"><img src="<:= upimg :>" <:= extras :> /></a>
381 <:.else -:>
382 <:= blankim |raw-:>
383 <:.end if -:>
384 <:.if loop.next -:>
385 <:.set downimg = cfg.entry("arrows", "downimg", dist_image_uri _ "/admin/move_down.gif") -:>
386 <a href="<:= cfg.admin_url("move", { "stepparent":parent.id, "d":"swap", "id":loop.current.id, "other":loop.next.id, "r":top.admin }) :>"><img src="<:= downimg :>" <:= extras :> /></a>
387 <:.else -:>
388 <:= blankim |raw-:>
389 <:.end if -:>
491223ee
TC
390<:.end if -:>
391
392<:.end define -:>
393
16fff1cd 394<:- include includes/custom/preload.tmpl optional -:>