jquery-rails 3.0.1 → 3.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of jquery-rails might be problematic. Click here for more details.

@@ -4,7 +4,7 @@
4
4
  * Unobtrusive scripting adapter for jQuery
5
5
  * https://github.com/rails/jquery-ujs
6
6
  *
7
- * Requires jQuery 1.7.0 or later.
7
+ * Requires jQuery 1.8.0 or later.
8
8
  *
9
9
  * Released under the MIT license
10
10
  *
@@ -18,13 +18,14 @@
18
18
 
19
19
  // Shorthand to make it a little easier to call public rails functions from within rails.js
20
20
  var rails;
21
+ var $document = $(document);
21
22
 
22
23
  $.rails = rails = {
23
24
  // Link elements bound by jquery-ujs
24
- 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]',
25
26
 
26
- // Button elements boud jquery-ujs
27
- buttonClickSelector: 'button[data-remote]',
27
+ // Button elements bound by jquery-ujs
28
+ buttonClickSelector: 'button[data-remote]:not(form button), button[data-confirm]:not(form button)',
28
29
 
29
30
  // Select elements bound by jquery-ujs
30
31
  inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
@@ -33,13 +34,13 @@
33
34
  formSubmitSelector: 'form',
34
35
 
35
36
  // Form input elements bound by jquery-ujs
36
- formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',
37
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type]), input[type=submit][form], input[type=image][form], button[type=submit][form], button[form]:not([type])',
37
38
 
38
39
  // Form input elements disabled during form submission
39
- 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',
40
41
 
41
42
  // Form input elements re-enabled after form submission
42
- 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',
43
44
 
44
45
  // Form required input elements
45
46
  requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])',
@@ -48,7 +49,10 @@
48
49
  fileInputSelector: 'input[type=file]',
49
50
 
50
51
  // Link onClick disable selector with possible reenable after remote submission
51
- 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]',
52
56
 
53
57
  // Make sure that every Ajax request sends the CSRF token
54
58
  CSRFProtection: function(xhr) {
@@ -56,6 +60,13 @@
56
60
  if (token) xhr.setRequestHeader('X-CSRF-Token', token);
57
61
  },
58
62
 
63
+ // making sure that all forms have actual up-to-date token(cached forms contain old one)
64
+ refreshCSRFTokens: function(){
65
+ var csrfToken = $('meta[name=csrf-token]').attr('content');
66
+ var csrfParam = $('meta[name=csrf-param]').attr('content');
67
+ $('form input[name="' + csrfParam + '"]').val(csrfToken);
68
+ },
69
+
59
70
  // Triggers an event on an element and returns false if the event result is false
60
71
  fire: function(obj, name, data) {
61
72
  var event = $.Event(name);
@@ -75,16 +86,14 @@
75
86
 
76
87
  // Default way to get an element's href. May be overridden at $.rails.href.
77
88
  href: function(element) {
78
- return element.attr('href');
89
+ return element[0].href;
79
90
  },
80
91
 
81
92
  // Submits "remote" forms and links with ajax
82
93
  handleRemote: function(element) {
83
- var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options;
94
+ var method, url, data, withCredentials, dataType, options;
84
95
 
85
96
  if (rails.fire(element, 'ajax:before')) {
86
- elCrossDomain = element.data('cross-domain');
87
- crossDomain = elCrossDomain === undefined ? null : elCrossDomain;
88
97
  withCredentials = element.data('with-credentials') || null;
89
98
  dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
90
99
 
@@ -121,7 +130,11 @@
121
130
  if (settings.dataType === undefined) {
122
131
  xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
123
132
  }
124
- return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
133
+ if (rails.fire(element, 'ajax:beforeSend', [xhr, settings])) {
134
+ element.trigger('ajax:send', xhr);
135
+ } else {
136
+ return false;
137
+ }
125
138
  },
126
139
  success: function(data, status, xhr) {
127
140
  element.trigger('ajax:success', [data, status, xhr]);
@@ -132,7 +145,7 @@
132
145
  error: function(xhr, status, error) {
133
146
  element.trigger('ajax:error', [xhr, status, error]);
134
147
  },
135
- crossDomain: crossDomain
148
+ crossDomain: rails.isCrossDomain(url)
136
149
  };
137
150
 
138
151
  // There is no withCredentials for IE6-8 when
@@ -146,61 +159,106 @@
146
159
  // Only pass url to `ajax` options if not blank
147
160
  if (url) { options.url = url; }
148
161
 
149
- var jqxhr = rails.ajax(options);
150
- element.trigger('ajax:send', jqxhr);
151
- return jqxhr;
162
+ return rails.ajax(options);
152
163
  } else {
153
164
  return false;
154
165
  }
155
166
  },
