API CHANGE Renamed JS event from 'reloadeditform' on 'aftersubmitform', 'beforesave' to 'beforesubmitform'. Listening to bubbled event on container to avoid binding to forms which are removed before the event is triggered.

This commit is contained in:
Ingo Schommer 2012-06-12 17:13:36 +02:00
parent ed9c856442
commit 2637969bcd
6 changed files with 20 additions and 33 deletions

View File

@ -6,7 +6,7 @@
// Can't bind this through jQuery // Can't bind this through jQuery
window.onbeforeunload = function(e) { window.onbeforeunload = function(e) {
var form = $('.cms-edit-form'); var form = $('.cms-edit-form');
form.trigger('beforesave'); form.trigger('beforesubmitform');
if(form.is('.changed')) return ss.i18n._t('LeftAndMain.CONFIRMUNSAVEDSHORT'); if(form.is('.changed')) return ss.i18n._t('LeftAndMain.CONFIRMUNSAVEDSHORT');
}; };
@ -142,7 +142,7 @@
* or the user wants to discard them. * or the user wants to discard them.
*/ */
confirmUnsavedChanges: function() { confirmUnsavedChanges: function() {
this.trigger('beforesave'); this.trigger('beforesubmitform');
return (this.is('.changed')) ? confirm(ss.i18n._t('LeftAndMain.CONFIRMUNSAVED')) : true; return (this.is('.changed')) ? confirm(ss.i18n._t('LeftAndMain.CONFIRMUNSAVED')) : true;
}, },

View File

@ -86,12 +86,9 @@
} }
self.updateItems(); self.updateItems();
}; };
$('.cms-container').live('afterstatechange', function(e, data) { $('.cms-container').live('afterstatechange aftersubmitform', function(e, data) {
updateMenuFromResponse(data.xhr); updateMenuFromResponse(data.xhr);
}); });
$('.cms-edit-form').live('reloadeditform', function(e, data) {
updateMenuFromResponse(data.xmlhttp);
});
// Sync collapsed state with parent panel // Sync collapsed state with parent panel
this.parents('.cms-panel:first').bind('toggle', function(e) { this.parents('.cms-panel:first').bind('toggle', function(e) {

View File

@ -57,15 +57,10 @@
} else { } else {
self.block(); self.block();
} }
} };
// Listen to form loads. Limit to CMS forms for the moment
$('.cms-edit-form').bind('reloadeditform', function(e, ui) {
updateAfterXhr();
});
// Listen to history state changes // Listen to history state changes
$('.cms-container').bind('afterstatechange', function(e) { $('.cms-container').bind('afterstatechange aftersubmitform', function(e) {
updateAfterXhr(); updateAfterXhr();
}); });

View File

