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

170 lines
5.8 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2012-05-21 10:38:18 +00:00
2011-05-16 08:24:46 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.ButtonGroup <f> ButtonGroup Object
2011-05-16 08:24:46 +00:00
options <o> Options object
2012-05-31 10:32:54 +00:00
buttons <[o]> array of button options
2011-05-16 08:24:46 +00:00
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
([options[, self]]) -> <o:Ox.Element> ButtonGroup Object
change <!> {id, value} selection within a group changed
2011-05-16 08:24:46 +00:00
@*/
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,
2012-12-09 00:43:33 +00:00
overlap: 'none',
2011-04-22 22:03:10 +00:00
selectable: false,
size: 'medium',
style: 'default',
2011-04-22 22:03:10 +00:00
type: 'text',
2011-12-21 13:42:47 +00:00
value: options.max != 1 ? [] : ''
2011-04-22 22:03:10 +00:00
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
value: function() {
2023-10-07 09:22:56 +00:00
var positions = [],
values = Ox.makeArray(self.options.value);
Ox.forEach(values, function(value) {
var position = Ox.getIndexById(
self.options.buttons, value
);
if (position > -1) {
positions.push(position);
}
});
if (positions.length < self.options.min) {
return;
2012-05-28 19:35:41 +00:00
}
Ox.forEach(self.$buttons, function(button, pos) {
var enabled = positions.indexOf(pos) > -1;
if (enabled !== button.value()) {
button.trigger('click');
}
});
2012-05-28 19:35:41 +00:00
}
})
2012-12-09 00:43:33 +00:00
.addClass(
'OxButtonGroup'
2014-01-05 10:00:46 +00:00
+ (self.options.style != 'default' ? ' Ox' + Ox.toTitleCase(self.options.style) : '')
2012-12-09 00:43:33 +00:00
+ (self.options.overlap != 'none' ? ' OxOverlap' + Ox.toTitleCase(self.options.overlap) : '')
);
2011-04-22 22:03:10 +00:00
2012-12-09 00:43:33 +00:00
self.options.buttons = self.options.buttons.map(function(button, i) {
2011-12-21 13:42:47 +00:00
return Ox.extend({
2011-12-22 05:52:46 +00:00
disabled: button.disabled,
2011-12-21 13:42:47 +00:00
id: button.id || button,
2012-12-09 00:43:33 +00:00
overlap: self.options.overlap == 'left' && i == 0 ? 'left'
: self.options.overlap == 'right' && i == self.options.buttons.length - 1 ? 'right'
: 'none',
2016-01-12 05:41:08 +00:00
style: self.options.style,
2011-12-22 05:52:46 +00:00
title: button.title || button,
tooltip: button.tooltip,
width: button.width
2011-12-21 13:42:47 +00:00
}, self.options.selectable ? {
selected: Ox.makeArray(self.options.value).indexOf(button.id || button) > -1
2011-12-21 13:42:47 +00:00
} : {});
});
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) {
self.$buttons[pos] = Ox.Button({
disabled: button.disabled || false, // FIXME: getset should handle undefined
2011-04-22 22:03:10 +00:00
group: true,
id: button.id,
2012-12-09 00:43:33 +00:00
overlap: button.overlap,
2011-04-22 22:03:10 +00:00
selectable: self.options.selectable,
size: self.options.size,
2014-05-05 22:13:25 +00:00
style: self.options.style == 'squared' ? 'default' : self.options.style, // FIXME: ugly
2011-04-22 22:03:10 +00:00
title: button.title,
tooltip: button.tooltip,
2011-12-21 13:42:47 +00:00
type: self.options.type,
value: button.selected || false, // FIXME: getset should handle undefined
width: button.width
2011-04-22 22:03:10 +00:00
})
.bindEvent(self.options.selectable ? {
change: function() {
toggleButton(pos);
}
} : {
click: function() {
that.triggerEvent('click', {id: button.id});
}
2011-04-22 22:03:10 +00:00
})
.appendTo(that);
});
function getButtonById(id) {
return self.$buttons[Ox.getIndexById(self.options.buttons, id)];
}
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', {
title: self.options.value === '' ? ''
: Ox.isString(self.options.value)
2011-12-21 15:33:52 +00:00
? 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
});
}
}
2012-06-17 22:38:26 +00:00
/*@
disableButton <f> Disable button
2012-06-17 22:38:26 +00:00
@*/
that.disableButton = function(id) {
getButtonById(id).options({disabled: true});
};
2012-06-17 22:38:26 +00:00
/*@
enableButton <f> Enable button
2012-06-17 22:38:26 +00:00
@*/
that.enableButton = function(id) {
getButtonById(id).options({disabled: false});
};
/*
buttonOptions <f> Get or set button options
*/
that.buttonOptions = function(id, options) {
return getButtonById(id).options(options);
};
2011-04-22 22:03:10 +00:00
return that;
2011-04-22 22:03:10 +00:00
};