1 /** 2 * @author Gillis Haasnoot <gillis.haasnoot@gmail.com> 3 * @package Banana.Controls 4 * @summary Checkbox list control 5 */ 6 7 goog.provide('Banana.Controls.DataControls.ListControls.CheckBoxList'); 8 9 goog.require('Banana.Controls.DataControls.ListControls.CustomListControl'); 10 11 /** @namespace Banana.Controls.CheckboxList */ 12 namespace('Banana.Controls').CheckboxList = Banana.Controls.CustomListControl.extend( 13 /** @lends Banana.Controls.CheckboxList.prototype */ 14 { 15 /** 16 * Creates a Checkbox list control 17 * use set datasource to populate the list and data to determine which checkbox is selected 18 * @constructs 19 * @extends Banana.Controls.CustomListControl 20 */ 21 init : function() 22 { 23 this._super(); 24 this.addCssClass('BCheckBoxList'); 25 } 26 }); 27 28 /** 29 * Invoked when data is datasource is changed 30 * @override 31 */ 32 Banana.Controls.CheckboxList.prototype.createControls = function() 33 { 34 this.panel = new Banana.Controls.Panel(); 35 this.panel.addCssClass('BCheckBoxListItemContainer'); 36 37 //When we call for data we loop over all created checkboxes 38 this.checkboxReferences = []; 39 40 for(var prop in this.datasource) 41 { 42 if (typeof(this.datasource[prop]) == 'function') continue; 43 44 var checkbox = new Banana.Controls.CheckBox(); 45 checkbox.setEnabled(this.enabled) 46 47 this.checkboxReferences.push(checkbox); 48 49 if (this.datasource[prop][this.dataKeyField]) 50 { 51 checkbox.setValue(this.datasource[prop][this.dataKeyField]); 52 } 53 else 54 { 55 checkbox.setValue(prop); 56 } 57 58 checkbox.setId(prop); 59 60 checkbox.bind('change',this.getProxy(function(sender) 61 { 62 this.isChanged = true; 63 64 this.data = this.getData(); 65 66 this.triggerEvent('dataChanged',this.data); 67 68 }),checkbox); 69 70 if (this.data) 71 { 72 for (var i = 0, len = this.data.length; i < len; i++) 73 { 74 if (this.datasource[prop][this.dataKeyField]) 75 { 76 if (this.datasource[prop][this.dataKeyField] == this.data[i][this.dataKeyField]) 77 { 78 checkbox.setData(true); 79 break; 80 } 81 else if (this.datasource[prop][this.dataKeyField] == this.data[i]) 82 { 83 checkbox.setData(true); 84 break; 85 } 86 } 87 else if(prop == this.data[i]) 88 { 89 checkbox.setData(true); 90 break; 91 } 92 } 93 } 94 95 var control = new Banana.Controls.Decorators.LabelDecoratorRight(checkbox); 96 if (this.datasource[prop][this.dataValueField]) 97 control.setData(this.datasource[prop][this.dataValueField]); 98 else 99 control.setData(this.datasource[prop]); 100 101 this.panel.addControl(control); 102 } 103 104 this.addControl(this.panel); 105 106 this.addControl('<div style="clear:both;"></div>'); 107 }; 108 109 /** 110 * @return {Array} of selected keys 111 */ 112 Banana.Controls.CheckboxList.prototype.getData = function() 113 { 114 var data =[]; 115 116 for (var i=0,len=this.checkboxReferences.length;i<len;i++) 117 { 118 if (this.checkboxReferences[i].data) 119 { 120 data.push(this.checkboxReferences[i].getValue()); 121 } 122 } 123 124 return data; 125 }