webmcp
view framework/env/ui/field/boolean.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.boolean{
3 ... -- generic ui.field.* arguments, as described for ui.autofield{...}
4 style = style, -- "radio" or "checkbox",
5 nil_allowed = nil_allowed -- set to true, if nil is allowed as third value
6 }
8 This function inserts a field for boolean values in the active slot. For description of the generic field helper arguments, see help for ui.autofield{...}.
10 --]]--
12 function ui.field.boolean(args)
13 local style = args.style
14 if not style then
15 if args.nil_allowed then
16 style = "radio"
17 else
18 style = "checkbox"
19 end
20 end
21 local extra_args = { fetch_value = true }
22 if not args.readonly and args.style == "radio" then
23 extra_args.disable_label_for_id = true
24 end
25 ui.form_element(args, extra_args, function(args)
26 local value = args.value
27 if value ~= true and value ~= false and value ~= nil then
28 error("Boolean value must be true, false or nil.")
29 end
30 if value == nil then
31 if args.nil_allowed then
32 value = args.default
33 else
34 value = args.default or false
35 end
36 end
37 if args.readonly then
38 ui.tag{
39 tag = args.tag,
40 attr = args.attr,
41 content = format.boolean(value, args.format_options)
42 }
43 elseif style == "radio" then
44 local attr = table.new(args.attr)
45 attr.type = "radio"
46 attr.name = args.html_name
47 attr.id = ui.create_unique_id()
48 attr.value = "1"
49 if value == true then
50 attr.checked = "checked"
51 else
52 attr.checked = nil
53 end
54 ui.container{
55 attr = { class = "ui_radio_div" },
56 label = args.true_as or "Yes", -- TODO: localize
57 label_for = attr.id,
58 label_attr = { class = "ui_radio_label" },
59 content_first = true,
60 content = function()
61 ui.tag{ tag = "input", attr = attr }
62 end
63 }
64 attr.id = ui.create_unique_id()
65 attr.value = "0"
66 if value == false then
67 attr.checked = "1"
68 else
69 attr.checked = nil
70 end
71 ui.container{
72 attr = { class = "ui_radio_div" },
73 label = args.false_as or "No", -- TODO: localize
74 label_for = attr.id,
75 label_attr = { class = "ui_radio_label" },
76 content_first = true,
77 content = function()
78 ui.tag{ tag = "input", attr = attr }
79 end
80 }
81 if args.nil_allowed then
82 attr.id = ui.create_unique_id()
83 attr.value = ""
84 if value == nil then
85 attr.checked = "1"
86 else
87 attr.checked = nil
88 end
89 ui.container{
90 attr = { class = "ui_radio_div" },
91 label = args.nil_as or "N/A", -- TODO: localize
92 label_for = attr.id,
93 label_attr = { class = "ui_radio_label" },
94 content_first = true,
95 content = function()
96 ui.tag{ tag = "input", attr = attr }
97 end
98 }
99 end
100 ui.hidden_field{
101 name = args.html_name .. "__format", value = "boolean"
102 }
103 elseif style == "checkbox" then
104 if args.nil_allowed then
105 error("Checkboxes do not support nil values.")
106 end
107 local attr = table.new(args.attr)
108 attr.type = "checkbox"
109 attr.name = args.html_name
110 attr.value = "1"
111 if value then
112 attr.checked = "checked"
113 else
114 attr.checked = nil
115 end
116 ui.tag{ tag = "input", attr = attr }
117 ui.hidden_field{
118 name = args.html_name .. "__format",
119 value = encode.format_info(
120 "boolean",
121 { true_as = "1", false_as = "" }
122 )
123 }
124 else
125 error("'style' attribute for ui.field.boolean{...} must be set to \"radio\", \"checkbox\" or nil.")
126 end
127 end)
128 end