FileUploadField.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Ext JS Library 2.2.1
  3. * Copyright(c) 2006-2009, Ext JS, LLC.
  4. * licensing@extjs.com
  5. *
  6. * http://extjs.com/license
  7. */
  8. Ext.form.FileUploadField = Ext.extend(Ext.form.TextField, {
  9. /**
  10. * @cfg {String} buttonText The button text to display on the upload button (defaults to
  11. * 'Browse...'). Note that if you supply a value for {@link #buttonCfg}, the buttonCfg.text
  12. * value will be used instead if available.
  13. */
  14. buttonText: 'Browse...',
  15. /**
  16. * @cfg {Boolean} buttonOnly True to display the file upload field as a button with no visible
  17. * text field (defaults to false). If true, all inherited TextField members will still be available.
  18. */
  19. buttonOnly: false,
  20. /**
  21. * @cfg {Number} buttonOffset The number of pixels of space reserved between the button and the text field
  22. * (defaults to 3). Note that this only applies if {@link #buttonOnly} = false.
  23. */
  24. buttonOffset: 3,
  25. /**
  26. * @cfg {Object} buttonCfg A standard {@link Ext.Button} config object.
  27. */
  28. // private
  29. readOnly: true,
  30. /**
  31. * @hide
  32. * @method autoSize
  33. */
  34. autoSize: Ext.emptyFn,
  35. // private
  36. initComponent: function(){
  37. Ext.form.FileUploadField.superclass.initComponent.call(this);
  38. this.addEvents(
  39. /**
  40. * @event fileselected
  41. * Fires when the underlying file input field's value has changed from the user
  42. * selecting a new file from the system file selection dialog.
  43. * @param {Ext.form.FileUploadField} this
  44. * @param {String} value The file value returned by the underlying file input field
  45. */
  46. 'fileselected'
  47. );
  48. },
  49. // private
  50. onRender : function(ct, position){
  51. Ext.form.FileUploadField.superclass.onRender.call(this, ct, position);
  52. this.wrap = this.el.wrap({cls:'x-form-field-wrap x-form-file-wrap'});
  53. this.el.addClass('x-form-file-text');
  54. this.el.dom.removeAttribute('name');
  55. this.fileInput = this.wrap.createChild({
  56. id: this.getFileInputId(),
  57. name: this.name||this.getId(),
  58. cls: 'x-form-file',
  59. tag: 'input',
  60. type: 'file',
  61. size: 1
  62. });
  63. var btnCfg = Ext.applyIf(this.buttonCfg || {}, {
  64. text: this.buttonText
  65. });
  66. this.button = new Ext.Button(Ext.apply(btnCfg, {
  67. renderTo: this.wrap,
  68. cls: 'x-form-file-btn' + (btnCfg.iconCls ? ' x-btn-icon' : '')
  69. }));
  70. if(this.buttonOnly){
  71. this.el.hide();
  72. this.wrap.setWidth(this.button.getEl().getWidth());
  73. }
  74. this.fileInput.on('change', function(){
  75. var v = this.fileInput.dom.value;
  76. this.setValue(v);
  77. this.fireEvent('fileselected', this, v);
  78. }, this);
  79. },
  80. // private
  81. getFileInputId: function(){
  82. return this.id+'-file';
  83. },
  84. // private
  85. onResize : function(w, h){
  86. Ext.form.FileUploadField.superclass.onResize.call(this, w, h);
  87. this.wrap.setWidth(w);
  88. if(!this.buttonOnly){
  89. var w = this.wrap.getWidth() - this.button.getEl().getWidth() - this.buttonOffset;
  90. this.el.setWidth(w);
  91. }
  92. },
  93. // private
  94. preFocus : Ext.emptyFn,
  95. // private
  96. getResizeEl : function(){
  97. return this.wrap;
  98. },
  99. // private
  100. getPositionEl : function(){
  101. return this.wrap;
  102. },
  103. // private
  104. alignErrorIcon : function(){
  105. this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
  106. }
  107. });
  108. Ext.reg('fileuploadfield', Ext.form.FileUploadField);