Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialize forms submitted by <button> #57

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/actions.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
41 changes: 12 additions & 29 deletions src/js/jquery/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() == "";
Expand Down Expand Up @@ -295,7 +277,6 @@ function onActionSuccess(json){
commands.forEach(processCommand);

execJsonCalls(json['on-load']);
applySubmitClickEvent();
}

function execJsonCalls (calls) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Copy link
Contributor Author

@hraban hraban Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could probably be even more generic as just a

if (event.submitter && "name" in event.submitter && "value" in event.submitter)

but catching all <input> and <button> is a decent start

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) {
Expand Down Expand Up @@ -512,11 +500,6 @@ $ = function(id){
}
};

jQuery(function(){
applySubmitClickEvent();
});


window.Event.observe = function(obj, evtType, func){
if(obj == window && evtType == 'load'){
jQuery(func);
Expand Down
Loading