76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
|
'use strict';
|
|
/*@
|
|
Ox.CheckboxGroup <f:Ox.Element> CheckboxGroup Object
|
|
() -> <f> CheckboxGroup Object
|
|
(options) -> <f> CheckboxGroup Object
|
|
(options, self) -> <f> CheckboxGroup Object
|
|
options <o> Options object
|
|
checkboxes <a|[]> array of checkboxes
|
|
max <n|1> integer
|
|
min <n|1> integer
|
|
width <n> integer, width in px
|
|
self <o> shared private variable
|
|
change <!> triggered when checked property changes
|
|
passes {checked, id, title}
|
|
@*/
|
|
|
|
Ox.CheckboxGroup = function(options, self) {
|
|
|
|
self = self || {};
|
|
var that = Ox.Element({}, self)
|
|
.defaults({
|
|
checkboxes: [],
|
|
max: 1,
|
|
min: 1,
|
|
width: 256
|
|
})
|
|
.options(options || {})
|
|
.addClass('OxCheckboxGroup');
|
|
|
|
self.optionGroup = new Ox.OptionGroup(
|
|
self.options.checkboxes,
|
|
self.options.min,
|
|
self.options.max);
|
|
self.options.checkboxes = self.optionGroup.init();
|
|
|
|
Ox.extend(self, {
|
|
$checkboxes: [],
|
|
checkboxWidth: Ox.divideInt(
|
|
self.options.width + (self.options.checkboxes.length - 1) * 6,
|
|
self.options.checkboxes.length
|
|
).map(function(v, i) {
|
|
return v + (i < self.options.checkboxes.length - 1 ? 10 : 0);
|
|
})
|
|
});
|
|
self.options.checkboxes.forEach(function(checkbox, position) {
|
|
var id = self.options.id + Ox.toTitleCase(checkbox.id)
|
|
self.$checkboxes[position] = Ox.Checkbox(Ox.extend(checkbox, {
|
|
group: true,
|
|
id: id,
|
|
width: self.checkboxWidth[position]
|
|
}))
|
|
.bindEvent('change', function() {
|
|
change(position);
|
|
})
|
|
.appendTo(that);
|
|
});
|
|
|
|
function change(pos) {
|
|
var toggled = self.optionGroup.toggle(pos);
|
|
//Ox.Log('Form', 'change', pos, 'toggled', toggled)
|
|
if (toggled.length) {
|
|
toggled.forEach(function(pos, i) {
|
|
self.$checkboxes[pos].toggleChecked();
|
|
});
|
|
that.triggerEvent('change', {
|
|
checked: self.optionGroup.checked().map(function(v) {
|
|
return self.options.checkboxes[v].id;
|
|
})
|
|
});
|
|
}
|
|
}
|
|
|
|
return that;
|
|
|
|
};
|