Commit 01121261cb277ef9bd3f4c2b50a99ea137c10780
1 parent
b5e3d046
Exists in
master
and in
1 other branch
Rails.js bump.
Showing
1 changed file
with
158 additions
and
132 deletions
Show diff stats
public/javascripts/rails.js
1 | -jQuery(function ($) { | |
2 | - var csrf_token = $('meta[name=csrf-token]').attr('content'), | |
3 | - csrf_param = $('meta[name=csrf-param]').attr('content'); | |
4 | - | |
5 | - $.fn.extend({ | |
6 | - /** | |
7 | - * Triggers a custom event on an element and returns the event result | |
8 | - * this is used to get around not being able to ensure callbacks are placed | |
9 | - * at the end of the chain. | |
10 | - * | |
11 | - * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our | |
12 | - * own events and placing ourselves at the end of the chain. | |
13 | - */ | |
14 | - triggerAndReturn: function (name, data) { | |
15 | - var event = new $.Event(name); | |
16 | - this.trigger(event, data); | |
17 | - | |
18 | - return event.result !== false; | |
19 | - }, | |
20 | - | |
21 | - /** | |
22 | - * Handles execution of remote calls firing overridable events along the way | |
23 | - */ | |
24 | - callRemote: function () { | |
25 | - var el = this, | |
26 | - method = el.attr('method') || el.attr('data-method') || 'GET', | |
27 | - url = el.attr('action') || el.attr('href'), | |
28 | - dataType = el.attr('data-type') || 'script'; | |
29 | - | |
30 | - if (url === undefined) { | |
31 | - throw "No URL specified for remote call (action or href must be present)."; | |
32 | - } else { | |
33 | - if (el.triggerAndReturn('ajax:before')) { | |
34 | - var data = el.is('form') ? el.serializeArray() : []; | |
35 | - $.ajax({ | |
36 | - url: url, | |
37 | - data: data, | |
38 | - dataType: dataType, | |
39 | - type: method.toUpperCase(), | |
40 | - beforeSend: function (xhr) { | |
41 | - el.trigger('ajax:loading', xhr); | |
42 | - }, | |
43 | - success: function (data, status, xhr) { | |
44 | - el.trigger('ajax:success', [data, status, xhr]); | |
45 | - }, | |
46 | - complete: function (xhr) { | |
47 | - el.trigger('ajax:complete', xhr); | |
48 | - }, | |
49 | - error: function (xhr, status, error) { | |
50 | - el.trigger('ajax:failure', [xhr, status, error]); | |
51 | - } | |
52 | - }); | |
53 | - } | |
54 | - | |
55 | - el.trigger('ajax:after'); | |
56 | - } | |
57 | - } | |
58 | - }); | |
59 | - | |
60 | - /** | |
61 | - * confirmation handler | |
62 | - */ | |
63 | - $('a[data-confirm],input[data-confirm]').live('click', function () { | |
64 | - var el = $(this); | |
65 | - if (el.triggerAndReturn('confirm')) { | |
66 | - if (!confirm(el.attr('data-confirm'))) { | |
67 | - return false; | |
68 | - } | |
69 | - } | |
70 | - }); | |
71 | - | |
72 | - | |
73 | - /** | |
74 | - * remote handlers | |
75 | - */ | |
76 | - $('form[data-remote]').live('submit', function (e) { | |
77 | - $(this).callRemote(); | |
78 | - e.preventDefault(); | |
79 | - }); | |
80 | - | |
81 | - $('a[data-remote],input[data-remote]').live('click', function (e) { | |
82 | - $(this).callRemote(); | |
83 | - e.preventDefault(); | |
84 | - }); | |
85 | - | |
86 | - $('a[data-method]:not([data-remote])').live('click', function (e){ | |
87 | - var link = $(this), | |
88 | - href = link.attr('href'), | |
89 | - method = link.attr('data-method'), | |
90 | - form = $('<form method="post" action="'+href+'"></form>'), | |
91 | - metadata_input = '<input name="_method" value="'+method+'" type="hidden" />'; | |
92 | - | |
93 | - if (csrf_param != null && csrf_token != null) { | |
94 | - metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />'; | |
95 | - } | |
96 | - | |
97 | - form.hide() | |
98 | - .append(metadata_input) | |
99 | - .appendTo('body'); | |
100 | - | |
101 | - e.preventDefault(); | |
102 | - form.submit(); | |
103 | - }); | |
104 | - | |
105 | - /** | |
106 | - * disable-with handlers | |
107 | - */ | |
108 | - var disable_with_input_selector = 'input[data-disable-with]'; | |
109 | - var disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')'; | |
110 | - var disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')'; | |
111 | - | |
112 | - var disable_with_input_function = function () { | |
113 | - $(this).find(disable_with_input_selector).each(function () { | |
114 | - var input = $(this); | |
115 | - input.data('enable-with', input.val()) | |
116 | - .attr('value', input.attr('data-disable-with')) | |
117 | - .attr('disabled', 'disabled'); | |
118 | - }); | |
119 | - }; | |
120 | - | |
121 | - $(disable_with_form_remote_selector).live('ajax:before', disable_with_input_function); | |
122 | - $(disable_with_form_not_remote_selector).live('submit', disable_with_input_function); | |
123 | - | |
124 | - $(disable_with_form_remote_selector).live('ajax:complete', function () { | |
125 | - $(this).find(disable_with_input_selector).each(function () { | |
126 | - var input = $(this); | |
127 | - input.removeAttr('disabled') | |
128 | - .val(input.data('enable-with')); | |
129 | - }); | |
130 | - }); | |
131 | - | |
132 | -}); | |
133 | 1 | \ No newline at end of file |
2 | +/** | |
3 | + * Unobtrusive scripting adapter for jQuery | |
4 | + * | |
5 | + * Requires jQuery 1.4.3 or later. | |
6 | + * https://github.com/rails/jquery-ujs | |
7 | + */ | |
8 | + | |
9 | +(function($) { | |
10 | + // Make sure that every Ajax request sends the CSRF token | |
11 | + function CSRFProtection(xhr) { | |
12 | + var token = $('meta[name="csrf-token"]').attr('content'); | |
13 | + if (token) xhr.setRequestHeader('X-CSRF-Token', token); | |
14 | + } | |
15 | + if ('ajaxPrefilter' in $) $.ajaxPrefilter(function(options, originalOptions, xhr){ CSRFProtection(xhr) }); | |
16 | + else $(document).ajaxSend(function(e, xhr){ CSRFProtection(xhr) }); | |
17 | + | |
18 | + // Triggers an event on an element and returns the event result | |
19 | + function fire(obj, name, data) { | |
20 | + var event = $.Event(name); | |
21 | + obj.trigger(event, data); | |
22 | + return event.result !== false; | |
23 | + } | |
24 | + | |
25 | + // Submits "remote" forms and links with ajax | |
26 | + function handleRemote(element) { | |
27 | + var method, url, data, | |
28 | + dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType); | |
29 | + | |
30 | + if (fire(element, 'ajax:before')) { | |
31 | + if (element.is('form')) { | |
32 | + method = element.attr('method'); | |
33 | + url = element.attr('action'); | |
34 | + data = element.serializeArray(); | |
35 | + // memoized value from clicked submit button | |
36 | + var button = element.data('ujs:submit-button'); | |
37 | + if (button) { | |
38 | + data.push(button); | |
39 | + element.data('ujs:submit-button', null); | |
40 | + } | |
41 | + } else { | |
42 | + method = element.data('method'); | |
43 | + url = element.attr('href'); | |
44 | + data = null; | |
45 | + } | |
46 | + $.ajax({ | |
47 | + url: url, type: method || 'GET', data: data, dataType: dataType, | |
48 | + // stopping the "ajax:beforeSend" event will cancel the ajax request | |
49 | + beforeSend: function(xhr, settings) { | |
50 | + if (settings.dataType === undefined) { | |
51 | + xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script); | |
52 | + } | |
53 | + return fire(element, 'ajax:beforeSend', [xhr, settings]); | |
54 | + }, | |
55 | + success: function(data, status, xhr) { | |
56 | + element.trigger('ajax:success', [data, status, xhr]); | |
57 | + }, | |
58 | + complete: function(xhr, status) { | |
59 | + element.trigger('ajax:complete', [xhr, status]); | |
60 | + }, | |
61 | + error: function(xhr, status, error) { | |
62 | + element.trigger('ajax:error', [xhr, status, error]); | |
63 | + } | |
64 | + }); | |
65 | + } | |
66 | + } | |
67 | + | |
68 | + // Handles "data-method" on links such as: | |
69 | + // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a> | |
70 | + function handleMethod(link) { | |
71 | + var href = link.attr('href'), | |
72 | + method = link.data('method'), | |
73 | + csrf_token = $('meta[name=csrf-token]').attr('content'), | |
74 | + csrf_param = $('meta[name=csrf-param]').attr('content'), | |
75 | + form = $('<form method="post" action="' + href + '"></form>'), | |
76 | + metadata_input = '<input name="_method" value="' + method + '" type="hidden" />'; | |
77 | + | |
78 | + if (csrf_param !== undefined && csrf_token !== undefined) { | |
79 | + metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />'; | |
80 | + } | |
81 | + | |
82 | + form.hide().append(metadata_input).appendTo('body'); | |
83 | + form.submit(); | |
84 | + } | |
85 | + | |
86 | + function disableFormElements(form) { | |
87 | + form.find('input[data-disable-with]').each(function() { | |
88 | + var input = $(this); | |
89 | + input.data('ujs:enable-with', input.val()) | |
90 | + .val(input.data('disable-with')) | |
91 | + .attr('disabled', 'disabled'); | |
92 | + }); | |
93 | + } | |
94 | + | |
95 | + function enableFormElements(form) { | |
96 | + form.find('input[data-disable-with]').each(function() { | |
97 | + var input = $(this); | |
98 | + input.val(input.data('ujs:enable-with')).removeAttr('disabled'); | |
99 | + }); | |
100 | + } | |
101 | + | |
102 | + function allowAction(element) { | |
103 | + var message = element.data('confirm'); | |
104 | + return !message || (fire(element, 'confirm') && confirm(message)); | |
105 | + } | |
106 | + | |
107 | + function requiredValuesMissing(form) { | |
108 | + var missing = false; | |
109 | + form.find('input[name][required]').each(function() { | |
110 | + if (!$(this).val()) missing = true; | |
111 | + }); | |
112 | + return missing; | |
113 | + } | |
114 | + | |
115 | + $('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) { | |
116 | + var link = $(this); | |
117 | + if (!allowAction(link)) return false; | |
118 | + | |
119 | + if (link.data('remote') != undefined) { | |
120 | + handleRemote(link); | |
121 | + return false; | |
122 | + } else if (link.data('method')) { | |
123 | + handleMethod(link); | |
124 | + return false; | |
125 | + } | |
126 | + }); | |
127 | + | |
128 | + $('form').live('submit.rails', function(e) { | |
129 | + var form = $(this), remote = form.data('remote') != undefined; | |
130 | + if (!allowAction(form)) return false; | |
131 | + | |
132 | + // skip other logic when required values are missing | |
133 | + if (requiredValuesMissing(form)) return !remote; | |
134 | + | |
135 | + if (remote) { | |
136 | + handleRemote(form); | |
137 | + return false; | |
138 | + } else { | |
139 | + // slight timeout so that the submit button gets properly serialized | |
140 | + setTimeout(function(){ disableFormElements(form) }, 13); | |
141 | + } | |
142 | + }); | |
143 | + | |
144 | + $('form input[type=submit], form button[type=submit], form button:not([type])').live('click.rails', function() { | |
145 | + var button = $(this); | |
146 | + if (!allowAction(button)) return false; | |
147 | + // register the pressed submit button | |
148 | + var name = button.attr('name'), data = name ? {name:name, value:button.val()} : null; | |
149 | + button.closest('form').data('ujs:submit-button', data); | |
150 | + }); | |
151 | + | |
152 | + $('form').live('ajax:beforeSend.rails', function(event) { | |
153 | + if (this == event.target) disableFormElements($(this)); | |
154 | + }); | |
155 | + | |
156 | + $('form').live('ajax:complete.rails', function(event) { | |
157 | + if (this == event.target) enableFormElements($(this)); | |
158 | + }); | |
159 | +})( jQuery ); | |
134 | 160 | \ No newline at end of file | ... | ... |