Comparison

util/dataforms.lua @ 9084:572b6858db03

util.dataforms: Detach generation of options from values (fixes #1177)
author Kim Alvefur <zash@zash.se>
date Fri, 03 Aug 2018 18:35:00 +0200
parent 9047:ab3488ee3ca5
child 9087:9e45e7adcebf
comparison
equal deleted inserted replaced
9083:5d3639e415bd 9084:572b6858db03
45 local value; 45 local value;
46 if data and data[field.name] ~= nil then 46 if data and data[field.name] ~= nil then
47 value = data[field.name]; 47 value = data[field.name];
48 else 48 else
49 value = field.value; 49 value = field.value;
50 end
51
52 if formtype ~= "result" and field.options then
53 local defaults = {};
54 for _, val in ipairs(field.options) do
55 if type(val) == "table" then
56 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
57 if val.default then
58 defaults[#defaults+1] = val.value;
59 end
60 else
61 form:tag("option", { label= val }):tag("value"):text(val):up():up();
62 end
63 end
64 if not value then
65 if field_type == "list-single" then
66 value = defaults[1];
67 elseif field_type == "list-multi" then
68 value = defaults;
69 end
70 end
50 end 71 end
51 72
52 if value ~= nil then 73 if value ~= nil then
53 -- Add value, depending on type 74 -- Add value, depending on type
54 if field_type == "hidden" then 75 if field_type == "hidden" then
76 -- Split into multiple <value> tags, one for each line 97 -- Split into multiple <value> tags, one for each line
77 for line in value:gmatch("([^\r\n]+)\r?\n*") do 98 for line in value:gmatch("([^\r\n]+)\r?\n*") do
78 form:tag("value"):text(line):up(); 99 form:tag("value"):text(line):up();
79 end 100 end
80 elseif field_type == "list-single" then 101 elseif field_type == "list-single" then
81 if formtype ~= "result" then
82 local has_default = false;
83 for _, val in ipairs(field.options or value) do
84 if type(val) == "table" then
85 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
86 if value == val.value or val.default and (not has_default) then
87 form:tag("value"):text(val.value):up();
88 has_default = true;
89 end
90 else
91 form:tag("option", { label= val }):tag("value"):text(val):up():up();
92 end
93 end
94 end
95 if (field.options or formtype == "result") and value then
96 form:tag("value"):text(value):up(); 102 form:tag("value"):text(value):up();
97 end
98 elseif field_type == "list-multi" then 103 elseif field_type == "list-multi" then
99 if formtype ~= "result" then
100 for _, val in ipairs(field.options or value) do
101 if type(val) == "table" then
102 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
103 if not field.options and val.default then
104 form:tag("value"):text(val.value):up();
105 end
106 else
107 form:tag("option", { label= val }):tag("value"):text(val):up():up();
108 end
109 end
110 end
111 if (field.options or formtype == "result") and value then
112 for _, val in ipairs(value) do 104 for _, val in ipairs(value) do
113 form:tag("value"):text(val):up(); 105 form:tag("value"):text(val):up();
114 end
115 end 106 end
116 end 107 end
117 end 108 end
118 109
119 local media = field.media; 110 local media = field.media;