streamline behaviour of component (trapped) forms with multiple submit buttons

this is because jquery can't know what button was pressed when the 
submit event is triggered. We circumvent it adding an hidden field before
triggering the submission

BTW: don't know why the commit seems huge. I just changed a bit the trap_form
function to handle the corner case and streamlined what disableElement, 
enableElement, disableFormElements, enableFormElements do, 
along with formInputClickSelector
This commit is contained in:
niphlod
2014-11-08 23:04:58 +01:00
parent 894babaed3
commit a6b50dcdcd
3 changed files with 1992 additions and 1980 deletions
+15 -11
View File
@@ -250,7 +250,6 @@
}); });
}, },
trap_form: function(action, target) { trap_form: function(action, target) {
/* traps any LOADed form */ /* traps any LOADed form */
$('#' + target + ' form').each(function(i) { $('#' + target + ' form').each(function(i) {
@@ -258,25 +257,29 @@
if(form.hasClass('no_trap')) { if(form.hasClass('no_trap')) {
return; return;
} }
form.attr('data-w2p_target', target); form.attr('data-w2p_target', target);
var url = form.attr('action'); var url = form.attr('action');
if((url === "") || (url === "#") || (typeof url === 'undefined')) { if((url === "") || (url === "#") || (typeof url === 'undefined')) {
/* form has no action. Use component url. */ /* form has no action. Use component url. */
url = action; url = action;
} }
form.submit(function(e) { form.submit(function(e) {
web2py.disableElement(form.find(web2py.formInputClickSelector)); web2py.disableElement(form.find(web2py.formInputClickSelector));
web2py.hide_flash(); web2py.hide_flash();
web2py.ajax_page('post', url, form.serialize(), target, form); web2py.ajax_page('post', url, form.serialize(), target, form);
e.preventDefault(); e.preventDefault();
}); });
form.on('click', web2py.formInputClickSelector, function(e) {
e.preventDefault();
var input_name = $(this).attr('name');
if(input_name != undefined) {
$('<input type="hidden" />').attr('name', input_name)
.attr('value', $(this).val()).appendTo(form)
}
form.trigger('submit');
});
}); });
}, },
ajax_page: function(method, action, data, target, element) { ajax_page: function(method, action, data, target, element) {
/* element is a new parameter, but should be put be put in front */ /* element is a new parameter, but should be put be put in front */
if(element == undefined) element = $(document); if(element == undefined) element = $(document);
@@ -461,7 +464,7 @@
}, },
/* new from here */ /* new from here */
/* Form input elements bound by web2py.js */ /* Form input elements bound by web2py.js */
formInputClickSelector: 'input[type=submit]:not([name]), input[type=image]:not([name]), button[type=submit]:not([name]), button:not([type]):not([name])', formInputClickSelector: 'input[type=submit], input[type=image], button[type=submit], button:not([type])',
/* Form input elements disabled during form submission */ /* Form input elements disabled during form submission */
disableSelector: 'input, button, textarea, select', disableSelector: 'input, button, textarea, select',
/* Form input elements re-enabled after form submission */ /* Form input elements re-enabled after form submission */
@@ -487,7 +490,8 @@
* and prevent clicking on it */ * and prevent clicking on it */
disableElement: function(el) { disableElement: function(el) {
el.addClass('disabled'); el.addClass('disabled');
var method = el.prop('type') == 'submit' ? 'val' : 'html'; var method = el.is('button') ? 'html' : 'val';
//method = el.attr('name') ? 'html' : 'val';
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working..."; var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
/*store enabled state if not already disabled */ /*store enabled state if not already disabled */
if(el.data('w2p:enable-with') === undefined) { if(el.data('w2p:enable-with') === undefined) {
@@ -511,7 +515,7 @@
/* restore element to its original state which was disabled by 'disableElement' above*/ /* restore element to its original state which was disabled by 'disableElement' above*/
enableElement: function(el) { enableElement: function(el) {
var method = el.prop('type') == 'submit' ? 'val' : 'html'; var method = el.is('button') ? 'val' : 'html';
if(el.data('w2p:enable-with') !== undefined) { if(el.data('w2p:enable-with') !== undefined) {
/* set to old enabled state */ /* set to old enabled state */
el[method](el.data('w2p:enable-with')); el[method](el.data('w2p:enable-with'));
@@ -630,7 +634,7 @@
}); });
}, },
/* Disables form elements: /* Disables form elements:
- Caches element value in 'ujs:enable-with' data store - Caches element value in 'w2p:enable-with' data store
- Replaces element text with value of 'data-disable-with' attribute - Replaces element text with value of 'data-disable-with' attribute
- Sets disabled property to true - Sets disabled property to true
*/ */
@@ -651,7 +655,7 @@
}, },
/* Re-enables disabled form elements: /* Re-enables disabled form elements:
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`) - Replaces element text with cached value from 'w2p:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false - Sets disabled property to false
*/ */
enableFormElements: function(form) { enableFormElements: function(form) {
+15 -11
View File
@@ -250,7 +250,6 @@
}); });
}, },
trap_form: function(action, target) { trap_form: function(action, target) {
/* traps any LOADed form */ /* traps any LOADed form */
$('#' + target + ' form').each(function(i) { $('#' + target + ' form').each(function(i) {
@@ -258,25 +257,29 @@
if(form.hasClass('no_trap')) { if(form.hasClass('no_trap')) {
return; return;
} }
form.attr('data-w2p_target', target); form.attr('data-w2p_target', target);
var url = form.attr('action'); var url = form.attr('action');
if((url === "") || (url === "#") || (typeof url === 'undefined')) { if((url === "") || (url === "#") || (typeof url === 'undefined')) {
/* form has no action. Use component url. */ /* form has no action. Use component url. */
url = action; url = action;
} }
form.submit(function(e) { form.submit(function(e) {
web2py.disableElement(form.find(web2py.formInputClickSelector)); web2py.disableElement(form.find(web2py.formInputClickSelector));
web2py.hide_flash(); web2py.hide_flash();
web2py.ajax_page('post', url, form.serialize(), target, form); web2py.ajax_page('post', url, form.serialize(), target, form);
e.preventDefault(); e.preventDefault();
}); });
form.on('click', web2py.formInputClickSelector, function(e) {
e.preventDefault();
var input_name = $(this).attr('name');
if(input_name != undefined) {
$('<input type="hidden" />').attr('name', input_name)
.attr('value', $(this).val()).appendTo(form)
}
form.trigger('submit');
});
}); });
}, },
ajax_page: function(method, action, data, target, element) { ajax_page: function(method, action, data, target, element) {
/* element is a new parameter, but should be put be put in front */ /* element is a new parameter, but should be put be put in front */
if(element == undefined) element = $(document); if(element == undefined) element = $(document);
@@ -461,7 +464,7 @@
}, },
/* new from here */ /* new from here */
/* Form input elements bound by web2py.js */ /* Form input elements bound by web2py.js */
formInputClickSelector: 'input[type=submit]:not([name]), input[type=image]:not([name]), button[type=submit]:not([name]), button:not([type]):not([name])', formInputClickSelector: 'input[type=submit], input[type=image], button[type=submit], button:not([type])',
/* Form input elements disabled during form submission */ /* Form input elements disabled during form submission */
disableSelector: 'input, button, textarea, select', disableSelector: 'input, button, textarea, select',
/* Form input elements re-enabled after form submission */ /* Form input elements re-enabled after form submission */
@@ -487,7 +490,8 @@
* and prevent clicking on it */ * and prevent clicking on it */
disableElement: function(el) { disableElement: function(el) {
el.addClass('disabled'); el.addClass('disabled');
var method = el.prop('type') == 'submit' ? 'val' : 'html'; var method = el.is('button') ? 'html' : 'val';
//method = el.attr('name') ? 'html' : 'val';
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working..."; var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
/*store enabled state if not already disabled */ /*store enabled state if not already disabled */
if(el.data('w2p:enable-with') === undefined) { if(el.data('w2p:enable-with') === undefined) {
@@ -511,7 +515,7 @@
/* restore element to its original state which was disabled by 'disableElement' above*/ /* restore element to its original state which was disabled by 'disableElement' above*/
enableElement: function(el) { enableElement: function(el) {
var method = el.prop('type') == 'submit' ? 'val' : 'html'; var method = el.is('button') ? 'val' : 'html';
if(el.data('w2p:enable-with') !== undefined) { if(el.data('w2p:enable-with') !== undefined) {
/* set to old enabled state */ /* set to old enabled state */
el[method](el.data('w2p:enable-with')); el[method](el.data('w2p:enable-with'));
@@ -630,7 +634,7 @@
}); });
}, },
/* Disables form elements: /* Disables form elements:
- Caches element value in 'ujs:enable-with' data store - Caches element value in 'w2p:enable-with' data store
- Replaces element text with value of 'data-disable-with' attribute - Replaces element text with value of 'data-disable-with' attribute
- Sets disabled property to true - Sets disabled property to true
*/ */
@@ -651,7 +655,7 @@
}, },
/* Re-enables disabled form elements: /* Re-enables disabled form elements:
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`) - Replaces element text with cached value from 'w2p:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false - Sets disabled property to false
*/ */
enableFormElements: function(form) { enableFormElements: function(form) {
+15 -11
View File
@@ -250,7 +250,6 @@
}); });
}, },
trap_form: function(action, target) { trap_form: function(action, target) {
/* traps any LOADed form */ /* traps any LOADed form */
$('#' + target + ' form').each(function(i) { $('#' + target + ' form').each(function(i) {
@@ -258,25 +257,29 @@
if(form.hasClass('no_trap')) { if(form.hasClass('no_trap')) {
return; return;
} }
form.attr('data-w2p_target', target); form.attr('data-w2p_target', target);
var url = form.attr('action'); var url = form.attr('action');
if((url === "") || (url === "#") || (typeof url === 'undefined')) { if((url === "") || (url === "#") || (typeof url === 'undefined')) {
/* form has no action. Use component url. */ /* form has no action. Use component url. */
url = action; url = action;
} }
form.submit(function(e) { form.submit(function(e) {
web2py.disableElement(form.find(web2py.formInputClickSelector)); web2py.disableElement(form.find(web2py.formInputClickSelector));
web2py.hide_flash(); web2py.hide_flash();
web2py.ajax_page('post', url, form.serialize(), target, form); web2py.ajax_page('post', url, form.serialize(), target, form);
e.preventDefault(); e.preventDefault();
}); });
form.on('click', web2py.formInputClickSelector, function(e) {
e.preventDefault();
var input_name = $(this).attr('name');
if(input_name != undefined) {
$('<input type="hidden" />').attr('name', input_name)
.attr('value', $(this).val()).appendTo(form)
}
form.trigger('submit');
});
}); });
}, },
ajax_page: function(method, action, data, target, element) { ajax_page: function(method, action, data, target, element) {
/* element is a new parameter, but should be put be put in front */ /* element is a new parameter, but should be put be put in front */
if(element == undefined) element = $(document); if(element == undefined) element = $(document);
@@ -461,7 +464,7 @@
}, },
/* new from here */ /* new from here */
/* Form input elements bound by web2py.js */ /* Form input elements bound by web2py.js */
formInputClickSelector: 'input[type=submit]:not([name]), input[type=image]:not([name]), button[type=submit]:not([name]), button:not([type]):not([name])', formInputClickSelector: 'input[type=submit], input[type=image], button[type=submit], button:not([type])',
/* Form input elements disabled during form submission */ /* Form input elements disabled during form submission */
disableSelector: 'input, button, textarea, select', disableSelector: 'input, button, textarea, select',
/* Form input elements re-enabled after form submission */ /* Form input elements re-enabled after form submission */
@@ -487,7 +490,8 @@
* and prevent clicking on it */ * and prevent clicking on it */
disableElement: function(el) { disableElement: function(el) {
el.addClass('disabled'); el.addClass('disabled');
var method = el.prop('type') == 'submit' ? 'val' : 'html'; var method = el.is('button') ? 'html' : 'val';
//method = el.attr('name') ? 'html' : 'val';
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working..."; var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
/*store enabled state if not already disabled */ /*store enabled state if not already disabled */
if(el.data('w2p:enable-with') === undefined) { if(el.data('w2p:enable-with') === undefined) {
@@ -511,7 +515,7 @@
/* restore element to its original state which was disabled by 'disableElement' above*/ /* restore element to its original state which was disabled by 'disableElement' above*/
enableElement: function(el) { enableElement: function(el) {
var method = el.prop('type') == 'submit' ? 'val' : 'html'; var method = el.is('button') ? 'val' : 'html';
if(el.data('w2p:enable-with') !== undefined) { if(el.data('w2p:enable-with') !== undefined) {
/* set to old enabled state */ /* set to old enabled state */
el[method](el.data('w2p:enable-with')); el[method](el.data('w2p:enable-with'));
@@ -630,7 +634,7 @@
}); });
}, },
/* Disables form elements: /* Disables form elements:
- Caches element value in 'ujs:enable-with' data store - Caches element value in 'w2p:enable-with' data store
- Replaces element text with value of 'data-disable-with' attribute - Replaces element text with value of 'data-disable-with' attribute
- Sets disabled property to true - Sets disabled property to true
*/ */
@@ -651,7 +655,7 @@
}, },
/* Re-enables disabled form elements: /* Re-enables disabled form elements:
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`) - Replaces element text with cached value from 'w2p:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false - Sets disabled property to false
*/ */
enableFormElements: function(form) { enableFormElements: function(form) {