FieldLabeler.js
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:5k
源码类别:

JavaScript

开发平台:

JavaScript

  1. /*!  * Ext JS Library 3.1.0  * Copyright(c) 2006-2009 Ext JS, LLC  * licensing@extjs.com  * http://www.extjs.com/license  */ Ext.ns("Ext.ux");
  2. /**
  3.  * @class Ext.ux.FieldLabeler
  4.  * <p>A plugin for Field Components which renders standard Ext form wrapping and labels
  5.  * round the Field at render time regardless of the layout of the Container.</p>
  6.  * <p>Usage:</p>
  7.  * <pre><code>
  8.     {
  9.         xtype: 'combo',
  10.         plugins: [ Ext.ux.FieldLabeler ],
  11.         triggerAction: 'all',
  12.         fieldLabel: 'Select type',
  13.         store: typeStore
  14.     }
  15.  * </code></pre>
  16.  */
  17. Ext.ux.FieldLabeler = (function(){
  18. //  Pulls a named property down from the first ancestor Container it's found in
  19.     function getParentProperty(propName) {
  20.         for (var p = this.ownerCt; p; p = p.ownerCt) {
  21.             if (p[propName]) {
  22.                 return p[propName];
  23.             }
  24.         }
  25.     }
  26.     return {
  27. //      Add behaviour at important points in the Field's lifecycle.
  28.         init: function(f) {
  29. //          Replace the Field's onRender method with a sequence that calls the plugin's onRender after the Field's onRender
  30.             f.onRender = f.onRender.createSequence(this.onRender);
  31. //          We need to completely override the onResize method because of the complexity
  32.             f.onResize = this.onResize;
  33. //          Replace the Field's onDestroy method with a sequence that calls the plugin's onDestroy after the Field's onRender
  34.             f.onDestroy = f.onDestroy.createSequence(this.onDestroy);
  35.         },
  36.         onRender: function() {
  37. //          Do nothing if being rendered by a form layout
  38.             if (this.ownerCt) {
  39.                 if (this.ownerCt.layout instanceof Ext.layout.FormLayout) {
  40.                     return;
  41.                 }
  42.             }
  43.             this.resizeEl = (this.wrap || this.el).wrap({
  44.                 cls: 'x-form-element',
  45.                 style: Ext.isIE ? 'position:absolute;top:0;left:0;overflow:visible' : ''
  46.             });
  47.             this.positionEl = this.itemCt = this.resizeEl.wrap({
  48.                 cls: 'x-form-item '
  49.             });
  50.             if (this.nextSibling()) {
  51.                 this.margins = {
  52.                     top: 0,
  53.                     right: 0,
  54.                     bottom: this.positionEl.getMargins('b'),
  55.                     left: 0
  56.                 };
  57.             }
  58.             this.actionMode = 'itemCt';
  59. //          If our Container is hiding labels, then we're done!
  60.             if (!Ext.isDefined(this.hideLabels)) {
  61.                 this.hideLabels = getParentProperty.call(this, "hideLabels");
  62.             }
  63.             if (this.hideLabels) {
  64.                 this.resizeEl.setStyle('padding-left', '0px');
  65.                 return;
  66.             }
  67. //          Collect the info we need to render the label from our Container.
  68.             if (!Ext.isDefined(this.labelSeparator)) {
  69.                 this.labelSeparator = getParentProperty.call(this, "labelSeparator");
  70.             }
  71.             if (!Ext.isDefined(this.labelPad)) {
  72.                 this.labelPad = getParentProperty.call(this, "labelPad");
  73.             }
  74.             if (!Ext.isDefined(this.labelAlign)) {
  75.                 this.labelAlign = getParentProperty.call(this, "labelAlign") || 'left';
  76.             }
  77.             this.itemCt.addClass('x-form-label-' + this.labelAlign);
  78.             if(this.labelAlign == 'top'){
  79.                 if (!this.labelWidth) {
  80.                     this.labelWidth = 'auto';
  81.                 }
  82.                 this.resizeEl.setStyle('padding-left', '0px');
  83.             } else {
  84.                 if (!Ext.isDefined(this.labelWidth)) {
  85.                     this.labelWidth = getParentProperty.call(this, "labelWidth") || 100;
  86.                 }
  87.                 this.resizeEl.setStyle('padding-left', (this.labelWidth + (this.labelPad || 5)) + 'px');
  88.                 this.labelWidth += 'px';
  89.             }
  90.             this.label = this.itemCt.insertFirst({
  91.                 tag: 'label',
  92.                 cls: 'x-form-item-label',
  93.                 style: {
  94.                     width: this.labelWidth
  95.                 },
  96.                 html: this.fieldLabel + (this.labelSeparator || ':')
  97.             });
  98.         },
  99. //      private
  100. //      Ensure the input field is sized to fit in the content area of the resizeEl (to the right of its padding-left)
  101. //      We perform all necessary sizing here. We do NOT call the current class's onResize because we need this control
  102. //      we skip that and go up the hierarchy to Ext.form.Field
  103.         onResize: function(w, h) {
  104.             Ext.form.Field.prototype.onResize.apply(this, arguments);
  105.             w -= this.resizeEl.getPadding('l');
  106.             if (this.getTriggerWidth) {
  107.                 this.wrap.setWidth(w);
  108.                 this.el.setWidth(w - this.getTriggerWidth());
  109.             } else {
  110.                 this.el.setWidth(w);
  111.             }
  112.             if (this.el.dom.tagName.toLowerCase() == 'textarea') {
  113.                 var h = this.resizeEl.getHeight(true);
  114.                 if (!this.hideLabels && (this.labelAlign == 'top')) {
  115.                     h -= this.label.getHeight();
  116.                 }
  117.                 this.el.setHeight(h);
  118.             }
  119.         },
  120. //      private
  121. //      Ensure that we clean up on destroy.
  122.         onDestroy: function() {
  123.             this.itemCt.remove();
  124.         }
  125.     };
  126. })();