From b334fe299c47f67a660a7262971ef1365321793f Mon Sep 17 00:00:00 2001 From: Steven Wanderski Date: Mon, 27 Apr 2015 09:51:21 -0500 Subject: [PATCH 1/4] Moves event delegation to namespaced object --- src/rails.js | 176 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 109 insertions(+), 67 deletions(-) diff --git a/src/rails.js b/src/rails.js index 39cf9158..2c893a0e 100644 --- a/src/rails.js +++ b/src/rails.js @@ -326,16 +326,8 @@ } }; - if (rails.fire($document, 'rails:attachBindings')) { - - $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }}); - - // This event works the same as the load event, except that it fires every - // time the page is loaded. - // - // See https://github.com/rails/jquery-ujs/issues/357 - // See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching - $(window).on("pageshow.rails", function () { + $.rails.handlers = { + pageshow: function(){ $($.rails.enableSelector).each(function () { var element = $(this); @@ -351,18 +343,65 @@ $.rails.enableElement(element); } }); - }); + }, - $document.delegate(rails.linkDisableSelector, 'ajax:complete', function() { - rails.enableElement($(this)); - }); + submit: function(e){ + var form = $(e.currentTarget), + remote = form.data('remote') !== undefined, + blankRequiredInputs, + nonBlankFileInputs; - $document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() { - rails.enableFormElement($(this)); - }); + if (!rails.allowAction(form)) return rails.stopEverything(e); - $document.delegate(rails.linkClickSelector, 'click.rails', function(e) { - var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey; + // skip other logic when required values are missing or file upload is present + if (form.attr('novalidate') == undefined) { + blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector); + if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) { + return rails.stopEverything(e); + } + } + + if (remote) { + nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector); + if (nonBlankFileInputs) { + // slight timeout so that the submit button gets properly serialized + // (make it easy for event handler to serialize form without disabled values) + setTimeout(function(){ rails.disableFormElements(form); }, 13); + var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]); + + // re-enable form elements if event bindings return false (canceling normal form submission) + if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); } + + return aborted; + } + + rails.handleRemote(form); + return false; + + } else { + // slight timeout so that the submit button gets properly serialized + setTimeout(function(){ rails.disableFormElements(form); }, 13); + } + }, + + ajaxCompleteLink: function($el){ + rails.enableElement($el); + }, + + ajaxCompleteButton: function($el){ + rails.enableFormElement($el); + }, + + ajaxCompleteForm: function(e, ctx){ + if (ctx == e.target) rails.enableFormElements($(ctx)); + }, + + ajaxSendForm: function(e, ctx){ + if (ctx == e.target) rails.disableFormElements($(ctx)); + }, + + clickLink: function(e){ + var link = $(e.currentTarget), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey; if (!rails.allowAction(link)) return rails.stopEverything(e); if (!metaClick && link.is(rails.linkDisableSelector)) rails.disableElement(link); @@ -383,10 +422,10 @@ rails.handleMethod(link); return false; } - }); + }, - $document.delegate(rails.buttonClickSelector, 'click.rails', function(e) { - var button = $(this); + clickButton: function(e){ + var button = $(e.currentTarget); if (!rails.allowAction(button)) return rails.stopEverything(e); @@ -400,73 +439,76 @@ handleRemote.fail( function() { rails.enableFormElement(button); } ); } return false; - }); + }, - $document.delegate(rails.inputChangeSelector, 'change.rails', function(e) { - var link = $(this); + clickInput: function(e){ + var button = $(e.currentTarget); + + if (!rails.allowAction(button)) return rails.stopEverything(e); + + // register the pressed submit button + var name = button.attr('name'), + data = name ? {name:name, value:button.val()} : null; + + button.closest('form').data('ujs:submit-button', data); + }, + + change: function(e){ + var link = $(e.currentTarget); if (!rails.allowAction(link)) return rails.stopEverything(e); rails.handleRemote(link); return false; - }); - - $document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) { - var form = $(this), - remote = form.data('remote') !== undefined, - blankRequiredInputs, - nonBlankFileInputs; - - if (!rails.allowAction(form)) return rails.stopEverything(e); + } + } - // skip other logic when required values are missing or file upload is present - if (form.attr('novalidate') == undefined) { - blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector); - if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) { - return rails.stopEverything(e); - } - } + if (rails.fire($document, 'rails:attachBindings')) { - if (remote) { - nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector); - if (nonBlankFileInputs) { - // slight timeout so that the submit button gets properly serialized - // (make it easy for event handler to serialize form without disabled values) - setTimeout(function(){ rails.disableFormElements(form); }, 13); - var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]); + $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }}); - // re-enable form elements if event bindings return false (canceling normal form submission) - if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); } + // This event works the same as the load event, except that it fires every + // time the page is loaded. + // + // See https://github.com/rails/jquery-ujs/issues/357 + // See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching + $(window).on("pageshow.rails", function(){ + $.rails.handlers.pageshow(); + }); - return aborted; - } + $document.delegate(rails.linkDisableSelector, 'ajax:complete', function() { + $.rails.handlers.ajaxCompleteLink($(this)); + }); - rails.handleRemote(form); - return false; + $document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() { + $.rails.handlers.ajaxCompleteButton($(this)); + }); - } else { - // slight timeout so that the submit button gets properly serialized - setTimeout(function(){ rails.disableFormElements(form); }, 13); - } + $document.delegate(rails.linkClickSelector, 'click.rails', function(e) { + return $.rails.handlers.clickLink(e); }); - $document.delegate(rails.formInputClickSelector, 'click.rails', function(event) { - var button = $(this); + $document.delegate(rails.buttonClickSelector, 'click.rails', function(e) { + return $.rails.handlers.clickButton(e); + }); - if (!rails.allowAction(button)) return rails.stopEverything(event); + $document.delegate(rails.inputChangeSelector, 'change.rails', function(e) { + return $.rails.handlers.change(e); + }); - // register the pressed submit button - var name = button.attr('name'), - data = name ? {name:name, value:button.val()} : null; + $document.delegate(rails.formSubmitSelector, 'submit.rails', function(e){ + return $.rails.handlers.submit(e); + }); - button.closest('form').data('ujs:submit-button', data); + $document.delegate(rails.formInputClickSelector, 'click.rails', function(event) { + $.rails.handlers.clickInput(event); }); $document.delegate(rails.formSubmitSelector, 'ajax:send.rails', function(event) { - if (this == event.target) rails.disableFormElements($(this)); + $.rails.handlers.ajaxSendForm(event, this); }); $document.delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) { - if (this == event.target) rails.enableFormElements($(this)); + $.rails.handlers.ajaxCompleteForm(event, this); }); $(function(){ From 500f17b6b396decda4098d224831d948f25d2a95 Mon Sep 17 00:00:00 2001 From: Steven Wanderski Date: Mon, 27 Apr 2015 12:32:56 -0500 Subject: [PATCH 2/4] Sends 'this' to callbacks and adds formatting --- src/rails.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/rails.js b/src/rails.js index 2c893a0e..e46b97b2 100644 --- a/src/rails.js +++ b/src/rails.js @@ -327,7 +327,7 @@ }; $.rails.handlers = { - pageshow: function(){ + pageshow: function() { $($.rails.enableSelector).each(function () { var element = $(this); @@ -345,7 +345,7 @@ }); }, - submit: function(e){ + submit: function(e) { var form = $(e.currentTarget), remote = form.data('remote') !== undefined, blankRequiredInputs, @@ -384,23 +384,23 @@ } }, - ajaxCompleteLink: function($el){ - rails.enableElement($el); + ajaxCompleteLink: function(el) { + rails.enableElement($(el)); }, - ajaxCompleteButton: function($el){ - rails.enableFormElement($el); + ajaxCompleteButton: function(el) { + rails.enableFormElement($(el)); }, - ajaxCompleteForm: function(e, ctx){ + ajaxCompleteForm: function(e, ctx) { if (ctx == e.target) rails.enableFormElements($(ctx)); }, - ajaxSendForm: function(e, ctx){ + ajaxSendForm: function(e, ctx) { if (ctx == e.target) rails.disableFormElements($(ctx)); }, - clickLink: function(e){ + clickLink: function(e) { var link = $(e.currentTarget), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey; if (!rails.allowAction(link)) return rails.stopEverything(e); @@ -424,7 +424,7 @@ } }, - clickButton: function(e){ + clickButton: function(e) { var button = $(e.currentTarget); if (!rails.allowAction(button)) return rails.stopEverything(e); @@ -441,7 +441,7 @@ return false; }, - clickInput: function(e){ + clickInput: function(e) { var button = $(e.currentTarget); if (!rails.allowAction(button)) return rails.stopEverything(e); @@ -453,7 +453,7 @@ button.closest('form').data('ujs:submit-button', data); }, - change: function(e){ + change: function(e) { var link = $(e.currentTarget); if (!rails.allowAction(link)) return rails.stopEverything(e); @@ -464,23 +464,23 @@ if (rails.fire($document, 'rails:attachBindings')) { - $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }}); + $.ajaxPrefilter(function(options, originalOptions, xhr) { if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }}); // This event works the same as the load event, except that it fires every // time the page is loaded. // // See https://github.com/rails/jquery-ujs/issues/357 // See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching - $(window).on("pageshow.rails", function(){ + $(window).on("pageshow.rails", function() { $.rails.handlers.pageshow(); }); $document.delegate(rails.linkDisableSelector, 'ajax:complete', function() { - $.rails.handlers.ajaxCompleteLink($(this)); + $.rails.handlers.ajaxCompleteLink(this); }); $document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() { - $.rails.handlers.ajaxCompleteButton($(this)); + $.rails.handlers.ajaxCompleteButton(this); }); $document.delegate(rails.linkClickSelector, 'click.rails', function(e) { From 52bc2e7993fe8def4557eb8145514026ef556848 Mon Sep 17 00:00:00 2001 From: Steven Wanderski Date: Mon, 27 Apr 2015 12:33:53 -0500 Subject: [PATCH 3/4] Adds function formatting --- src/rails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rails.js b/src/rails.js index e46b97b2..fbe5480f 100644 --- a/src/rails.js +++ b/src/rails.js @@ -380,7 +380,7 @@ } else { // slight timeout so that the submit button gets properly serialized - setTimeout(function(){ rails.disableFormElements(form); }, 13); + setTimeout(function() { rails.disableFormElements(form); }, 13); } }, From a1a80d905df5c6582abebd29e2e5effe9b54439b Mon Sep 17 00:00:00 2001 From: Steven Wanderski Date: Mon, 27 Apr 2015 12:35:09 -0500 Subject: [PATCH 4/4] Adds more simple formatting --- src/rails.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rails.js b/src/rails.js index fbe5480f..52ff1a2e 100644 --- a/src/rails.js +++ b/src/rails.js @@ -366,11 +366,11 @@ if (nonBlankFileInputs) { // slight timeout so that the submit button gets properly serialized // (make it easy for event handler to serialize form without disabled values) - setTimeout(function(){ rails.disableFormElements(form); }, 13); + setTimeout(function() { rails.disableFormElements(form); }, 13); var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]); // re-enable form elements if event bindings return false (canceling normal form submission) - if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); } + if (!aborted) { setTimeout(function() { rails.enableFormElements(form); }, 13); } return aborted; }