Skip to content

Commit cbf37a1

Browse files
committed
Updated to latest jquery-ujs.
1 parent e90cb05 commit cbf37a1

File tree

2 files changed

+74
-27
lines changed

2 files changed

+74
-27
lines changed

lib/jquery/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module Jquery
22
module Rails
33
VERSION = "3.1.0"
44
JQUERY_VERSION = "1.11.1"
5-
JQUERY_UJS_VERSION = "f160fa2f4615f93e1a0d75e49de59d19c18c8728"
5+
JQUERY_UJS_VERSION = "c7a5b12a3b37c4a20f4e5fb22da843f24ef2d356"
66
end
77
end

vendor/assets/javascripts/jquery_ujs.js

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
$.rails = rails = {
2424
// Link elements bound by jquery-ujs
25-
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
25+
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with], a[data-disable]',
2626

2727
// Button elements bound by jquery-ujs
28-
buttonClickSelector: 'button[data-remote]',
28+
buttonClickSelector: 'button[data-remote], button[data-confirm]',
2929

3030
// Select elements bound by jquery-ujs
3131
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
@@ -37,10 +37,10 @@
3737
formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',
3838

3939
// Form input elements disabled during form submission
40-
disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
40+
disableSelector: 'input[data-disable-with]:enabled, button[data-disable-with]:enabled, textarea[data-disable-with]:enabled, input[data-disable]:enabled, button[data-disable]:enabled, textarea[data-disable]:enabled',
4141

4242
// Form input elements re-enabled after form submission
43-
enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
43+
enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled, input[data-disable]:disabled, button[data-disable]:disabled, textarea[data-disable]:disabled',
4444

4545
// Form required input elements
4646
requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])',
@@ -49,7 +49,10 @@
4949
fileInputSelector: 'input[type=file]',
5050

5151
// Link onClick disable selector with possible reenable after remote submission
52-
linkDisableSelector: 'a[data-disable-with]',
52+
linkDisableSelector: 'a[data-disable-with], a[data-disable]',
53+
54+
// Button onClick disable selector with possible reenable after remote submission
55+
buttonDisableSelector: 'button[data-remote][data-disable-with], button[data-remote][data-disable]',
5356

5457
// Make sure that every Ajax request sends the CSRF token
5558
CSRFProtection: function(xhr) {
@@ -129,7 +132,11 @@
129132
if (settings.dataType === undefined) {
130133
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
131134
}
132-
return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
135+
if (rails.fire(element, 'ajax:beforeSend', [xhr, settings])) {
136+
element.trigger('ajax:send', xhr);
137+
} else {
138+
return false;
139+
}
133140
},
134141
success: function(data, status, xhr) {
135142
element.trigger('ajax:success', [data, status, xhr]);
@@ -154,9 +161,7 @@
154161
// Only pass url to `ajax` options if not blank
155162
if (url) { options.url = url; }
156163

157-
var jqxhr = rails.ajax(options);
158-
element.trigger('ajax:send', jqxhr);
159-
return jqxhr;
164+
return rails.ajax(options);
160165
} else {
161166
return false;
162167
}
@@ -183,32 +188,54 @@
183188
form.submit();
184189
},
185190

