vanilla-ujs 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +18 -0
- data/lib/vanilla-ujs.rb +4 -0
- data/vanilla-ujs.gemspec +18 -0
- data/vendor/assets/javascripts/src/confirm.js +17 -0
- data/vendor/assets/javascripts/src/csrf.js +52 -0
- data/vendor/assets/javascripts/src/liteajax.js +53 -0
- data/vendor/assets/javascripts/src/method.js +67 -0
- data/vendor/assets/javascripts/src/polyfills.js +18 -0
- data/vendor/assets/javascripts/vanilla-ujs.js +1 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19fc8b7fed4191579e3470edff79f673e1ca50b7
|
4
|
+
data.tar.gz: 46d027b35b3132dcd45ed13afbfd3e88707e9b40
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d0467d38e7eb42108bd019429767df979663c14dc529a97e00d73d1283c0ed004bc1d4b67f66e26716722da39413305c7362638e2299f68cc1679544fc731718
|
7
|
+
data.tar.gz: 279ca95cea0c444d65fd89a899bc21ede487f13792709df9984979cb4b9bed743b2207c0e49705f596e3a3be423874bd7b55a9de57fd6816a61540d7f75ded4d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Tsokurov Alex
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Vanilla UJS
|
2
|
+
|
3
|
+
It is implementation of Rails jQuery UJS in pure JavaScript. Based on [code of Łukasz Niemier](http://github.com/hauleth/vanilla-ujs).
|
4
|
+
No extra dependencies.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
_Vanilla UJS_ is meant to work as a Rails plugin. To install it in your current application, add the following to your `Gemfile`:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'vanilla-ujs'
|
12
|
+
```
|
13
|
+
|
14
|
+
The _Vanilla UJS_ files will be added to the asset pipeline and available for you to use. Just add these lines in `app/assets/javascripts/application.js`:
|
15
|
+
|
16
|
+
```js
|
17
|
+
//= require vanilla-ujs
|
18
|
+
```
|
data/lib/vanilla-ujs.rb
ADDED
data/vanilla-ujs.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "vanilla-ujs"
|
5
|
+
s.version = "0.1.1"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["Alex Tsokurov", "Łukasz Niemier"]
|
8
|
+
s.email = ["me@ximik.net"]
|
9
|
+
s.homepage = "https://github.com/Ximik/vanilla-ujs"
|
10
|
+
s.summary = "UJS without jQuery dependency"
|
11
|
+
s.description = "This gem provides Rails UJS features without jQuery library."
|
12
|
+
s.license = "MIT"
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.require_path = 'lib'
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
document.addEventListener('click', function (event) {
|
2
|
+
var message, element;
|
3
|
+
|
4
|
+
element = event.target;
|
5
|
+
|
6
|
+
if (matches.call(element, 'a[data-confirm], button[data-confirm]')) {
|
7
|
+
message = element.getAttribute('data-confirm');
|
8
|
+
if (!confirm(message)) {
|
9
|
+
event.stopPropagation();
|
10
|
+
event.stopImmediatePropagation();
|
11
|
+
event.preventDefault();
|
12
|
+
return false;
|
13
|
+
}
|
14
|
+
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
}, false);
|
@@ -0,0 +1,52 @@
|
|
1
|
+
var CSRF = {
|
2
|
+
token: function () {
|
3
|
+
var token = document.querySelector('meta[name="csrf-token"]');
|
4
|
+
return token && token.getAttribute('content');
|
5
|
+
},
|
6
|
+
param: function () {
|
7
|
+
var param = document.querySelector('meta[name="csrf-param"]');
|
8
|
+
return param && param.getAttribute('content');
|
9
|
+
}
|
10
|
+
};
|
11
|
+
|
12
|
+
var sameOrigin = function (url) {
|
13
|
+
var a = document.create('a'), origin;
|
14
|
+
a.href = url;
|
15
|
+
origin = a.href.split('/', 3).join('/');
|
16
|
+
|
17
|
+
return window.location.href.indexOf(origin) === 0;
|
18
|
+
};
|
19
|
+
|
20
|
+
window.CSRF = CSRF;
|
21
|
+
|
22
|
+
document.addEventListener('ajax:before', function (e) {
|
23
|
+
var token = CSRF.token(), xhr = e.detail;
|
24
|
+
if (token)
|
25
|
+
xhr.setRequestHeader('X-CSRF-Token', token);
|
26
|
+
});
|
27
|
+
|
28
|
+
document.addEventListener('submit', function (e) {
|
29
|
+
var token = CSRF.token(),
|
30
|
+
param = CSRF.param(),
|
31
|
+
form = e.target;
|
32
|
+
|
33
|
+
if (matches.call(form, 'form')) {
|
34
|
+
if (matches.call(form, 'form[data-remote]'))
|
35
|
+
return true;
|
36
|
+
if (!form.method || form.method.toUpperCase() == 'GET')
|
37
|
+
return true;
|
38
|
+
if (!sameOrigin(form.action))
|
39
|
+
return true;
|
40
|
+
|
41
|
+
if (param && token && !form.querySelector('input[name='+param+']')) {
|
42
|
+
var input = document.createElement('input');
|
43
|
+
input.setAttribute('type', 'hidden');
|
44
|
+
input.setAttribute('name', param);
|
45
|
+
input.setAttribute('value', token);
|
46
|
+
|
47
|
+
form.appendChild(input);
|
48
|
+
}
|
49
|
+
|
50
|
+
return true;
|
51
|
+
}
|
52
|
+
});
|
@@ -0,0 +1,53 @@
|
|
1
|
+
var LiteAjax = (function () {
|
2
|
+
var LiteAjax = {};
|
3
|
+
|
4
|
+
LiteAjax.options = {
|
5
|
+
method: 'GET',
|
6
|
+
url: window.location.href,
|
7
|
+
async: true,
|
8
|
+
};
|
9
|
+
|
10
|
+
LiteAjax.ajax = function (url, options) {
|
11
|
+
if (typeof url == 'object') {
|
12
|
+
options = url;
|
13
|
+
url = undefined;
|
14
|
+
}
|
15
|
+
|
16
|
+
options = options || {};
|
17
|
+
url = url || options.url || location.href || '';
|
18
|
+
|
19
|
+
var xhr;
|
20
|
+
|
21
|
+
xhr = new XMLHttpRequest();
|
22
|
+
|
23
|
+
xhr.addEventListener('load', function () {
|
24
|
+
var event = new CustomEvent('ajaxComplete', {detail: xhr});
|
25
|
+
document.dispatchEvent(event);
|
26
|
+
});
|
27
|
+
|
28
|
+
if (typeof options.success == 'function')
|
29
|
+
xhr.addEventListener('load', function (event) {
|
30
|
+
if (xhr.status >= 200 && xhr.status < 300)
|
31
|
+
options.success(xhr);
|
32
|
+
});
|
33
|
+
|
34
|
+
if (typeof options.error == 'function') {
|
35
|
+
xhr.addEventListener('load', function (event) {
|
36
|
+
if (xhr.status < 200 || xhr.status >= 300)
|
37
|
+
options.error(xhr);
|
38
|
+
});
|
39
|
+
xhr.addEventListener('error', function (event) {
|
40
|
+
options.error(xhr);
|
41
|
+
});
|
42
|
+
}
|
43
|
+
|
44
|
+
xhr.open(options.method || 'GET', url, options.async);
|
45
|
+
var beforeSend = new CustomEvent('ajax:before', {detail: xhr});
|
46
|
+
document.dispatchEvent(beforeSend);
|
47
|
+
xhr.send(options.data);
|
48
|
+
|
49
|
+
return xhr;
|
50
|
+
};
|
51
|
+
|
52
|
+
return LiteAjax;
|
53
|
+
})();
|
@@ -0,0 +1,67 @@
|
|
1
|
+
document.addEventListener('click', function(event) {
|
2
|
+
var element, url, method, data, handler;
|
3
|
+
|
4
|
+
element = event.target;
|
5
|
+
|
6
|
+
if (matches.call(element, 'a[data-method]')) {
|
7
|
+
url = element.getAttribute('href');
|
8
|
+
method = element.getAttribute('data-method').toUpperCase();
|
9
|
+
data = {};
|
10
|
+
|
11
|
+
if (CSRF.param() && CSRF.token()) {
|
12
|
+
data[CSRF.param()] = CSRF.token();
|
13
|
+
}
|
14
|
+
|
15
|
+
if (matches.call(element, 'a[data-remote]')) {
|
16
|
+
handler = xhr;
|
17
|
+
} else {
|
18
|
+
handler = submit;
|
19
|
+
}
|
20
|
+
|
21
|
+
if (handler({ url: url, method: method, data: data })) {
|
22
|
+
event.preventDefault();
|
23
|
+
} else {
|
24
|
+
return true;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
function submit(options) {
|
29
|
+
var form, input, param;
|
30
|
+
|
31
|
+
if (options.method == 'GET') {
|
32
|
+
return false;
|
33
|
+
}
|
34
|
+
|
35
|
+
form = document.createElement('form');
|
36
|
+
form.method = 'POST';
|
37
|
+
form.action = options.url;
|
38
|
+
form.style.display = 'none';
|
39
|
+
|
40
|
+
for (param in options.data) {
|
41
|
+
if (Object.prototype.hasOwnProperty.call(options.data, param)) {
|
42
|
+
input = document.createElement('input');
|
43
|
+
input.setAttribute('type', 'hidden');
|
44
|
+
input.setAttribute('name', param);
|
45
|
+
input.setAttribute('value', options.data[param]);
|
46
|
+
form.appendChild(input);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
if (options.method != 'POST') {
|
51
|
+
input = document.createElement('input');
|
52
|
+
input.setAttribute('type', 'hidden');
|
53
|
+
input.setAttribute('name', '_method');
|
54
|
+
input.setAttribute('value', options.method);
|
55
|
+
form.appendChild(input);
|
56
|
+
}
|
57
|
+
|
58
|
+
document.body.appendChild(form);
|
59
|
+
form.submit();
|
60
|
+
return true;
|
61
|
+
}
|
62
|
+
|
63
|
+
function xhr(options) {
|
64
|
+
LiteAjax.ajax(options);
|
65
|
+
return true;
|
66
|
+
}
|
67
|
+
}, false);
|
@@ -0,0 +1,18 @@
|
|
1
|
+
var matches = (function(doc) {
|
2
|
+
return doc.matchesSelector ||
|
3
|
+
doc.webkitMatchesSelector ||
|
4
|
+
doc.mozMatchesSelector ||
|
5
|
+
doc.oMatchesSelector ||
|
6
|
+
doc.msMatchesSelector;
|
7
|
+
})(document.documentElement);
|
8
|
+
|
9
|
+
var CustomEvent = function (event, params) {
|
10
|
+
params = params || {bubbles: false, cancelable: false, detail: undefined};
|
11
|
+
var evt = document.createEvent('CustomEvent');
|
12
|
+
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
|
13
|
+
return evt;
|
14
|
+
};
|
15
|
+
|
16
|
+
CustomEvent.prototype = window.CustomEvent.prototype;
|
17
|
+
|
18
|
+
window.CustomEvent = CustomEvent;
|
@@ -0,0 +1 @@
|
|
1
|
+
//= require_tree ./src
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vanilla-ujs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Tsokurov
|
8
|
+
- Łukasz Niemier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This gem provides Rails UJS features without jQuery library.
|
15
|
+
email:
|
16
|
+
- me@ximik.net
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- lib/vanilla-ujs.rb
|
24
|
+
- vanilla-ujs.gemspec
|
25
|
+
- vendor/assets/javascripts/src/confirm.js
|
26
|
+
- vendor/assets/javascripts/src/csrf.js
|
27
|
+
- vendor/assets/javascripts/src/liteajax.js
|
28
|
+
- vendor/assets/javascripts/src/method.js
|
29
|
+
- vendor/assets/javascripts/src/polyfills.js
|
30
|
+
- vendor/assets/javascripts/vanilla-ujs.js
|
31
|
+
homepage: https://github.com/Ximik/vanilla-ujs
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.3.6
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 2.2.2
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: UJS without jQuery dependency
|
55
|
+
test_files: []
|