oxjs/source/Ox.UI/js/Form/Ox.ButtonGroup.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 08:24:46 +00:00
/*@
Ox.ButtonGroup <f:Ox.Element> ButtonGroup Object
() -> <f> ButtonGroup Object
(options) -> <f> ButtonGroup Object
(options, self) -> <f> ButtonGroup Object
options <o> Options object
buttons <a> array of buttons
max <n> integer, maximum number of selected buttons, 0 for all
min <n> integer, minimum number of selected buttons, 0 for none
selectable <b> if true, buttons are selectable
type <s> string, 'image' or 'text'
self <o> Shared private variable
change <!> {id, value} selection within a group changed
@*/
2011-04-22 22:03:10 +00:00
2011-05-16 08:24:46 +00:00
Ox.ButtonGroup = function(options, self) {
2011-04-22 22:03:10 +00:00
self = self || {};
var that = Ox.Element({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
buttons: [],
max: 1,
min: 1,
selectable: false,
size: 'medium',
style: '',
type: 'text',
})
.options(options || {})
.addClass('OxButtonGroup');
if (self.options.selectable) {
2011-06-19 18:25:37 +00:00
self.optionGroup = new Ox.OptionGroup(
2011-04-22 22:03:10 +00:00
self.options.buttons,
self.options.min,
self.options.max,
'selected'
);
self.options.buttons = self.optionGroup.init();
}
self.$buttons = [];
self.options.buttons.forEach(function(button, position) {
var id = self.options.id + Ox.toTitleCase(button.id)
self.$buttons[position] = Ox.Button({
disabled: button.disabled,
group: true,
id: id,
selectable: self.options.selectable,
selected: button.selected,
size: self.options.size,
style: self.options.style,
title: button.title,
type: self.options.type
})
.bindEvent('select', function() {
selectButton(position);
})
.appendTo(that);
});
function selectButton(pos) {
var toggled = self.optionGroup.toggle(pos);
if (toggled.length) {
2011-09-18 02:06:33 +00:00
toggled.forEach(function(pos) {
2011-04-22 22:03:10 +00:00
self.$buttons[pos].toggleSelected();
});
that.triggerEvent('change', {
selected: self.optionGroup.selected().map(function(i) {
2011-09-18 02:06:33 +00:00
return self.options.buttons[i].id;
2011-04-22 22:03:10 +00:00
})
});
}
}
that.select = function(id) {
// fixme: this doesn't work in cases where
// multiple buttons can be selected
var position = Ox.getPositionById(self.options.buttons, id);
if (position > -1) {
self.$buttons[position].trigger('click');
}
};
2011-04-22 22:03:10 +00:00
return that;
2011-04-22 22:03:10 +00:00
};