191+
// Helper function that returns form elements that match the specified CSS selector
192+
// If form is actually a "form" element this will return associated elements outside the from that have
193+
// the html form attribute set
194+
formElements: function(form, selector) {
195+
return form.is('form') ? $(form[0].elements).filter(selector) : form.find(selector);
196+
},
197+
186198
/* Disables form elements:
187199
- Caches element value in 'ujs:enable-with' data store
188200
- Replaces element text with value of 'data-disable-with' attribute
189201
- Sets disabled property to true
190202
*/
191203
disableFormElements: function(form) {
192-
form.find(rails.disableSelector).each(function() {
193-
var element = $(this), method = element.is('button') ? 'html' : 'val';
194-
element.data('ujs:enable-with', element[method]());
195-
element[method](element.data('disable-with'));
196-
element.prop('disabled', true);
204+
rails.formElements(form, rails.disableSelector).each(function() {
205+
rails.disableFormElement($(this));
197206
});
198207
},
199208

209+
disableFormElement: function(element) {
210+
var method, replacement;
211+
212+
method = element.is('button') ? 'html' : 'val';
213+
replacement = element.data('disable-with');
214+
215+
element.data('ujs:enable-with', element[method]());
216+
if (replacement !== undefined) {
217+
element[method](replacement);
218+
}
219+
220+
element.prop('disabled', true);
221+
},
222+
200223
/* Re-enables disabled form elements:
201224
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
202225
- Sets disabled property to false
203226
*/
204227
enableFormElements: function(form) {
205-
form.find(rails.enableSelector).each(function() {
206-
var element = $(this), method = element.is('button') ? 'html' : 'val';
207-
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
208-
element.prop('disabled', false);
228+
rails.formElements(form, rails.enableSelector).each(function() {
229+
rails.enableFormElement($(this));
209230
});
210231
},
211232

233+
enableFormElement: function(element) {
234+
var method = element.is('button') ? 'html' : 'val';
235+
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
236+
element.prop('disabled', false);
237+
},
238+
212239
/* For 'data-confirm' attribute:
213240
- Fires `confirm` event
214241
- Shows the confirmation dialog
@@ -269,8 +296,13 @@
269296
// replace element's html with the 'data-disable-with' after storing original html
270297
// and prevent clicking on it
271298
disableElement: function(element) {
299+
var replacement = element.data('disable-with');
300+
272301
element.data('ujs:enable-with', element.html()); // store enabled state
273-
element.html(element.data('disable-with')); // set to disabled state
302+
if (replacement !== undefined) {
303+
element.html(replacement);
304+
}
305+
274306
element.bind('click.railsDisable', function(e) { // prevent further clicking
275307
return rails.stopEverything(e);
276308
});
@@ -284,7 +316,6 @@
284316
}
285317
element.unbind('click.railsDisable'); // enable element
286318
}
287-
288319
};
289320

290321
if (rails.fire($document, 'rails:attachBindings')) {
@@ -295,6 +326,10 @@
295326
rails.enableElement($(this));
296327
});
297328

329+
$document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() {
330+
rails.enableFormElement($(this));
331+
});
332+
298333
$document.delegate(rails.linkClickSelector, 'click.rails', function(e) {
299334
var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
300335
if (!rails.allowAction(link)) return rails.stopEverything(e);
@@ -323,7 +358,15 @@
323358
var button = $(this);
324359
if (!rails.allowAction(button)) return rails.stopEverything(e);
325360

326-
rails.handleRemote(button);
361+
if (button.is(rails.buttonDisableSelector)) rails.disableFormElement(button);
362+
363+
var handleRemote = rails.handleRemote(button);
364+
// response from rails.handleRemote() will either be false or a deferred object promise.
365+
if (handleRemote === false) {
366+
rails.enableFormElement(button);
367+
} else {
368+
handleRemote.error( function() { rails.enableFormElement(button); } );
369+
}
327370
return false;
328371
});
329372

@@ -338,17 +381,21 @@
338381
$document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
339382
var form = $(this),
340383
remote = form.data('remote') !== undefined,
341-
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
342-
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
384+
blankRequiredInputs,
385+
nonBlankFileInputs;
343386

344387
if (!rails.allowAction(form)) return rails.stopEverything(e);
345388

346389
// skip other logic when required values are missing or file upload is present
347-
if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
348-
return rails.stopEverything(e);
390+
if (form.attr('novalidate') == undefined) {
391+
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector);
392+
if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
393+
return rails.stopEverything(e);
394+
}
349395
}
350396

351397
if (remote) {
398+
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
352399
if (nonBlankFileInputs) {
353400
// slight timeout so that the submit button gets properly serialized
354401
// (make it easy for event handler to serialize form without disabled values)
@@ -382,7 +429,7 @@
382429
button.closest('form').data('ujs:submit-button', data);
383430
});
384431

385-
$document.delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
432+
$document.delegate(rails.formSubmitSelector, 'ajax:send.rails', function(event) {
386433
if (this == event.target) rails.disableFormElements($(this));
387434
});
388435

0 commit comments

Comments
 (0)