LSform JS events: Add possibility to callback to confirm/cancel event

This commit is contained in:
Benjamin Renard 2020-06-11 20:05:28 +02:00
parent 6e14916bae
commit 9c11a81220
2 changed files with 125 additions and 21 deletions

View file

@ -3,10 +3,14 @@ var LSform = new Class({
this._modules=[]; this._modules=[];
this._fields=[]; this._fields=[];
this._elements=[]; this._elements=[];
this.listeners = { this.listeners = new Hash({
init: new Array(), init: new Hash(),
submit: new Array() submit: new Hash()
}; });
this.listeners_answers = new Hash();
// On non-ajax submit form, store confirmation status;
this.submit_confirmed = false;
if ($type($('LSform_idform'))) { if ($type($('LSform_idform'))) {
this.objecttype = $('LSform_objecttype').value; this.objecttype = $('LSform_objecttype').value;
@ -14,6 +18,8 @@ var LSform = new Class({
this.idform = $('LSform_idform').value; this.idform = $('LSform_idform').value;
} }
this.submiting = false;
this.initializeLSform(); this.initializeLSform();
this.initializeLSformLayout(); this.initializeLSformLayout();
}, },
@ -59,7 +65,7 @@ var LSform = new Class({
LSforms = $$('form.LSform'); LSforms = $$('form.LSform');
if ($type(LSforms[0])) { if ($type(LSforms[0])) {
this.LSform = LSforms[0]; this.LSform = LSforms[0];
this.LSform.addEvent('submit',this.ajaxSubmit.bindWithEvent(this)); this.LSform.addEvent('submit',this.onSubmit.bindWithEvent(this));
} }
this.fireEvent.bind(this)('init'); this.fireEvent.bind(this)('init');
@ -273,15 +279,40 @@ var LSform = new Class({
return retVal; return retVal;
}, },
ajaxSubmit: function(event) { onSubmit: function(event) {
if (this.submit_confirmed) {
// On non-ajax form, leave form submiting if already confirmed
LSdebug('onSubmit(): form submission already confirmed');
return;
}
// Stop form submiting event
event = new Event(event);
event.stop();
// Check if form is already submitting
if (this.submiting) {
// Form is already submitting: stop
LSdebug('onSubmit(): form already submiting...');
return;
}
this.submiting = true;
// Fire
LSdebug('onSubmit(): fire submit event');
this.fireEvent.bind(this)('submit', this.onSubmitConfirm.bind(this));
},
onSubmitConfirm: function (confirmed, event) {
if (!confirmed) {
this.submiting = false;
return;
}
// Check file upload to disable ajax submission in this case
this.checkUploadFileDefined(); this.checkUploadFileDefined();
if (this._ajaxSubmit) { if (this._ajaxSubmit) {
event = new Event(event);
event.stop();
this.fireEvent.bind(this)('submit');
this.LSformAjaxInput = new Element('input'); this.LSformAjaxInput = new Element('input');
this.LSformAjaxInput.setProperties ({ this.LSformAjaxInput.setProperties ({
type: 'hidden', type: 'hidden',
@ -302,6 +333,8 @@ var LSform = new Class({
if($type(this.LSformAjaxInput)) { if($type(this.LSformAjaxInput)) {
this.LSformAjaxInput.dispose(); this.LSformAjaxInput.dispose();
} }
this.submit_confirmed = true;
this.LSform.fireEvent('submit');
} }
}, },
@ -314,6 +347,7 @@ var LSform = new Class({
}, },
onAjaxSubmitComplete: function(responseText, responseXML) { onAjaxSubmitComplete: function(responseText, responseXML) {
this.submiting = false;
var data = JSON.decode(responseText); var data = JSON.decode(responseText);
if ( varLSdefault.checkAjaxReturn(data) ) { if ( varLSdefault.checkAjaxReturn(data) ) {
this.resetErrors(); this.resetErrors();
@ -381,27 +415,95 @@ var LSform = new Class({
document.location=url; document.location=url;
}, },
addEvent: function(event,fnct) { addEvent: function(event,fnct,fnct_name) {
if ($type(this.listeners[event])) { if ($type(this.listeners[event])) {
if ($type(fnct)=="function") { if ($type(fnct)=="function") {
this.listeners[event].include(fnct); if ($type(fnct_name)!="string") {
fnct_name = generate_uuid();
}
else if (this.listeners[event].has(fnct_name)) {
fnct_name = fnct_name+"_"+generate_uuid();
}
this.listeners[event].set(fnct_name, fnct);
} }
} }
}, },
fireEvent: function(event) { fireEvent: function(event, callback) {
LSdebug('LSform :: fireEvent('+event+')'); LSdebug('LSform :: fireEvent('+event+')');
if ($type(this.listeners[event])) { if (this.listeners.has(event)) {
this.listeners[event].each(function(fnct) { // If no listener configured, considered as confirmed and run callback
if ($type(callback) == "function" && this.listeners[event].getLength() == 0) {
callback(true, event);
return;
}
// Reset listeners answers state
this.listeners_answers[event] = new Hash();
// Run listeners callback
this.listeners[event].each(function(fnct, listener_uuid) {
var result;
try { try {
fnct(this); fnct(
this,
function() {
LSdebug('Listener '+listener_uuid+' confirmed');
this.eventListenerCallback.bind(this)(event, listener_uuid, true, callback);
}.bind(this),
function() {
LSdebug('Listener '+listener_uuid+' cancel');
this.eventListenerCallback.bind(this)(event, listener_uuid, false, callback);
}.bind(this)
);
} }
catch(e) { catch(e) {
LSdebug('LSform :: '+event+'() -> rater'); LSdebug('LSform :: fireEvent('+event+') :: exception occured running listener '+listener_uuid+' => considered as not-confirmed.');
LSdebug(e);
result = false;
} }
},this); },this);
} }
} },
eventListenerCallback: function(event, listener_uuid, listener_answer, final_callback) {
// Check event & listener_uuid
if (!this.listeners.has(event) || !this.listeners[event].has(listener_uuid))
return;
// Set listener answers
this.listeners_answers[event].set(listener_uuid, listener_answer);
LSdebug('LSform :: eventListenerCallback('+event+', '+listener_uuid+', '+listener_answer+')');
// Check all listeners have answered
if (this.listeners_answers[event].getLength() != this.listeners[event].getLength())
return;
// Run final callback
this.onFinalEventListenerCallback.bind(this)(event, final_callback);
},
onFinalEventListenerCallback: function(event, final_callback) {
LSdebug('LSform :: onFinalEventListenerCallback('+event+')');
if ($type(final_callback) != "function") {
LSdebug('LSform :: onFinalEventListenerCallback('+event+') : final_callback is not a function, stop.');
return;
}
// Combine all listeners answers
var final_result = true;
this.listeners[event].each(function(fnct, listener_uuid) {
if (!this.listeners_answers[event].has(listener_uuid) || !this.listeners_answers[event][listener_uuid]) {
final_result = false;
}
}, this);
LSdebug('LSform :: onFinalEventListenerCallback('+event+'): final result = '+final_result);
// Run final callback
final_callback(final_result, event);
},
}); });
window.addEvent(window.ie ? 'load' : 'domready', function() { window.addEvent(window.ie ? 'load' : 'domready', function() {

View file

@ -4,7 +4,7 @@ var LSformElement_wysiwyg = new Class({
this.initialiseLSformElement_wysiwyg(); this.initialiseLSformElement_wysiwyg();
if ($type(varLSform)) { if ($type(varLSform)) {
varLSform.addModule("LSformElement_wysiwyg",this); varLSform.addModule("LSformElement_wysiwyg",this);
varLSform.addEvent("submit",this.onLSformSubmit.bind(this)); varLSform.addEvent("submit", this.onLSformSubmit.bind(this), 'LSformElement_wysiwyg :: tinyMCE.triggerSave()');
} }
}, },
@ -47,8 +47,10 @@ var LSformElement_wysiwyg = new Class({
this.initialiseLSformElement_wysiwyg(el); this.initialiseLSformElement_wysiwyg(el);
}, },
onLSformSubmit: function(form) { onLSformSubmit: function(form, on_confirm, on_cancel) {
tinyMCE.triggerSave(); tinyMCE.triggerSave();
if ($type(on_confirm) == 'function')
on_confirm();
} }
}); });
window.addEvent(window.ie ? 'load' : 'domready', function() { window.addEvent(window.ie ? 'load' : 'domready', function() {