2011-07-29 18:48:43 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
|
|
|
Ox.Checkbox <f:Ox.Element> Checkbox Element
|
|
|
|
() -> <f> Checkbox Element
|
|
|
|
(options) -> <f> Checkbox Element
|
|
|
|
(options, self) -> <f> Checkbox Element
|
|
|
|
options <o> Options object
|
2011-10-03 15:02:19 +00:00
|
|
|
checked <b> if true, checkbox is checked
|
2011-05-16 10:49:48 +00:00
|
|
|
disabled <b> if true, checkbox is disabled
|
|
|
|
group <b> if true, checkbox is part of a group
|
2011-10-03 15:02:19 +00:00
|
|
|
id <s> element id
|
|
|
|
label <s> Label (on the left side)
|
|
|
|
labelWidth <n|64> Label width
|
|
|
|
title <s> Title (on the right side)
|
2011-05-16 10:49:48 +00:00
|
|
|
width <n> width in px
|
|
|
|
self <o> Shared private variable
|
|
|
|
change <!> triggered when checked property changes, passes {checked, id, title}
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
Ox.Checkbox = 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({
|
2011-10-03 15:02:19 +00:00
|
|
|
checked: false,
|
2011-04-22 22:03:10 +00:00
|
|
|
disabled: false,
|
|
|
|
group: false,
|
2011-10-03 15:02:19 +00:00
|
|
|
id: '',
|
|
|
|
label: '',
|
|
|
|
labelWidth: 64,
|
2011-04-22 22:03:10 +00:00
|
|
|
overlap: 'none',
|
|
|
|
title: '',
|
|
|
|
width: 'auto'
|
|
|
|
})
|
|
|
|
.options(options || {})
|
2011-11-03 15:42:41 +00:00
|
|
|
.addClass('OxCheckbox' + (
|
|
|
|
self.options.overlap == 'none'
|
|
|
|
? '' : ' OxOverlap' + Ox.toTitleCase(self.options.overlap)
|
|
|
|
))
|
2011-10-06 04:42:58 +00:00
|
|
|
.attr({
|
|
|
|
disabled: self.options.disabled
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
if (self.options.title) {
|
|
|
|
self.options.width != 'auto' && that.css({
|
2011-06-04 01:41:47 +00:00
|
|
|
width: self.options.width + 'px'
|
|
|
|
});
|
2011-06-19 17:48:32 +00:00
|
|
|
self.$title = Ox.Label({
|
2011-04-22 22:03:10 +00:00
|
|
|
disabled: self.options.disabled,
|
|
|
|
id: self.options.id + 'Label',
|
|
|
|
overlap: 'left',
|
|
|
|
title: self.options.title,
|
2011-11-02 10:23:15 +00:00
|
|
|
width: getTitleWidth()
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
2011-10-03 15:02:19 +00:00
|
|
|
.css({float: 'right'})
|
2011-04-22 22:03:10 +00:00
|
|
|
.click(clickTitle)
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
|
2011-10-03 15:02:19 +00:00
|
|
|
if (self.options.label) {
|
|
|
|
self.$label = Ox.Label({
|
|
|
|
overlap: 'right',
|
|
|
|
textAlign: 'right',
|
|
|
|
title: self.options.label,
|
|
|
|
width: self.options.labelWidth
|
|
|
|
})
|
|
|
|
.css({float: 'left'})
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self.$button = Ox.Button({
|
2011-04-22 22:03:10 +00:00
|
|
|
disabled: self.options.disabled,
|
|
|
|
id: self.options.id + 'Button',
|
|
|
|
title: [
|
|
|
|
{id: 'none', title: 'none', selected: !self.options.checked},
|
|
|
|
{id: 'check', title: 'check', selected: self.options.checked}
|
|
|
|
],
|
|
|
|
type: 'image'
|
|
|
|
})
|
|
|
|
.addClass('OxCheckbox')
|
2011-10-27 11:20:00 +00:00
|
|
|
.bindEvent({
|
|
|
|
click: clickButton
|
|
|
|
})
|
2011-04-22 22:03:10 +00:00
|
|
|
.appendTo(that);
|
|
|
|
|
|
|
|
function clickButton() {
|
|
|
|
self.options.checked = !self.options.checked;
|
|
|
|
// click will have toggled the button,
|
|
|
|
// if it is part of a group, we have to revert that
|
|
|
|
self.options.group && that.toggleChecked();
|
|
|
|
that.triggerEvent('change', {
|
|
|
|
checked: self.options.checked,
|
|
|
|
id: self.options.id,
|
|
|
|
title: self.options.title
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function clickTitle() {
|
|
|
|
!self.options.disabled && self.$button.trigger('click');
|
|
|
|
}
|
|
|
|
|
2011-11-02 10:23:15 +00:00
|
|
|
function getTitleWidth() {
|
|
|
|
return self.options.width - 16
|
|
|
|
- !!self.options.label * self.options.labelWidth;
|
|
|
|
}
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
self.setOption = function(key, value) {
|
2011-04-22 22:03:10 +00:00
|
|
|
if (key == 'checked') {
|
|
|
|
that.toggleChecked();
|
2011-10-07 16:42:35 +00:00
|
|
|
} else if (key == 'disabled') {
|
2011-10-27 11:20:00 +00:00
|
|
|
that.attr({disabled: value});
|
|
|
|
self.$button.options({disabled: value});
|
|
|
|
self.$title && self.$title.options({disabled: value});
|
2011-10-03 13:23:40 +00:00
|
|
|
} else if (key == 'title') {
|
|
|
|
self.$title.options({title: value});
|
2011-11-02 10:23:15 +00:00
|
|
|
} else if (key == 'width') {
|
|
|
|
that.css({width: value + 'px'});
|
|
|
|
self.$title && self.$title.options({width: getTitleWidth()});
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
|
|
|
checked <f> get current checked state
|
|
|
|
() -> <b> returns self.options.checked
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.checked = function() {
|
|
|
|
return self.options.checked;
|
|
|
|
}
|
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
|
|
|
toggleChecked <f> toggle current checked state
|
|
|
|
() -> <f> toggle state, returns Checkbox Element
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.toggleChecked = function() {
|
|
|
|
self.$button.toggleTitle();
|
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
2011-06-04 01:41:47 +00:00
|
|
|
// fixme: added for forms, duplicated, checked() shouldn't be neccessary
|
|
|
|
that.value = function() {
|
|
|
|
return self.options.checked;
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|