1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.form.CheckboxGroup |
---|
11 | * @extends Ext.form.Field |
---|
12 | * A grouping container for {@link Ext.form.Checkbox} controls. |
---|
13 | * @constructor |
---|
14 | * Creates a new CheckboxGroup |
---|
15 | * @param {Object} config Configuration options |
---|
16 | */ |
---|
17 | Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, { |
---|
18 | /** |
---|
19 | * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects |
---|
20 | * to arrange in the group. |
---|
21 | */ |
---|
22 | /** |
---|
23 | * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped |
---|
24 | * checkbox/radio controls using automatic layout. This config can take several types of values: |
---|
25 | * <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width |
---|
26 | * of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li> |
---|
27 | * <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be |
---|
28 | * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li> |
---|
29 | * <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer |
---|
30 | * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will |
---|
31 | * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float |
---|
32 | * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field |
---|
33 | * container you should do so.</p></li></ul> |
---|
34 | */ |
---|
35 | columns : 'auto', |
---|
36 | /** |
---|
37 | * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column |
---|
38 | * top to bottom before starting on the next column. The number of controls in each column will be automatically |
---|
39 | * calculated to keep columns as even as possible. The default value is false, so that controls will be added |
---|
40 | * to columns one at a time, completely filling each row left to right before starting on the next row. |
---|
41 | */ |
---|
42 | vertical : false, |
---|
43 | /** |
---|
44 | * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true). |
---|
45 | * If no items are selected at validation time, {@link @blankText} will be used as the error text. |
---|
46 | */ |
---|
47 | allowBlank : true, |
---|
48 | /** |
---|
49 | * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must |
---|
50 | * select at least one item in this group") |
---|
51 | */ |
---|
52 | blankText : "You must select at least one item in this group", |
---|
53 | |
---|
54 | // private |
---|
55 | defaultType : 'checkbox', |
---|
56 | |
---|
57 | // private |
---|
58 | groupCls: 'x-form-check-group', |
---|
59 | |
---|
60 | // private |
---|
61 | onRender : function(ct, position){ |
---|
62 | if(!this.el){ |
---|
63 | var panelCfg = { |
---|
64 | cls: this.groupCls, |
---|
65 | layout: 'column', |
---|
66 | border: false, |
---|
67 | renderTo: ct |
---|
68 | }; |
---|
69 | var colCfg = { |
---|
70 | defaultType: this.defaultType, |
---|
71 | layout: 'form', |
---|
72 | border: false, |
---|
73 | defaults: { |
---|
74 | hideLabel: true, |
---|
75 | anchor: '100%' |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | if(this.items[0].items){ |
---|
80 | |
---|
81 | // The container has standard ColumnLayout configs, so pass them in directly |
---|
82 | |
---|
83 | Ext.apply(panelCfg, { |
---|
84 | layoutConfig: {columns: this.items.length}, |
---|
85 | defaults: this.defaults, |
---|
86 | items: this.items |
---|
87 | }) |
---|
88 | for(var i=0, len=this.items.length; i<len; i++){ |
---|
89 | Ext.applyIf(this.items[i], colCfg); |
---|
90 | }; |
---|
91 | |
---|
92 | }else{ |
---|
93 | |
---|
94 | // The container has field item configs, so we have to generate the column |
---|
95 | // panels first then move the items into the columns as needed. |
---|
96 | |
---|
97 | var numCols, cols = []; |
---|
98 | |
---|
99 | if(typeof this.columns == 'string'){ // 'auto' so create a col per item |
---|
100 | this.columns = this.items.length; |
---|
101 | } |
---|
102 | if(!Ext.isArray(this.columns)){ |
---|
103 | var cs = []; |
---|
104 | for(var i=0; i<this.columns; i++){ |
---|
105 | cs.push((100/this.columns)*.01); // distribute by even % |
---|
106 | } |
---|
107 | this.columns = cs; |
---|
108 | } |
---|
109 | |
---|
110 | numCols = this.columns.length; |
---|
111 | |
---|
112 | // Generate the column configs with the correct width setting |
---|
113 | for(var i=0; i<numCols; i++){ |
---|
114 | var cc = Ext.apply({items:[]}, colCfg); |
---|
115 | cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i]; |
---|
116 | if(this.defaults){ |
---|
117 | cc.defaults = Ext.apply(cc.defaults || {}, this.defaults) |
---|
118 | } |
---|
119 | cols.push(cc); |
---|
120 | }; |
---|
121 | |
---|
122 | // Distribute the original items into the columns |
---|
123 | if(this.vertical){ |
---|
124 | var rows = Math.ceil(this.items.length / numCols), ri = 0; |
---|
125 | for(var i=0, len=this.items.length; i<len; i++){ |
---|
126 | if(i>0 && i%rows==0){ |
---|
127 | ri++; |
---|
128 | } |
---|
129 | if(this.items[i].fieldLabel){ |
---|
130 | this.items[i].hideLabel = false; |
---|
131 | } |
---|
132 | cols[ri].items.push(this.items[i]); |
---|
133 | }; |
---|
134 | }else{ |
---|
135 | for(var i=0, len=this.items.length; i<len; i++){ |
---|
136 | var ci = i % numCols; |
---|
137 | if(this.items[i].fieldLabel){ |
---|
138 | this.items[i].hideLabel = false; |
---|
139 | } |
---|
140 | cols[ci].items.push(this.items[i]); |
---|
141 | }; |
---|
142 | } |
---|
143 | |
---|
144 | Ext.apply(panelCfg, { |
---|
145 | layoutConfig: {columns: numCols}, |
---|
146 | items: cols |
---|
147 | }); |
---|
148 | } |
---|
149 | |
---|
150 | this.panel = new Ext.Panel(panelCfg); |
---|
151 | this.el = this.panel.getEl(); |
---|
152 | |
---|
153 | if(this.forId && this.itemCls){ |
---|
154 | var l = this.el.up(this.itemCls).child('label', true); |
---|
155 | if(l){ |
---|
156 | l.setAttribute('htmlFor', this.forId); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | var fields = this.panel.findBy(function(c){ |
---|
161 | return c.isFormField; |
---|
162 | }, this); |
---|
163 | |
---|
164 | this.items = new Ext.util.MixedCollection(); |
---|
165 | this.items.addAll(fields); |
---|
166 | } |
---|
167 | Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position); |
---|
168 | }, |
---|
169 | |
---|
170 | // private |
---|
171 | validateValue : function(value){ |
---|
172 | if(!this.allowBlank){ |
---|
173 | var blank = true; |
---|
174 | this.items.each(function(f){ |
---|
175 | if(f.checked){ |
---|
176 | return blank = false; |
---|
177 | } |
---|
178 | }, this); |
---|
179 | if(blank){ |
---|
180 | this.markInvalid(this.blankText); |
---|
181 | return false; |
---|
182 | } |
---|
183 | } |
---|
184 | return true; |
---|
185 | }, |
---|
186 | |
---|
187 | // private |
---|
188 | onDisable : function(){ |
---|
189 | this.items.each(function(item){ |
---|
190 | item.disable(); |
---|
191 | }) |
---|
192 | }, |
---|
193 | |
---|
194 | // private |
---|
195 | onEnable : function(){ |
---|
196 | this.items.each(function(item){ |
---|
197 | item.enable(); |
---|
198 | }) |
---|
199 | }, |
---|
200 | |
---|
201 | // private |
---|
202 | onResize : function(w, h){ |
---|
203 | this.panel.setSize(w, h); |
---|
204 | this.panel.doLayout(); |
---|
205 | }, |
---|
206 | |
---|
207 | // inherit docs from Field |
---|
208 | reset : function(){ |
---|
209 | Ext.form.CheckboxGroup.superclass.reset.call(this); |
---|
210 | this.items.each(function(c){ |
---|
211 | if(c.reset){ |
---|
212 | c.reset(); |
---|
213 | } |
---|
214 | }, this); |
---|
215 | }, |
---|
216 | |
---|
217 | /** |
---|
218 | * @cfg {String} name |
---|
219 | * @hide |
---|
220 | */ |
---|
221 | /** |
---|
222 | * @method initValue |
---|
223 | * @hide |
---|
224 | */ |
---|
225 | initValue : Ext.emptyFn, |
---|
226 | /** |
---|
227 | * @method getValue |
---|
228 | * @hide |
---|
229 | */ |
---|
230 | getValue : Ext.emptyFn, |
---|
231 | /** |
---|
232 | * @method getRawValue |
---|
233 | * @hide |
---|
234 | */ |
---|
235 | getRawValue : Ext.emptyFn, |
---|
236 | /** |
---|
237 | * @method setValue |
---|
238 | * @hide |
---|
239 | */ |
---|
240 | setValue : Ext.emptyFn, |
---|
241 | /** |
---|
242 | * @method setRawValue |
---|
243 | * @hide |
---|
244 | */ |
---|
245 | setRawValue : Ext.emptyFn |
---|
246 | |
---|
247 | }); |
---|
248 | |
---|
249 | Ext.reg('checkboxgroup', Ext.form.CheckboxGroup); |
---|