2 * @class Ext.layout.component.field.Field
3 * @extends Ext.layout.component.Component
4 * Layout class for components with {@link Ext.form.Labelable field labeling}, handling the sizing and alignment of
5 * the form control, label, and error message treatment.
8 Ext.define('Ext.layout.component.field.Field', {
10 /* Begin Definitions */
12 alias: ['layout.field'],
14 extend: 'Ext.layout.component.Component',
16 uses: ['Ext.tip.QuickTip', 'Ext.util.TextMetrics'],
22 beforeLayout: function(width, height) {
24 return me.callParent(arguments) || (!me.owner.preventMark && me.activeError !== me.owner.getActiveError());
27 onLayout: function(width, height) {
30 labelStrategy = me.getLabelStrategy(),
31 errorStrategy = me.getErrorStrategy(),
32 isDefined = Ext.isDefined,
33 isNumber = Ext.isNumber,
34 lastSize, autoWidth, autoHeight, info, undef;
36 lastSize = me.lastComponentSize || {};
37 if (!isDefined(width)) {
38 width = lastSize.width;
39 if (width < 0) { //first pass lastComponentSize.width is -Infinity
43 if (!isDefined(height)) {
44 height = lastSize.height;
45 if (height < 0) { //first pass lastComponentSize.height is -Infinity
49 autoWidth = !isNumber(width);
50 autoHeight = !isNumber(height);
54 autoHeight: autoHeight,
55 width: autoWidth ? owner.getBodyNaturalWidth() : width, //always give a pixel width
58 // insets for the bodyEl from each side of the component layout area
67 // NOTE the order of calculating insets and setting styles here is very important; we must first
68 // calculate and set horizontal layout alone, as the horizontal sizing of elements can have an impact
69 // on the vertical sizes due to wrapping, then calculate and set the vertical layout.
71 // perform preparation on the label and error (setting css classes, qtips, etc.)
72 labelStrategy.prepare(owner, info);
73 errorStrategy.prepare(owner, info);
75 // calculate the horizontal insets for the label and error
76 labelStrategy.adjustHorizInsets(owner, info);
77 errorStrategy.adjustHorizInsets(owner, info);
79 // set horizontal styles for label and error based on the current insets
80 labelStrategy.layoutHoriz(owner, info);
81 errorStrategy.layoutHoriz(owner, info);
83 // calculate the vertical insets for the label and error
84 labelStrategy.adjustVertInsets(owner, info);
85 errorStrategy.adjustVertInsets(owner, info);
87 // set vertical styles for label and error based on the current insets
88 labelStrategy.layoutVert(owner, info);
89 errorStrategy.layoutVert(owner, info);
91 // perform sizing of the elements based on the final dimensions and insets
92 if (autoWidth && autoHeight) {
93 // Don't use setTargetSize if auto-sized, so the calculated size is not reused next time
94 me.setElementSize(owner.el, info.width, info.height);
96 me.setTargetSize(info.width, info.height);
100 me.activeError = owner.getActiveError();
105 * Perform sizing and alignment of the bodyEl (and children) to match the calculated insets.
107 sizeBody: function(info) {
110 insets = info.insets,
111 totalWidth = info.width,
112 totalHeight = info.height,
113 width = Ext.isNumber(totalWidth) ? totalWidth - insets.left - insets.right : totalWidth,
114 height = Ext.isNumber(totalHeight) ? totalHeight - insets.top - insets.bottom : totalHeight;
117 me.setElementSize(owner.bodyEl, width, height);
119 // size the bodyEl's inner contents if necessary
120 me.sizeBodyContents(width, height);
124 * Size the contents of the field body, given the full dimensions of the bodyEl. Does nothing by
125 * default, subclasses can override to handle their specific contents.
126 * @param {Number} width The bodyEl width
127 * @param {Number} height The bodyEl height
129 sizeBodyContents: Ext.emptyFn,
133 * Return the set of strategy functions from the {@link #labelStrategies labelStrategies collection}
134 * that is appropriate for the field's {@link Ext.form.field.Field#labelAlign labelAlign} config.
136 getLabelStrategy: function() {
138 strategies = me.labelStrategies,
139 labelAlign = me.owner.labelAlign;
140 return strategies[labelAlign] || strategies.base;
144 * Return the set of strategy functions from the {@link #errorStrategies errorStrategies collection}
145 * that is appropriate for the field's {@link Ext.form.field.Field#msgTarget msgTarget} config.
147 getErrorStrategy: function() {
150 strategies = me.errorStrategies,
151 msgTarget = owner.msgTarget;
152 return !owner.preventMark && Ext.isString(msgTarget) ?
153 (strategies[msgTarget] || strategies.elementId) :
160 * Collection of named strategies for laying out and adjusting labels to accommodate error messages.
161 * An appropriate one will be chosen based on the owner field's {@link Ext.form.field.Field#labelAlign} config.
163 labelStrategies: (function() {
164 var applyIf = Ext.applyIf,
165 emptyFn = Ext.emptyFn,
167 prepare: function(owner, info) {
168 var cls = owner.labelCls + '-' + owner.labelAlign,
169 labelEl = owner.labelEl;
170 if (labelEl && !labelEl.hasCls(cls)) {
174 adjustHorizInsets: emptyFn,
175 adjustVertInsets: emptyFn,
176 layoutHoriz: emptyFn,
180 prepare: function(owner, info) {
181 base.prepare(owner, info);
182 // If auto width, add the label width to the body's natural width.
183 if (info.autoWidth) {
184 info.width += (!owner.labelEl ? 0 : owner.labelWidth + owner.labelPad);
187 adjustHorizInsets: function(owner, info) {
189 info.insets.left += owner.labelWidth + owner.labelPad;
192 layoutHoriz: function(owner, info) {
193 // For content-box browsers we can't rely on Labelable.js#getLabelableRenderData
194 // setting the width style because it needs to account for the final calculated
195 // padding/border styles for the label. So we set the width programmatically here to
196 // normalize content-box sizing, while letting border-box browsers use the original
198 var labelEl = owner.labelEl;
199 if (labelEl && !owner.isLabelSized && !Ext.isBorderBox) {
200 labelEl.setWidth(owner.labelWidth);
201 owner.isLabelSized = true;
211 * Label displayed above the bodyEl
214 adjustVertInsets: function(owner, info) {
215 var labelEl = owner.labelEl;
217 info.insets.top += Ext.util.TextMetrics.measure(labelEl, owner.fieldLabel, info.width).height +
218 labelEl.getFrameWidth('tb') + owner.labelPad;
224 * Label displayed to the left of the bodyEl
229 * Same as left, only difference is text-align in CSS
238 * Collection of named strategies for laying out and adjusting insets to accommodate error messages.
239 * An appropriate one will be chosen based on the owner field's {@link Ext.form.field.Field#msgTarget} config.
241 errorStrategies: (function() {
242 function setDisplayed(el, displayed) {
243 var wasDisplayed = el.getStyle('display') !== 'none';
244 if (displayed !== wasDisplayed) {
245 el.setDisplayed(displayed);
249 function setStyle(el, name, value) {
250 if (el.getStyle(name) !== value) {
251 el.setStyle(name, value);
255 var applyIf = Ext.applyIf,
256 emptyFn = Ext.emptyFn,
258 prepare: function(owner) {
259 setDisplayed(owner.errorEl, false);
261 adjustHorizInsets: emptyFn,
262 adjustVertInsets: emptyFn,
263 layoutHoriz: emptyFn,
271 * Error displayed as icon (with QuickTip on hover) to right of the bodyEl
274 prepare: function(owner) {
275 var errorEl = owner.errorEl;
276 errorEl.addCls(Ext.baseCSSPrefix + 'form-invalid-icon');
277 Ext.layout.component.field.Field.initTip();
278 errorEl.dom.setAttribute('data-errorqtip', owner.getActiveError() || '');
279 setDisplayed(errorEl, owner.hasActiveError());
281 adjustHorizInsets: function(owner, info) {
282 if (owner.autoFitErrors && owner.hasActiveError()) {
283 info.insets.right += owner.errorEl.getWidth();
286 layoutHoriz: function(owner, info) {
287 if (owner.hasActiveError()) {
288 setStyle(owner.errorEl, 'left', info.width - info.insets.right + 'px');
291 layoutVert: function(owner, info) {
292 if (owner.hasActiveError()) {
293 setStyle(owner.errorEl, 'top', info.insets.top + 'px');
299 * Error message displayed underneath the bodyEl
302 prepare: function(owner) {
303 var errorEl = owner.errorEl,
304 cls = Ext.baseCSSPrefix + 'form-invalid-under';
305 if (!errorEl.hasCls(cls)) {
308 setDisplayed(errorEl, owner.hasActiveError());
310 adjustVertInsets: function(owner, info) {
311 if (owner.autoFitErrors) {
312 info.insets.bottom += owner.errorEl.getHeight();
315 layoutHoriz: function(owner, info) {
316 var errorEl = owner.errorEl,
317 insets = info.insets;
319 setStyle(errorEl, 'width', info.width - insets.right - insets.left + 'px');
320 setStyle(errorEl, 'marginLeft', insets.left + 'px');
325 * Error displayed as QuickTip on hover of the field container
328 prepare: function(owner) {
329 setDisplayed(owner.errorEl, false);
330 Ext.layout.component.field.Field.initTip();
331 owner.getActionEl().dom.setAttribute('data-errorqtip', owner.getActiveError() || '');
336 * Error displayed as title tip on hover of the field container
339 prepare: function(owner) {
340 setDisplayed(owner.errorEl, false);
341 owner.el.dom.title = owner.getActiveError() || '';
346 * Error message displayed as content of an element with a given id elsewhere in the app
349 prepare: function(owner) {
350 setDisplayed(owner.errorEl, false);
351 var targetEl = Ext.fly(owner.msgTarget);
353 targetEl.dom.innerHTML = owner.getActiveError() || '';
354 targetEl.setDisplayed(owner.hasActiveError());
363 * Use a custom QuickTip instance separate from the main QuickTips singleton, so that we
364 * can give it a custom frame style. Responds to errorqtip rather than the qtip property.
366 initTip: function() {
369 tip = this.tip = Ext.create('Ext.tip.QuickTip', {
370 baseCls: Ext.baseCSSPrefix + 'form-invalid-tip',
371 renderTo: Ext.getBody()
373 tip.tagConfig = Ext.apply({}, {attribute: 'errorqtip'}, tip.tagConfig);
378 * Destroy the error tip instance.
380 destroyTip: function() {