ENHANCEMENT Better response handling in LeftAndMain.EditForm

BUGFIX Fixed event cancellation in LeftAndMain.EditForm javascript buttons

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@92672 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-11-21 02:39:06 +00:00
parent b99ee16d88
commit 41c1c49dfe

View File

@ -57,20 +57,21 @@
} }
// get all data from the form // get all data from the form
var data = this.serializeArray(); var formData = this.serializeArray();
// add button action // add button action
data.push({name: $(button).attr('name'), value:'1'}); formData.push({name: $(button).attr('name'), value:'1'});
$.post(
this.attr('action'),
data,
function(response) {
$(button).removeClass('loading');
self._loadResponse(response); $.ajax({
url: this.attr('action'),
data: formData,
type: 'POST',
complete: function(xmlhttp, status) {
$(button).removeClass('loading');
// pass along original form data to enable old/new comparisons
self._loadResponse(xmlhttp.responseText, status, xmlhttp, formData);
}, },
// @todo Currently all responses are assumed to be evaluated dataType: 'html'
'script' });
);
return false; return false;
}, },
@ -96,15 +97,14 @@
*/ */
load: function(url, callback) { load: function(url, callback) {
var self = this; var self = this;
$.get( $.ajax({
url, url: url,
function(response) { complete: function(xmlhttp, status) {
self._loadResponse(response); self._loadResponse(xmlhttp.responseText, status, xmlhttp);
if(callback) callback.apply(self, [response]); if(callback) callback.apply(self, arguments);
}, },
// @todo Currently all responses are assumed to be evaluated dataType: 'html'
'script' });
);
}, },
/** /**
@ -114,7 +114,8 @@
* @param {String} removeText Short note why the form has been removed, displayed in <p> tags. * @param {String} removeText Short note why the form has been removed, displayed in <p> tags.
* Falls back to the default RemoveText() option (Optional) * Falls back to the default RemoveText() option (Optional)
*/ */
remove: function(removeText) { removeForm: function(removeText) {
if(!removeText) removeText = this.RemoveText();
this.html('<p>' + removeText + '</p>'); this.html('<p>' + removeText + '</p>');
}, },
@ -133,49 +134,65 @@
}, },
/** /**
* @param {String} result Either HTML for straight insertion, or eval'ed JavaScript. * @param {String} data Either HTML for straight insertion, or eval'ed JavaScript.
* If passed as HTML, it is assumed that everying inside the <form> tag is replaced, * If passed as HTML, it is assumed that everying inside the <form> tag is replaced,
* but the old <form> tag itself stays intact. * but the old <form> tag itself stays intact.
* @param {String} status
* @param {XMLHTTPRequest} xmlhttp
* @param {Array} origData The original submitted data, useful to do comparisons of changed
* values in new form output, e.g. to detect a URLSegment being changed on the serverside.
* Array in jQuery serializeArray() notation.
*/ */
_loadResponse: function(response) { _loadResponse: function(data, status, xmlhttp, origData) {
this.cleanup(); if(status == 'success') {
this.cleanup();
var html = response; var html = data;
// Rewrite # links // Rewrite # links
html = html.replace(/(<a[^>]+href *= *")#/g, '$1' + window.location.href.replace(/#.*$/,'') + '#'); html = html.replace(/(<a[^>]+href *= *")#/g, '$1' + window.location.href.replace(/#.*$/,'') + '#');
// Rewrite iframe links (for IE) // Rewrite iframe links (for IE)
html = html.replace(/(<iframe[^>]*src=")([^"]+)("[^>]*>)/g, '$1' + $('base').attr('href') + '$2$3'); html = html.replace(/(<iframe[^>]*src=")([^"]+)("[^>]*>)/g, '$1' + $('base').attr('href') + '$2$3');
// Prepare iframes for removal, otherwise we get loading bugs // Prepare iframes for removal, otherwise we get loading bugs
this.find('iframe').each(function() { this.find('iframe').each(function() {
this.contentWindow.location.href = 'about:blank'; this.contentWindow.location.href = 'about:blank';
this.remove(); this.remove();
}); });
this.html(html); // update form content
// Optionally get the form attributes from embedded fields, see Form->formHtmlContent() if(html) {
for(var overrideAttr in {'action':true,'method':true,'enctype':true,'name':true}) { this.html(html);
var el = this.find(':input[name='+ '_form_' + overrideAttr + ']'); } else {
if(el) { this.removeForm();
this.attr(overrideAttr, el.val());
el.remove();
} }
// Optionally get the form attributes from embedded fields, see Form->formHtmlContent()
for(var overrideAttr in {'action':true,'method':true,'enctype':true,'name':true}) {
var el = this.find(':input[name='+ '_form_' + overrideAttr + ']');
if(el) {
this.attr(overrideAttr, el.val());
el.remove();
}
}
Behaviour.apply(); // refreshes ComplexTableField
// focus input on first form element
this.find(':input:visible:first').focus();
this.trigger('loadnewpage', {data: data, origData: origData});
} }
// set status message based on response
var _statusMessage = (xmlhttp.getResponseHeader('X-Status')) ? xmlhttp.getResponseHeader('X-Status') : xmlhttp.statusText;
if(this.hasClass('validationerror')) { if(this.hasClass('validationerror')) {
// TODO validation shouldnt need a special case
statusMessage(ss.i18n._t('ModelAdmin.VALIDATIONERROR', 'Validation Error'), 'bad'); statusMessage(ss.i18n._t('ModelAdmin.VALIDATIONERROR', 'Validation Error'), 'bad');
} else { } else {
statusMessage(ss.i18n._t('ModelAdmin.SAVED', 'Saved'), 'good'); statusMessage(_statusMessage, (xmlhttp.status >= 400) ? 'bad' : 'good');
} }
Behaviour.apply(); // refreshes ComplexTableField
// focus input on first form element
this.find(':input:visible:first').focus();
this.trigger('loadnewpage', {response: response});
} }
}; };
}); });
@ -191,7 +208,7 @@
onmatch: function() { onmatch: function() {
var self = this; var self = this;
// TODO Fix once concrete library is updated // TODO Fix once concrete library is updated
this.bind('click', function(e) {self.clickFake(e);}); this.bind('click', function(e) {return self.clickFake(e);});
}, },
clickFake: function(e) { clickFake: function(e) {
$(this[0].form).concrete('ss').ajaxSubmit(this); $(this[0].form).concrete('ss').ajaxSubmit(this);