156
167
 
168
+ // Determines if the request is a cross domain request.
169
+ isCrossDomain: function(url) {
170
+ var originAnchor = document.createElement("a");
171
+ originAnchor.href = location.href;
172
+ var urlAnchor = document.createElement("a");
173
+
174
+ try {
175
+ urlAnchor.href = url;
176
+ // This is a workaround to a IE bug.
177
+ urlAnchor.href = urlAnchor.href;
178
+
179
+ // If URL protocol is false or is a string containing a single colon
180
+ // *and* host are false, assume it is not a cross-domain request
181
+ // (should only be the case for IE7 and IE compatibility mode).
182
+ // Otherwise, evaluate protocol and host of the URL against the origin
183
+ // protocol and host
184
+ return !(((!urlAnchor.protocol || urlAnchor.protocol === ':') && !urlAnchor.host) ||
185
+ (originAnchor.protocol + "//" + originAnchor.host ===
186
+ urlAnchor.protocol + "//" + urlAnchor.host)); //
187
+ } catch (e) {
188
+ // If there is an error parsing the URL, assume it is crossDomain.
189
+ return true;
190
+ }
191
+ },
192
+
157
193
  // Handles "data-method" on links such as:
158
194
  // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
159
195
  handleMethod: function(link) {
160
196
  var href = rails.href(link),
161
197
  method = link.data('method'),
162
198
  target = link.attr('target'),
163
- csrf_token = $('meta[name=csrf-token]').attr('content'),
164
- csrf_param = $('meta[name=csrf-param]').attr('content'),
199
+ csrfToken = $('meta[name=csrf-token]').attr('content'),
200
+ csrfParam = $('meta[name=csrf-param]').attr('content'),
165
201
  form = $('<form method="post" action="' + href + '"></form>'),
166
- metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
202
+ metadataInput = '<input name="_method" value="' + method + '" type="hidden" />';
167
203
 
168
- if (csrf_param !== undefined && csrf_token !== undefined) {
169
- metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
204
+ if (csrfParam !== undefined && csrfToken !== undefined && !rails.isCrossDomain(href)) {
205
+ metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
170
206
  }
171
207
 
172
208
  if (target) { form.attr('target', target); }
173
209
 
174
- form.hide().append(metadata_input).appendTo('body');
210
+ form.hide().append(metadataInput).appendTo('body');
175
211
  form.submit();
176
212
  },
177
213
 
214
+ // Helper function that returns form elements that match the specified CSS selector
215
+ // If form is actually a "form" element this will return associated elements outside the from that have
216
+ // the html form attribute set
217
+ formElements: function(form, selector) {
218
+ return form.is('form') ? $(form[0].elements).filter(selector) : form.find(selector);
219
+ },
220
+
178
221
  /* Disables form elements:
179
222
  - Caches element value in 'ujs:enable-with' data store
180
223
  - Replaces element text with value of 'data-disable-with' attribute
181
224
  - Sets disabled property to true
182
225
  */
183
226
  disableFormElements: function(form) {
184
- form.find(rails.disableSelector).each(function() {
185
- var element = $(this), method = element.is('button') ? 'html' : 'val';
186
- element.data('ujs:enable-with', element[method]());
187
- element[method](element.data('disable-with'));
188
- element.prop('disabled', true);
227
+ rails.formElements(form, rails.disableSelector).each(function() {
228
+ rails.disableFormElement($(this));
189
229
  });
190
230
  },
191
231
 
232
+ disableFormElement: function(element) {
233
+ var method, replacement;
234
+
235
+ method = element.is('button') ? 'html' : 'val';
236
+ replacement = element.data('disable-with');
237
+
238
+ element.data('ujs:enable-with', element[method]());
239
+ if (replacement !== undefined) {
240
+ element[method](replacement);
241
+ }
242
+
243
+ element.prop('disabled', true);
244
+ },
245
+
192
246
  /* Re-enables disabled form elements:
193
247
  - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
194
248
  - Sets disabled property to false
195
249
  */
196
250
  enableFormElements: function(form) {
197
- form.find(rails.enableSelector).each(function() {
198
- var element = $(this), method = element.is('button') ? 'html' : 'val';
199
- if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
200
- element.prop('disabled', false);
251
+ rails.formElements(form, rails.enableSelector).each(function() {
252
+ rails.enableFormElement($(this));
201
253
  });
202
254
  },
203
255
 
256
+ enableFormElement: function(element) {
257
+ var method = element.is('button') ? 'html' : 'val';
258
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
259
+ element.prop('disabled', false);
260
+ },
261
+
204
262
  /* For 'data-confirm' attribute:
205
263
  - Fires `confirm` event
206
264
  - Shows the confirmation dialog
@@ -261,8 +319,13 @@
261
319
  // replace element's html with the 'data-disable-with' after storing original html
262
320
  // and prevent clicking on it
263
321
  disableElement: function(element) {
322
+ var replacement = element.data('disable-with');
323
+
264
324
  element.data('ujs:enable-with', element.html()); // store enabled state
265
- element.html(element.data('disable-with')); // set to disabled state
325
+ if (replacement !== undefined) {
326
+ element.html(replacement);
327
+ }
328
+
266
329
  element.bind('click.railsDisable', function(e) { // prevent further clicking
267
330
  return rails.stopEverything(e);
268
331
  });
@@ -276,25 +339,28 @@
276
339
  }
277
340
  element.unbind('click.railsDisable'); // enable element
278
341
  }
279
-
280
342
  };
281
343
 
282
- if (rails.fire($(document), 'rails:attachBindings')) {
344
+ if (rails.fire($document, 'rails:attachBindings')) {
283
345
 
284
346
  $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
285
347
 
286
- $(document).delegate(rails.linkDisableSelector, 'ajax:complete', function() {
348
+ $document.delegate(rails.linkDisableSelector, 'ajax:complete', function() {
287
349
  rails.enableElement($(this));
288
350
  });
289
351
 
290
- $(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
291
- var link = $(this), method = link.data('method'), data = link.data('params');
352
+ $document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() {
353
+ rails.enableFormElement($(this));
354
+ });
355
+
356
+ $document.delegate(rails.linkClickSelector, 'click.rails', function(e) {
357
+ var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
292
358
  if (!rails.allowAction(link)) return rails.stopEverything(e);
293
359
 
294
- if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
360
+ if (!metaClick && link.is(rails.linkDisableSelector)) rails.disableElement(link);
295
361
 
296
362
  if (link.data('remote') !== undefined) {
297
- if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
363
+ if (metaClick && (!method || method === 'GET') && !data) { return true; }
298
364
 
299
365
  var handleRemote = rails.handleRemote(link);
300
366
  // response from rails.handleRemote() will either be false or a deferred object promise.
@@ -311,15 +377,24 @@
311
377
  }
312
378
  });
313
379
 
314
- $(document).delegate(rails.buttonClickSelector, 'click.rails', function(e) {
380
+ $document.delegate(rails.buttonClickSelector, 'click.rails', function(e) {
315
381
  var button = $(this);
382
+
316
383
  if (!rails.allowAction(button)) return rails.stopEverything(e);
317
384
 
318
- rails.handleRemote(button);
385
+ if (button.is(rails.buttonDisableSelector)) rails.disableFormElement(button);
386
+
387
+ var handleRemote = rails.handleRemote(button);
388
+ // response from rails.handleRemote() will either be false or a deferred object promise.
389
+ if (handleRemote === false) {
390
+ rails.enableFormElement(button);
391
+ } else {
392
+ handleRemote.error( function() { rails.enableFormElement(button); } );
393
+ }
319
394
  return false;
320
395
  });
321
396
 
322
- $(document).delegate(rails.inputChangeSelector, 'change.rails', function(e) {
397
+ $document.delegate(rails.inputChangeSelector, 'change.rails', function(e) {
323
398
  var link = $(this);
324
399
  if (!rails.allowAction(link)) return rails.stopEverything(e);
325
400
 
@@ -327,20 +402,24 @@
327
402
  return false;
328
403
  });
329
404
 
330
- $(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
405
+ $document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
331
406
  var form = $(this),
332
407
  remote = form.data('remote') !== undefined,
333
- blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
334
- nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
408
+ blankRequiredInputs,
409
+ nonBlankFileInputs;
335
410
 
336
411
  if (!rails.allowAction(form)) return rails.stopEverything(e);
337
412
 
338
413
  // skip other logic when required values are missing or file upload is present
339
- if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
340
- return rails.stopEverything(e);
414
+ if (form.attr('novalidate') == undefined) {
415
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector);
416
+ if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
417
+ return rails.stopEverything(e);
418
+ }
341
419
  }
342
420
 
343
421
  if (remote) {
422
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
344
423
  if (nonBlankFileInputs) {
345
424
  // slight timeout so that the submit button gets properly serialized
346
425
  // (make it easy for event handler to serialize form without disabled values)
@@ -362,7 +441,7 @@
362
441
  }
363
442
  });
364
443
 
365
- $(document).delegate(rails.formInputClickSelector, 'click.rails', function(event) {
444
+ $document.delegate(rails.formInputClickSelector, 'click.rails', function(event) {
366
445
  var button = $(this);
367
446
 
368
447
  if (!rails.allowAction(button)) return rails.stopEverything(event);
@@ -374,19 +453,16 @@
374
453
  button.closest('form').data('ujs:submit-button', data);
375
454
  });
376
455
 
377
- $(document).delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
456
+ $document.delegate(rails.formSubmitSelector, 'ajax:send.rails', function(event) {
378
457
  if (this == event.target) rails.disableFormElements($(this));
379
458
  });
380
459
 
381
- $(document).delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
460
+ $document.delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
382
461
  if (this == event.target) rails.enableFormElements($(this));
383
462
  });
384
463
 
385
464
  $(function(){
386
- // making sure that all forms have actual up-to-date token(cached forms contain old one)
387
- var csrf_token = $('meta[name=csrf-token]').attr('content');
388
- var csrf_param = $('meta[name=csrf-param]').attr('content');
389
- $('form input[name="' + csrf_param + '"]').val(csrf_token);
465
+ rails.refreshCSRFTokens();
390
466
  });
391
467
  }
392
468
 
metadata CHANGED
@@ -1,58 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
5
- prerelease:
4
+ version: 3.1.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - André Arko
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-08 00:00:00.000000000Z
11
+ date: 2018-04-18 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: railties
16
- requirement: &70143350807520 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.0'
22
- - - <
20
+ - - "<"
23
21
  - !ruby/object:Gem::Version
24
22
  version: '5.0'
25
23
  type: :runtime
26
24
  prerelease: false
27
- version_requirements: *70143350807520
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
28
33
  - !ruby/object:Gem::Dependency
29
34
  name: thor
30
- requirement: &70143350837740 !ruby/object:Gem::Requirement
31
- none: false
35
+ requirement: !ruby/object:Gem::Requirement
32
36
  requirements:
33
- - - ! '>='
37
+ - - ">="
34
38
  - !ruby/object:Gem::Version
35
39
  version: '0.14'
36
- - - <
40
+ - - "<"
37
41
  - !ruby/object:Gem::Version
38
42
  version: '2.0'
39
43
  type: :runtime
40
44
  prerelease: false
41
- version_requirements: *70143350837740
42
- description: This gem provides jQuery and the jQuery-ujs driver for your Rails 3 application.
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0.14'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2.0'
53
+ description: This gem provides jQuery and the jQuery-ujs driver for your Rails 3+
54
+ application.
43
55
  email:
44
56
  - andre@arko.net
45
57
  executables: []
46
58
  extensions: []
47
59
  extra_rdoc_files: []
48
60
  files:
49
- - .gitignore
61
+ - ".gitignore"
50
62
  - CHANGELOG.md
51
63
  - CONTRIBUTING.md
52
64
  - Gemfile
53
65
  - LICENSE
54
66
  - README.md
55
67
  - Rakefile
68
+ - VERSIONS.md
56
69
  - jquery-rails.gemspec
57
70
  - lib/generators/jquery/install/install_generator.rb
58
71
  - lib/jquery-rails.rb
@@ -68,26 +81,25 @@ files:
68
81
  homepage: http://rubygems.org/gems/jquery-rails
69
82
  licenses:
70
83
  - MIT
84
+ metadata: {}
71
85
  post_install_message:
72
86
  rdoc_options: []
73
87
  require_paths:
74
88
  - lib
75
89
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
90
  requirements:
78
- - - ! '>='
91
+ - - ">="
79
92
  - !ruby/object:Gem::Version
80
93
  version: '0'
81
94
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
95
  requirements:
84
- - - ! '>='
96
+ - - ">="
85
97
  - !ruby/object:Gem::Version
86
98
  version: 1.3.6
87
99
  requirements: []
88
100
  rubyforge_project: jquery-rails
89
- rubygems_version: 1.8.15
101
+ rubygems_version: 2.7.6
90
102
  signing_key:
91
- specification_version: 3
92
- summary: Use jQuery with Rails 3
103
+ specification_version: 4
104
+ summary: Use jQuery with Rails 3+
93
105
  test_files: []