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
|
|
|
|
2011-06-19 17:48:32 +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',
|
2011-12-21 13:42:47 +00:00
|
|
|
value: options.max != 1 ? [] : ''
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.addClass('OxButtonGroup');
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.buttons = self.options.buttons.map(function(button) {
|
|
|
|
return Ox.extend({
|
|
|
|
id: button.id || button,
|
|
|
|
title: button.title || button
|
|
|
|
}, self.options.selectable ? {
|
|
|
|
selected: Ox.toArray(self.options.value).indexOf(button.id || button) > -1
|
|
|
|
} : {});
|
|
|
|
});
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
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();
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.value = self.optionGroup.value();
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.$buttons = [];
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.buttons.forEach(function(button, pos) {
|
2011-04-22 22:03:10 +00:00
|
|
|
var id = self.options.id + Ox.toTitleCase(button.id)
|
2011-12-21 13:42:47 +00:00
|
|
|
self.$buttons[pos] = Ox.Button({
|
2011-04-22 22:03:10 +00:00
|
|
|
disabled: button.disabled,
|
|
|
|
group: true,
|
|
|
|
id: id,
|
|
|
|
selectable: self.options.selectable,
|
|
|
|
size: self.options.size,
|
|
|
|
style: self.options.style,
|
|
|
|
title: button.title,
|
2011-12-18 09:29:35 +00:00
|
|
|
tooltip: button.tooltip,
|
2011-12-21 13:42:47 +00:00
|
|
|
type: self.options.type,
|
|
|
|
value: button.selected
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
2011-12-21 13:42:47 +00:00
|
|
|
.bindEvent('change', function() {
|
|
|
|
self.options.selectable && toggleButton(pos);
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
});
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function toggleButton(pos) {
|
2011-04-22 22:03:10 +00:00
|
|
|
var toggled = self.optionGroup.toggle(pos);
|
2011-12-21 13:42:47 +00:00
|
|
|
if (!toggled.length) {
|
2011-12-21 15:33:52 +00:00
|
|
|
// FIXME: fix and use that.toggleOption()
|
|
|
|
self.$buttons[pos].value(!self.$buttons[pos].value());
|
2011-12-21 13:42:47 +00:00
|
|
|
} else {
|
|
|
|
toggled.forEach(function(i) {
|
2011-12-21 15:33:52 +00:00
|
|
|
i != pos && self.$buttons[i].value(!self.$buttons[i].value());
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.value = self.optionGroup.value();
|
2011-04-22 22:03:10 +00:00
|
|
|
that.triggerEvent('change', {
|
2011-12-21 15:33:52 +00:00
|
|
|
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;
|
|
|
|
}),
|
2011-12-21 13:42:47 +00:00
|
|
|
value: self.options.value
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
2011-11-06 14:08:13 +00:00
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|