File.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-form-field-File'>/**
  19. </span> * @docauthor Jason Johnston &lt;jason@sencha.com&gt;
  20. *
  21. * A file upload field which has custom styling and allows control over the button text and other
  22. * features of {@link Ext.form.field.Text text fields} like {@link Ext.form.field.Text#emptyText empty text}.
  23. * It uses a hidden file input element behind the scenes to allow user selection of a file and to
  24. * perform the actual upload during {@link Ext.form.Basic#submit form submit}.
  25. *
  26. * Because there is no secure cross-browser way to programmatically set the value of a file input,
  27. * the standard Field `setValue` method is not implemented. The `{@link #getValue}` method will return
  28. * a value that is browser-dependent; some have just the file name, some have a full path, some use
  29. * a fake path.
  30. *
  31. * **IMPORTANT:** File uploads are not performed using normal 'Ajax' techniques; see the description for
  32. * {@link Ext.form.Basic#hasUpload} for details.
  33. *
  34. * # Example Usage
  35. *
  36. * @example
  37. * Ext.create('Ext.form.Panel', {
  38. * title: 'Upload a Photo',
  39. * width: 400,
  40. * bodyPadding: 10,
  41. * frame: true,
  42. * renderTo: Ext.getBody(),
  43. * items: [{
  44. * xtype: 'filefield',
  45. * name: 'photo',
  46. * fieldLabel: 'Photo',
  47. * labelWidth: 50,
  48. * msgTarget: 'side',
  49. * allowBlank: false,
  50. * anchor: '100%',
  51. * buttonText: 'Select Photo...'
  52. * }],
  53. *
  54. * buttons: [{
  55. * text: 'Upload',
  56. * handler: function() {
  57. * var form = this.up('form').getForm();
  58. * if(form.isValid()){
  59. * form.submit({
  60. * url: 'photo-upload.php',
  61. * waitMsg: 'Uploading your photo...',
  62. * success: function(fp, o) {
  63. * Ext.Msg.alert('Success', 'Your photo &quot;' + o.result.file + '&quot; has been uploaded.');
  64. * }
  65. * });
  66. * }
  67. * }
  68. * }]
  69. * });
  70. */
  71. Ext.define(&quot;Ext.form.field.File&quot;, {
  72. extend: 'Ext.form.field.Trigger',
  73. alias: ['widget.filefield', 'widget.fileuploadfield'],
  74. alternateClassName: ['Ext.form.FileUploadField', 'Ext.ux.form.FileUploadField', 'Ext.form.File'],
  75. uses: ['Ext.button.Button', 'Ext.layout.component.field.Field'],
  76. //&lt;locale&gt;
  77. <span id='Ext-form-field-File-cfg-buttonText'> /**
  78. </span> * @cfg {String} buttonText
  79. * The button text to display on the upload button. Note that if you supply a value for
  80. * {@link #buttonConfig}, the buttonConfig.text value will be used instead if available.
  81. */
  82. buttonText: 'Browse...',
  83. //&lt;/locale&gt;
  84. <span id='Ext-form-field-File-cfg-buttonOnly'> /**
  85. </span> * @cfg {Boolean} buttonOnly
  86. * True to display the file upload field as a button with no visible text field. If true, all
  87. * inherited Text members will still be available.
  88. */
  89. buttonOnly: false,
  90. <span id='Ext-form-field-File-cfg-buttonMargin'> /**
  91. </span> * @cfg {Number} buttonMargin
  92. * The number of pixels of space reserved between the button and the text field. Note that this only
  93. * applies if {@link #buttonOnly} = false.
  94. */
  95. buttonMargin: 3,
  96. <span id='Ext-form-field-File-cfg-buttonConfig'> /**
  97. </span> * @cfg {Object} buttonConfig
  98. * A standard {@link Ext.button.Button} config object.
  99. */
  100. <span id='Ext-form-field-File-event-change'> /**
  101. </span> * @event change
  102. * Fires when the underlying file input field's value has changed from the user selecting a new file from the system
  103. * file selection dialog.
  104. * @param {Ext.ux.form.FileUploadField} this
  105. * @param {String} value The file value returned by the underlying file input field
  106. */
  107. <span id='Ext-form-field-File-property-fileInputEl'> /**
  108. </span> * @property {Ext.Element} fileInputEl
  109. * A reference to the invisible file input element created for this upload field. Only populated after this
  110. * component is rendered.
  111. */
  112. <span id='Ext-form-field-File-property-button'> /**
  113. </span> * @property {Ext.button.Button} button
  114. * A reference to the trigger Button component created for this upload field. Only populated after this component is
  115. * rendered.
  116. */
  117. <span id='Ext-form-field-File-cfg-fieldBodyCls'> /**
  118. </span> * @cfg {String} [fieldBodyCls='x-form-file-wrap']
  119. * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
  120. */
  121. fieldBodyCls: Ext.baseCSSPrefix + 'form-file-wrap',
  122. <span id='Ext-form-field-File-cfg-readOnly'> /**
  123. </span> * @cfg {Boolean} readOnly
  124. * Unlike with other form fields, the readOnly config defaults to true in File field.
  125. */
  126. readOnly: true,
  127. <span id='Ext-form-field-File-property-triggerNoEditCls'> /**
  128. </span> * Do not show hand pointer over text field since file choose dialog is only shown when clicking in the button
  129. * @private
  130. */
  131. triggerNoEditCls: '',
  132. // private
  133. componentLayout: 'triggerfield',
  134. // private. Extract the file element, button outer element, and button active element.
  135. childEls: ['fileInputEl', 'buttonEl', 'buttonEl-btnEl', 'browseButtonWrap'],
  136. // private
  137. onRender: function() {
  138. var me = this,
  139. inputEl;
  140. me.callParent(arguments);
  141. inputEl = me.inputEl;
  142. inputEl.dom.name = ''; //name goes on the fileInput, not the text input
  143. me.fileInputEl.dom.name = me.getName();
  144. me.fileInputEl.on({
  145. scope: me,
  146. change: me.onFileChange
  147. });
  148. if (me.buttonOnly) {
  149. me.inputCell.setDisplayed(false);
  150. }
  151. // Ensure the trigger cell is sized correctly upon render
  152. me.browseButtonWrap.dom.style.width = (me.browseButtonWrap.dom.lastChild.offsetWidth + me.buttonEl.getMargin('lr')) + 'px';
  153. if (Ext.isIE) {
  154. me.buttonEl.repaint();
  155. }
  156. },
  157. <span id='Ext-form-field-File-method-getTriggerMarkup'> /**
  158. </span> * Gets the markup to be inserted into the subTplMarkup.
  159. */
  160. getTriggerMarkup: function() {
  161. var me = this,
  162. result,
  163. btn = Ext.widget('button', Ext.apply({
  164. id: me.id + '-buttonEl',
  165. ui: me.ui,
  166. disabled: me.disabled,
  167. text: me.buttonText,
  168. cls: Ext.baseCSSPrefix + 'form-file-btn',
  169. preventDefault: false,
  170. style: me.buttonOnly ? '' : 'margin-left:' + me.buttonMargin + 'px'
  171. }, me.buttonConfig)),
  172. btnCfg = btn.getRenderTree(),
  173. inputElCfg = {
  174. id: me.id + '-fileInputEl',
  175. cls: Ext.baseCSSPrefix + 'form-file-input',
  176. tag: 'input',
  177. type: 'file',
  178. size: 1
  179. };
  180. if (me.disabled) {
  181. inputElCfg.disabled = true;
  182. }
  183. btnCfg.cn = inputElCfg;
  184. result = '&lt;td id=&quot;' + me.id + '-browseButtonWrap&quot;&gt;' + Ext.DomHelper.markup(btnCfg) + '&lt;/td&gt;';
  185. btn.destroy();
  186. return result;
  187. },
  188. <span id='Ext-form-field-File-method-createFileInput'> /**
  189. </span> * @private
  190. * Creates the file input element. It is inserted into the trigger button component, made
  191. * invisible, and floated on top of the button's other content so that it will receive the
  192. * button's clicks.
  193. */
  194. createFileInput : function() {
  195. var me = this;
  196. me.fileInputEl = me.buttonEl.createChild({
  197. name: me.getName(),
  198. id: me.id + '-fileInputEl',
  199. cls: Ext.baseCSSPrefix + 'form-file-input',
  200. tag: 'input',
  201. type: 'file',
  202. size: 1
  203. });
  204. me.fileInputEl.on({
  205. scope: me,
  206. change: me.onFileChange
  207. });
  208. },
  209. <span id='Ext-form-field-File-method-onFileChange'> /**
  210. </span> * @private Event handler fired when the user selects a file.
  211. */
  212. onFileChange: function() {
  213. this.lastValue = null; // force change event to get fired even if the user selects a file with the same name
  214. Ext.form.field.File.superclass.setValue.call(this, this.fileInputEl.dom.value);
  215. },
  216. <span id='Ext-form-field-File-method-setValue'> /**
  217. </span> * Overridden to do nothing
  218. * @method
  219. */
  220. setValue: Ext.emptyFn,
  221. reset : function(){
  222. var me = this;
  223. if (me.rendered) {
  224. me.fileInputEl.remove();
  225. me.createFileInput();
  226. me.inputEl.dom.value = '';
  227. }
  228. me.callParent();
  229. },
  230. onDisable: function(){
  231. this.callParent();
  232. this.disableItems();
  233. },
  234. disableItems: function(){
  235. var file = this.fileInputEl;
  236. if (file) {
  237. file.dom.disabled = true;
  238. }
  239. this['buttonEl-btnEl'].dom.disabled = true;
  240. },
  241. onEnable: function(){
  242. var me = this;
  243. me.callParent();
  244. me.fileInputEl.dom.disabled = false;
  245. this['buttonEl-btnEl'].dom.disabled = false;
  246. },
  247. isFileUpload: function() {
  248. return true;
  249. },
  250. extractFileInput: function() {
  251. var fileInput = this.fileInputEl.dom;
  252. this.reset();
  253. return fileInput;
  254. },
  255. onDestroy: function(){
  256. Ext.destroyMembers(this, 'fileInputEl', 'buttonEl');
  257. this.callParent();
  258. }
  259. });</pre>
  260. </body>
  261. </html>