// vim: et:ts=4:sw=4:sts=4:ft=javascript /*@ Ox.Checkbox Checkbox Element () -> Checkbox Element (options) -> Checkbox Element (options, self) -> Checkbox Element options Options object checked if true, checkbox is checked disabled if true, checkbox is disabled group if true, checkbox is part of a group id element id label Label (on the left side) labelWidth Label width title Title (on the right side) width width in px self Shared private variable change triggered when checked property changes, passes {checked, id, title} @*/ Ox.Checkbox = function(options, self) { self = self || {}; var that = Ox.Element({}, self) .defaults({ checked: false, disabled: false, group: false, id: '', label: '', labelWidth: 64, overlap: 'none', title: '', width: 'auto' }) .options(options || {}) .addClass('OxCheckbox' + ( self.options.overlap == 'none' ? '' : ' OxOverlap' + Ox.toTitleCase(self.options.overlap) )) .attr({ disabled: self.options.disabled }); if (self.options.title) { self.options.width != 'auto' && that.css({ width: self.options.width + 'px' }); self.$title = Ox.Label({ disabled: self.options.disabled, id: self.options.id + 'Label', overlap: 'left', title: self.options.title, width: getTitleWidth() }) .css({float: 'right'}) .click(clickTitle) .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({ 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') .bindEvent({ click: clickButton }) .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'); } function getTitleWidth() { return self.options.width - 16 - !!self.options.label * self.options.labelWidth; } self.setOption = function(key, value) { if (key == 'checked') { that.toggleChecked(); } else if (key == 'disabled') { that.attr({disabled: value}); self.$button.options({disabled: value}); self.$title && self.$title.options({disabled: value}); } else if (key == 'title') { self.$title.options({title: value}); } else if (key == 'width') { that.css({width: value + 'px'}); self.$title && self.$title.options({width: getTitleWidth()}); } }; /*@ checked get current checked state () -> returns self.options.checked @*/ that.checked = function() { return self.options.checked; } /*@ toggleChecked toggle current checked state () -> toggle state, returns Checkbox Element @*/ that.toggleChecked = function() { self.$button.toggleTitle(); return that; } // fixme: added for forms, duplicated, checked() shouldn't be neccessary that.value = function() { return self.options.checked; } return that; };