diff --git a/src/actions.lisp b/src/actions.lisp index 1b29b848..49fbaa88 100644 --- a/src/actions.lisp +++ b/src/actions.lisp @@ -46,6 +46,9 @@ (in-readtable pythonic-string-syntax) +(eval-when (:compile-toplevel :load-toplevel :execute) + (defvar *js-default-action* "return initiateAction(\"~A\"~@[, ~A~])") + (defvar *js-default-form-action* "return initiateFormAction(\"~A\", event, this)")) (defgeneric on-missing-action (app action-name) (:documentation "Must be overridden by application to prevent default @@ -227,13 +230,12 @@ situation (e.g. redirect, signal an error, etc.).")) (cond (args (let ((options (dict "args" args))) - (format nil "initiateAction(\"~A\", ~A); return false;" + (format nil *js-default-action* action-code (yason:with-output-to-string* () (yason:encode options))))) (t - (format nil "initiateAction(\"~A\"); return false;" - action-code))))) + (format nil *js-default-action* action-code))))) (defun make-js-form-action (action) @@ -244,8 +246,7 @@ situation (e.g. redirect, signal an error, etc.).")) On form submit given action will be executed and all input values will be passed as arguments." (let* ((action-code (make-action action))) - (format nil "initiateFormAction(\"~A\", $(this)); return false;" - action-code))) + (format nil *js-default-form-action* action-code))) (defun get-session-action (action-name) diff --git a/src/js/jquery/jquery.js b/src/js/jquery/jquery.js index 7255a83f..5645371c 100644 --- a/src/js/jquery/jquery.js +++ b/src/js/jquery/jquery.js @@ -40,15 +40,6 @@ jQuery.fn.focusFirstElement = function(){ } }; -jQuery.fn.serializeObjectWithSubmit = function(){ - var ret = this.serializeObject(); - var submitElement = jQuery(this).find('input[type=submit][clicked=true]'); - ret[submitElement.attr('name')] = submitElement.val(); - submitElement.attr('clicked', null); - - return ret; -}; - // Utilities function updateElementBody(element, newBody) { element.update(newBody); @@ -59,15 +50,6 @@ function updateElement(element, newElement) { element.replaceWith($newElement); } -function applySubmitClickEvent() { - jQuery("form input[type=submit]") - .unbind('click.reblocks-submit-event') - .bind('click.reblocks-submit-event', function() { - $("input[type=submit]", $(this).parents("form")).removeAttr("clicked"); - $(this).attr("clicked", "true"); - }); -} - function selectionEmpty() { if(document.getSelection) { return document.getSelection() == ""; @@ -295,7 +277,6 @@ function onActionSuccess(json){ commands.forEach(processCommand); execJsonCalls(json['on-load']); - applySubmitClickEvent(); } function execJsonCalls (calls) { @@ -344,13 +325,13 @@ function initiateAction(actionCode, options) { var url = options.url || getActionUrl(actionCode); var on_success = options.on_success; var on_failure = options.on_failure || onActionFailure; - + args.action = actionCode; // This logging is for debug only // log('Fireing action', actionCode); // log('with options', options); - + var ajax_options = { type: method, success: function(first, second, third) { @@ -366,17 +347,24 @@ function initiateAction(actionCode, options) { ajax_options.data = JSON.stringify(args) } jQuery.ajax(url, ajax_options); + + return false; } -function initiateFormAction(actionCode, form, options) { - // Hidden "action" field should not be serialized on AJAX +function initiateFormAction(actionCode, event, eventThis, options) { + const form = $(eventThis); var options = options || {}; - var action_arguments = form.serializeObjectWithSubmit(); + var action_arguments = form.serializeObject(); + if (["BUTTON", "INPUT"].includes(event.submitter && event.submitter.tagName)) { + action_arguments[event.submitter.name] = event.submitter.value; + } delete(action_arguments['action']); options['args'] = Object.assign({}, options.args || {}, action_arguments); options['method'] = options['method'] || form.attr('method'); initiateAction(actionCode, options); + + return false; } function disableIrrelevantButtons(currentButton) { @@ -512,11 +500,6 @@ $ = function(id){ } }; -jQuery(function(){ - applySubmitClickEvent(); -}); - - window.Event.observe = function(obj, evtType, func){ if(obj == window && evtType == 'load'){ jQuery(func);