fileupload.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*!
  2. * Ext JS Library 3.4.0
  3. * Copyright(c) 2006-2011 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.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-fileupload-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. bindListeners: function(){
  77. this.fileInput.on({
  78. scope: this,
  79. mouseenter: function() {
  80. this.button.addClass(['x-btn-over','x-btn-focus'])
  81. },
  82. mouseleave: function(){
  83. this.button.removeClass(['x-btn-over','x-btn-focus','x-btn-click'])
  84. },
  85. mousedown: function(){
  86. this.button.addClass('x-btn-click')
  87. },
  88. mouseup: function(){
  89. this.button.removeClass(['x-btn-over','x-btn-focus','x-btn-click'])
  90. },
  91. change: function(){
  92. var v = this.fileInput.dom.value;
  93. this.setValue(v);
  94. this.fireEvent('fileselected', this, v);
  95. }
  96. });
  97. },
  98. createFileInput : function() {
  99. this.fileInput = this.wrap.createChild({
  100. id: this.getFileInputId(),
  101. name: this.name||this.getId(),
  102. cls: 'x-form-file',
  103. tag: 'input',
  104. type: 'file',
  105. size: 1
  106. });
  107. },
  108. reset : function(){
  109. if (this.rendered) {
  110. this.fileInput.remove();
  111. this.createFileInput();
  112. this.bindListeners();
  113. }
  114. Ext.ux.form.FileUploadField.superclass.reset.call(this);
  115. },
  116. // private
  117. getFileInputId: function(){
  118. return this.id + '-file';
  119. },
  120. // private
  121. onResize : function(w, h){
  122. Ext.ux.form.FileUploadField.superclass.onResize.call(this, w, h);
  123. this.wrap.setWidth(w);
  124. if(!this.buttonOnly){
  125. var w = this.wrap.getWidth() - this.button.getEl().getWidth() - this.buttonOffset;
  126. this.el.setWidth(w);
  127. }
  128. },
  129. // private
  130. onDestroy: function(){
  131. Ext.ux.form.FileUploadField.superclass.onDestroy.call(this);
  132. Ext.destroy(this.fileInput, this.button, this.wrap);
  133. },
  134. onDisable: function(){
  135. Ext.ux.form.FileUploadField.superclass.onDisable.call(this);
  136. this.doDisable(true);
  137. },
  138. onEnable: function(){
  139. Ext.ux.form.FileUploadField.superclass.onEnable.call(this);
  140. this.doDisable(false);
  141. },
  142. // private
  143. doDisable: function(disabled){
  144. this.fileInput.dom.disabled = disabled;
  145. this.button.setDisabled(disabled);
  146. },
  147. // private
  148. preFocus : Ext.emptyFn,
  149. // private
  150. alignErrorIcon : function(){
  151. this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
  152. }
  153. });
  154. Ext.reg('fileuploadfield', Ext.ux.form.FileUploadField);
  155. // backwards compat
  156. Ext.form.FileUploadField = Ext.ux.form.FileUploadField;