@ -97,16 +97,12 @@
}); });
}); });
$('.cms-container').bind('afterstatechange.tree', function(e, data) { $('.cms-container').bind('afterstatechange.tree aftersubmitform.tree', function(e, data) {
self.updateFromEditForm(e.origData);
});
$('.cms-content').bind('reloadeditform.tree', function(e, data) {
self.updateFromEditForm(e.origData); self.updateFromEditForm(e.origData);
}); });
}, },
onunmatch: function() { onunmatch: function() {
$('.cms-container').unbind('afterstatechange.tree'); $('.cms-container').unbind('afterstatechange.tree aftersubmitform.tree');
$('.cms-content').unbind('reloadeditform.tree');
this._super(); this._super();
}, },
@ -208,7 +204,9 @@
/** /**
* Assumes to be triggered by a form element with the following input fields: * Assumes to be triggered by a form element with the following input fields:
* ID, ParentID, TreeTitle (or Title), ClassName * ID, ParentID, TreeTitle (or Title), ClassName.
*
* @todo Serverside node refresh, see http://open.silverstripe.org/ticket/7450
*/ */
updateFromEditForm: function(origData) { updateFromEditForm: function(origData) {
var self = this, var self = this,

View File

@ -64,7 +64,7 @@ jQuery.noConflict();
* Events: * Events:
* ajaxsubmit - ... * ajaxsubmit - ...
* validate - ... * validate - ...
* reloadeditform - ... * aftersubmitform - ...
*/ */
$('.cms-container').entwine({ $('.cms-container').entwine({
@ -102,10 +102,6 @@ jQuery.noConflict();
self.redraw(); self.redraw();
}); });
$('.cms-edit-form').live('reloadeditform', function(e, data) {
self.redraw();
});
// Remove loading screen // Remove loading screen
$('.ss-loading-screen').hide(); $('.ss-loading-screen').hide();
$('body').removeClass('loading'); $('body').removeClass('loading');
@ -120,6 +116,9 @@ jQuery.noConflict();
onunmatch: function() { onunmatch: function() {
this._super(); this._super();
}, },
onaftersubmitform: function() {
this.redraw();
},
redraw: function() { redraw: function() {
if(window.debug) console.log('redraw', this.attr('class'), this.get(0)); if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
@ -196,7 +195,7 @@ jQuery.noConflict();
// default to first button if none given - simulates browser behaviour // default to first button if none given - simulates browser behaviour
if(!button) button = this.find('.Actions :submit:first'); if(!button) button = this.find('.Actions :submit:first');
form.trigger('beforesave'); form.trigger('beforesubmitform');
this.trigger('submitform', {form: form, button: button}); this.trigger('submitform', {form: form, button: button});
// set button to "submitting" state // set button to "submitting" state
@ -256,10 +255,7 @@ jQuery.noConflict();
newForm.find('#' + selectedTab.id).tabs('select', selectedTab.selected); newForm.find('#' + selectedTab.id).tabs('select', selectedTab.selected);
}); });
// Redraw the layout newForm.trigger('aftersubmitform', {status: status, xhr: xhr, formData: formData});
$('.cms-container').redraw();
form.trigger('reloadeditform', {form: newForm, formData: formData, xmlhttp: xhr});
}, },
dataType: 'json' dataType: 'json'
}, ajaxOptions)); }, ajaxOptions));
@ -337,7 +333,7 @@ jQuery.noConflict();
var title = xhr.getResponseHeader('X-Title'); var title = xhr.getResponseHeader('X-Title');
if(title) document.title = title; if(title) document.title = title;
var newFragments = {}, newContentEls = $([]); var newFragments = {}, newContentEls;
if(xhr.getResponseHeader('Content-Type') == 'text/json') { if(xhr.getResponseHeader('Content-Type') == 'text/json') {
newFragments = data; newFragments = data;
} else { } else {
@ -352,7 +348,8 @@ jQuery.noConflict();
}), newContentEl = $(html); }), newContentEl = $(html);
// Add to result collection // Add to result collection
newContentEls.add(newContentEl); if(newContentEls) newContentEls.add(newContentEl);
else newContentEls = newContentEl;
// Update panels // Update panels
if(newContentEl.find('.cms-container').length) { if(newContentEl.find('.cms-container').length) {

View File

@ -205,7 +205,7 @@ ss.editorWrappers['default'] = ss.editorWrappers.tinyMCE;
onmatch : function() { onmatch : function() {
var self = this, edClass = this.data('editor') || ss.editorWrappers['default'], ed = edClass(); var self = this, edClass = this.data('editor') || ss.editorWrappers['default'], ed = edClass();
this.setEditor(ed); this.setEditor(ed);
this.closest('form').bind('beforesave', function() { this.closest('form').bind('beforesubmitform', function() {
// TinyMCE modifies input, so change tracking might get false // TinyMCE modifies input, so change tracking might get false
// positives when comparing string values - don't save if the editor doesn't think its dirty. // positives when comparing string values - don't save if the editor doesn't think its dirty.
if(self.isChanged()) { if(self.isChanged()) {