oxjs/source/UI/js/Form/OptionGroup.js

125 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 10:49:48 +00:00
/*@
Ox.OptionGroup <f> OptionGroup
Helper object, used by ButtonGroup, CheckboxGroup, Select and Menu
2011-05-16 10:49:48 +00:00
(items, min, max, property) -> <f> OptionGroup
items <a> array of items
min <n> minimum number of selected items
max <n> maximum number of selected items
2011-12-21 13:42:47 +00:00
property <s|'checked'> property to check
2011-05-16 10:49:48 +00:00
@*/
// FIXME: Should be moved to Ox.js
2011-04-22 22:03:10 +00:00
Ox.OptionGroup = function(items, min, max, property) {
var length = items.length;
property = property || 'checked';
max = max == -1 ? length : max;
2011-04-22 22:03:10 +00:00
function getLastBefore(pos) {
// returns the position of the last checked item before position pos
var last = -1;
// fixme: why is length not == items.length here?
2012-05-24 07:45:33 +00:00
Ox.forEach([].concat(
2011-04-22 22:03:10 +00:00
pos > 0 ? Ox.range(pos - 1, -1, -1) : [],
pos < items.length - 1 ? Ox.range(items.length - 1, pos, -1) : []
), function(v) {
if (items[v][property]) {
last = v;
2012-07-05 08:58:08 +00:00
return false; // break
2011-04-22 22:03:10 +00:00
}
});
return last;
}
function getNumber() {
// returns the number of checked items
2011-12-21 13:42:47 +00:00
return items.reduce(function(prev, curr) {
2011-12-22 15:47:46 +00:00
return prev + !!curr[property];
2011-12-21 13:42:47 +00:00
}, 0);
2011-04-22 22:03:10 +00:00
}
2011-05-16 10:49:48 +00:00
/*@
2011-12-21 13:42:47 +00:00
[property] <f> returns an array with the positions of all checked items
() -> <a> positions of checked items
2011-05-16 10:49:48 +00:00
@*/
2011-12-21 13:42:47 +00:00
// FIXME: isn't value more useful in all cases?
2011-04-22 22:03:10 +00:00
this[property] = function() {
2012-05-22 14:29:37 +00:00
return Ox.indicesOf(items, function(item) {
return item[property];
2011-12-21 13:42:47 +00:00
});
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
init <f> init group
() -> <a> returns items
@*/
2011-04-22 22:03:10 +00:00
this.init = function() {
var num = getNumber(),
count = 0;
//if (num < min || num > max) {
items.forEach(function(item) {
if (Ox.isUndefined(item[property])) {
item[property] = false;
}
if (item[property]) {
count++;
if (count > max) {
item[property] = false;
}
} else {
if (num < min) {
item[property] = true;
num++;
}
}
});
//}
return items;
};
2011-05-16 10:49:48 +00:00
/*@
toggle <f> toggle options
(pos) -> <a> returns toggled state
@*/
2011-04-22 22:03:10 +00:00
this.toggle = function(pos) {
var last,
num = getNumber(),
toggled = [];
if (!items[pos][property]) { // check
if (num >= max) {
last = getLastBefore(pos);
items[last][property] = false;
toggled.push(last);
}
if (!items[pos][property]) {
items[pos][property] = true;
toggled.push(pos);
}
} else { // uncheck
if (num > min) {
items[pos][property] = false;
toggled.push(pos);
}
}
return toggled;
2013-10-31 13:15:50 +00:00
};
2011-04-22 22:03:10 +00:00
2012-05-21 10:38:18 +00:00
/*@
value <f> get value
@*/
2011-12-21 13:42:47 +00:00
this.value = function() {
2012-05-22 15:34:25 +00:00
var value = items.filter(function(item) {
return item[property];
2012-05-22 15:35:26 +00:00
}).map(function(item) {
2012-05-22 15:34:25 +00:00
return item.id;
2011-12-21 13:42:47 +00:00
});
return max == 1 ? (value.length ? value[0] : '') : value;
};
2011-04-22 22:03:10 +00:00
return this;
}