diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-11-25 15:37:44 +0200 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-11-25 15:37:59 +0200 |
commit | a03cf9336873f63a98d0049996465c26b40405b8 (patch) | |
tree | 02b82bc5f2ce5b68ffe66ac012e31c4b94fcc16d | |
parent | 91fd2ad0f9fb9a0cb4482a6405c4475036c999b3 (diff) | |
download | aconf-a03cf9336873f63a98d0049996465c26b40405b8.tar.bz2 aconf-a03cf9336873f63a98d0049996465c26b40405b8.tar.xz |
convert field choices to objects to allow additional parameters
-rw-r--r-- | acf2/model/field.lua | 16 | ||||
-rw-r--r-- | protocol.txt | 6 | ||||
-rw-r--r-- | web/client.js | 8 |
3 files changed, 23 insertions, 7 deletions
diff --git a/acf2/model/field.lua b/acf2/model/field.lua index 64c727b..0600080 100644 --- a/acf2/model/field.lua +++ b/acf2/model/field.lua @@ -59,8 +59,18 @@ function M.Field:init(params) if self.choice then self.choice = util.map( - function(ch) - return type(ch) == 'table' and ch or {ch, self:auto_ui_name(ch)} + function(choice) + if type(choice) ~= 'table' then + choice = {choice, self:auto_ui_name(choice)} + end + for i, k in ipairs{'value', 'ui-value'} do + if choice[i] then + assert(not choice[k]) + choice[k] = choice[i] + choice[i] = nil + end + end + return choice end, self.choice ) @@ -106,7 +116,7 @@ function M.Field:_validate(context, value) raise(context.path, 'Required value not set') end if self.choice and value ~= nil and not util.contains( - util.map(function(ch) return ch[1] end, self.choice), value + util.map(function(ch) return ch.value end, self.choice), value ) then raise(context.path, 'Invalid value') end diff --git a/protocol.txt b/protocol.txt index f0ad9cb..68f62d7 100644 --- a/protocol.txt +++ b/protocol.txt @@ -62,8 +62,10 @@ resp: JSON object, with the following attributes: - required (boolean) - default - max-length - - choices (optional array of allowed values, which are - tuples of actual values and user-friendly versions) + - choices: optional array of allowed values, which are + objects with the following attributes: + - value + - ui-value (user-friendly version) - fields (model only): array of field metadata JSON objects - members (collections and sets only): metadata for diff --git a/web/client.js b/web/client.js index 38c8515..cf7910f 100644 --- a/web/client.js +++ b/web/client.js @@ -585,7 +585,11 @@ $(function() { _.each( meta.choice, function(choice) { - opt(choice[0], choice[1], value == choice[0]); + opt( + choice.value, + choice["ui-value"], + value == choice.value + ); } ); } @@ -1009,7 +1013,7 @@ $(function() { var def = $.Deferred(); txnMgr.query(meta.scope).done(function(data) { meta.choice = _.map(_.values(data.data), function(path) { - return [path, split(path).pop()]; + return {value: path, "ui-value": split(path).pop()}; }); def.resolve(value, meta); }); |