Skip to content

Commit

Permalink
Fetch changes from Ximik fork
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Jan 30, 2016
1 parent 854bb98 commit 47b109b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/assets/javascripts/vanilla-ujs/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ document.addEventListener('click', function (event) {

element = event.target;

if (matches.call(element, 'a[data-confirm], button[data-confirm]')) {
if (matches.call(element, 'a[data-confirm], button[data-confirm], input[data-confirm]')) {
message = element.getAttribute('data-confirm');
if (!confirm(message)) {
event.stopPropagation();
Expand Down
20 changes: 20 additions & 0 deletions lib/assets/javascripts/vanilla-ujs/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
document.addEventListener('submit', function(event) {

var form = event.target;

if (matches.call(form, 'form[data-remote]')) {
url = form.action;
method = form.method || form.getAttribute('data-method').toUpperCase() || 'POST';
data = new FormData(form);

if (CSRF.param() && CSRF.token()) {
data[CSRF.param()] = CSRF.token();
}

if (LiteAjax.ajax({ url: url, method: method, data: data })){
event.preventDefault();
} else {
return true;
}
}
});
24 changes: 17 additions & 7 deletions lib/assets/javascripts/vanilla-ujs/liteajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ var LiteAjax = (function () {

LiteAjax.options = {
method: 'GET',
url: window.location.href,
async: true,
url: window.location.href
};

LiteAjax.ajax = function (url, options) {
Expand All @@ -15,12 +14,16 @@ var LiteAjax = (function () {

options = options || {};
url = url || options.url || location.href || '';
var data = options.data;

var xhr;

xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest();

xhr.addEventListener('load', function () {
responseType = xhr.getResponseHeader('content-type');
if(responseType === 'text/javascript; charset=utf-8') {
eval(xhr.response);
}

var event = new CustomEvent('ajaxComplete', {detail: xhr});
document.dispatchEvent(event);
});
Expand All @@ -41,10 +44,17 @@ var LiteAjax = (function () {
});
}

xhr.open(options.method || 'GET', url, options.async);
xhr.open(options.method || 'GET', url);
xhr.setRequestHeader('X-Requested-With', 'XmlHttpRequest');

if(options.json) {
xhr.setRequestHeader('Content-type', 'application/json');
data = JSON.stringify(data);
}

var beforeSend = new CustomEvent('ajax:before', {detail: xhr});
document.dispatchEvent(beforeSend);
xhr.send(options.data);
xhr.send(data);

return xhr;
};
Expand Down

0 comments on commit 47b109b

Please sign in to comment.