You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.8 KiB

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