78a3be9dc880e0f0f7fb2f1953f72ea12376f58e.svn-base 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.layout.FormLayout
  8. * @extends Ext.layout.AnchorLayout
  9. * <p>
  10. * This is a layout specifically designed for creating forms. This
  11. * class can be extended or created via the layout:'form'
  12. * {@link Ext.Container#layout} config, and should generally not need
  13. * to be created directly via the new keyword. However, when used in an
  14. * application, it will usually be preferrable to use a
  15. * {@link Ext.form.FormPanel} (which automatically uses FormLayout as
  16. * its layout class) since it also provides built-in functionality for
  17. * loading, validating and submitting the form.
  18. * </p>
  19. * <p>
  20. * Note that when creating a layout via config, the layout-specific
  21. * config properties must be passed in via the
  22. * {@link Ext.Container#layoutConfig} object which will then be applied
  23. * internally to the layout. The container using the FormLayout can
  24. * also supply the following form-specific config properties which will
  25. * be applied by the layout:
  26. * <ul>
  27. * <li><b>hideLabels</b>: (Boolean) True to hide field labels by
  28. * default (defaults to false)</li>
  29. * <li><b>itemCls</b>: (String) A CSS class to add to the div wrapper
  30. * that contains each field label and field element (the default class
  31. * is 'x-form-item' and itemCls will be added to that)</li>
  32. * <li><b>labelAlign</b>: (String) The default label alignment. The
  33. * default value is empty string '' for left alignment, but specifying
  34. * 'top' will align the labels above the fields.</li>
  35. * <li><b>labelPad</b>: (Number) The default padding in pixels for
  36. * field labels (defaults to 5). labelPad only applies if labelWidth is
  37. * also specified, otherwise it will be ignored.</li>
  38. * <li><b>labelWidth</b>: (Number) The default width in pixels of
  39. * field labels (defaults to 100)</li>
  40. * </ul>
  41. * </p>
  42. * <p>
  43. * Any type of components can be added to a FormLayout, but items that
  44. * inherit from {@link Ext.form.Field} can also supply the following
  45. * field-specific config properties:
  46. * <ul>
  47. * <li><b>clearCls</b>: (String) The CSS class to apply to the
  48. * special clearing div rendered directly after each form field wrapper
  49. * (defaults to 'x-form-clear-left')</li>
  50. * <li><b>fieldLabel</b>: (String) The text to display as the label
  51. * for this field (defaults to '')</li>
  52. * <li><b>hideLabel</b>: (Boolean) True to hide the label and
  53. * separator for this field (defaults to false).</li>
  54. * <li><b>itemCls</b>: (String) A CSS class to add to the div wrapper
  55. * that contains this field label and field element (the default class
  56. * is 'x-form-item' and itemCls will be added to that). If supplied,
  57. * itemCls at the field level will override the default itemCls
  58. * supplied at the container level.</li>
  59. * <li><b>labelSeparator</b>: (String) The separator to display after
  60. * the text of the label for this field (defaults to a colon ':' or the
  61. * layout's value for {@link #labelSeparator}). To hide the separator
  62. * use empty string ''.</li>
  63. * <li><b>labelStyle</b>: (String) A CSS style specification string
  64. * to add to the field label for this field (defaults to '' or the
  65. * layout's value for {@link #labelStyle}).</li>
  66. * </ul>
  67. * Example usage:
  68. * </p>
  69. *
  70. * <pre><code>
  71. * // Required if showing validation messages
  72. * Ext.QuickTips.init();
  73. *
  74. * // While you can create a basic Panel with layout:'form', practically
  75. * // you should usually use a FormPanel to also get its form functionality
  76. * // since it already creates a FormLayout internally.
  77. * var form = new Ext.form.FormPanel({
  78. * labelWidth : 75,
  79. * title : 'Form Layout',
  80. * bodyStyle : 'padding:15px',
  81. * width : 350,
  82. * labelPad : 10,
  83. * defaultType : 'textfield',
  84. * defaults : {
  85. * // applied to each contained item
  86. * width : 230,
  87. * msgTarget : 'side'
  88. * },
  89. * layoutConfig : {
  90. * // layout-specific configs go here
  91. * labelSeparator : ''
  92. * },
  93. * items : [{
  94. * fieldLabel : 'First Name',
  95. * name : 'first',
  96. * allowBlank : false
  97. * }, {
  98. * fieldLabel : 'Last Name',
  99. * name : 'last'
  100. * }, {
  101. * fieldLabel : 'Company',
  102. * name : 'company'
  103. * }, {
  104. * fieldLabel : 'Email',
  105. * name : 'email',
  106. * vtype : 'email'
  107. * }],
  108. * buttons : [{
  109. * text : 'Save'
  110. * }, {
  111. * text : 'Cancel'
  112. * }]
  113. * });
  114. * </code></pre>
  115. */
  116. Ext.layout.FormLayout = Ext.extend(Ext.layout.AnchorLayout, {
  117. /**
  118. * @cfg {String} labelStyle A CSS style specification string to add to each
  119. * field label in this layout (defaults to '').
  120. */
  121. /**
  122. * @cfg {String} elementStyle A CSS style specification string to add to
  123. * each field element in this layout (defaults to '').
  124. */
  125. /**
  126. * @cfg {String} labelSeparator The standard separator to display after the
  127. * text of each form label (defaults to a colon ':'). To turn off
  128. * separators for all fields in this layout by default specify empty
  129. * string '' (if the labelSeparator value is explicitly set at the
  130. * field level, those will still be displayed).
  131. */
  132. labelSeparator : ':',
  133. // private
  134. getAnchorViewSize : function(ct, target) {
  135. return ct.body.getStyleSize();
  136. },
  137. // private
  138. setContainer : function(ct) {
  139. Ext.layout.FormLayout.superclass.setContainer.call(this, ct);
  140. if (ct.labelAlign) {
  141. ct.addClass('x-form-label-' + ct.labelAlign);
  142. }
  143. if (ct.hideLabels) {
  144. this.labelStyle = "display:none";
  145. this.elementStyle = "padding-left:0;";
  146. this.labelAdjust = 0;
  147. } else {
  148. this.labelSeparator = ct.labelSeparator || this.labelSeparator;
  149. if (typeof ct.labelWidth == 'number') {
  150. var pad = (typeof ct.labelPad == 'number' ? ct.labelPad : 5);
  151. this.labelAdjust = ct.labelWidth + pad;
  152. this.labelStyle = "width:" + ct.labelWidth + "px;";
  153. this.elementStyle = "padding-left:" + (ct.labelWidth + pad)
  154. + 'px';
  155. }
  156. if (ct.labelAlign == 'top') {
  157. this.labelStyle = "width:auto;";
  158. this.labelAdjust = 0;
  159. this.elementStyle = "padding-left:0;";
  160. }
  161. }
  162. if (!this.fieldTpl) {
  163. // the default field template used by all form layouts
  164. var t = new Ext.Template(
  165. '<div class="x-form-item {5}" tabIndex="-1">',
  166. '<label for="{0}" style="{2}" class="x-form-item-label">{1}{4}</label>',
  167. '<div class="x-form-element" id="x-form-el-{0}" style="{3}">',
  168. '</div><div class="{6}"></div>', '</div>');
  169. t.disableFormats = true;
  170. t.compile();
  171. Ext.layout.FormLayout.prototype.fieldTpl = t;
  172. }
  173. },
  174. // private
  175. renderItem : function(c, position, target) {
  176. if (c && !c.rendered && c.isFormField && c.inputType != 'hidden') {
  177. var args = [
  178. c.id,
  179. c.fieldLabel,
  180. c.labelStyle || this.labelStyle || '',
  181. this.elementStyle || '',
  182. typeof c.labelSeparator == 'undefined'
  183. ? this.labelSeparator
  184. : c.labelSeparator,
  185. (c.itemCls || this.container.itemCls || '')
  186. + (c.hideLabel ? ' x-hide-label' : ''),
  187. c.clearCls || 'x-form-clear-left'];
  188. if (typeof position == 'number') {
  189. position = target.dom.childNodes[position] || null;
  190. }
  191. if (position) {
  192. this.fieldTpl.insertBefore(position, args);
  193. } else {
  194. this.fieldTpl.append(target, args);
  195. }
  196. c.render('x-form-el-' + c.id);
  197. } else {
  198. Ext.layout.FormLayout.superclass.renderItem.apply(this, arguments);
  199. }
  200. },
  201. // private
  202. adjustWidthAnchor : function(value, comp) {
  203. return value - (comp.hideLabel ? 0 : this.labelAdjust);
  204. },
  205. // private
  206. isValidParent : function(c, target) {
  207. return true;
  208. }
  209. /**
  210. * @property activeItem
  211. * @hide
  212. */
  213. });
  214. Ext.Container.LAYOUTS['form'] = Ext.layout.FormLayout;