/
rails.js
196 lines (175 loc) · 6.82 KB
/
rails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Unobtrusive scripting adapter for jQuery
*
* 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($) {
// Make sure that every Ajax request sends the CSRF token
function CSRFProtection(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) {
var event = $.Event(name);
obj.trigger(event, data);
return event.result !== false;
}
// Submits "remote" forms and links with ajax
function handleRemote(element) {
var method, url, data,
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
if (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);
}
return 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:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
function handleMethod(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 = $('<form method="post" action="' + href + '"></form>'),
metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
if (csrf_param !== undefined && csrf_token !== undefined) {
metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
}
form.hide().append(metadata_input).appendTo('body');
form.submit();
}
function disableFormElements(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) {
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) {
var message = element.data('confirm');
return !message || (fire(element, 'confirm') && confirm(message));
}
function blankInputs(form, specifiedSelector) {
var blankExists = false,
selector = specifiedSelector || 'input';
form.find(selector).each(function() {
if (!$(this).val()) blankExists = true;
});
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) {
var link = $(this);
if (!allowAction(link)) return false;
if (link.data('remote') != undefined) {
handleRemote(link);
return false;
} else if (link.data('method')) {
handleMethod(link);
return false;
}
});
$('form').live('submit.rails', function(e) {
var form = $(this), remote = form.data('remote') != undefined;
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');
return !remote;
}
if (nonBlankInputs(form, 'input:file')) {
return fire(form, 'ajax:aborted:file');
}
if (remote) {
handleRemote(form);
return false;
} else {
// slight timeout so that the submit button gets properly serialized
setTimeout(function(){ 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 false;
// 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));
});
$('form').live('ajax:complete.rails', function(event) {
if (this == event.target) enableFormElements($(this));
});
})( jQuery );