Nullity
22 Jun 2007, 7:24 AM
Keeping with the same spirit as the InlineTextField (http://extjs.com/forum/showthread.php?t=7617), here is an InlineTextArea field:
Ext.form.InlineTextArea = function(config) {
Ext.form.InlineTextArea.superclass.constructor.call(this, config);
this.on('specialkey', function(f, e) {
if (e.getKey() == e.ESC) {
f.setValue(this.startValue);
f.blur();
}
}, this);
};
Ext.extend(Ext.form.InlineTextArea, Ext.form.TextArea, {
inlineClass: 'x-form-inline-field',
disabledClass: 'x-form-inline-field-disabled',
saveOnlyOnChange: true,
confirmSave: false,
confirmText: 'The data has been successfully saved.',
doSave : function() {
var cfg = this.autoSave;
this.params = (this.name || this.id) + '=' + this.getValue();
if (typeof cfg == 'object') {
this.method = cfg.method || 'POST';
this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
this.scope = cfg.scope || this.callback;
if (this.confirmSave === true) {
var success = function() {
Ext.MessageBox.alert('Success', this.confirmText);
}.createDelegate(this);
this.callback.success = success.createSequence(this.callback.success);
}
var p = (cfg.params) ? cfg.params : '';
if (p) {
if (typeof p == 'object') {
this.params += '&' + Ext.urlEncode(p);
}
else if (typeof p == 'string' && p.length) {
this.params += '&' + p;
}
}
this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
}
else if (typeof cfg == 'string') {
this.method = 'POST';
this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
}
Ext.Ajax.request({
url: this.url,
method: this.method,
success: this.callback.success,
failure: this.callback.failure,
params: this.params,
scope: this.scope
});
},
reset : function() {
Ext.form.TextField.superclass.reset.call(this);
if (this.value) {
this.setRawValue(this.value);
}
else if (this.emptyText && this.getRawValue().length < 1) {
this.setRawValue(this.emptyText);
this.el.addClass(this.emptyClass);
}
}
});
Ext.override(Ext.form.InlineTextArea, {
onRender : Ext.form.TextArea.prototype.onRender.createSequence(function() {
this.el.addClass(this.inlineClass);
if (this.editable === false) {
this.disabled = true;
}
}),
onFocus : Ext.form.TextArea.prototype.onFocus.createSequence(function() {
if (this.editable !== false) {
this.el.removeClass(this.inlineClass);
}
}),
onBlur : Ext.form.TextArea.prototype.onBlur.createSequence(function() {
if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
this.el.addClass(this.inlineClass);
if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.startValue)) {
this.doSave();
}
}
})
});
... and the needed CSS (same as InlineTextField, just with a small addition):
.x-form-inline-field, textarea.x-form-inline-field {
background: transparent;
border: 1px solid transparent;
cursor: pointer;
overflow: hidden;
}
.x-form-inline-field-disabled {
color: #000000;
cursor: default;
opacity: 1.0;
-moz-opacity: 1.0;
filter: alpha(opacity=100);
}
UPDATE 1: I added code suggested by schmidetzki in the InlineTextField thread which cancels the edit if the ESC key is pressed.
UPDATE 2 (9/10/2007): Updated code to version 1.2. Added new options: 'confirmSave' and 'confirmText'. The first, 'confirmSave' is a boolean which when true, will display a MessageBox containing the text set by 'confirmText' (optional) when the changes are saved successfully. Also, the 'autoSave' object now supports a 'scope' setting. Lastly, I fixed a CSS bug where under certain circumstances the scrollbars would show even when the field is blurred (pointed out by sintax.era).
UPDATE 3 (9/11/2007): Updated code for version 1.3. Changed the way 'confirmSave' was handled to fix a bug. Refactored some code to be cleaner.
Ext.form.InlineTextArea = function(config) {
Ext.form.InlineTextArea.superclass.constructor.call(this, config);
this.on('specialkey', function(f, e) {
if (e.getKey() == e.ESC) {
f.setValue(this.startValue);
f.blur();
}
}, this);
};
Ext.extend(Ext.form.InlineTextArea, Ext.form.TextArea, {
inlineClass: 'x-form-inline-field',
disabledClass: 'x-form-inline-field-disabled',
saveOnlyOnChange: true,
confirmSave: false,
confirmText: 'The data has been successfully saved.',
doSave : function() {
var cfg = this.autoSave;
this.params = (this.name || this.id) + '=' + this.getValue();
if (typeof cfg == 'object') {
this.method = cfg.method || 'POST';
this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
this.scope = cfg.scope || this.callback;
if (this.confirmSave === true) {
var success = function() {
Ext.MessageBox.alert('Success', this.confirmText);
}.createDelegate(this);
this.callback.success = success.createSequence(this.callback.success);
}
var p = (cfg.params) ? cfg.params : '';
if (p) {
if (typeof p == 'object') {
this.params += '&' + Ext.urlEncode(p);
}
else if (typeof p == 'string' && p.length) {
this.params += '&' + p;
}
}
this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
}
else if (typeof cfg == 'string') {
this.method = 'POST';
this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
}
Ext.Ajax.request({
url: this.url,
method: this.method,
success: this.callback.success,
failure: this.callback.failure,
params: this.params,
scope: this.scope
});
},
reset : function() {
Ext.form.TextField.superclass.reset.call(this);
if (this.value) {
this.setRawValue(this.value);
}
else if (this.emptyText && this.getRawValue().length < 1) {
this.setRawValue(this.emptyText);
this.el.addClass(this.emptyClass);
}
}
});
Ext.override(Ext.form.InlineTextArea, {
onRender : Ext.form.TextArea.prototype.onRender.createSequence(function() {
this.el.addClass(this.inlineClass);
if (this.editable === false) {
this.disabled = true;
}
}),
onFocus : Ext.form.TextArea.prototype.onFocus.createSequence(function() {
if (this.editable !== false) {
this.el.removeClass(this.inlineClass);
}
}),
onBlur : Ext.form.TextArea.prototype.onBlur.createSequence(function() {
if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
this.el.addClass(this.inlineClass);
if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.startValue)) {
this.doSave();
}
}
})
});
... and the needed CSS (same as InlineTextField, just with a small addition):
.x-form-inline-field, textarea.x-form-inline-field {
background: transparent;
border: 1px solid transparent;
cursor: pointer;
overflow: hidden;
}
.x-form-inline-field-disabled {
color: #000000;
cursor: default;
opacity: 1.0;
-moz-opacity: 1.0;
filter: alpha(opacity=100);
}
UPDATE 1: I added code suggested by schmidetzki in the InlineTextField thread which cancels the edit if the ESC key is pressed.
UPDATE 2 (9/10/2007): Updated code to version 1.2. Added new options: 'confirmSave' and 'confirmText'. The first, 'confirmSave' is a boolean which when true, will display a MessageBox containing the text set by 'confirmText' (optional) when the changes are saved successfully. Also, the 'autoSave' object now supports a 'scope' setting. Lastly, I fixed a CSS bug where under certain circumstances the scrollbars would show even when the field is blurred (pointed out by sintax.era).
UPDATE 3 (9/11/2007): Updated code for version 1.3. Changed the way 'confirmSave' was handled to fix a bug. Refactored some code to be cleaner.