/ext-4.0.7/src/layout/container/CheckboxGroup.js

https://bitbucket.org/srogerf/javascript · JavaScript · 154 lines · 91 code · 19 blank · 44 comment · 16 complexity · 6d578ce23463543ccf5aa33d1fa25f34 MD5 · raw file

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