oxjs/source/UI/js/Form/Checkbox.js

146 lines
4.9 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 10:49:48 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.Checkbox <f> Checkbox Element
2011-05-16 10:49:48 +00:00
options <o> Options object
disabled <b> if true, checkbox is disabled
group <b> if true, checkbox is part of a group
2016-02-10 07:26:19 +00:00
indeterminate <b> if true, checkbox appears as indeterminate
label <s> Label (on the left side)
labelWidth <n|64> Label width
title <s> Title (on the right side)
2011-12-21 13:42:47 +00:00
value <b> if true, checkbox is checked
2011-05-16 10:49:48 +00:00
width <n> width in px
self <o> Shared private variable
([options[, self]]) -> <o:Ox.Element> Checkbox Element
change <!> triggered when value changes
2011-05-16 10:49:48 +00:00
@*/
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
self = self || {};
var that = Ox.Element({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
disabled: false,
group: false,
2016-02-10 07:26:19 +00:00
indeterminate: false,
label: '',
labelWidth: 64,
2011-04-22 22:03:10 +00:00
overlap: 'none',
2016-01-12 04:20:00 +00:00
style: 'rounded',
2011-04-22 22:03:10 +00:00
title: '',
2011-12-21 13:42:47 +00:00
value: false,
width: options && (options.label || options.title) ? 'auto' : 16
2011-04-22 22:03:10 +00:00
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
disabled: function() {
var disabled = self.options.disabled;
that.attr({disabled: disabled});
self.$button.options({disabled: disabled});
self.$title && self.$title.options({disabled: disabled});
},
2016-02-10 07:26:19 +00:00
indeterminate: function() {
if (self.options.indeterminate) {
2016-02-10 07:38:10 +00:00
self.$button.options({values: ['remove']});
self.$button.options({value: 'remove'});
2016-02-10 07:26:19 +00:00
} else {
2016-02-10 07:33:36 +00:00
self.$button.options({values: ['none', 'check']});
2016-02-10 07:47:17 +00:00
self.$button.options({
2016-02-10 07:54:57 +00:00
value: self.options.value ? 'check' : 'none'
2016-02-10 07:47:17 +00:00
});
2016-02-10 07:26:19 +00:00
}
},
2014-02-16 07:06:48 +00:00
label: function() {
self.$label.options({title: self.options.label});
},
2012-05-28 19:35:41 +00:00
title: function() {
self.$title.options({title: self.options.title});
},
value: function() {
self.$button.toggle();
},
width: function() {
that.css({width: self.options.width + 'px'});
self.$title && self.$title.options({width: getTitleWidth()});
}
})
2011-11-03 15:42:41 +00:00
.addClass('OxCheckbox' + (
self.options.overlap == 'none'
? '' : ' OxOverlap' + Ox.toTitleCase(self.options.overlap)
))
.attr({
disabled: self.options.disabled
2012-07-09 14:00:14 +00:00
})
.css(self.options.width != 'auto' ? {
width: self.options.width
} : {});
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'
});
self.$title = Ox.Label({
2011-04-22 22:03:10 +00:00
disabled: self.options.disabled,
id: self.options.id + 'Label',
overlap: 'left',
2016-01-12 04:20:00 +00:00
style: self.options.style,
2011-04-22 22:03:10 +00:00
title: self.options.title,
2011-11-02 10:23:15 +00:00
width: getTitleWidth()
2011-04-22 22:03:10 +00:00
})
.css({float: 'right'})
2013-12-06 20:43:00 +00:00
.on({click: clickTitle})
2011-04-22 22:03:10 +00:00
.appendTo(that);
}
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);
}
self.$button = Ox.Button({
2011-04-22 22:03:10 +00:00
disabled: self.options.disabled,
id: self.options.id + 'Button',
2016-01-12 05:41:08 +00:00
style: self.options.style != 'rounded' ? self.options.style : '',
2011-12-21 13:42:47 +00:00
type: 'image',
2016-02-10 07:38:10 +00:00
value: self.options.indeterminate ? 'remove'
2016-02-10 07:26:19 +00:00
: self.options.value ? 'check' : 'none',
2016-02-10 07:38:10 +00:00
values: self.options.indeterminate ? ['remove'] : ['none', 'check']
2011-04-22 22:03:10 +00:00
})
.addClass('OxCheckbox')
.bindEvent({
2011-12-21 13:42:47 +00:00
change: clickButton
})
2011-04-22 22:03:10 +00:00
.appendTo(that);
function clickButton() {
2011-12-21 13:42:47 +00:00
self.options.value = !self.options.value;
2016-02-10 07:54:57 +00:00
if (self.options.indeterminate) {
self.options.indeterminate = false;
self.$button.options({values: ['none', 'check']});
self.$button.options({value: self.options.value ? 'check' : 'none'});
}
2011-04-22 22:03:10 +00:00
that.triggerEvent('change', {
2011-12-21 13:42:47 +00:00
value: self.options.value
2011-04-22 22:03:10 +00:00
});
}
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-22 22:03:10 +00:00
return that;
};