91 lines
3.3 KiB
JavaScript
91 lines
3.3 KiB
JavaScript
/*!
|
|
* Subcon GoGo Tools v1.0.0
|
|
* Licensed under BOML-1.0.0 license.
|
|
*/
|
|
(function (root, factory) {
|
|
if (typeof define === "function" && define.amd) { //AMD
|
|
define(factory);
|
|
} else if (typeof exports === "object") { //CommonJS
|
|
module.exports = factory();
|
|
} else { //Browser global
|
|
root.gogo = factory();
|
|
}
|
|
|
|
//Let everything know the library is ready
|
|
const event = new Event("gogoReady");
|
|
document.dispatchEvent(event);
|
|
}(this, function () {
|
|
let gogo = {};
|
|
|
|
//Query selectors
|
|
gogo.get = function(selector) {
|
|
return document.querySelector(selector);
|
|
};
|
|
|
|
gogo.getAll = function(selector) {
|
|
return document.querySelectorAll(selector);
|
|
};
|
|
|
|
//Ajax
|
|
gogo.ajax = async function(url = '', method = 'GET', data = {}) {
|
|
//Set up fetch settings
|
|
let settings = {
|
|
method: method, // *GET, POST, PUT, DELETE, etc.
|
|
mode: 'cors', // no-cors, *cors, same-origin
|
|
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
|
|
credentials: 'same-origin', // include, *same-origin, omit
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
// 'Content-Type': 'application/x-www-form-urlencoded',
|
|
//'X-CSRF-TOKEN': qs('meta[name="csrf-token"]').getAttribute('content'),
|
|
'X-Requested-With': 'XMLHttpRequest' //Allow laravel to properly detect this as an AJAX request
|
|
},
|
|
redirect: 'follow', // manual, *follow, error
|
|
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
|
|
}
|
|
|
|
//If method is get or head, prepare data as query string
|
|
if(method === 'GET' || method === 'HEAD') {
|
|
if(Object.keys(data).length !== 0) {
|
|
let query = new URLSearchParams(data).toString()
|
|
url = url + '?' + query;
|
|
}
|
|
} else {
|
|
settings['body'] = JSON.stringify(data);
|
|
}
|
|
|
|
//Fetch
|
|
const response = await fetch(url, settings);
|
|
|
|
if(!response.ok || response.headers.get('content-type').indexOf('application/json') === -1) {
|
|
return {
|
|
'error': 'An error occurred while trying to fetch page data. Please refresh and try again.'
|
|
}
|
|
}
|
|
|
|
//Return response (uses Promise API)
|
|
return response.json();
|
|
}
|
|
|
|
//Used to allow injecting html and javascript from ajax requests
|
|
gogo.setHTML = function (element, html) {
|
|
element.innerHTML = html;
|
|
|
|
Array.from(element.querySelectorAll("script"))
|
|
.forEach( oldScriptEl => {
|
|
const newScriptEl = document.createElement("script");
|
|
|
|
Array.from(oldScriptEl.attributes).forEach( attr => {
|
|
newScriptEl.setAttribute(attr.name, attr.value)
|
|
});
|
|
|
|
const scriptText = document.createTextNode(oldScriptEl.innerHTML);
|
|
newScriptEl.appendChild(scriptText);
|
|
|
|
oldScriptEl.parentNode.replaceChild(newScriptEl, oldScriptEl);
|
|
});
|
|
}
|
|
|
|
return gogo;
|
|
})); |