Ext.define("Ext.form.field.File", {
extend: 'Ext.form.field.Trigger',
alias: ['widget.filefield', 'widget.fileuploadfield'],
alternateClassName: ['Ext.form.FileUploadField', 'Ext.ux.form.FileUploadField', 'Ext.form.File'],
uses: ['Ext.button.Button', 'Ext.layout.component.field.Field'],
//<locale>
buttonText: 'Browse...',
//</locale>
buttonOnly: false,
buttonMargin: 3,
fieldBodyCls: Ext.baseCSSPrefix + 'form-file-wrap',
readOnly: true,
triggerNoEditCls: '',
// private
componentLayout: 'triggerfield',
// private. Extract the file element, button outer element, and button active element.
childEls: ['fileInputEl', 'buttonEl', 'buttonEl-btnEl', 'browseButtonWrap'],
// private
onRender: function() {
var me = this,
inputEl;
me.callParent(arguments);
inputEl = me.inputEl;
inputEl.dom.name = '';
me.fileInputEl.dom.name = me.getName();
me.fileInputEl.on({
scope: me,
change: me.onFileChange
});
if (me.buttonOnly) {
me.inputCell.setDisplayed(false);
}
me.browseButtonWrap.dom.style.width = (me.browseButtonWrap.dom.lastChild.offsetWidth + me.buttonEl.getMargin('lr')) + 'px';
if (Ext.isIE) {
me.buttonEl.repaint();
}
},
getTriggerMarkup: function() {
var me = this,
result,
btn = Ext.widget('button', Ext.apply({
id: me.id + '-buttonEl',
ui: me.ui,
disabled: me.disabled,
text: me.buttonText,
cls: Ext.baseCSSPrefix + 'form-file-btn',
preventDefault: false,
style: me.buttonOnly ? '' : 'margin-left:' + me.buttonMargin + 'px'
}, me.buttonConfig)),
btnCfg = btn.getRenderTree(),
inputElCfg = {
id: me.id + '-fileInputEl',
cls: Ext.baseCSSPrefix + 'form-file-input',
tag: 'input',
type: 'file',
size: 1
};
if (me.disabled) {
inputElCfg.disabled = true;
}
btnCfg.cn = inputElCfg;
result = '<td id="' + me.id + '-browseButtonWrap">' + Ext.DomHelper.markup(btnCfg) + '</td>';
btn.destroy();
return result;
},
createFileInput : function() {
var me = this;
me.fileInputEl = me.buttonEl.createChild({
name: me.getName(),
id: me.id + '-fileInputEl',
cls: Ext.baseCSSPrefix + 'form-file-input',
tag: 'input',
type: 'file',
size: 1
});
me.fileInputEl.on({
scope: me,
change: me.onFileChange
});
},
onFileChange: function() {
this.lastValue = null;
Ext.form.field.File.superclass.setValue.call(this, this.fileInputEl.dom.value);
},
setValue: Ext.emptyFn,
reset : function(){
var me = this;
if (me.rendered) {
me.fileInputEl.remove();
me.createFileInput();
me.inputEl.dom.value = '';
}
me.callParent();
},
onDisable: function(){
this.callParent();
this.disableItems();
},
disableItems: function(){
var file = this.fileInputEl;
if (file) {
file.dom.disabled = true;
}
this['buttonEl-btnEl'].dom.disabled = true;
},
onEnable: function(){
var me = this;
me.callParent();
me.fileInputEl.dom.disabled = false;
this['buttonEl-btnEl'].dom.disabled = false;
},
isFileUpload: function() {
return true;
},
extractFileInput: function() {
var fileInput = this.fileInputEl.dom;
this.reset();
return fileInput;
},
onDestroy: function(){
Ext.destroyMembers(this, 'fileInputEl', 'buttonEl');
this.callParent();
}
});