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