webmcp
view framework/env/ui/field/date.lua @ 3:795b764629ca
Version 1.0.3
Important bugfix related to internal forwards (Bug was introduced by the restriction of views with underscore prefix in Version 1.0.2)
Important bugfix related to internal forwards (Bug was introduced by the restriction of views with underscore prefix in Version 1.0.2)
author | jbe |
---|---|
date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) |
parents | 9fdfb27f8e67 |
children |
line source
1 --[[--
2 ui.field.date{
3 ... -- generic ui.field.* arguments, as described for ui.autofield{...}
4 }
6 This function inserts a field for dates in the active slot. If the JavaScript library "gregor.js" has been loaded, a rich input field is used. For description of the generic field helper arguments, see help for ui.autofield{...}.
8 --]]--
10 function ui.field.date(args)
11 ui.form_element(args, {fetch_value = true}, function(args)
12 local value_string = format.date(args.value, args.format_options)
13 if args.readonly then
14 ui.tag{ tag = args.tag, attr = args.attr, content = value_string }
15 else
16 local fallback_data = slot.use_temporary(function()
17 local attr = table.new(args.attr)
18 attr.type = "text"
19 attr.name = args.html_name
20 attr.value = value_string
21 attr.class = attr.class or "ui_field_date"
22 ui.tag{ tag = "input", attr = attr }
23 ui.hidden_field{
24 name = args.html_name .. "__format",
25 value = encode.format_info("date", args.format_options)
26 }
27 end)
28 local user_field_id, hidden_field_id
29 local helper_data = slot.use_temporary(function()
30 local attr = table.new(args.attr)
31 user_field_id = attr.id or ui.create_unique_id()
32 hidden_field_id = ui.create_unique_id()
33 attr.id = user_field_id
34 attr.type = "text"
35 attr.class = attr.class or "ui_field_date"
36 ui.tag{ tag = "input", attr = attr }
37 local attr = table.new(args.attr)
38 attr.id = hidden_field_id
39 attr.type = "hidden"
40 attr.name = args.html_name
41 attr.value = atom.dump(args.value) -- extra safety for JS failure
42 ui.tag{
43 tag = "input",
44 attr = {
45 id = hidden_field_id,
46 type = "hidden",
47 name = args.html_name
48 }
49 }
50 end)
51 -- TODO: localization
52 ui.script{
53 noscript = fallback_data,
54 type = "text/javascript",
55 content = function()
56 slot.put(
57 "if (gregor_addGui == null) document.write(",
58 encode.json(fallback_data),
59 "); else { document.write(",
60 encode.json(helper_data),
61 "); gregor_addGui({element_id: ",
62 encode.json(user_field_id),
63 ", month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], weekday_names: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'], week_numbers: 'left', format: 'DD.MM.YYYY', selected: "
64 )
65 if (args.value) then
66 slot.put(
67 "{year: ", tostring(args.value.year),
68 ", month: ", tostring(args.value.month),
69 ", day: ", tostring(args.value.day),
70 "}"
71 )
72 else
73 slot.put("null")
74 end
75 slot.put(
76 ", select_callback: function(date) { document.getElementById(",
77 encode.json(hidden_field_id),
78 ").value = (date == null) ? '' : date.iso_string; } } ) }"
79 )
80 end
81 }
82 end
83 end)
84 end