2009-11-21 03:38:20 +01:00
|
|
|
(function($) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Base edit form, provides ajaxified saving
|
|
|
|
* and reloading itself through the ajax return values.
|
|
|
|
* Takes care of resizing tabsets within the layout container.
|
|
|
|
* @name ss.Form_EditForm
|
|
|
|
*/
|
|
|
|
$('#Form_EditForm').concrete('ss',function($){
|
|
|
|
return/** @lends ss.Form_EditForm */{
|
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* @type String HTML text to show when the form has been deleted.
|
2009-11-21 03:38:46 +01:00
|
|
|
* @todo i18n
|
2009-11-21 03:38:31 +01:00
|
|
|
*/
|
2009-11-21 03:39:20 +01:00
|
|
|
RemoveText: 'Removed',
|
2009-11-21 03:38:46 +01:00
|
|
|
|
|
|
|
onmatch: function() {
|
|
|
|
// Don't bind any events here, as we dont replace the
|
|
|
|
// full <form> tag by any ajax updates they won't automatically reapply
|
|
|
|
|
|
|
|
_super();
|
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* Suppress submission unless it is handled through ajaxSubmit()
|
|
|
|
*/
|
|
|
|
onsubmit: function(e) {
|
|
|
|
return false;
|
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* @param {DOMElement} button The pressed button (optional)
|
|
|
|
*/
|
|
|
|
ajaxSubmit: function(button) {
|
|
|
|
// default to first button if none given - simulates browser behaviour
|
|
|
|
if(!button) button = this.find(':submit:first');
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
var self = this;
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
this.trigger('ajaxsubmit', {button: button});
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
// set button to "submitting" state
|
|
|
|
$(button).addClass('loading');
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
// @todo TinyMCE coupling
|
|
|
|
if(typeof tinyMCE != 'undefined') tinyMCE.triggerSave();
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
// validate if required
|
|
|
|
if(!this.validate()) {
|
|
|
|
// TODO Automatically switch to the tab/position of the first error
|
|
|
|
statusMessage("Validation failed.", "bad");
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
$(button).removeClass('loading');
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
// get all data from the form
|
2009-11-21 03:39:06 +01:00
|
|
|
var formData = this.serializeArray();
|
2009-11-21 03:38:31 +01:00
|
|
|
// add button action
|
2009-11-21 03:39:06 +01:00
|
|
|
formData.push({name: $(button).attr('name'), value:'1'});
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: this.attr('action'),
|
|
|
|
data: formData,
|
|
|
|
type: 'POST',
|
|
|
|
complete: function(xmlhttp, status) {
|
2009-11-21 03:38:31 +01:00
|
|
|
$(button).removeClass('loading');
|
2009-11-21 03:39:06 +01:00
|
|
|
// pass along original form data to enable old/new comparisons
|
|
|
|
self._loadResponse(xmlhttp.responseText, status, xmlhttp, formData);
|
2009-11-21 03:38:31 +01:00
|
|
|
},
|
2009-11-21 03:39:06 +01:00
|
|
|
dataType: 'html'
|
|
|
|
});
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
return false;
|
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* Hook in (optional) validation routines.
|
|
|
|
* Currently clientside validation is not supported out of the box in the CMS.
|
|
|
|
*
|
|
|
|
* @todo Placeholder implementation
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
validate: function() {
|
|
|
|
var isValid = true;
|
|
|
|
this.trigger('validate', {isValid: isValid});
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
return isValid;
|
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* @param String url
|
|
|
|
* @param Function callback (Optional)
|
|
|
|
*/
|
|
|
|
load: function(url, callback) {
|
|
|
|
var self = this;
|
2009-11-21 03:39:06 +01:00
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
complete: function(xmlhttp, status) {
|
|
|
|
self._loadResponse(xmlhttp.responseText, status, xmlhttp);
|
|
|
|
if(callback) callback.apply(self, arguments);
|
2009-11-21 03:38:31 +01:00
|
|
|
},
|
2009-11-21 03:39:06 +01:00
|
|
|
dataType: 'html'
|
|
|
|
});
|
2009-11-21 03:38:31 +01:00
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* Remove everying inside the <form> tag
|
|
|
|
* with a custom HTML fragment. Useful e.g. for deleting a page in the CMS.
|
|
|
|
*
|
2009-11-21 03:38:46 +01:00
|
|
|
* @param {String} removeText Short note why the form has been removed, displayed in <p> tags.
|
|
|
|
* Falls back to the default RemoveText() option (Optional)
|
2009-11-21 03:38:31 +01:00
|
|
|
*/
|
2009-11-21 03:39:06 +01:00
|
|
|
removeForm: function(removeText) {
|
|
|
|
if(!removeText) removeText = this.RemoveText();
|
2009-11-21 03:38:46 +01:00
|
|
|
this.html('<p>' + removeText + '</p>');
|
2009-11-21 03:38:31 +01:00
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
|
|
|
* Remove all the currently active TinyMCE editors.
|
|
|
|
* Note: Everything that calls this externally has an inappropriate coupling to TinyMCE.
|
|
|
|
*/
|
|
|
|
cleanup: function() {
|
|
|
|
if((typeof tinymce != 'undefined') && tinymce.EditorManager) {
|
|
|
|
var id;
|
|
|
|
for(id in tinymce.EditorManager.editors) {
|
|
|
|
tinymce.EditorManager.editors[id].remove();
|
|
|
|
}
|
|
|
|
tinymce.EditorManager.editors = {};
|
2009-11-21 03:38:20 +01:00
|
|
|
}
|
2009-11-21 03:38:31 +01:00
|
|
|
},
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:38:31 +01:00
|
|
|
/**
|
2009-11-21 03:39:06 +01:00
|
|
|
* @param {String} data Either HTML for straight insertion, or eval'ed JavaScript.
|
2009-11-21 03:38:31 +01:00
|
|
|
* If passed as HTML, it is assumed that everying inside the <form> tag is replaced,
|
|
|
|
* but the old <form> tag itself stays intact.
|
2009-11-21 03:39:06 +01:00
|
|
|
* @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.
|
2009-11-21 03:38:31 +01:00
|
|
|
*/
|
2009-11-21 03:39:06 +01:00
|
|
|
_loadResponse: function(data, status, xmlhttp, origData) {
|
|
|
|
if(status == 'success') {
|
|
|
|
this.cleanup();
|
|
|
|
|
|
|
|
var html = data;
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:39:06 +01:00
|
|
|
// Rewrite # links
|
|
|
|
html = html.replace(/(<a[^>]+href *= *")#/g, '$1' + window.location.href.replace(/#.*$/,'') + '#');
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:39:06 +01:00
|
|
|
// Rewrite iframe links (for IE)
|
|
|
|
html = html.replace(/(<iframe[^>]*src=")([^"]+)("[^>]*>)/g, '$1' + $('base').attr('href') + '$2$3');
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:39:06 +01:00
|
|
|
// Prepare iframes for removal, otherwise we get loading bugs
|
|
|
|
this.find('iframe').each(function() {
|
|
|
|
this.contentWindow.location.href = 'about:blank';
|
|
|
|
this.remove();
|
|
|
|
});
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:39:06 +01:00
|
|
|
// update form content
|
|
|
|
if(html) {
|
|
|
|
this.html(html);
|
|
|
|
} else {
|
|
|
|
this.removeForm();
|
2009-11-21 03:38:48 +01:00
|
|
|
}
|
2009-11-21 03:39:06 +01:00
|
|
|
|
|
|
|
// 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});
|
2009-11-21 03:38:48 +01:00
|
|
|
}
|
2009-11-21 03:38:20 +01:00
|
|
|
|
2009-11-21 03:39:06 +01:00
|
|
|
// set status message based on response
|
|
|
|
var _statusMessage = (xmlhttp.getResponseHeader('X-Status')) ? xmlhttp.getResponseHeader('X-Status') : xmlhttp.statusText;
|
2009-11-21 03:38:31 +01:00
|
|
|
if(this.hasClass('validationerror')) {
|
2009-11-21 03:39:06 +01:00
|
|
|
// TODO validation shouldnt need a special case
|
2009-11-21 03:38:31 +01:00
|
|
|
statusMessage(ss.i18n._t('ModelAdmin.VALIDATIONERROR', 'Validation Error'), 'bad');
|
|
|
|
} else {
|
2009-11-21 03:39:06 +01:00
|
|
|
statusMessage(_statusMessage, (xmlhttp.status >= 400) ? 'bad' : 'good');
|
2009-11-21 03:38:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2009-11-21 03:38:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class All buttons in the right CMS form go through here by default.
|
|
|
|
* We need this onclick overloading because we can't get to the
|
|
|
|
* clicked button from a form.onsubmit event.
|
|
|
|
* @name ss.Form_EditForm.Actions.submit
|
|
|
|
*/
|
|
|
|
$('#Form_EditForm .Actions :submit').concrete('ss', function($){
|
|
|
|
return/** @lends ss.Form_EditForm.Actions.submit */{
|
2009-11-21 03:38:33 +01:00
|
|
|
onmatch: function() {
|
|
|
|
var self = this;
|
|
|
|
// TODO Fix once concrete library is updated
|
2009-11-21 03:39:06 +01:00
|
|
|
this.bind('click', function(e) {return self.clickFake(e);});
|
2009-11-21 03:38:33 +01:00
|
|
|
},
|
|
|
|
clickFake: function(e) {
|
|
|
|
$(this[0].form).concrete('ss').ajaxSubmit(this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2009-11-21 03:38:20 +01:00
|
|
|
}(jQuery));
|