From 8597352316710e40fec9541a41e58fb63eaea930 Mon Sep 17 00:00:00 2001 From: rolux Date: Wed, 30 Nov 2011 15:47:43 +0100 Subject: [PATCH] add type option to CheckboxGroup ('group' or 'list') --- source/Ox.UI/js/Form/Ox.CheckboxGroup.js | 57 +++++++++++++++--------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/source/Ox.UI/js/Form/Ox.CheckboxGroup.js b/source/Ox.UI/js/Form/Ox.CheckboxGroup.js index f6cd2bba..4bb2e8e1 100644 --- a/source/Ox.UI/js/Form/Ox.CheckboxGroup.js +++ b/source/Ox.UI/js/Form/Ox.CheckboxGroup.js @@ -6,10 +6,11 @@ Ox.CheckboxGroup CheckboxGroup Object (options) -> CheckboxGroup Object (options, self) -> CheckboxGroup Object options Options object - checkboxes array of checkboxes - max integer - min integer - width integer, width in px + checkboxes array of checkboxes + max max selected + min min selected + type type ("group" or "list") + width width in px self shared private variable change triggered when checked property changes passes {checked, id, title} @@ -20,48 +21,52 @@ Ox.CheckboxGroup = function(options, self) { self = self || {}; var that = Ox.Element({}, self) .defaults({ + // fixme: should 'checkboxes' be 'items'? checkboxes: [], max: 1, min: 1, + type: 'group', width: 256 }) .options(options || {}) - .addClass('OxCheckboxGroup'); + .addClass('OxCheckboxGroup Ox' + Ox.toTitleCase(self.options.type)); self.optionGroup = new Ox.OptionGroup( self.options.checkboxes, self.options.min, - self.options.max); + self.options.max + ); self.options.checkboxes = self.optionGroup.init(); - Ox.extend(self, { - $checkboxes: [], - checkboxWidth: Ox.divideInt( + self.$checkboxes = []; + if (self.options.type == 'group') { + self.checkboxWidth = Ox.divideInt( self.options.width + (self.options.checkboxes.length - 1) * 6, self.options.checkboxes.length ).map(function(v, i) { return v + (i < self.options.checkboxes.length - 1 ? 10 : 0); - }) - }); - self.options.checkboxes.forEach(function(checkbox, position) { - var id = self.options.id + Ox.toTitleCase(checkbox.id) - self.$checkboxes[position] = Ox.Checkbox(Ox.extend(checkbox, { + }); + }; + + self.options.checkboxes.forEach(function(checkbox, index) { + self.$checkboxes[index] = Ox.Checkbox(Ox.extend(checkbox, { group: true, - id: id, - width: self.checkboxWidth[position] + id: checkbox.id, + width: self.options.type == 'group' + ? self.checkboxWidth[index] : self.options.width })) .bindEvent('change', function() { - change(position); + change(index); }) .appendTo(that); }); - function change(pos) { - var toggled = self.optionGroup.toggle(pos); - //Ox.Log('Form', 'change', pos, 'toggled', toggled) + function change(index) { + var toggled = self.optionGroup.toggle(index); + //Ox.Log('Form', 'change', index, 'toggled', toggled) if (toggled.length) { - toggled.forEach(function(pos, i) { - self.$checkboxes[pos].toggleChecked(); + toggled.forEach(function(index, i) { + self.$checkboxes[index].toggleChecked(); }); that.triggerEvent('change', { checked: self.optionGroup.checked().map(function(v) { @@ -71,6 +76,14 @@ Ox.CheckboxGroup = function(options, self) { } } + that.value = function() { + return self.options.checkboxes.filter(function(checkbox) { + return checkbox.checked; + }).map(function(checkbox) { + return checkbox.id; + }); + }; + return that; };