113 lines
3.9 KiB
JavaScript
113 lines
3.9 KiB
JavaScript
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
|
'use strict';
|
|
/*@
|
|
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
|
|
@*/
|
|
|
|
Ox.ButtonGroup = function(options, self) {
|
|
|
|
self = self || {};
|
|
var that = Ox.Element({}, self)
|
|
.defaults({
|
|
buttons: [],
|
|
max: 1,
|
|
min: 1,
|
|
selectable: false,
|
|
size: 'medium',
|
|
style: '',
|
|
type: 'text',
|
|
value: options.max != 1 ? [] : ''
|
|
})
|
|
.options(options || {})
|
|
.addClass('OxButtonGroup');
|
|
|
|
self.options.buttons = self.options.buttons.map(function(button) {
|
|
return Ox.extend({
|
|
disabled: button.disabled,
|
|
id: button.id || button,
|
|
title: button.title || button,
|
|
tooltip: button.tooltip,
|
|
width: button.width
|
|
}, self.options.selectable ? {
|
|
selected: Ox.toArray(self.options.value).indexOf(button.id || button) > -1
|
|
} : {});
|
|
});
|
|
|
|
if (self.options.selectable) {
|
|
self.optionGroup = new Ox.OptionGroup(
|
|
self.options.buttons,
|
|
self.options.min,
|
|
self.options.max,
|
|
'selected'
|
|
);
|
|
self.options.buttons = self.optionGroup.init();
|
|
self.options.value = self.optionGroup.value();
|
|
}
|
|
|
|
self.$buttons = [];
|
|
self.options.buttons.forEach(function(button, pos) {
|
|
var id = self.options.id + Ox.toTitleCase(button.id);
|
|
self.$buttons[pos] = Ox.Button({
|
|
disabled: button.disabled,
|
|
group: true,
|
|
id: id,
|
|
selectable: self.options.selectable,
|
|
size: self.options.size,
|
|
style: self.options.style,
|
|
title: button.title,
|
|
tooltip: button.tooltip,
|
|
type: self.options.type,
|
|
value: button.selected,
|
|
width: button.width
|
|
})
|
|
.bindEvent('change', function() {
|
|
self.options.selectable && toggleButton(pos);
|
|
})
|
|
.appendTo(that);
|
|
});
|
|
|
|
function toggleButton(pos) {
|
|
var toggled = self.optionGroup.toggle(pos);
|
|
if (!toggled.length) {
|
|
// FIXME: fix and use that.toggleOption()
|
|
self.$buttons[pos].value(!self.$buttons[pos].value());
|
|
} else {
|
|
toggled.forEach(function(i) {
|
|
i != pos && self.$buttons[i].value(!self.$buttons[i].value());
|
|
});
|
|
self.options.value = self.optionGroup.value();
|
|
that.triggerEvent('change', {
|
|
title: Ox.isString(self.options.value)
|
|
? Ox.getObjectById(self.options.buttons, self.options.value).title
|
|
: self.options.value.map(function(value) {
|
|
return Ox.getObjectById(self.options.buttons, value).title;
|
|
}),
|
|
value: self.options.value
|
|
});
|
|
}
|
|
}
|
|
|
|
self.setOption = function(key, value) {
|
|
if (key == 'value') {
|
|
// fixme: this doesn't work in cases where
|
|
// multiple buttons can be selected
|
|
var position = Ox.getIndexById(self.options.buttons, value);
|
|
if (position > -1) {
|
|
self.$buttons[position].trigger('click');
|
|
}
|
|
}
|
|
}
|
|
return that;
|
|
|
|
};
|