From ca575e184e93b3efe1a858cf598f8a37f0a760cc Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Thu, 10 Feb 2011 09:10:53 +0800
Subject: [PATCH 001/282] Refactored and added check to abort ajax form
submission if non-blank file uploads are present. Added would-be test since
this cannot be autotested. Added triggered callbacks for when ajax form
submission is aborted, either due to missing required inputs or non-blank
file upload fields, to make it easy for custom handler binding.
ajax:aborted:file trigger allows overriding of ajax file upload
functionality, but still submits the form normal-style by default.
---
src/rails.js | 51 +++++++++++++++++++----
test/public/test/call-remote-callbacks.js | 23 ++++++++++
2 files changed, 67 insertions(+), 7 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 5f14c5ae..3a51d441 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -3,6 +3,27 @@
*
* Requires jQuery 1.4.3 or later.
* https://github.com/rails/jquery-ujs
+
+ * Uploading file using rails.js
+ *
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, when this rails.js adapter submits remote forms,
+ * any file input fields are excluded from the request parameters sent to the server. You may cancel the whole form submission by
+ * binding a handler function that returns false to the `ajax:aborted:file` hook.
+ *
+ * Ex:
+ * $('form').live('ajax:aborted:file', function(){
+ * alert("File detected. Form submission canceled.");
+ * return false;
+ * });
+ *
+ * The `ajax:aborted:file` event is fired when a form is submitted and both conditions are met:
+ * a) file-type input field is detected, and
+ * b) the value of the input:file field is not blank.
+ *
+ * Third party tools can use this hook to detect when an AJAX file upload is attempted, and then use techniques like the iframe method to upload the file instead.
+ *
+ * Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected, providing the `ajax:aborted:required` hook.
+ * Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
*/
(function($) {
@@ -104,12 +125,22 @@
return !message || (fire(element, 'confirm') && confirm(message));
}
- function requiredValuesMissing(form) {
- var missing = false;
- form.find('input[name][required]').each(function() {
- if (!$(this).val()) missing = true;
+ function blankInputs(form, specifiedSelector) {
+ var blankExists = false,
+ selector = specifiedSelector || 'input';
+ form.find(selector).each(function() {
+ if (!$(this).val()) blankExists = true;
});
- return missing;
+ return blankExists;
+ }
+
+ function nonBlankInputs(form, specifiedSelector) {
+ var nonBlankExists = false,
+ selector = specifiedSelector || 'input';
+ form.find(selector).each(function() {
+ if ($(this).val()) nonBlankExists = true;
+ });
+ return nonBlankExists;
}
$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
@@ -129,8 +160,14 @@
var form = $(this), remote = form.data('remote') != undefined;
if (!allowAction(form)) return false;
- // skip other logic when required values are missing
- if (requiredValuesMissing(form)) return !remote;
+ // skip other logic when required values are missing or file upload is present
+ if (blankInputs(form, 'input[name][required]')) {
+ form.trigger('ajax:aborted:required');
+ return !remote;
+ }
+ if (nonBlankInputs(form, 'input:file')) {
+ return fire(form, 'ajax:aborted:file');
+ }
if (remote) {
handleRemote(form);
diff --git a/test/public/test/call-remote-callbacks.js b/test/public/test/call-remote-callbacks.js
index 762acb9e..2517e12c 100644
--- a/test/public/test/call-remote-callbacks.js
+++ b/test/public/test/call-remote-callbacks.js
@@ -81,6 +81,29 @@ asyncTest('blank required form input field should abort request', 1, function()
}, 13);
});
+function skipIt() {
+ // This test cannot work due to the security feature in browsers which makes the value
+ // attribute of file input fields readonly, so it cannot be set with default value.
+ // This is what the test would look like though if browsers let us automate this test.
+ asyncTest('non-blank file form input field should abort request', 1, function() {
+ var form = $('form[data-remote]')
+ .append($(''))
+ .bind('ajax:beforeSend', function() {
+ ok(true, 'ajax:beforeSend should not run');
+ })
+ .bind('iframe:loading', function() {
+ ok(true, 'form should not get submitted');
+ })
+ .trigger('submit');
+
+ setTimeout(function() {
+ form.find('input[type="file"]').val('');
+ form.unbind('ajax:beforeSend');
+ submit();
+ }, 13);
+ });
+}
+
asyncTest('"ajax:beforeSend" can be observed and stopped with event delegation', 1, function() {
$('form[data-remote]').live('ajax:beforeSend', function() {
ok(true, 'ajax:beforeSend observed with event delegation');
From af290503d0c5e67bbb45f796b70ca8bbf8da7d4f Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Thu, 14 Apr 2011 12:33:00 -0400
Subject: [PATCH 002/282] removes end of line white space
---
src/rails.js | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 3a51d441..c3f2adc3 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -3,11 +3,11 @@
*
* Requires jQuery 1.4.3 or later.
* https://github.com/rails/jquery-ujs
-
+
* Uploading file using rails.js
- *
+ *
* By default, browsers do not allow files to be uploaded via AJAX. As a result, when this rails.js adapter submits remote forms,
- * any file input fields are excluded from the request parameters sent to the server. You may cancel the whole form submission by
+ * any file input fields are excluded from the request parameters sent to the server. You may cancel the whole form submission by
* binding a handler function that returns false to the `ajax:aborted:file` hook.
*
* Ex:
@@ -18,8 +18,8 @@
*
* The `ajax:aborted:file` event is fired when a form is submitted and both conditions are met:
* a) file-type input field is detected, and
- * b) the value of the input:file field is not blank.
- *
+ * b) the value of the input:file field is not blank.
+ *
* Third party tools can use this hook to detect when an AJAX file upload is attempted, and then use techniques like the iframe method to upload the file instead.
*
* Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected, providing the `ajax:aborted:required` hook.
@@ -133,7 +133,7 @@
});
return blankExists;
}
-
+
function nonBlankInputs(form, specifiedSelector) {
var nonBlankExists = false,
selector = specifiedSelector || 'input';
From b10d9d5b4c1302b3f188658c29e81e22e56f5a38 Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Thu, 14 Apr 2011 12:35:58 -0400
Subject: [PATCH 003/282] use curly braces for even single line if statements.
Even jQuery codebase now prefers this style of coding.
---
src/rails.js | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index c3f2adc3..a39d7ace 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -32,8 +32,11 @@
var token = $('meta[name="csrf-token"]').attr('content');
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
}
- if ('ajaxPrefilter' in $) $.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr) });
- else $(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr) });
+ if ('ajaxPrefilter' in $) {
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr) });
+ } else {
+ $(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr) });
+ }
// Triggers an event on an element and returns the event result
function fire(obj, name, data) {
From 9b26d6748cb0ad4dea5d8d1597aa13bab6f0fca0 Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Thu, 14 Apr 2011 12:37:10 -0400
Subject: [PATCH 004/282] remove lint warnings by having semicolons
---
src/rails.js | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index a39d7ace..9241821f 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -33,9 +33,9 @@
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
}
if ('ajaxPrefilter' in $) {
- $.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr) });
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr); });
} else {
- $(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr) });
+ $(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr); });
}
// Triggers an event on an element and returns the event result
@@ -177,7 +177,7 @@
return false;
} else {
// slight timeout so that the submit button gets properly serialized
- setTimeout(function(){ disableFormElements(form) }, 13);
+ setTimeout(function(){ disableFormElements(form); }, 13);
}
});
From 98fa8387635e42c7767605578b16de656ab87c06 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Fri, 15 Apr 2011 02:19:37 +0800
Subject: [PATCH 005/282] Added test for 'ajax:aborted:required' triggered
event.
---
test/public/test/call-remote-callbacks.js | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/test/public/test/call-remote-callbacks.js b/test/public/test/call-remote-callbacks.js
index 2517e12c..3453e11f 100644
--- a/test/public/test/call-remote-callbacks.js
+++ b/test/public/test/call-remote-callbacks.js
@@ -63,7 +63,7 @@ asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function
});
});
-asyncTest('blank required form input field should abort request', 1, function() {
+asyncTest('blank required form input field should abort request and trigger "ajax:aborted:required" event', 2, function() {
var form = $('form[data-remote]')
.append($(''))
.bind('ajax:beforeSend', function() {
@@ -72,6 +72,9 @@ asyncTest('blank required form input field should abort request', 1, function()
.bind('iframe:loading', function() {
ok(false, 'form should not get submitted');
})
+ .bind('ajax:aborted:required', function(){
+ ok(true, 'ajax:aborted:required should run');
+ })
.trigger('submit');
setTimeout(function() {
From b95852934b2ea6e908a8e4519766fe31cafda395 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Fri, 15 Apr 2011 02:37:10 +0800
Subject: [PATCH 006/282] Made 'ajax:aborted:required' a cancellable event to
make form submit anyway, with test.
---
src/rails.js | 3 +--
test/public/test/call-remote-callbacks.js | 16 ++++++++++++++++
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 9241821f..a5be7d31 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -164,8 +164,7 @@
if (!allowAction(form)) return false;
// skip other logic when required values are missing or file upload is present
- if (blankInputs(form, 'input[name][required]')) {
- form.trigger('ajax:aborted:required');
+ if (blankInputs(form, 'input[name][required]') && fire(form, 'ajax:aborted:required')) {
return !remote;
}
if (nonBlankInputs(form, 'input:file')) {
diff --git a/test/public/test/call-remote-callbacks.js b/test/public/test/call-remote-callbacks.js
index 3453e11f..d5d1a21e 100644
--- a/test/public/test/call-remote-callbacks.js
+++ b/test/public/test/call-remote-callbacks.js
@@ -84,6 +84,22 @@ asyncTest('blank required form input field should abort request and trigger "aja
}, 13);
});
+asyncTest('form should be submitted with blank required fields if handler is bound to "ajax:aborted:required" event that returns false', 1, function(){
+ var form = $('form[data-remote]')
+ .append($(''))
+ .bind('ajax:beforeSend', function() {
+ ok(true, 'ajax:beforeSend should run');
+ })
+ .bind('ajax:aborted:required', function() {
+ return false;
+ })
+ .trigger('submit');
+
+ setTimeout(function() {
+ start();
+ }, 13);
+});
+
function skipIt() {
// This test cannot work due to the security feature in browsers which makes the value
// attribute of file input fields readonly, so it cannot be set with default value.
From a82502a2b7d905f5f2b43d3d28f9893747182c55 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Fri, 15 Apr 2011 03:05:15 +0800
Subject: [PATCH 007/282] Added additional description and example for
canceling 'ajax:aborted:required' event at top of rails.js.
---
src/rails.js | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/rails.js b/src/rails.js
index a5be7d31..6ae9eae7 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -24,6 +24,14 @@
*
* Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected, providing the `ajax:aborted:required` hook.
* Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
+ *
+ * The default behavior for aborting the remote form submission when required inputs are missing may be canceled (thereby submitting the form via AJAX anyway)
+ * by binding a handler function that returns false to the `ajax:aborted:required` hook.
+ *
+ * Ex:
+ * $('form').live('ajax:aborted:required', function(){
+ * return ! confirm("Would you like to submit the form with missing info?");
+ * });
*/
(function($) {
From f05e6dc8c72b213977b6785a65fd37d6818fb9e3 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Thu, 14 Apr 2011 13:29:50 -0400
Subject: [PATCH 008/282] Updated description to more accurately describe
default functionality for aborted ajax events.
---
src/rails.js | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 6ae9eae7..0557873e 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -6,13 +6,14 @@
* Uploading file using rails.js
*
- * By default, browsers do not allow files to be uploaded via AJAX. As a result, when this rails.js adapter submits remote forms,
- * any file input fields are excluded from the request parameters sent to the server. You may cancel the whole form submission by
- * binding a handler function that returns false to the `ajax:aborted:file` hook.
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
+ * in the remote form, this adapter aborts the AJAX submission and allows the browser to submit the form normally.
+ * You may intercept the form submission and implement your own work-around for submitting file uploads without refreshing the page,
+ * by binding a handler function that returns false to the `ajax:aborted:file` hook.
*
* Ex:
* $('form').live('ajax:aborted:file', function(){
- * alert("File detected. Form submission canceled.");
+ * // Implement own remote file-transfer handler here.
* return false;
* });
*
@@ -20,7 +21,7 @@
* a) file-type input field is detected, and
* b) the value of the input:file field is not blank.
*
- * Third party tools can use this hook to detect when an AJAX file upload is attempted, and then use techniques like the iframe method to upload the file instead.
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use techniques like the iframe method to upload the file instead.
*
* Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected, providing the `ajax:aborted:required` hook.
* Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
From f8619200dbdc8240ebd173b1b30ac2ae85a29bd1 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Thu, 14 Apr 2011 14:43:10 -0400
Subject: [PATCH 009/282] Updated and added pseudo-tests for
'ajax:aborted:file' event behavior to be more descriptive and accurate for
intended behavior.
---
test/public/test/call-remote-callbacks.js | 30 ++++++++++++++++++++---
1 file changed, 27 insertions(+), 3 deletions(-)
diff --git a/test/public/test/call-remote-callbacks.js b/test/public/test/call-remote-callbacks.js
index d5d1a21e..204339e0 100644
--- a/test/public/test/call-remote-callbacks.js
+++ b/test/public/test/call-remote-callbacks.js
@@ -104,15 +104,39 @@ function skipIt() {
// This test cannot work due to the security feature in browsers which makes the value
// attribute of file input fields readonly, so it cannot be set with default value.
// This is what the test would look like though if browsers let us automate this test.
- asyncTest('non-blank file form input field should abort request', 1, function() {
+ asyncTest('non-blank file form input field should abort remote request, but submit normally', 3, function() {
var form = $('form[data-remote]')
.append($(''))
.bind('ajax:beforeSend', function() {
- ok(true, 'ajax:beforeSend should not run');
+ ok(false, 'ajax:beforeSend should not run');
})
.bind('iframe:loading', function() {
- ok(true, 'form should not get submitted');
+ ok(true, 'form should get submitted');
})
+ .bind('ajax:aborted:file', function() {
+ ok(true, 'ajax:aborted:file event should run');
+ })
+ .trigger('submit');
+
+ setTimeout(function() {
+ form.find('input[type="file"]').val('');
+ form.unbind('ajax:beforeSend');
+ submit();
+ }, 13);
+ });
+
+ syncTest('blank file input field should abort request entirely if handler bound to "ajax:aborted:file" event that returns false', 1, function() {
+ var form = $('form[data-remote]')
+ .append($(''))
+ .bind('ajax:beforeSend', function() {
+ ok(false, 'ajax:beforeSend should not run');
+ })
+ .bind('iframe:loading', function() {
+ ok(false, 'form should not get submitted');
+ })
+ .bind('ajax:aborted:file', function() {
+ return false;
+ })
.trigger('submit');
setTimeout(function() {
From 253021446d5c86e8687b9338a398472afbb6b867 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Wed, 9 Mar 2011 19:17:38 -0500
Subject: [PATCH 010/282] Fixed bug in IE, caused by jQuery's implementation of
submit propagation. Closes #114.
---
src/rails.js | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 0557873e..3b745f49 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -155,9 +155,14 @@
return nonBlankExists;
}
+ function stopEverything(e) {
+ e.stopImmediatePropagation();
+ return false;
+ }
+
$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
var link = $(this);
- if (!allowAction(link)) return false;
+ if (!allowAction(link)) return stopEverything(e);
if (link.data('remote') != undefined) {
handleRemote(link);
@@ -170,7 +175,7 @@
$('form').live('submit.rails', function(e) {
var form = $(this), remote = form.data('remote') != undefined;
- if (!allowAction(form)) return false;
+ if (!allowAction(form)) return stopEverything(e);
// skip other logic when required values are missing or file upload is present
if (blankInputs(form, 'input[name][required]') && fire(form, 'ajax:aborted:required')) {
@@ -191,7 +196,7 @@
$('form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])').live('click.rails', function() {
var button = $(this);
- if (!allowAction(button)) return false;
+ if (!allowAction(button)) return 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);
From d2abd6f9df4e4a426c17c218b7d5e05004c768d0 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Fri, 15 Apr 2011 14:51:49 -0400
Subject: [PATCH 011/282] Created test cases and solutions for correcting
submit handler call order for browsers that don't support event bubbling.
Signed-off-by: Neeraj Singh
---
src/rails.js | 14 +++++++++
test/public/test/data-remote.js | 51 +++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+)
diff --git a/src/rails.js b/src/rails.js
index 3b745f49..98b1e507 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -160,6 +160,16 @@
return false;
}
+ function callFormSubmitBindings(form) {
+ var events = form.data('events'), continuePropagation = true;
+ if (events != undefined && events['submit'] != undefined) {
+ $.each(events['submit'], function(i, obj){
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
+ })
+ }
+ return continuePropagation;
+ }
+
$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
var link = $(this);
if (!allowAction(link)) return stopEverything(e);
@@ -185,6 +195,10 @@
return fire(form, 'ajax:aborted:file');
}
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
+ if (!$.support.submitBubbles && callFormSubmitBindings(form) == false) return stopEverything(e)
+
if (remote) {
handleRemote(form);
return false;
diff --git a/test/public/test/data-remote.js b/test/public/test/data-remote.js
index ac78e0ef..694143b9 100644
--- a/test/public/test/data-remote.js
+++ b/test/public/test/data-remote.js
@@ -37,3 +37,54 @@ asyncTest('submitting form with data-remote attribute', 4, function() {
.bind('ajax:complete', function() { start() })
.trigger('submit');
});
+
+asyncTest('form\'s submit bindings in browsers that don\'t support submit bubbling', 4, function() {
+ var form = $('form[data-remote]'), directBindingCalled = false;
+
+ ok(!directBindingCalled, 'nothing is called');
+
+ form
+ .append($(''))
+ .bind('submit', function(){
+ ok(true, 'binding handler is called');
+ directBindingCalled = true;
+ })
+ .bind('ajax:beforeSend', function(){
+ ok(true, 'form being submitted via ajax');
+ ok(directBindingCalled, 'binding handler already called');
+ })
+ .bind('ajax:complete', function(){
+ start();
+ });
+
+ if(!$.support.submitBubbles) {
+ // Must indrectly submit form via click to trigger jQuery's manual submit bubbling in IE
+ form.find('input[type=submit]')
+ .trigger('click');
+ } else {
+ form.trigger('submit');
+ }
+});
+
+asyncTest('returning false in form\'s submit bindings in non-submit-bubbling browsers', 1, function(){
+ var form = $('form[data-remote]');
+
+ form
+ .append($(''))
+ .bind('submit', function(){
+ ok(true, 'binding handler is called');
+ return false;
+ })
+ .bind('ajax:beforeSend', function(){
+ ok(false, 'form should not be submitted');
+ });
+
+ if (!$.support.submitBubbles) {
+ // Must indrectly submit form via click to trigger jQuery's manual submit bubbling in IE
+ form.find('input[type=submit]').trigger('click');
+ } else {
+ form.trigger('submit');
+ }
+
+ setTimeout(function(){ start(); }, 13);
+});
From d59144177d86790891fdb99b0e3437312e04fda2 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Mon, 18 Apr 2011 05:29:54 +0800
Subject: [PATCH 012/282] Gave external api to internal rails.js functions to
make them available to be called and modified from outside of rails.js via
$.rails object. Closes #98.
---
src/rails.js | 90 ++++++++++++++++++++++++++++------------------------
1 file changed, 48 insertions(+), 42 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 98b1e507..05b5c89d 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -36,30 +36,29 @@
*/
(function($) {
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
+ var rails;
+
+ $.rails = rails = {
// Make sure that every Ajax request sends the CSRF token
- function CSRFProtection(xhr) {
+ CSRFProtection: function(xhr) {
var token = $('meta[name="csrf-token"]').attr('content');
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
- }
- if ('ajaxPrefilter' in $) {
- $.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr); });
- } else {
- $(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr); });
- }
+ },
// Triggers an event on an element and returns the event result
- function fire(obj, name, data) {
+ fire: function(obj, name, data) {
var event = $.Event(name);
obj.trigger(event, data);
return event.result !== false;
- }
+ },
// Submits "remote" forms and links with ajax
- function handleRemote(element) {
+ handleRemote: function(element) {
var method, url, data,
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
- if (fire(element, 'ajax:before')) {
+ if (rails.fire(element, 'ajax:before')) {
if (element.is('form')) {
method = element.attr('method');
url = element.attr('action');
@@ -82,7 +81,7 @@
if (settings.dataType === undefined) {
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
}
- return fire(element, 'ajax:beforeSend', [xhr, settings]);
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
},
success: function(data, status, xhr) {
element.trigger('ajax:success', [data, status, xhr]);
@@ -95,11 +94,11 @@
}
});
}
- }
+ },
// Handles "data-method" on links such as:
// Delete
- function handleMethod(link) {
+ handleMethod: function(link) {
var href = link.attr('href'),
method = link.data('method'),
csrf_token = $('meta[name=csrf-token]').attr('content'),
@@ -113,54 +112,54 @@
form.hide().append(metadata_input).appendTo('body');
form.submit();
- }
+ },
- function disableFormElements(form) {
+ disableFormElements: function(form) {
form.find('input[data-disable-with], button[data-disable-with]').each(function() {
var element = $(this), method = element.is('button') ? 'html' : 'val';
element.data('ujs:enable-with', element[method]());
element[method](element.data('disable-with'));
element.attr('disabled', 'disabled');
});
- }
+ },
- function enableFormElements(form) {
+ enableFormElements: function(form) {
form.find('input[data-disable-with]:disabled, button[data-disable-with]:disabled').each(function() {
var element = $(this), method = element.is('button') ? 'html' : 'val';
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
element.removeAttr('disabled');
});
- }
+ },
- function allowAction(element) {
+ allowAction: function(element) {
var message = element.data('confirm');
- return !message || (fire(element, 'confirm') && confirm(message));
- }
+ return !message || (rails.fire(element, 'confirm') && confirm(message));
+ },
- function blankInputs(form, specifiedSelector) {
+ blankInputs: function(form, specifiedSelector) {
var blankExists = false,
selector = specifiedSelector || 'input';
form.find(selector).each(function() {
if (!$(this).val()) blankExists = true;
});
return blankExists;
- }
+ },
- function nonBlankInputs(form, specifiedSelector) {
+ nonBlankInputs: function(form, specifiedSelector) {
var nonBlankExists = false,
selector = specifiedSelector || 'input';
form.find(selector).each(function() {
if ($(this).val()) nonBlankExists = true;
});
return nonBlankExists;
- }
+ },
- function stopEverything(e) {
+ stopEverything: function(e) {
e.stopImmediatePropagation();
return false;
- }
+ },
- function callFormSubmitBindings(form) {
+ callFormSubmitBindings: function(form) {
var events = form.data('events'), continuePropagation = true;
if (events != undefined && events['submit'] != undefined) {
$.each(events['submit'], function(i, obj){
@@ -169,58 +168,65 @@
}
return continuePropagation;
}
+ };
+
+ if ('ajaxPrefilter' in $) {
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
+ } else {
+ $(document).ajaxSend(function(e, xhr){ rails.CSRFProtection(xhr); });
+ }
$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
var link = $(this);
- if (!allowAction(link)) return stopEverything(e);
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
if (link.data('remote') != undefined) {
- handleRemote(link);
+ rails.handleRemote(link);
return false;
} else if (link.data('method')) {
- handleMethod(link);
+ rails.handleMethod(link);
return false;
}
});
$('form').live('submit.rails', function(e) {
var form = $(this), remote = form.data('remote') != undefined;
- if (!allowAction(form)) return stopEverything(e);
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
// skip other logic when required values are missing or file upload is present
- if (blankInputs(form, 'input[name][required]') && fire(form, 'ajax:aborted:required')) {
+ if (rails.blankInputs(form, 'input[name][required]') && rails.fire(form, 'ajax:aborted:required')) {
return !remote;
}
- if (nonBlankInputs(form, 'input:file')) {
- return fire(form, 'ajax:aborted:file');
+ if (rails.nonBlankInputs(form, 'input:file')) {
+ return rails.fire(form, 'ajax:aborted:file');
}
// If browser does not support submit bubbling, then this live-binding will be called before direct
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
- if (!$.support.submitBubbles && callFormSubmitBindings(form) == false) return stopEverything(e)
+ if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) == false) return rails.stopEverything(e)
if (remote) {
- handleRemote(form);
+ rails.handleRemote(form);
return false;
} else {
// slight timeout so that the submit button gets properly serialized
- setTimeout(function(){ disableFormElements(form); }, 13);
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
}
});
$('form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])').live('click.rails', function() {
var button = $(this);
- if (!allowAction(button)) return stopEverything(e);
+ 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);
});
$('form').live('ajax:beforeSend.rails', function(event) {
- if (this == event.target) disableFormElements($(this));
+ if (this == event.target) rails.disableFormElements($(this));
});
$('form').live('ajax:complete.rails', function(event) {
- if (this == event.target) enableFormElements($(this));
+ if (this == event.target) rails.enableFormElements($(this));
});
})( jQuery );
From 73290132ddad4654fce8eddf6a83b1063b5da676 Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Mon, 18 Apr 2011 11:33:30 +0800
Subject: [PATCH 013/282] Added CHANGELOG.
---
CHANGELOG.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 CHANGELOG.md
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 00000000..0f652a09
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,18 @@
+CHANGELOG
+=========
+
+Here is a non-exhaustive list of notable changes to jquery-ujs (oldest
+to newest):
+
+- [`085d910a5ec07b69`](https://github.com/rails/jquery-ujs/commit/085d910a5ec07b69f31beabce286141aa26f3005) last version before callback names updated
+- [`72d875a8d57c6bb4`](https://github.com/rails/jquery-ujs/commit/72d875a8d57c6bb466170980a5142c66ac74e8f0) callback name updates completed
+- [`e076121248913143`](https://github.com/rails/jquery-ujs/commit/e0761212489131437402a92fa8f548a78f685ae2) dropped support for jQuery 1.4, 1.4.1, 1.4.2 (separate [v1.4 branch](https://github.com/rails/jquery-ujs/commits/v1.4) created)
+- [`498b35e24cdb14f2`](https://github.com/rails/jquery-ujs/commit/498b35e24cdb14f2d94486e8a1f4a1f661091426) last version before `.callRemote()` removed
+- [`ec96408a746d3b06`](https://github.com/rails/jquery-ujs/commit/ec96408a746d3b0692da9249f218a3943fbffc28) `ACCEPTS` header data-type default changed to prefer `:js` but not require it
+- [`fc639928d1e15c88`](https://github.com/rails/jquery-ujs/commit/fc639928d1e15c885b85de5b517346db7f963f44) default form method changed from `POST` to `GET`
+- [`e9311550fdb3afeb`](https://github.com/rails/jquery-ujs/commit/e9311550fdb3afeb2917bcb1fef39767bf715003) added CSRF Protection to remote requests
+- [`a284dd706e7d76e8`](https://github.com/rails/jquery-ujs/commit/a284dd706e7d76e85471ef39ab3efdf07feef374) CSRF fixed - changed to only add if token is present
+- [`f9b21b3a3c7c4684`](https://github.com/rails/jquery-ujs/commit/f9b21b3a3c7c46840fed8127a90def26911fad3d) `ajax:before` added back
+- [`ca575e184e93b3ef`](https://github.com/rails/jquery-ujs/commit/ca575e184e93b3efe1a858cf598f8a37f0a760cc) added `ajax:aborted:required` and `ajax:aborted:file` event hooks
+- [`d2abd6f9df4e4a42`](https://github.com/rails/jquery-ujs/commit/d2abd6f9df4e4a426c17c218b7d5e05004c768d0) fixed submit and bubbling behavior for IE
+- [`d59144177d867908`](https://github.com/rails/jquery-ujs/commit/d59144177d86790891fdb99b0e3437312e04fda2) created external api via `$.rails` object
From 1335ba4d82b005f5df92f0b16d212316840d684e Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 09:25:06 -0400
Subject: [PATCH 014/282] fix lint warnings
---
src/rails.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 05b5c89d..288a2277 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -164,7 +164,7 @@
if (events != undefined && events['submit'] != undefined) {
$.each(events['submit'], function(i, obj){
if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
- })
+ });
}
return continuePropagation;
}
@@ -203,7 +203,7 @@
// If browser does not support submit bubbling, then this live-binding will be called before direct
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
- if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) == false) return rails.stopEverything(e)
+ if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
if (remote) {
rails.handleRemote(form);
From 8635365111a325cc9323b2297b8523e4a70947d3 Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 15:28:07 -0400
Subject: [PATCH 015/282] wrap long lines so that one could read comments on
github without horizontal scroll
---
src/rails.js | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 288a2277..c7b6cef6 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -8,8 +8,8 @@
*
* By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
* in the remote form, this adapter aborts the AJAX submission and allows the browser to submit the form normally.
- * You may intercept the form submission and implement your own work-around for submitting file uploads without refreshing the page,
- * by binding a handler function that returns false to the `ajax:aborted:file` hook.
+ * You may intercept the form submission and implement your own work-around for submitting file uploads without
+ * refreshing the page, by binding a handler function that returns false to the `ajax:aborted:file` hook.
*
* Ex:
* $('form').live('ajax:aborted:file', function(){
@@ -21,13 +21,16 @@
* a) file-type input field is detected, and
* b) the value of the input:file field is not blank.
*
- * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use techniques like the iframe method to upload the file instead.
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
+ * techniques like the iframe method to upload the file instead.
*
- * Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected, providing the `ajax:aborted:required` hook.
+ * Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected,
+ * providing the `ajax:aborted:required` hook.
* Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
*
- * The default behavior for aborting the remote form submission when required inputs are missing may be canceled (thereby submitting the form via AJAX anyway)
- * by binding a handler function that returns false to the `ajax:aborted:required` hook.
+ * The default behavior for aborting the remote form submission when required inputs are missing may be
+ * canceled (thereby submitting the form via AJAX anyway) by binding a handler function that returns
+ * false to the `ajax:aborted:required` hook.
*
* Ex:
* $('form').live('ajax:aborted:required', function(){
From 5ee23ebfbc50fac8d9f5e73854a90b207b109f1f Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 15:38:05 -0400
Subject: [PATCH 016/282] fix indentations
---
src/rails.js | 265 ++++++++++++++++++++++++++-------------------------
1 file changed, 135 insertions(+), 130 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index c7b6cef6..bf5fbfb4 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -39,139 +39,143 @@
*/
(function($) {
- // Shorthand to make it a little easier to call public rails functions from within rails.js
- var rails;
-
- $.rails = rails = {
- // Make sure that every Ajax request sends the CSRF token
- CSRFProtection: function(xhr) {
- var token = $('meta[name="csrf-token"]').attr('content');
- if (token) xhr.setRequestHeader('X-CSRF-Token', token);
- },
-
- // Triggers an event on an element and returns the event result
- fire: function(obj, name, data) {
- var event = $.Event(name);
- obj.trigger(event, data);
- return event.result !== false;
- },
-
- // Submits "remote" forms and links with ajax
- handleRemote: function(element) {
- var method, url, data,
- dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
-
- if (rails.fire(element, 'ajax:before')) {
- if (element.is('form')) {
- method = element.attr('method');
- url = element.attr('action');
- data = element.serializeArray();
- // memoized value from clicked submit button
- var button = element.data('ujs:submit-button');
- if (button) {
- data.push(button);
- element.data('ujs:submit-button', null);
- }
- } else {
- method = element.data('method');
- url = element.attr('href');
- data = null;
- }
- $.ajax({
- url: url, type: method || 'GET', data: data, dataType: dataType,
- // stopping the "ajax:beforeSend" event will cancel the ajax request
- beforeSend: function(xhr, settings) {
- if (settings.dataType === undefined) {
- xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
+
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
+ var rails;
+
+ $.rails = rails = {
+
+ // Make sure that every Ajax request sends the CSRF token
+ CSRFProtection: function(xhr) {
+ var token = $('meta[name="csrf-token"]').attr('content');
+ if (token) xhr.setRequestHeader('X-CSRF-Token', token);
+ },
+
+ // Triggers an event on an element and returns the event result
+ fire: function(obj, name, data) {
+ var event = $.Event(name);
+ obj.trigger(event, data);
+ return event.result !== false;
+ },
+
+ // Submits "remote" forms and links with ajax
+ handleRemote: function(element) {
+ var method, url, data,
+ dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
+
+ if (rails.fire(element, 'ajax:before')) {
+
+ if (element.is('form')) {
+ method = element.attr('method');
+ url = element.attr('action');
+ data = element.serializeArray();
+ // memoized value from clicked submit button
+ var button = element.data('ujs:submit-button');
+ if (button) {
+ data.push(button);
+ element.data('ujs:submit-button', null);
}
- return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
- },
- success: function(data, status, xhr) {
- element.trigger('ajax:success', [data, status, xhr]);
- },
- complete: function(xhr, status) {
- element.trigger('ajax:complete', [xhr, status]);
- },
- error: function(xhr, status, error) {
- element.trigger('ajax:error', [xhr, status, error]);
+ } else {
+ method = element.data('method');
+ url = element.attr('href');
+ data = null;
}
+
+ $.ajax({
+ url: url, type: method || 'GET', data: data, dataType: dataType,
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
+ beforeSend: function(xhr, settings) {
+ if (settings.dataType === undefined) {
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
+ }
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
+ },
+ success: function(data, status, xhr) {
+ element.trigger('ajax:success', [data, status, xhr]);
+ },
+ complete: function(xhr, status) {
+ element.trigger('ajax:complete', [xhr, status]);
+ },
+ error: function(xhr, status, error) {
+ element.trigger('ajax:error', [xhr, status, error]);
+ }
+ });
+ }
+ },
+
+ // Handles "data-method" on links such as:
+ // Delete
+ handleMethod: function(link) {
+ var href = link.attr('href'),
+ method = link.data('method'),
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
+ form = $(''),
+ metadata_input = '';
+
+ if (csrf_param !== undefined && csrf_token !== undefined) {
+ metadata_input += '';
+ }
+
+ form.hide().append(metadata_input).appendTo('body');
+ form.submit();
+ },
+
+ disableFormElements: function(form) {
+ form.find('input[data-disable-with], button[data-disable-with]').each(function() {
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
+ element.data('ujs:enable-with', element[method]());
+ element[method](element.data('disable-with'));
+ element.attr('disabled', 'disabled');
});
- }
- },
-
- // Handles "data-method" on links such as:
- // Delete
- handleMethod: function(link) {
- var href = link.attr('href'),
- method = link.data('method'),
- csrf_token = $('meta[name=csrf-token]').attr('content'),
- csrf_param = $('meta[name=csrf-param]').attr('content'),
- form = $(''),
- metadata_input = '';
-
- if (csrf_param !== undefined && csrf_token !== undefined) {
- metadata_input += '';
- }
+ },
- form.hide().append(metadata_input).appendTo('body');
- form.submit();
- },
-
- disableFormElements: function(form) {
- form.find('input[data-disable-with], button[data-disable-with]').each(function() {
- var element = $(this), method = element.is('button') ? 'html' : 'val';
- element.data('ujs:enable-with', element[method]());
- element[method](element.data('disable-with'));
- element.attr('disabled', 'disabled');
- });
- },
-
- enableFormElements: function(form) {
- form.find('input[data-disable-with]:disabled, button[data-disable-with]:disabled').each(function() {
- var element = $(this), method = element.is('button') ? 'html' : 'val';
- if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
- element.removeAttr('disabled');
- });
- },
-
- allowAction: function(element) {
- var message = element.data('confirm');
- return !message || (rails.fire(element, 'confirm') && confirm(message));
- },
-
- blankInputs: function(form, specifiedSelector) {
- var blankExists = false,
+ enableFormElements: function(form) {
+ form.find('input[data-disable-with]:disabled, button[data-disable-with]:disabled').each(function() {
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
+ element.removeAttr('disabled');
+ });
+ },
+
+ allowAction: function(element) {
+ var message = element.data('confirm');
+ return !message || (rails.fire(element, 'confirm') && confirm(message));
+ },
+
+ blankInputs: function(form, specifiedSelector) {
+ var blankExists = false,
selector = specifiedSelector || 'input';
- form.find(selector).each(function() {
- if (!$(this).val()) blankExists = true;
- });
- return blankExists;
- },
-
- nonBlankInputs: function(form, specifiedSelector) {
- var nonBlankExists = false,
+ form.find(selector).each(function() {
+ if (!$(this).val()) blankExists = true;
+ });
+ return blankExists;
+ },
+
+ nonBlankInputs: function(form, specifiedSelector) {
+ var nonBlankExists = false,
selector = specifiedSelector || 'input';
- form.find(selector).each(function() {
- if ($(this).val()) nonBlankExists = true;
- });
- return nonBlankExists;
- },
-
- stopEverything: function(e) {
- e.stopImmediatePropagation();
- return false;
- },
-
- callFormSubmitBindings: function(form) {
- var events = form.data('events'), continuePropagation = true;
- if (events != undefined && events['submit'] != undefined) {
- $.each(events['submit'], function(i, obj){
- if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
- });
- }
- return continuePropagation;
- }
- };
+ form.find(selector).each(function() {
+ if ($(this).val()) nonBlankExists = true;
+ });
+ return nonBlankExists;
+ },
+
+ stopEverything: function(e) {
+ e.stopImmediatePropagation();
+ return false;
+ },
+
+ callFormSubmitBindings: function(form) {
+ var events = form.data('events'), continuePropagation = true;
+ if (events != undefined && events['submit'] != undefined) {
+ $.each(events['submit'], function(i, obj){
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
+ });
+ }
+ return continuePropagation;
+ }
+ };
if ('ajaxPrefilter' in $) {
$.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
@@ -204,9 +208,9 @@
return rails.fire(form, 'ajax:aborted:file');
}
- // If browser does not support submit bubbling, then this live-binding will be called before direct
- // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
- if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
+ if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
if (remote) {
rails.handleRemote(form);
@@ -232,4 +236,5 @@
$('form').live('ajax:complete.rails', function(event) {
if (this == event.target) rails.enableFormElements($(this));
});
+
})( jQuery );
From a14563915e1150dfdc5a4286e9ddb839cd92cfde Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 16:20:16 -0400
Subject: [PATCH 017/282] edit comment
---
src/rails.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/rails.js b/src/rails.js
index bf5fbfb4..a9f0fa76 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -51,7 +51,7 @@
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
},
- // Triggers an event on an element and returns the event result
+ // Triggers an event on an element and returns false if the event result is false
fire: function(obj, name, data) {
var event = $.Event(name);
obj.trigger(event, data);
From 8fe10cc048f92380a9fe77a07aadb33e5e098a5f Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 16:20:44 -0400
Subject: [PATCH 018/282] prefer triple equal over double equal
---
src/rails.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index a9f0fa76..d50e31bf 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -168,7 +168,7 @@
callFormSubmitBindings: function(form) {
var events = form.data('events'), continuePropagation = true;
- if (events != undefined && events['submit'] != undefined) {
+ if (events !== undefined && events['submit'] !== undefined) {
$.each(events['submit'], function(i, obj){
if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
});
@@ -187,7 +187,7 @@
var link = $(this);
if (!rails.allowAction(link)) return rails.stopEverything(e);
- if (link.data('remote') != undefined) {
+ if (link.data('remote') !== undefined) {
rails.handleRemote(link);
return false;
} else if (link.data('method')) {
From 325e608b8550afa0d45b7393b4023cdcd1f15889 Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 16:20:55 -0400
Subject: [PATCH 019/282] blank lines and indentation
---
src/rails.js | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/rails.js b/src/rails.js
index d50e31bf..d060ea0e 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -204,6 +204,7 @@
if (rails.blankInputs(form, 'input[name][required]') && rails.fire(form, 'ajax:aborted:required')) {
return !remote;
}
+
if (rails.nonBlankInputs(form, 'input:file')) {
return rails.fire(form, 'ajax:aborted:file');
}
@@ -223,9 +224,13 @@
$('form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])').live('click.rails', function() {
var button = $(this);
+
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;
+ var name = button.attr('name'),
+ data = name ? {name:name, value:button.val()} : null;
+
button.closest('form').data('ujs:submit-button', data);
});
From 7ffd00110441f2169b29782da2677618632185b0 Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 17:44:13 -0400
Subject: [PATCH 020/282] edit comments
---
src/rails.js | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index d060ea0e..49f8f972 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -7,9 +7,13 @@
* Uploading file using rails.js
*
* By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
- * in the remote form, this adapter aborts the AJAX submission and allows the browser to submit the form normally.
- * You may intercept the form submission and implement your own work-around for submitting file uploads without
- * refreshing the page, by binding a handler function that returns false to the `ajax:aborted:file` hook.
+ * in the remote form, this adapter aborts the AJAX submission and submits the form through standard means.
+ *
+ * When AJAX submission is aborted then event `ajax:aborted:file` is fired and this allows you to bind your own
+ * handler to process the form submission the way you want.
+ *
+ * For example if you want remote form submission to be cancelled and you do not want to submit the form through
+ * standard means then you can write following handler.
*
* Ex:
* $('form').live('ajax:aborted:file', function(){
@@ -28,8 +32,8 @@
* providing the `ajax:aborted:required` hook.
* Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
*
- * The default behavior for aborting the remote form submission when required inputs are missing may be
- * canceled (thereby submitting the form via AJAX anyway) by binding a handler function that returns
+ * The default behavior of aborting the remote form submission when required inputs are missing, may be
+ * changed (thereby submitting the form via AJAX anyway) by binding a handler function that returns
* false to the `ajax:aborted:required` hook.
*
* Ex:
From 23a6668e01b05b1c0aa79da7e94080a566b4325e Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 17:44:47 -0400
Subject: [PATCH 021/282] comments
---
src/rails.js | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/rails.js b/src/rails.js
index 49f8f972..4443a30e 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -170,6 +170,8 @@
return false;
},
+ // find all the submit events directly bound to the form and
+ // manually invoke them. If anyone returns false then stop the loop
callFormSubmitBindings: function(form) {
var events = form.data('events'), continuePropagation = true;
if (events !== undefined && events['submit'] !== undefined) {
@@ -181,6 +183,7 @@
}
};
+ // ajaxPrefilter is a jQuery 1.5 feature
if ('ajaxPrefilter' in $) {
$.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
} else {
From 818c975820b169c188f986147de76d975d69b8fe Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 17:45:14 -0400
Subject: [PATCH 022/282] terminate the loop sooner
---
src/rails.js | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 4443a30e..69029750 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -151,7 +151,10 @@
var blankExists = false,
selector = specifiedSelector || 'input';
form.find(selector).each(function() {
- if (!$(this).val()) blankExists = true;
+ if (!$(this).val()) {
+ blankExists = true;
+ return false; //this terminates the each loop
+ }
});
return blankExists;
},
@@ -160,7 +163,10 @@
var nonBlankExists = false,
selector = specifiedSelector || 'input';
form.find(selector).each(function() {
- if ($(this).val()) nonBlankExists = true;
+ if ($(this).val()) {
+ nonBlankExists = true;
+ return false; //this terminates the each loop
+ }
});
return nonBlankExists;
},
From 329040073434a66f29b8db32f7a3a0c87acde6a0 Mon Sep 17 00:00:00 2001
From: Neeraj Singh
Date: Mon, 18 Apr 2011 17:45:35 -0400
Subject: [PATCH 023/282] prefer !== over !=
---
src/rails.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/rails.js b/src/rails.js
index 69029750..dbe10626 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -210,7 +210,7 @@
});
$('form').live('submit.rails', function(e) {
- var form = $(this), remote = form.data('remote') != undefined;
+ var form = $(this), remote = form.data('remote') !== undefined;
if (!rails.allowAction(form)) return rails.stopEverything(e);
// skip other logic when required values are missing or file upload is present
From 8e9b85a8fef0a7eb684c1a9769af399cc568b3ed Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Mon, 18 Apr 2011 19:00:59 -0400
Subject: [PATCH 024/282] Updated rails.js description to be a little more
succinct and clear. Added short descriptions to all $.rails functions.
---
src/rails.js | 44 +++++++++++++++++++++++++++++---------------
1 file changed, 29 insertions(+), 15 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index dbe10626..1fd63da4 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -5,39 +5,39 @@
* https://github.com/rails/jquery-ujs
* Uploading file using rails.js
+ * =============================
*
* By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
- * in the remote form, this adapter aborts the AJAX submission and submits the form through standard means.
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
*
- * When AJAX submission is aborted then event `ajax:aborted:file` is fired and this allows you to bind your own
- * handler to process the form submission the way you want.
- *
- * For example if you want remote form submission to be cancelled and you do not want to submit the form through
- * standard means then you can write following handler.
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
*
* Ex:
* $('form').live('ajax:aborted:file', function(){
* // Implement own remote file-transfer handler here.
+ * // Returning false in this handler tells rails.js to disallow standard form submission
* return false;
* });
*
- * The `ajax:aborted:file` event is fired when a form is submitted and both conditions are met:
- * a) file-type input field is detected, and
- * b) the value of the input:file field is not blank.
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
*
* Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
* techniques like the iframe method to upload the file instead.
*
- * Similarly, rails.js aborts AJAX form submissions if any non-blank input[required] fields are detected,
- * providing the `ajax:aborted:required` hook.
- * Unlike file uploads, however, blank required input fields cancel the whole form submission by default.
+ * Required fields in rails.js
+ * ===========================
+ *
+ * This adapter aborts AJAX form submissions if any non-blank input[required] fields are detected.
+ * Unlike file uploads, however, blank required input fields cancel the whole form submission, by default.
+ *
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
*
- * The default behavior of aborting the remote form submission when required inputs are missing, may be
- * changed (thereby submitting the form via AJAX anyway) by binding a handler function that returns
- * false to the `ajax:aborted:required` hook.
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs,
+ * so this event may never get fired in Opera.
*
* Ex:
* $('form').live('ajax:aborted:required', function(){
+ * // Returning false in this handler tells rails.js to submit the form anyway
* return ! confirm("Would you like to submit the form with missing info?");
* });
*/
@@ -125,6 +125,11 @@
form.submit();
},
+ /* Disables form elements:
+ - Caches element value in 'ujs:enable-with' data store
+ - Replaces element text with value of 'data-disable-with' attribute
+ - Adds disabled=disabled attribute
+ */
disableFormElements: function(form) {
form.find('input[data-disable-with], button[data-disable-with]').each(function() {
var element = $(this), method = element.is('button') ? 'html' : 'val';
@@ -134,6 +139,10 @@
});
},
+ /* Re-enables disabled form elements:
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
+ - Removes disabled attribute
+ */
enableFormElements: function(form) {
form.find('input[data-disable-with]:disabled, button[data-disable-with]:disabled').each(function() {
var element = $(this), method = element.is('button') ? 'html' : 'val';
@@ -142,11 +151,14 @@
});
},
+ // If message provided in 'data-confirm' attribute, fires `confirm` event and returns result of confirm dialog.
+ // Attaching a handler to the element's `confirm` event that returns false cancels the confirm dialog.
allowAction: function(element) {
var message = element.data('confirm');
return !message || (rails.fire(element, 'confirm') && confirm(message));
},
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
blankInputs: function(form, specifiedSelector) {
var blankExists = false,
selector = specifiedSelector || 'input';
@@ -159,6 +171,7 @@
return blankExists;
},
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
nonBlankInputs: function(form, specifiedSelector) {
var nonBlankExists = false,
selector = specifiedSelector || 'input';
@@ -171,6 +184,7 @@
return nonBlankExists;
},
+ // Helper function, needed to provide consistent behavior in IE
stopEverything: function(e) {
e.stopImmediatePropagation();
return false;
From b583a6a4c0c2118c562642fbec1498e07989cebe Mon Sep 17 00:00:00 2001
From: Steve Schwartz
Date: Mon, 18 Apr 2011 20:40:50 -0400
Subject: [PATCH 025/282] Refactored required field and file field abort
events.
-Required field matching now also matches required textareas.
-Removed duplicate code in rails.nonBlankInputs function.
-`ajax:aborted:required` and `ajax:aborted:file` events are now passed
jQuery collection of the inputs that triggered the event.
---
src/rails.js | 36 ++++++++++-------------
test/public/test/call-remote-callbacks.js | 16 ++++++----
2 files changed, 27 insertions(+), 25 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 1fd63da4..d91e04b0 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -159,29 +159,22 @@
},
// Helper function which checks for blank inputs in a form that match the specified CSS selector
- blankInputs: function(form, specifiedSelector) {
- var blankExists = false,
- selector = specifiedSelector || 'input';
+ blankInputs: function(form, specifiedSelector, nonBlank) {
+ var inputs = $(), input,
+ selector = specifiedSelector || 'input,textarea';
form.find(selector).each(function() {
- if (!$(this).val()) {
- blankExists = true;
- return false; //this terminates the each loop
+ input = $(this);
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
+ if (nonBlank ? input.val() : !input.val()) {
+ inputs = inputs.add(input);
}
});
- return blankExists;
+ return inputs.length ? inputs : false;
},
// Helper function which checks for non-blank inputs in a form that match the specified CSS selector
nonBlankInputs: function(form, specifiedSelector) {
- var nonBlankExists = false,
- selector = specifiedSelector || 'input';
- form.find(selector).each(function() {
- if ($(this).val()) {
- nonBlankExists = true;
- return false; //this terminates the each loop
- }
- });
- return nonBlankExists;
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
},
// Helper function, needed to provide consistent behavior in IE
@@ -224,16 +217,19 @@
});
$('form').live('submit.rails', function(e) {
- var form = $(this), remote = form.data('remote') !== undefined;
+ var form = $(this),
+ remote = form.data('remote') !== undefined,
+ blankRequiredInputs = rails.blankInputs(form, 'input[name][required],textarea[name][required]'),
+ nonBlankFileInputs = rails.nonBlankInputs(form, 'input:file');
if (!rails.allowAction(form)) return rails.stopEverything(e);
// skip other logic when required values are missing or file upload is present
- if (rails.blankInputs(form, 'input[name][required]') && rails.fire(form, 'ajax:aborted:required')) {
+ if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
return !remote;
}
- if (rails.nonBlankInputs(form, 'input:file')) {
- return rails.fire(form, 'ajax:aborted:file');
+ if (nonBlankFileInputs) {
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
}
// If browser does not support submit bubbling, then this live-binding will be called before direct
diff --git a/test/public/test/call-remote-callbacks.js b/test/public/test/call-remote-callbacks.js
index 204339e0..42627a97 100644
--- a/test/public/test/call-remote-callbacks.js
+++ b/test/public/test/call-remote-callbacks.js
@@ -63,22 +63,26 @@ asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function
});
});
-asyncTest('blank required form input field should abort request and trigger "ajax:aborted:required" event', 2, function() {
+asyncTest('blank required form input field should abort request and trigger "ajax:aborted:required" event', 5, function() {
var form = $('form[data-remote]')
.append($(''))
+ .append($('