/ext-4.0.7/src/layout/container/CheckboxGroup.js
JavaScript | 154 lines | 91 code | 19 blank | 44 comment | 16 complexity | 6d578ce23463543ccf5aa33d1fa25f34 MD5 | raw file
1/*
2
3This file is part of Ext JS 4
4
5Copyright (c) 2011 Sencha Inc
6
7Contact: http://www.sencha.com/contact
8
9GNU General Public License Usage
10This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14*/
15/**
16 * @class Ext.layout.container.CheckboxGroup
17 * @extends Ext.layout.container.Container
18 * <p>This layout implements the column arrangement for {@link Ext.form.CheckboxGroup} and {@link Ext.form.RadioGroup}.
19 * It groups the component's sub-items into columns based on the component's
20 * {@link Ext.form.CheckboxGroup#columns columns} and {@link Ext.form.CheckboxGroup#vertical} config properties.</p>
21 *
22 */
23Ext.define('Ext.layout.container.CheckboxGroup', {
24 extend: 'Ext.layout.container.Container',
25 alias: ['layout.checkboxgroup'],
26
27
28 onLayout: function() {
29 var numCols = this.getColCount(),
30 shadowCt = this.getShadowCt(),
31 owner = this.owner,
32 items = owner.items,
33 shadowItems = shadowCt.items,
34 numItems = items.length,
35 colIndex = 0,
36 i, numRows;
37
38 // Distribute the items into the appropriate column containers. We add directly to the
39 // containers' items collection rather than calling container.add(), because we need the
40 // checkboxes to maintain their original ownerCt. The distribution is done on each layout
41 // in case items have been added, removed, or reordered.
42
43 shadowItems.each(function(col) {
44 col.items.clear();
45 });
46
47 // If columns="auto", then the number of required columns may change as checkboxes are added/removed
48 // from the CheckboxGroup; adjust to match.
49 while (shadowItems.length > numCols) {
50 shadowCt.remove(shadowItems.last());
51 }
52 while (shadowItems.length < numCols) {
53 shadowCt.add({
54 xtype: 'container',
55 cls: owner.groupCls,
56 flex: 1
57 });
58 }
59
60 if (owner.vertical) {
61 numRows = Math.ceil(numItems / numCols);
62 for (i = 0; i < numItems; i++) {
63 if (i > 0 && i % numRows === 0) {
64 colIndex++;
65 }
66 shadowItems.getAt(colIndex).items.add(items.getAt(i));
67 }
68 } else {
69 for (i = 0; i < numItems; i++) {
70 colIndex = i % numCols;
71 shadowItems.getAt(colIndex).items.add(items.getAt(i));
72 }
73 }
74
75 if (!shadowCt.rendered) {
76 shadowCt.render(this.getRenderTarget());
77 } else {
78 // Ensure all items are rendered in the correct place in the correct column - this won't
79 // get done by the column containers themselves if their dimensions are not changing.
80 shadowItems.each(function(col) {
81 var layout = col.getLayout();
82 layout.renderItems(layout.getLayoutItems(), layout.getRenderTarget());
83 });
84 }
85
86 shadowCt.doComponentLayout();
87 },
88
89
90 // We don't want to render any items to the owner directly, that gets handled by each column's own layout
91 renderItems: Ext.emptyFn,
92
93
94 /**
95 * @private
96 * Creates and returns the shadow hbox container that will be used to arrange the owner's items
97 * into columns.
98 */
99 getShadowCt: function() {
100 var me = this,
101 shadowCt = me.shadowCt,
102 owner, items, item, columns, columnsIsArray, numCols, i;
103
104 if (!shadowCt) {
105 // Create the column containers based on the owner's 'columns' config
106 owner = me.owner;
107 columns = owner.columns;
108 columnsIsArray = Ext.isArray(columns);
109 numCols = me.getColCount();
110 items = [];
111 for(i = 0; i < numCols; i++) {
112 item = {
113 xtype: 'container',
114 cls: owner.groupCls
115 };
116 if (columnsIsArray) {
117 // Array can contain mixture of whole numbers, used as fixed pixel widths, and fractional
118 // numbers, used as relative flex values.
119 if (columns[i] < 1) {
120 item.flex = columns[i];
121 } else {
122 item.width = columns[i];
123 }
124 }
125 else {
126 // All columns the same width
127 item.flex = 1;
128 }
129 items.push(item);
130 }
131
132 // Create the shadow container; delay rendering until after items are added to the columns
133 shadowCt = me.shadowCt = Ext.createWidget('container', {
134 layout: 'hbox',
135 items: items,
136 ownerCt: owner
137 });
138 }
139
140 return shadowCt;
141 },
142
143
144 /**
145 * @private Get the number of columns in the checkbox group
146 */
147 getColCount: function() {
148 var owner = this.owner,
149 colsCfg = owner.columns;
150 return Ext.isArray(colsCfg) ? colsCfg.length : (Ext.isNumber(colsCfg) ? colsCfg : owner.items.length);
151 }
152
153});
154