mercredi 16 mai 2012

Override javascript of joomla toolbar

At times, you might need to override the javascript code that joomla runs when you hit the submit button or any other button in the toolbar.


I for instance had to override it to set a hidden input to a given value.



What you need to know

When you hit a button in the toolbar, Joomla runs one javascript function defined as

function submitbutton(pressbutton) { submitform(pressbutton); }

As you can see, this function has one argument which is actually the button you clicked on.For instance it can be 'apply' or 'save'...etc.From the previous definition, we can see that the function calls another function submitform(), passing it the same argument "pressbutton".

This function is defined as so:
function submitform(pressbutton){ if (pressbutton) { document.adminForm.task.value=pressbutton; } if (typeof document.adminForm.onsubmit == "function") { document.adminForm.onsubmit(); } document.adminForm.submit(); }

What this function does is that it sets the hidden input "task" to the value of the argument and then submits the form.

Overriding the javascript behavior

In order for you to override this behavior, you just have to redefine the function submitbutton() in your view file.

This is exactly what I do in the following code,


//saving some values for future reference!
var stade_procedure = document.getElementById('affair_stage').options[document.getElementById('affair_stage').selectedIndex].value;
var admin_comment = document.getElementById('admin_comment').value;





function submitbutton (button) { switch (button){ case 'save': case 'apply': //has there been a change? if(document.getElementById('admin_comment').value!=admin_comment||document.getElementById('affair_stage').options[document.getElementById('affair_stage').selectedIndex].value!=stade_procedure){ document.getElementById('sendmail').value=confirm("Tu viens de changer les informations de l'affaire. Informer le partenaire?"); } //go ahead and submmit! submitform(button); break; default: //go ahead and submmit! submitform(button); } }

In my particular case, I had to check if the values of two fields have been changed, in this case I ask the administrator if he wants to run a given plugin.
document.getElementById('sendmail').value=confirm("You just changed one of the two fields or probably both, do you wanna send email to partner?");

I now just have to use the value of "sendmail" in my controller code to see if the plugin needs to be called.


Hope this helps.

Aucun commentaire:

Enregistrer un commentaire

Messages les plus consultés