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

JavaScript

开发平台:

JavaScript

  1. /*!
  2.  * Ext JS Library 3.1.0
  3.  * Copyright(c) 2006-2009 Ext JS, LLC
  4.  * licensing@extjs.com
  5.  * http://www.extjs.com/license
  6.  */
  7. Ext.ns('Ext.ux.form');
  8. /**
  9.  * @class Ext.ux.form.FileUploadField
  10.  * @extends Ext.form.TextField
  11.  * Creates a file upload field.
  12.  * @xtype fileuploadfield
  13.  */
  14. Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField,  {
  15.     /**
  16.      * @cfg {String} buttonText The button text to display on the upload button (defaults to
  17.      * 'Browse...').  Note that if you supply a value for {@link #buttonCfg}, the buttonCfg.text
  18.      * value will be used instead if available.
  19.      */
  20.     buttonText: 'Browse...',
  21.     /**
  22.      * @cfg {Boolean} buttonOnly True to display the file upload field as a button with no visible
  23.      * text field (defaults to false).  If true, all inherited TextField members will still be available.
  24.      */
  25.     buttonOnly: false,
  26.     /**
  27.      * @cfg {Number} buttonOffset The number of pixels of space reserved between the button and the text field
  28.      * (defaults to 3).  Note that this only applies if {@link #buttonOnly} = false.
  29.      */
  30.     buttonOffset: 3,
  31.     /**
  32.      * @cfg {Object} buttonCfg A standard {@link Ext.Button} config object.
  33.      */
  34.     // private
  35.     readOnly: true,
  36.     /**
  37.      * @hide
  38.      * @method autoSize
  39.      */
  40.     autoSize: Ext.emptyFn,
  41.     // private
  42.     initComponent: function(){
  43.         Ext.ux.form.FileUploadField.superclass.initComponent.call(this);
  44.         this.addEvents(
  45.             /**
  46.              * @event fileselected
  47.              * Fires when the underlying file input field's value has changed from the user
  48.              * selecting a new file from the system file selection dialog.
  49.              * @param {Ext.ux.form.FileUploadField} this
  50.              * @param {String} value The file value returned by the underlying file input field
  51.              */
  52.             'fileselected'
  53.         );
  54.     },
  55.     // private
  56.     onRender : function(ct, position){
  57.         Ext.ux.form.FileUploadField.superclass.onRender.call(this, ct, position);
  58.         this.wrap = this.el.wrap({cls:'x-form-field-wrap x-form-file-wrap'});
  59.         this.el.addClass('x-form-file-text');
  60.         this.el.dom.removeAttribute('name');
  61.         this.createFileInput();
  62.         var btnCfg = Ext.applyIf(this.buttonCfg || {}, {
  63.             text: this.buttonText
  64.         });
  65.         this.button = new Ext.Button(Ext.apply(btnCfg, {
  66.             renderTo: this.wrap,
  67.             cls: 'x-form-file-btn' + (btnCfg.iconCls ? ' x-btn-icon' : '')
  68.         }));
  69.         if(this.buttonOnly){
  70.             this.el.hide();
  71.             this.wrap.setWidth(this.button.getEl().getWidth());
  72.         }
  73.         this.bindListeners();
  74.         this.resizeEl = this.positionEl = this.wrap;
  75.     },
  76.     
  77.     bindListeners: function(){
  78.         this.fileInput.on({
  79.             scope: this,
  80.             mouseenter: function() {
  81.                 this.button.addClass(['x-btn-over','x-btn-focus'])
  82.             },
  83.             mouseleave: function(){
  84.                 this.button.removeClass(['x-btn-over','x-btn-focus','x-btn-click'])
  85.             },
  86.             mousedown: function(){
  87.                 this.button.addClass('x-btn-click')
  88.             },
  89.             mouseup: function(){
  90.                 this.button.removeClass(['x-btn-over','x-btn-focus','x-btn-click'])
  91.             },
  92.             change: function(){
  93.                 var v = this.fileInput.dom.value;
  94.                 this.setValue(v);
  95.                 this.fireEvent('fileselected', this, v);    
  96.             }
  97.         }); 
  98.     },
  99.     
  100.     createFileInput : function() {
  101.         this.fileInput = this.wrap.createChild({
  102.             id: this.getFileInputId(),
  103.             name: this.name||this.getId(),
  104.             cls: 'x-form-file',
  105.             tag: 'input',
  106.             type: 'file',
  107.             size: 1
  108.         });
  109.     },
  110.     
  111.     reset : function(){
  112.         this.fileInput.remove();
  113.         this.createFileInput();
  114.         this.bindListeners();
  115.         Ext.ux.form.FileUploadField.superclass.reset.call(this);
  116.     },
  117.     // private
  118.     getFileInputId: function(){
  119.         return this.id + '-file';
  120.     },
  121.     // private
  122.     onResize : function(w, h){
  123.         Ext.ux.form.FileUploadField.superclass.onResize.call(this, w, h);
  124.         this.wrap.setWidth(w);
  125.         if(!this.buttonOnly){
  126.             var w = this.wrap.getWidth() - this.button.getEl().getWidth() - this.buttonOffset;
  127.             this.el.setWidth(w);
  128.         }
  129.     },
  130.     // private
  131.     onDestroy: function(){
  132.         Ext.ux.form.FileUploadField.superclass.onDestroy.call(this);
  133.         Ext.destroy(this.fileInput, this.button, this.wrap);
  134.     },
  135.     
  136.     onDisable: function(){
  137.         Ext.ux.form.FileUploadField.superclass.onDisable.call(this);
  138.         this.doDisable(true);
  139.     },
  140.     
  141.     onEnable: function(){
  142.         Ext.ux.form.FileUploadField.superclass.onEnable.call(this);
  143.         this.doDisable(false);
  144.     },
  145.     
  146.     // private
  147.     doDisable: function(disabled){
  148.         this.fileInput.dom.disabled = disabled;
  149.         this.button.setDisabled(disabled);
  150.     },
  151.     // private
  152.     preFocus : Ext.emptyFn,
  153.     // private
  154.     alignErrorIcon : function(){
  155.         this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
  156.     }
  157. });
  158. Ext.reg('fileuploadfield', Ext.ux.form.FileUploadField);
  159. // backwards compat
  160. Ext.form.FileUploadField = Ext.ux.form.FileUploadField;