2011-05-20 01:38:36 +02:00
|
|
|
(function($) {
|
|
|
|
|
|
|
|
$.entwine('ss', function($){
|
2011-07-05 11:34:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The "content" area contains all of the section specific UI (excluding the menu).
|
|
|
|
* This area can be a form itself, as well as contain one or more forms.
|
|
|
|
* For example, a page edit form might fill the whole area,
|
|
|
|
* while a ModelAdmin layout shows a search form on the left, and edit form on the right.
|
|
|
|
*/
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-content').entwine({
|
2011-05-20 01:38:36 +02:00
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
onmatch: function() {
|
|
|
|
var self = this;
|
|
|
|
|
2011-07-15 10:37:46 +02:00
|
|
|
// Force initialization of tabsets to avoid layout glitches
|
2012-04-17 17:40:37 +02:00
|
|
|
this.find('.cms-tabset').redrawTabs();
|
2011-07-15 10:37:46 +02:00
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
this._super();
|
|
|
|
},
|
2012-05-11 07:36:18 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2011-10-29 20:58:48 +02:00
|
|
|
|
2011-07-21 18:39:26 +02:00
|
|
|
redraw: function() {
|
2011-12-15 20:37:20 +01:00
|
|
|
// Force initialization of tabsets to avoid layout glitches
|
2012-04-17 17:40:37 +02:00
|
|
|
this.add(this.find('.cms-tabset')).redrawTabs();
|
2011-12-15 20:37:20 +01:00
|
|
|
|
2011-07-21 18:39:26 +02:00
|
|
|
this.layout();
|
|
|
|
},
|
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
/**
|
|
|
|
* Function: ajaxSubmit
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* {DOMElement} button - The pressed button (optional)
|
|
|
|
* {Function} callback - Called in complete() handler of jQuery.ajax()
|
|
|
|
* {Object} ajaxOptions - Object literal to merge into $.ajax() call
|
|
|
|
* {boolean} loadResponse - Render response through _loadResponse() (Default: true)
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* (boolean)
|
|
|
|
*/
|
|
|
|
submitForm: function(form, button, callback, ajaxOptions, loadResponse) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// look for save button
|
|
|
|
if(!button) button = this.find('.Actions :submit[name=action_save]');
|
|
|
|
// default to first button if none given - simulates browser behaviour
|
|
|
|
if(!button) button = this.find('.Actions :submit:first');
|
|
|
|
|
2011-12-15 12:23:32 +01:00
|
|
|
form.trigger('beforesave');
|
2011-06-08 01:35:56 +02:00
|
|
|
this.trigger('submitform', {form: form, button: button});
|
|
|
|
|
|
|
|
// set button to "submitting" state
|
|
|
|
$(button).addClass('loading');
|
|
|
|
|
|
|
|
// validate if required
|
|
|
|
if(!form.validate()) {
|
|
|
|
// TODO Automatically switch to the tab/position of the first error
|
|
|
|
statusMessage("Validation failed.", "bad");
|
|
|
|
|
|
|
|
$(button).removeClass('loading');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// save tab selections in order to reconstruct them later
|
|
|
|
var selectedTabs = [];
|
2012-04-17 17:40:37 +02:00
|
|
|
form.find('.cms-tabset').each(function(i, el) {
|
2011-06-08 01:35:56 +02:00
|
|
|
if($(el).attr('id')) selectedTabs.push({id:$(el).attr('id'), selected:$(el).tabs('option', 'selected')});
|
|
|
|
});
|
|
|
|
|
|
|
|
// get all data from the form
|
|
|
|
var formData = form.serializeArray();
|
|
|
|
// add button action
|
|
|
|
formData.push({name: $(button).attr('name'), value:'1'});
|
2012-03-30 15:58:54 +02:00
|
|
|
// Artificial HTTP referer, IE doesn't submit them via ajax.
|
|
|
|
// Also rewrites anchors to their page counterparts, which is important
|
|
|
|
// as automatic browser ajax response redirects seem to discard the hash/fragment.
|
|
|
|
formData.push({name: 'BackURL', value:History.getPageUrl()});
|
|
|
|
|
2012-05-18 07:36:06 +02:00
|
|
|
// Some action buttons are redirecting to content areas as oposed to reloading the form.
|
|
|
|
// They will have pjax-content class on them.
|
|
|
|
var pjaxType = 'CurrentForm', pjaxSelector = '.cms-edit-form';
|
|
|
|
if ($(button).hasClass('pjax-content')) {
|
|
|
|
pjaxType = 'Content';
|
|
|
|
pjaxSelector = '.cms-content';
|
|
|
|
}
|
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
jQuery.ajax(jQuery.extend({
|
2012-04-10 12:27:40 +02:00
|
|
|
headers: {
|
2012-05-18 07:36:06 +02:00
|
|
|
"X-Pjax" : pjaxType,
|
|
|
|
'X-Pjax-Selector': pjaxSelector
|
2012-04-10 12:27:40 +02:00
|
|
|
},
|
2011-06-08 01:35:56 +02:00
|
|
|
url: form.attr('action'),
|
|
|
|
data: formData,
|
|
|
|
type: 'POST',
|
|
|
|
complete: function(xmlhttp, status) {
|
|
|
|
$(button).removeClass('loading');
|
|
|
|
|
|
|
|
// TODO This should be using the plugin API
|
|
|
|
form.removeClass('changed');
|
|
|
|
|
|
|
|
if(callback) callback(xmlhttp, status);
|
|
|
|
|
|
|
|
// pass along original form data to enable old/new comparisons
|
|
|
|
if(loadResponse !== false) {
|
|
|
|
self.submitForm_responseHandler(form, xmlhttp.responseText, status, xmlhttp, formData);
|
|
|
|
}
|
2012-03-08 17:24:17 +01:00
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
// Re-init tabs (in case the form tag itself is a tabset)
|
2012-04-17 17:40:37 +02:00
|
|
|
if(self.hasClass('cms-tabset')) self.removeClass('cms-tabset').addClass('cms-tabset');
|
2012-02-16 12:17:03 +01:00
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
// re-select previously saved tabs
|
|
|
|
$.each(selectedTabs, function(i, selectedTab) {
|
|
|
|
form.find('#' + selectedTab.id).tabs('select', selectedTab.selected);
|
|
|
|
});
|
2012-03-08 17:24:17 +01:00
|
|
|
|
|
|
|
// Redraw the layout
|
|
|
|
$('.cms-container').redraw();
|
2011-06-08 01:35:56 +02:00
|
|
|
},
|
|
|
|
dataType: 'html'
|
|
|
|
}, ajaxOptions));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: _loadResponse
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* {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,
|
|
|
|
* but the old <form> tag itself stays intact.
|
|
|
|
* {String} status
|
|
|
|
* {XMLHTTPRequest} xmlhttp - ..
|
|
|
|
* {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.
|
|
|
|
*/
|
|
|
|
submitForm_responseHandler: function(oldForm, data, status, xmlhttp, origData) {
|
|
|
|
if(status == 'success') {
|
2012-04-05 22:13:56 +02:00
|
|
|
if(!data) return;
|
|
|
|
|
2012-03-12 13:30:17 +01:00
|
|
|
var form, newContent = $(data);
|
|
|
|
|
|
|
|
// HACK If response contains toplevel panel rather than a form, replace it instead.
|
|
|
|
// For example, a page view shows tree + edit form. Deleting this page redirects to
|
|
|
|
// the "pages" overview, which doesn't have a separate tree panel.
|
|
|
|
if(newContent.is('.cms-content')) {
|
|
|
|
$('.cms-content').replaceWith(newContent);
|
|
|
|
} else {
|
|
|
|
form = this.replaceForm(oldForm, newContent);
|
|
|
|
}
|
2011-06-08 01:35:56 +02:00
|
|
|
|
2012-02-09 17:17:39 +01:00
|
|
|
if(typeof(Behaviour) != 'undefined') Behaviour.apply(); // refreshes ComplexTableField
|
2012-02-24 04:46:46 +01:00
|
|
|
|
2012-02-27 00:47:36 +01:00
|
|
|
this.trigger('reloadeditform', {form: form, origData: origData, xmlhttp: xmlhttp});
|
2011-06-08 01:35:56 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {jQuery} New form element
|
|
|
|
*/
|
|
|
|
replaceForm: function(form, html) {
|
|
|
|
if(html) {
|
|
|
|
var parent = form.parent(), id = form.attr('id');
|
|
|
|
form.replaceWith(html);
|
|
|
|
// Try to get the new form by ID (assuming they're identical), otherwise fall back to the first form in the parent
|
|
|
|
return id ? $('#' + id) : parent.children('form:first');
|
|
|
|
} else {
|
|
|
|
this.removeForm(form);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: removeForm
|
|
|
|
*
|
|
|
|
* Remove everying inside the <form> tag
|
|
|
|
* with a custom HTML fragment. Useful e.g. for deleting a page in the CMS.
|
|
|
|
* Checks for unsaved changes before removing the form
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* {String} placeholderHtml - Short note why the form has been removed, displayed in <p> tags.
|
|
|
|
* Falls back to the default RemoveText() option (Optional)
|
|
|
|
*/
|
|
|
|
removeForm: function(form, placeholderHtml) {
|
|
|
|
if(!placeholderHtml) placeholderHtml = this.getPlaceholderHtml();
|
|
|
|
// Alert when unsaved changes are present
|
2011-12-15 12:23:32 +01:00
|
|
|
if(!form.confirmUnsavedChanges()) return;
|
2011-06-08 01:35:56 +02:00
|
|
|
this.trigger('removeform');
|
|
|
|
this.html(placeholderHtml);
|
|
|
|
// TODO This should be using the plugin API
|
|
|
|
this.removeClass('changed');
|
2011-05-20 01:38:36 +02:00
|
|
|
}
|
|
|
|
});
|
2012-02-15 17:40:27 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
/**
|
|
|
|
* Load edit form for the selected node when its clicked.
|
|
|
|
*/
|
|
|
|
$('.cms-content .cms-tree').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
var self = this;
|
2012-02-15 17:40:27 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
this._super();
|
2012-02-15 17:40:27 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
this.bind('select_node.jstree', function(e, data) {
|
|
|
|
var node = data.rslt.obj, loadedNodeID = self.find(':input[name=ID]').val(), origEvent = data.args[2], container = $('.cms-container');
|
|
|
|
|
|
|
|
// Don't trigger unless coming from a click event.
|
|
|
|
// Avoids problems with automated section switches from tree to detail view
|
|
|
|
// when JSTree auto-selects elements on first load.
|
|
|
|
if(!origEvent) {
|
|
|
|
return false;
|
|
|
|
}else if($(origEvent.target).hasClass('jstree-icon') || $(origEvent.target).hasClass('jstree-pageicon')){
|
|
|
|
// in case the click is not on the node title, ie on pageicon or dragicon,
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow checking disabled nodes
|
|
|
|
if($(node).hasClass('disabled')) return false;
|
|
|
|
|
|
|
|
// Don't allow reloading of currently selected node,
|
|
|
|
// mainly to avoid doing an ajax request on initial page load
|
|
|
|
if($(node).data('id') == loadedNodeID) return;
|
|
|
|
|
|
|
|
var url = $(node).find('a:first').attr('href');
|
|
|
|
if(url && url != '#') {
|
|
|
|
|
|
|
|
// Ensure URL is absolute (important for IE)
|
|
|
|
if($.path.isExternal($(node).find('a:first'))) url = url = $.path.makeUrlAbsolute(url, $('base').attr('href'));
|
|
|
|
// Retain search parameters
|
|
|
|
if(document.location.search) url = $.path.addSearchParams(url, document.location.search.replace(/^\?/, ''));
|
|
|
|
// Load new page
|
|
|
|
container.loadPanel(url);
|
|
|
|
} else {
|
|
|
|
self.removeForm();
|
|
|
|
}
|
|
|
|
});
|
2012-05-11 07:36:18 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
|
|
|
});
|
2012-04-17 22:29:45 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.cms-content.loading,.cms-edit-form.loading,.cms-content-fields.loading,.cms-content-view.loading').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
this.append('<div class="cms-content-loading-overlay ui-widget-overlay-light"></div><div class="cms-content-loading-spinner"></div>');
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2012-05-11 05:31:12 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this.find('.cms-content-loading-overlay,.cms-content-loading-spinner').remove();
|
2012-05-11 07:36:18 +02:00
|
|
|
this._super();
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
|
|
|
});
|
2011-07-15 10:37:46 +02:00
|
|
|
});
|
2012-02-15 19:32:23 +01:00
|
|
|
|
2012-04-03 01:29:44 +02:00
|
|
|
})(jQuery);
|