rails-ujs 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +76 -0
- data/dist/rails-ujs.js +663 -0
- data/lib/rails-ujs.rb +7 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 20ba1a08ae29bc5d6be251b2e05d54a0accd97fc
|
4
|
+
data.tar.gz: 8e328e11b7ce2bfdc9cb1a2aa4c65459681a0910
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b42667a758e3b9caccd2e2f68d9c7530ceda2359385e704c547def21df94e52f52255e6b0a0a2c87e296f1a183765719f898e50d45c5259b8387e7ae9202ebbc
|
7
|
+
data.tar.gz: 80644a9ade1f819a410b6c06076bfdf533238352e0bb21612efa423d557dfc57407059e424b1327e6d2ac4e01509c9347b8f7b8938fd4e0d6ddbb7119f68fb57
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007-2016 Contributors at http://github.com/rails/rails-ujs/contributors
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
Ruby on Rails unobtrusive scripting adapter.
|
2
|
+
========================================
|
3
|
+
|
4
|
+
This unobtrusive scripting support file is developed for the Ruby on Rails framework, but is not strictly tied to any specific backend. You can drop this into any application to:
|
5
|
+
|
6
|
+
- force confirmation dialogs for various actions;
|
7
|
+
- make non-GET requests from hyperlinks;
|
8
|
+
- make forms or hyperlinks submit data asynchronously with Ajax;
|
9
|
+
- have submit buttons become automatically disabled on form submit to prevent double-clicking.
|
10
|
+
|
11
|
+
These features are achieved by adding certain ["data" attributes][data] to your HTML markup. In Rails, they are added by the framework's template helpers.
|
12
|
+
|
13
|
+
Requirements
|
14
|
+
------------
|
15
|
+
|
16
|
+
- HTML5 doctype (optional).
|
17
|
+
|
18
|
+
If you don't use HTML5, adding "data" attributes to your HTML4 or XHTML pages might make them fail [W3C markup validation][validator]. However, this shouldn't create any issues for web browsers or other user agents.
|
19
|
+
|
20
|
+
Installation using the rails-ujs gem
|
21
|
+
------------
|
22
|
+
|
23
|
+
For automated installation in Rails, use the "rails-ujs" gem. Place this in your Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'rails-ujs'
|
27
|
+
```
|
28
|
+
|
29
|
+
And run:
|
30
|
+
|
31
|
+
```shell
|
32
|
+
$ bundle install
|
33
|
+
```
|
34
|
+
|
35
|
+
Installation using Bower
|
36
|
+
------------
|
37
|
+
|
38
|
+
Run `bower install rails-ujs --save` to install the rails-ujs package.
|
39
|
+
|
40
|
+
Installation using npm
|
41
|
+
------------
|
42
|
+
|
43
|
+
Run `npm install rails-ujs --save` to install the rails-ujs package.
|
44
|
+
|
45
|
+
Installation using Yarn
|
46
|
+
------------
|
47
|
+
|
48
|
+
Run `yarn add rails-ujs` to install the rails-ujs package.
|
49
|
+
|
50
|
+
Usage
|
51
|
+
------------
|
52
|
+
|
53
|
+
Require `rails-ujs` into your application.js manifest.
|
54
|
+
|
55
|
+
```javascript
|
56
|
+
//= require rails-ujs
|
57
|
+
```
|
58
|
+
|
59
|
+
How to run tests
|
60
|
+
------------
|
61
|
+
|
62
|
+
Run `bundle exec rake test:server` first, and then run the web tests by visiting [[http://localhost:4567]] in your browser.
|
63
|
+
|
64
|
+
## Contributing to rails-ujs
|
65
|
+
|
66
|
+
rails-ujs is work of many contributors. You're encouraged to submit pull requests, propose
|
67
|
+
features and discuss issues.
|
68
|
+
|
69
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
70
|
+
|
71
|
+
## License
|
72
|
+
rails-ujs is released under the [MIT License](MIT-LICENSE).
|
73
|
+
|
74
|
+
[data]: http://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes "Embedding custom non-visible data with the data-* attributes"
|
75
|
+
[validator]: http://validator.w3.org/
|
76
|
+
[csrf]: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html
|
data/dist/rails-ujs.js
ADDED
@@ -0,0 +1,663 @@
|
|
1
|
+
(function() {
|
2
|
+
(function() {
|
3
|
+
(function() {
|
4
|
+
this.Rails = {
|
5
|
+
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote]:not([disabled]), a[data-disable-with], a[data-disable]',
|
6
|
+
buttonClickSelector: {
|
7
|
+
selector: 'button[data-remote]:not([form]), button[data-confirm]:not([form])',
|
8
|
+
exclude: 'form button'
|
9
|
+
},
|
10
|
+
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
|
11
|
+
formSubmitSelector: 'form',
|
12
|
+
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])',
|
13
|
+
formDisableSelector: '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',
|
14
|
+
formEnableSelector: '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',
|
15
|
+
fileInputSelector: 'input[name][type=file]:not([disabled])',
|
16
|
+
linkDisableSelector: 'a[data-disable-with], a[data-disable]',
|
17
|
+
buttonDisableSelector: 'button[data-remote][data-disable-with], button[data-remote][data-disable]'
|
18
|
+
};
|
19
|
+
|
20
|
+
}).call(this);
|
21
|
+
}).call(this);
|
22
|
+
|
23
|
+
var Rails = this.Rails;
|
24
|
+
|
25
|
+
(function() {
|
26
|
+
(function() {
|
27
|
+
var expando, m;
|
28
|
+
|
29
|
+
m = Element.prototype.matches || Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.webkitMatchesSelector;
|
30
|
+
|
31
|
+
Rails.matches = function(element, selector) {
|
32
|
+
if (selector.exclude != null) {
|
33
|
+
return m.call(element, selector.selector) && !m.call(element, selector.exclude);
|
34
|
+
} else {
|
35
|
+
return m.call(element, selector);
|
36
|
+
}
|
37
|
+
};
|
38
|
+
|
39
|
+
expando = '_ujsData';
|
40
|
+
|
41
|
+
Rails.getData = function(element, key) {
|
42
|
+
var ref;
|
43
|
+
return (ref = element[expando]) != null ? ref[key] : void 0;
|
44
|
+
};
|
45
|
+
|
46
|
+
Rails.setData = function(element, key, value) {
|
47
|
+
if (element[expando] == null) {
|
48
|
+
element[expando] = {};
|
49
|
+
}
|
50
|
+
return element[expando][key] = value;
|
51
|
+
};
|
52
|
+
|
53
|
+
Rails.$ = function(selector) {
|
54
|
+
return Array.prototype.slice.call(document.querySelectorAll(selector));
|
55
|
+
};
|
56
|
+
|
57
|
+
}).call(this);
|
58
|
+
(function() {
|
59
|
+
var $, csrfParam, csrfToken;
|
60
|
+
|
61
|
+
$ = Rails.$;
|
62
|
+
|
63
|
+
csrfToken = Rails.csrfToken = function() {
|
64
|
+
var meta;
|
65
|
+
meta = document.querySelector('meta[name=csrf-token]');
|
66
|
+
return meta && meta.content;
|
67
|
+
};
|
68
|
+
|
69
|
+
csrfParam = Rails.csrfParam = function() {
|
70
|
+
var meta;
|
71
|
+
meta = document.querySelector('meta[name=csrf-param]');
|
72
|
+
return meta && meta.content;
|
73
|
+
};
|
74
|
+
|
75
|
+
Rails.CSRFProtection = function(xhr) {
|
76
|
+
var token;
|
77
|
+
token = csrfToken();
|
78
|
+
if (token != null) {
|
79
|
+
return xhr.setRequestHeader('X-CSRF-Token', token);
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
Rails.refreshCSRFTokens = function() {
|
84
|
+
var param, token;
|
85
|
+
token = csrfToken();
|
86
|
+
param = csrfParam();
|
87
|
+
if ((token != null) && (param != null)) {
|
88
|
+
return $('form input[name="' + param + '"]').forEach(function(input) {
|
89
|
+
return input.value = token;
|
90
|
+
});
|
91
|
+
}
|
92
|
+
};
|
93
|
+
|
94
|
+
}).call(this);
|
95
|
+
(function() {
|
96
|
+
var CustomEvent, fire, matches;
|
97
|
+
|
98
|
+
matches = Rails.matches;
|
99
|
+
|
100
|
+
CustomEvent = window.CustomEvent;
|
101
|
+
|
102
|
+
if (typeof CustomEvent !== 'function') {
|
103
|
+
CustomEvent = function(event, params) {
|
104
|
+
var evt;
|
105
|
+
evt = document.createEvent('CustomEvent');
|
106
|
+
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
|
107
|
+
return evt;
|
108
|
+
};
|
109
|
+
CustomEvent.prototype = window.Event.prototype;
|
110
|
+
}
|
111
|
+
|
112
|
+
fire = Rails.fire = function(obj, name, data) {
|
113
|
+
var event;
|
114
|
+
event = new CustomEvent(name, {
|
115
|
+
bubbles: true,
|
116
|
+
cancelable: true,
|
117
|
+
detail: data
|
118
|
+
});
|
119
|
+
obj.dispatchEvent(event);
|
120
|
+
return !event.defaultPrevented;
|
121
|
+
};
|
122
|
+
|
123
|
+
Rails.stopEverything = function(e) {
|
124
|
+
fire(e.target, 'ujs:everythingStopped');
|
125
|
+
e.preventDefault();
|
126
|
+
e.stopPropagation();
|
127
|
+
return e.stopImmediatePropagation();
|
128
|
+
};
|
129
|
+
|
130
|
+
Rails.delegate = function(element, selector, eventType, handler) {
|
131
|
+
return element.addEventListener(eventType, function(e) {
|
132
|
+
var target;
|
133
|
+
target = e.target;
|
134
|
+
while (!(!(target instanceof Element) || matches(target, selector))) {
|
135
|
+
target = target.parentNode;
|
136
|
+
}
|
137
|
+
if (target instanceof Element && handler.call(target, e) === false) {
|
138
|
+
e.preventDefault();
|
139
|
+
return e.stopPropagation();
|
140
|
+
}
|
141
|
+
});
|
142
|
+
};
|
143
|
+
|
144
|
+
}).call(this);
|
145
|
+
(function() {
|
146
|
+
var AcceptHeaders, CSRFProtection, createXHR, fire, prepareOptions, processResponse;
|
147
|
+
|
148
|
+
CSRFProtection = Rails.CSRFProtection, fire = Rails.fire;
|
149
|
+
|
150
|
+
AcceptHeaders = {
|
151
|
+
'*': '*/*',
|
152
|
+
text: 'text/plain',
|
153
|
+
html: 'text/html',
|
154
|
+
xml: 'application/xml, text/xml',
|
155
|
+
json: 'application/json, text/javascript',
|
156
|
+
script: 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript'
|
157
|
+
};
|
158
|
+
|
159
|
+
Rails.ajax = function(options) {
|
160
|
+
var xhr;
|
161
|
+
options = prepareOptions(options);
|
162
|
+
xhr = createXHR(options, function() {
|
163
|
+
var response;
|
164
|
+
response = processResponse(xhr.response, xhr.getResponseHeader('Content-Type'));
|
165
|
+
if (Math.floor(xhr.status / 100) === 2) {
|
166
|
+
if (typeof options.success === "function") {
|
167
|
+
options.success(response, xhr.statusText, xhr);
|
168
|
+
}
|
169
|
+
} else {
|
170
|
+
if (typeof options.error === "function") {
|
171
|
+
options.error(response, xhr.statusText, xhr);
|
172
|
+
}
|
173
|
+
}
|
174
|
+
return typeof options.complete === "function" ? options.complete(xhr, xhr.statusText) : void 0;
|
175
|
+
});
|
176
|
+
if (typeof options.beforeSend === "function") {
|
177
|
+
options.beforeSend(xhr, options);
|
178
|
+
}
|
179
|
+
if (xhr.readyState === XMLHttpRequest.OPENED) {
|
180
|
+
return xhr.send(options.data);
|
181
|
+
} else {
|
182
|
+
return fire(document, 'ajaxStop');
|
183
|
+
}
|
184
|
+
};
|
185
|
+
|
186
|
+
prepareOptions = function(options) {
|
187
|
+
options.type = options.type.toUpperCase();
|
188
|
+
if (options.type === 'GET' && options.data) {
|
189
|
+
if (options.url.indexOf('?') < 0) {
|
190
|
+
options.url += '?' + options.data;
|
191
|
+
} else {
|
192
|
+
options.url += '&' + options.data;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
if (AcceptHeaders[options.dataType] == null) {
|
196
|
+
options.dataType = '*';
|
197
|
+
}
|
198
|
+
options.accept = AcceptHeaders[options.dataType];
|
199
|
+
if (options.dataType !== '*') {
|
200
|
+
options.accept += ', */*; q=0.01';
|
201
|
+
}
|
202
|
+
return options;
|
203
|
+
};
|
204
|
+
|
205
|
+
createXHR = function(options, done) {
|
206
|
+
var xhr;
|
207
|
+
xhr = new XMLHttpRequest();
|
208
|
+
xhr.open(options.type, options.url, true);
|
209
|
+
xhr.setRequestHeader('Accept', options.accept);
|
210
|
+
if (typeof options.data === 'string') {
|
211
|
+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
212
|
+
}
|
213
|
+
if (!options.crossDomain) {
|
214
|
+
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
215
|
+
}
|
216
|
+
CSRFProtection(xhr);
|
217
|
+
xhr.withCredentials = !!options.withCredentials;
|
218
|
+
xhr.onreadystatechange = function() {
|
219
|
+
if (xhr.readyState === XMLHttpRequest.DONE) {
|
220
|
+
return done(xhr);
|
221
|
+
}
|
222
|
+
};
|
223
|
+
return xhr;
|
224
|
+
};
|
225
|
+
|
226
|
+
processResponse = function(response, type) {
|
227
|
+
var parser, script;
|
228
|
+
if (typeof response === 'string' && typeof type === 'string') {
|
229
|
+
if (type.match(/\bjson\b/)) {
|
230
|
+
try {
|
231
|
+
response = JSON.parse(response);
|
232
|
+
} catch (undefined) {}
|
233
|
+
} else if (type.match(/\bjavascript\b/)) {
|
234
|
+
script = document.createElement('script');
|
235
|
+
script.innerHTML = response;
|
236
|
+
document.body.appendChild(script);
|
237
|
+
} else if (type.match(/\b(xml|html|svg)\b/)) {
|
238
|
+
parser = new DOMParser();
|
239
|
+
type = type.replace(/;.+/, '');
|
240
|
+
try {
|
241
|
+
response = parser.parseFromString(response, type);
|
242
|
+
} catch (undefined) {}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
return response;
|
246
|
+
};
|
247
|
+
|
248
|
+
Rails.href = function(element) {
|
249
|
+
return element.href;
|
250
|
+
};
|
251
|
+
|
252
|
+
Rails.isCrossDomain = function(url) {
|
253
|
+
var e, error, originAnchor, urlAnchor;
|
254
|
+
originAnchor = document.createElement('a');
|
255
|
+
originAnchor.href = location.href;
|
256
|
+
urlAnchor = document.createElement('a');
|
257
|
+
try {
|
258
|
+
urlAnchor.href = url;
|
259
|
+
return !(((!urlAnchor.protocol || urlAnchor.protocol === ':') && !urlAnchor.host) || (originAnchor.protocol + '//' + originAnchor.host === urlAnchor.protocol + '//' + urlAnchor.host));
|
260
|
+
} catch (error) {
|
261
|
+
e = error;
|
262
|
+
return true;
|
263
|
+
}
|
264
|
+
};
|
265
|
+
|
266
|
+
}).call(this);
|
267
|
+
(function() {
|
268
|
+
var matches, toArray;
|
269
|
+
|
270
|
+
matches = Rails.matches;
|
271
|
+
|
272
|
+
toArray = function(e) {
|
273
|
+
return Array.prototype.slice.call(e);
|
274
|
+
};
|
275
|
+
|
276
|
+
Rails.serializeElement = function(element, additionalParam) {
|
277
|
+
var inputs, params;
|
278
|
+
inputs = [element];
|
279
|
+
if (matches(element, 'form')) {
|
280
|
+
inputs = toArray(element.elements);
|
281
|
+
}
|
282
|
+
params = [];
|
283
|
+
inputs.forEach(function(input) {
|
284
|
+
if (!input.name) {
|
285
|
+
return;
|
286
|
+
}
|
287
|
+
if (matches(input, 'select')) {
|
288
|
+
return toArray(input.options).forEach(function(option) {
|
289
|
+
if (option.selected) {
|
290
|
+
return params.push({
|
291
|
+
name: input.name,
|
292
|
+
value: option.value
|
293
|
+
});
|
294
|
+
}
|
295
|
+
});
|
296
|
+
} else if (input.checked || ['radio', 'checkbox', 'submit'].indexOf(input.type) === -1) {
|
297
|
+
return params.push({
|
298
|
+
name: input.name,
|
299
|
+
value: input.value
|
300
|
+
});
|
301
|
+
}
|
302
|
+
});
|
303
|
+
if (additionalParam) {
|
304
|
+
params.push(additionalParam);
|
305
|
+
}
|
306
|
+
return params.map(function(param) {
|
307
|
+
if (param.name != null) {
|
308
|
+
return (encodeURIComponent(param.name)) + "=" + (encodeURIComponent(param.value));
|
309
|
+
} else {
|
310
|
+
return param;
|
311
|
+
}
|
312
|
+
}).join('&');
|
313
|
+
};
|
314
|
+
|
315
|
+
Rails.formElements = function(form, selector) {
|
316
|
+
if (matches(form, 'form')) {
|
317
|
+
return toArray(form.elements).filter(function(el) {
|
318
|
+
return matches(el, selector);
|
319
|
+
});
|
320
|
+
} else {
|
321
|
+
return toArray(form.querySelectorAll(selector));
|
322
|
+
}
|
323
|
+
};
|
324
|
+
|
325
|
+
}).call(this);
|
326
|
+
(function() {
|
327
|
+
var allowAction, fire, stopEverything;
|
328
|
+
|
329
|
+
fire = Rails.fire, stopEverything = Rails.stopEverything;
|
330
|
+
|
331
|
+
Rails.handleConfirm = function(e) {
|
332
|
+
if (!allowAction(this)) {
|
333
|
+
return stopEverything(e);
|
334
|
+
}
|
335
|
+
};
|
336
|
+
|
337
|
+
allowAction = function(element) {
|
338
|
+
var answer, callback, message;
|
339
|
+
message = element.getAttribute('data-confirm');
|
340
|
+
if (!message) {
|
341
|
+
return true;
|
342
|
+
}
|
343
|
+
answer = false;
|
344
|
+
if (fire(element, 'confirm')) {
|
345
|
+
try {
|
346
|
+
answer = confirm(message);
|
347
|
+
} catch (undefined) {}
|
348
|
+
callback = fire(element, 'confirm:complete', [answer]);
|
349
|
+
}
|
350
|
+
return answer && callback;
|
351
|
+
};
|
352
|
+
|
353
|
+
}).call(this);
|
354
|
+
(function() {
|
355
|
+
var disableFormElement, disableFormElements, disableLinkElement, enableFormElement, enableFormElements, enableLinkElement, formElements, getData, matches, setData, stopEverything;
|
356
|
+
|
357
|
+
matches = Rails.matches, getData = Rails.getData, setData = Rails.setData, stopEverything = Rails.stopEverything, formElements = Rails.formElements;
|
358
|
+
|
359
|
+
Rails.enableElement = function(e) {
|
360
|
+
var element;
|
361
|
+
element = e instanceof Event ? e.target : e;
|
362
|
+
if (matches(element, Rails.linkDisableSelector)) {
|
363
|
+
return enableLinkElement(element);
|
364
|
+
} else if (matches(element, Rails.buttonDisableSelector) || matches(element, Rails.formEnableSelector)) {
|
365
|
+
return enableFormElement(element);
|
366
|
+
} else if (matches(element, Rails.formSubmitSelector)) {
|
367
|
+
return enableFormElements(element);
|
368
|
+
}
|
369
|
+
};
|
370
|
+
|
371
|
+
Rails.disableElement = function(e) {
|
372
|
+
var element;
|
373
|
+
element = e instanceof Event ? e.target : e;
|
374
|
+
if (matches(element, Rails.linkDisableSelector)) {
|
375
|
+
return disableLinkElement(element);
|
376
|
+
} else if (matches(element, Rails.buttonDisableSelector) || matches(element, Rails.formDisableSelector)) {
|
377
|
+
return disableFormElement(element);
|
378
|
+
} else if (matches(element, Rails.formSubmitSelector)) {
|
379
|
+
return disableFormElements(element);
|
380
|
+
}
|
381
|
+
};
|
382
|
+
|
383
|
+
disableLinkElement = function(element) {
|
384
|
+
var replacement;
|
385
|
+
replacement = element.getAttribute('data-disable-with');
|
386
|
+
if (replacement != null) {
|
387
|
+
setData(element, 'ujs:enable-with', element.innerHTML);
|
388
|
+
element.innerHTML = replacement;
|
389
|
+
}
|
390
|
+
element.addEventListener('click', stopEverything);
|
391
|
+
return setData(element, 'ujs:disabled', true);
|
392
|
+
};
|
393
|
+
|
394
|
+
enableLinkElement = function(element) {
|
395
|
+
var originalText;
|
396
|
+
originalText = getData(element, 'ujs:enable-with');
|
397
|
+
if (originalText != null) {
|
398
|
+
element.innerHTML = originalText;
|
399
|
+
setData(element, 'ujs:enable-with', null);
|
400
|
+
}
|
401
|
+
element.removeEventListener('click', stopEverything);
|
402
|
+
return setData(element, 'ujs:disabled', null);
|
403
|
+
};
|
404
|
+
|
405
|
+
disableFormElements = function(form) {
|
406
|
+
return formElements(form, Rails.formDisableSelector).forEach(disableFormElement);
|
407
|
+
};
|
408
|
+
|
409
|
+
disableFormElement = function(element) {
|
410
|
+
var replacement;
|
411
|
+
replacement = element.getAttribute('data-disable-with');
|
412
|
+
if (replacement != null) {
|
413
|
+
if (matches(element, 'button')) {
|
414
|
+
setData(element, 'ujs:enable-with', element.innerHTML);
|
415
|
+
element.innerHTML = replacement;
|
416
|
+
} else {
|
417
|
+
setData(element, 'ujs:enable-with', element.value);
|
418
|
+
element.value = replacement;
|
419
|
+
}
|
420
|
+
}
|
421
|
+
element.disabled = true;
|
422
|
+
return setData(element, 'ujs:disabled', true);
|
423
|
+
};
|
424
|
+
|
425
|
+
enableFormElements = function(form) {
|
426
|
+
return formElements(form, Rails.formEnableSelector).forEach(enableFormElement);
|
427
|
+
};
|
428
|
+
|
429
|
+
enableFormElement = function(element) {
|
430
|
+
var originalText;
|
431
|
+
originalText = getData(element, 'ujs:enable-with');
|
432
|
+
if (originalText != null) {
|
433
|
+
if (matches(element, 'button')) {
|
434
|
+
element.innerHTML = originalText;
|
435
|
+
} else {
|
436
|
+
element.value = originalText;
|
437
|
+
}
|
438
|
+
setData(element, 'ujs:enable-with', null);
|
439
|
+
}
|
440
|
+
element.disabled = false;
|
441
|
+
return setData(element, 'ujs:disabled', null);
|
442
|
+
};
|
443
|
+
|
444
|
+
}).call(this);
|
445
|
+
(function() {
|
446
|
+
var stopEverything;
|
447
|
+
|
448
|
+
stopEverything = Rails.stopEverything;
|
449
|
+
|
450
|
+
Rails.handleMethod = function(e) {
|
451
|
+
var csrfParam, csrfToken, form, formContent, href, link, method;
|
452
|
+
link = this;
|
453
|
+
method = link.getAttribute('data-method');
|
454
|
+
if (!method) {
|
455
|
+
return;
|
456
|
+
}
|
457
|
+
href = Rails.href(link);
|
458
|
+
csrfToken = Rails.csrfToken();
|
459
|
+
csrfParam = Rails.csrfParam();
|
460
|
+
form = document.createElement('form');
|
461
|
+
formContent = "<input name='_method' value='" + method + "' type='hidden' />";
|
462
|
+
if ((csrfParam != null) && (csrfToken != null) && !Rails.isCrossDomain(href)) {
|
463
|
+
formContent += "<input name='" + csrfParam + "' value='" + csrfToken + "' type='hidden' />";
|
464
|
+
}
|
465
|
+
formContent += '<input type="submit" />';
|
466
|
+
form.method = 'post';
|
467
|
+
form.action = href;
|
468
|
+
form.target = link.target;
|
469
|
+
form.innerHTML = formContent;
|
470
|
+
form.style.display = 'none';
|
471
|
+
document.body.appendChild(form);
|
472
|
+
form.querySelector('[type="submit"]').click();
|
473
|
+
return stopEverything(e);
|
474
|
+
};
|
475
|
+
|
476
|
+
}).call(this);
|
477
|
+
(function() {
|
478
|
+
var ajax, fire, getData, isCrossDomain, isRemote, matches, serializeElement, setData, stopEverything,
|
479
|
+
slice = [].slice;
|
480
|
+
|
481
|
+
matches = Rails.matches, getData = Rails.getData, setData = Rails.setData, fire = Rails.fire, stopEverything = Rails.stopEverything, ajax = Rails.ajax, isCrossDomain = Rails.isCrossDomain, serializeElement = Rails.serializeElement;
|
482
|
+
|
483
|
+
isRemote = function(element) {
|
484
|
+
var value;
|
485
|
+
value = element.getAttribute('data-remote');
|
486
|
+
return (value != null) && value !== 'false';
|
487
|
+
};
|
488
|
+
|
489
|
+
Rails.handleRemote = function(e) {
|
490
|
+
var button, data, dataType, element, method, url, withCredentials;
|
491
|
+
element = this;
|
492
|
+
if (!isRemote(element)) {
|
493
|
+
return true;
|
494
|
+
}
|
495
|
+
if (!fire(element, 'ajax:before')) {
|
496
|
+
fire(element, 'ajax:stopped');
|
497
|
+
return false;
|
498
|
+
}
|
499
|
+
withCredentials = element.getAttribute('data-with-credentials');
|
500
|
+
dataType = element.getAttribute('data-type') || 'script';
|
501
|
+
if (matches(element, Rails.formSubmitSelector)) {
|
502
|
+
button = getData(element, 'ujs:submit-button');
|
503
|
+
method = getData(element, 'ujs:submit-button-formmethod') || element.method;
|
504
|
+
url = getData(element, 'ujs:submit-button-formaction') || element.getAttribute('action') || location.href;
|
505
|
+
if (method.toUpperCase() === 'GET') {
|
506
|
+
url = url.replace(/\?.*$/, '');
|
507
|
+
}
|
508
|
+
if (element.enctype === 'multipart/form-data') {
|
509
|
+
data = new FormData(element);
|
510
|
+
if (button != null) {
|
511
|
+
data.append(button.name, button.value);
|
512
|
+
}
|
513
|
+
} else {
|
514
|
+
data = serializeElement(element, button);
|
515
|
+
}
|
516
|
+
setData(element, 'ujs:submit-button', null);
|
517
|
+
setData(element, 'ujs:submit-button-formmethod', null);
|
518
|
+
setData(element, 'ujs:submit-button-formaction', null);
|
519
|
+
} else if (matches(element, Rails.buttonClickSelector) || matches(element, Rails.inputChangeSelector)) {
|
520
|
+
method = element.getAttribute('data-method');
|
521
|
+
url = element.getAttribute('data-url');
|
522
|
+
data = serializeElement(element, element.getAttribute('data-params'));
|
523
|
+
} else {
|
524
|
+
method = element.getAttribute('data-method');
|
525
|
+
url = Rails.href(element);
|
526
|
+
data = element.getAttribute('data-params');
|
527
|
+
}
|
528
|
+
ajax({
|
529
|
+
type: method || 'GET',
|
530
|
+
url: url,
|
531
|
+
data: data,
|
532
|
+
dataType: dataType,
|
533
|
+
beforeSend: function(xhr, options) {
|
534
|
+
if (fire(element, 'ajax:beforeSend', [xhr, options])) {
|
535
|
+
return fire(element, 'ajax:send', [xhr]);
|
536
|
+
} else {
|
537
|
+
fire(element, 'ajax:stopped');
|
538
|
+
return xhr.abort();
|
539
|
+
}
|
540
|
+
},
|
541
|
+
success: function() {
|
542
|
+
var args;
|
543
|
+
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
544
|
+
return fire(element, 'ajax:success', args);
|
545
|
+
},
|
546
|
+
error: function() {
|
547
|
+
var args;
|
548
|
+
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
549
|
+
return fire(element, 'ajax:error', args);
|
550
|
+
},
|
551
|
+
complete: function() {
|
552
|
+
var args;
|
553
|
+
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
554
|
+
return fire(element, 'ajax:complete', args);
|
555
|
+
},
|
556
|
+
crossDomain: isCrossDomain(url),
|
557
|
+
withCredentials: (withCredentials != null) && withCredentials !== 'false'
|
558
|
+
});
|
559
|
+
return stopEverything(e);
|
560
|
+
};
|
561
|
+
|
562
|
+
Rails.formSubmitButtonClick = function(e) {
|
563
|
+
var button, form;
|
564
|
+
button = this;
|
565
|
+
form = button.form;
|
566
|
+
if (!form) {
|
567
|
+
return;
|
568
|
+
}
|
569
|
+
if (button.name) {
|
570
|
+
setData(form, 'ujs:submit-button', {
|
571
|
+
name: button.name,
|
572
|
+
value: button.value
|
573
|
+
});
|
574
|
+
}
|
575
|
+
setData(form, 'ujs:formnovalidate-button', button.formNoValidate);
|
576
|
+
setData(form, 'ujs:submit-button-formaction', button.getAttribute('formaction'));
|
577
|
+
return setData(form, 'ujs:submit-button-formmethod', button.getAttribute('formmethod'));
|
578
|
+
};
|
579
|
+
|
580
|
+
Rails.handleMetaClick = function(e) {
|
581
|
+
var data, link, metaClick, method;
|
582
|
+
link = this;
|
583
|
+
method = (link.getAttribute('data-method') || 'GET').toUpperCase();
|
584
|
+
data = link.getAttribute('data-params');
|
585
|
+
metaClick = e.metaKey || e.ctrlKey;
|
586
|
+
if (metaClick && method === 'GET' && !data) {
|
587
|
+
return e.stopImmediatePropagation();
|
588
|
+
}
|
589
|
+
};
|
590
|
+
|
591
|
+
}).call(this);
|
592
|
+
(function() {
|
593
|
+
var $, CSRFProtection, delegate, disableElement, enableElement, fire, formSubmitButtonClick, getData, handleConfirm, handleMetaClick, handleMethod, handleRemote, refreshCSRFTokens;
|
594
|
+
|
595
|
+
fire = Rails.fire, delegate = Rails.delegate, getData = Rails.getData, $ = Rails.$, refreshCSRFTokens = Rails.refreshCSRFTokens, CSRFProtection = Rails.CSRFProtection, enableElement = Rails.enableElement, disableElement = Rails.disableElement, handleConfirm = Rails.handleConfirm, handleRemote = Rails.handleRemote, formSubmitButtonClick = Rails.formSubmitButtonClick, handleMetaClick = Rails.handleMetaClick, handleMethod = Rails.handleMethod;
|
596
|
+
|
597
|
+
if ((typeof jQuery !== "undefined" && jQuery !== null) && !jQuery.rails) {
|
598
|
+
jQuery.rails = Rails;
|
599
|
+
jQuery.ajaxPrefilter(function(options, originalOptions, xhr) {
|
600
|
+
if (!options.crossDomain) {
|
601
|
+
return CSRFProtection(xhr);
|
602
|
+
}
|
603
|
+
});
|
604
|
+
}
|
605
|
+
|
606
|
+
Rails.start = function() {
|
607
|
+
if (window._rails_loaded) {
|
608
|
+
throw new Error('rails-ujs has already been loaded!');
|
609
|
+
}
|
610
|
+
window.addEventListener('pageshow', function() {
|
611
|
+
$(Rails.formEnableSelector).forEach(function(el) {
|
612
|
+
if (getData(el, 'ujs:disabled')) {
|
613
|
+
return enableElement(el);
|
614
|
+
}
|
615
|
+
});
|
616
|
+
return $(Rails.linkDisableSelector).forEach(function(el) {
|
617
|
+
if (getData(el, 'ujs:disabled')) {
|
618
|
+
return enableElement(el);
|
619
|
+
}
|
620
|
+
});
|
621
|
+
});
|
622
|
+
delegate(document, Rails.linkDisableSelector, 'ajax:complete', enableElement);
|
623
|
+
delegate(document, Rails.linkDisableSelector, 'ajax:stopped', enableElement);
|
624
|
+
delegate(document, Rails.buttonDisableSelector, 'ajax:complete', enableElement);
|
625
|
+
delegate(document, Rails.buttonDisableSelector, 'ajax:stopped', enableElement);
|
626
|
+
delegate(document, Rails.linkClickSelector, 'click', handleConfirm);
|
627
|
+
delegate(document, Rails.linkClickSelector, 'click', handleMetaClick);
|
628
|
+
delegate(document, Rails.linkClickSelector, 'click', disableElement);
|
629
|
+
delegate(document, Rails.linkClickSelector, 'click', handleRemote);
|
630
|
+
delegate(document, Rails.linkClickSelector, 'click', handleMethod);
|
631
|
+
delegate(document, Rails.buttonClickSelector, 'click', handleConfirm);
|
632
|
+
delegate(document, Rails.buttonClickSelector, 'click', disableElement);
|
633
|
+
delegate(document, Rails.buttonClickSelector, 'click', handleRemote);
|
634
|
+
delegate(document, Rails.inputChangeSelector, 'change', handleConfirm);
|
635
|
+
delegate(document, Rails.inputChangeSelector, 'change', handleRemote);
|
636
|
+
delegate(document, Rails.formSubmitSelector, 'submit', handleConfirm);
|
637
|
+
delegate(document, Rails.formSubmitSelector, 'submit', handleRemote);
|
638
|
+
delegate(document, Rails.formSubmitSelector, 'submit', function(e) {
|
639
|
+
return setTimeout((function() {
|
640
|
+
return disableElement(e);
|
641
|
+
}), 13);
|
642
|
+
});
|
643
|
+
delegate(document, Rails.formSubmitSelector, 'ajax:send', disableElement);
|
644
|
+
delegate(document, Rails.formSubmitSelector, 'ajax:complete', enableElement);
|
645
|
+
delegate(document, Rails.formInputClickSelector, 'click', handleConfirm);
|
646
|
+
delegate(document, Rails.formInputClickSelector, 'click', formSubmitButtonClick);
|
647
|
+
document.addEventListener('DOMContentLoaded', refreshCSRFTokens);
|
648
|
+
return window._rails_loaded = true;
|
649
|
+
};
|
650
|
+
|
651
|
+
if (window.Rails === Rails && fire(document, 'rails:attachBindings')) {
|
652
|
+
Rails.start();
|
653
|
+
}
|
654
|
+
|
655
|
+
}).call(this);
|
656
|
+
}).call(this);
|
657
|
+
|
658
|
+
if (typeof module === "object" && module.exports) {
|
659
|
+
module.exports = Rails;
|
660
|
+
} else if (typeof define === "function" && define.amd) {
|
661
|
+
define(Rails);
|
662
|
+
}
|
663
|
+
}).call(this);
|
data/lib/rails-ujs.rb
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-ujs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Heinemeier Hansson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
description: This gem provides rails-ujs driver for Rails applications.
|
28
|
+
email: david@loudthinking.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README.md
|
35
|
+
- dist/rails-ujs.js
|
36
|
+
- lib/rails-ujs.rb
|
37
|
+
homepage: https://github.com/rails/rails-ujs
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '2.0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.6.10
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Ruby on Rails unobtrusive scripting adapter.
|
61
|
+
test_files: []
|