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.

182 lines
5.2 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 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-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. 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. this.fileInput.remove();
  110. this.createFileInput();
  111. this.bindListeners();
  112. Ext.ux.form.FileUploadField.superclass.reset.call(this);
  113. },
  114. // private
  115. getFileInputId: function(){
  116. return this.id + '-file';
  117. },
  118. // private
  119. onResize : function(w, h){
  120. Ext.ux.form.FileUploadField.superclass.onResize.call(this, w, h);
  121. this.wrap.setWidth(w);
  122. if(!this.buttonOnly){
  123. var w = this.wrap.getWidth() - this.button.getEl().getWidth() - this.buttonOffset;
  124. this.el.setWidth(w);
  125. }
  126. },
  127. // private
  128. onDestroy: function(){
  129. Ext.ux.form.FileUploadField.superclass.onDestroy.call(this);
  130. Ext.destroy(this.fileInput, this.button, this.wrap);
  131. },
  132. onDisable: function(){
  133. Ext.ux.form.FileUploadField.superclass.onDisable.call(this);
  134. this.doDisable(true);
  135. },
  136. onEnable: function(){
  137. Ext.ux.form.FileUploadField.superclass.onEnable.call(this);
  138. this.doDisable(false);
  139. },
  140. // private
  141. doDisable: function(disabled){
  142. this.fileInput.dom.disabled = disabled;
  143. this.button.setDisabled(disabled);
  144. },
  145. // private
  146. preFocus : Ext.emptyFn,
  147. // private
  148. alignErrorIcon : function(){
  149. this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
  150. }
  151. });
  152. Ext.reg('fileuploadfield', Ext.ux.form.FileUploadField);
  153. // backwards compat
  154. Ext.form.FileUploadField = Ext.ux.form.FileUploadField;