FileUploadField.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*!
  2. * Ext JS Library 3.0.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.fileInput = this.wrap.createChild({
  62. id: this.getFileInputId(),
  63. name: this.name||this.getId(),
  64. cls: 'x-form-file',
  65. tag: 'input',
  66. type: 'file',
  67. size: 1
  68. });
  69. var btnCfg = Ext.applyIf(this.buttonCfg || {}, {
  70. text: this.buttonText
  71. });
  72. this.button = new Ext.Button(Ext.apply(btnCfg, {
  73. renderTo: this.wrap,
  74. cls: 'x-form-file-btn' + (btnCfg.iconCls ? ' x-btn-icon' : '')
  75. }));
  76. if(this.buttonOnly){
  77. this.el.hide();
  78. this.wrap.setWidth(this.button.getEl().getWidth());
  79. }
  80. this.fileInput.on('change', function(){
  81. var v = this.fileInput.dom.value;
  82. this.setValue(v);
  83. this.fireEvent('fileselected', this, v);
  84. }, this);
  85. },
  86. // private
  87. getFileInputId: function(){
  88. return this.id + '-file';
  89. },
  90. // private
  91. onResize : function(w, h){
  92. Ext.ux.form.FileUploadField.superclass.onResize.call(this, w, h);
  93. this.wrap.setWidth(w);
  94. if(!this.buttonOnly){
  95. var w = this.wrap.getWidth() - this.button.getEl().getWidth() - this.buttonOffset;
  96. this.el.setWidth(w);
  97. }
  98. },
  99. // private
  100. onDestroy: function(){
  101. Ext.ux.form.FileUploadField.superclass.onDestroy.call(this);
  102. Ext.destroy(this.fileInput, this.button, this.wrap);
  103. },
  104. // private
  105. preFocus : Ext.emptyFn,
  106. // private
  107. getResizeEl : function(){
  108. return this.wrap;
  109. },
  110. // private
  111. getPositionEl : function(){
  112. return this.wrap;
  113. },
  114. // private
  115. alignErrorIcon : function(){
  116. this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
  117. }
  118. });
  119. Ext.reg('fileuploadfield', Ext.ux.form.FileUploadField);
  120. // backwards compat
  121. Ext.form.FileUploadField = Ext.ux.form.FileUploadField;