form elements rewrite, part 1

This commit is contained in:
rlx 2011-12-21 13:42:47 +00:00
commit 7f83cd3141
30 changed files with 1061 additions and 958 deletions

View file

@ -9,7 +9,7 @@ Ox.OptionGroup <f> OptionGroup
items <a> array of items
min <n> minimum number of selected items
max <n> maximum number of selected items
property <s|checked> property to check
property <s|'checked'> property to check
@*/
Ox.OptionGroup = function(items, min, max, property) {
@ -36,28 +36,20 @@ Ox.OptionGroup = function(items, min, max, property) {
function getNumber() {
// returns the number of checked items
var num = 0;
items.forEach(function(item) {
if (item[property]) {
num++;
}
});
return num;
return items.reduce(function(prev, curr) {
return prev + curr[property];
}, 0);
}
/*@
[property] <f> returns an array with the positions of all checked item
() -> <a> returns checked items
[property] <f> returns an array with the positions of all checked items
() -> <a> positions of checked items
@*/
// FIXME: isn't value more useful in all cases?
this[property] = function() {
// returns an array with the positions of all checked item
var checked = [];
items.forEach(function(item, i) {
if (item[property]) {
checked.push(i);
}
})
return checked;
return Ox.map(items, function(item, i) {
return item[property] ? i : null;
});
};
/*@
@ -115,6 +107,13 @@ Ox.OptionGroup = function(items, min, max, property) {
return toggled;
}
this.value = function() {
var value = Ox.map(items, function(item) {
return item[property] ? item.id : null;
});
return max == 1 ? (value.length ? value[0] : '') : value;
};
return this;
}