[Test] Redefinir proxy
by Francisco
HTML
<script src="http://mapea4-sigc.juntadeandalucia.es/js/mapea.ol.min.js"></script>
<link rel="stylesheet" href="http://mapea4-sigc.juntadeandalucia.es/assets/css/mapea.ol.min.css">
<script src="http://mapea4-sigc.juntadeandalucia.es/js/configuration.js"></script>
<div id="map"></div>
CSS
html,
body,
#map {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}
JavaScript
let useproxy = true;
let proxyExceptions = ['http://geostematicos-sigc.juntadeandalucia.es'];
// Funciones auxiliares
const createScriptTag = (proxyUrl, jsonpHandlerName) => {
const scriptTag = document.createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.id = jsonpHandlerName;
scriptTag.src = proxyUrl;
scriptTag.setAttribute('async', '');
window.document.body.appendChild(scriptTag);
};
const removeScriptTag = (jsonpHandlerName) => {
const scriptTag = document.getElementById(jsonpHandlerName);
scriptTag.parentNode.removeChild(scriptTag);
};
const method = {
GET: 'GET',
POST: 'POST',
};
const manageProxy = (url, methodType) => {
// deafult GET
let proxyUrl = M.config.PROXY_URL;
if (methodType === method.POST) {
proxyUrl = M.config.PROXY_POST_URL;
}
proxyUrl = M.utils.addParameters(proxyUrl, {
url,
});
return proxyUrl;
};
const jsonp = (urlVar, data, options) => {
let url = urlVar;
if (!M.utils.isNullOrEmpty(data)) {
url = M.utils.addParameters(url, data);
}
if (useproxy) {
url = manageProxy(url, method.GET);
}
// creates a random name to avoid clonflicts
const jsonpHandlerName = M.utils.generateRandom('mapea_jsonphandler_');
url = M.utils.addParameters(url, {
callback: jsonpHandlerName,
});
const req = new Promise((success, fail) => {
const userCallback = success;
// get the promise of the script tag
const scriptTagPromise = new Promise((scriptTagSuccess) => {
window[jsonpHandlerName] = scriptTagSuccess;
});
/* when the script tag was executed remove
* the handler and execute the callback
*/
scriptTagPromise.then((proxyResponse) => {
// remove the jsonp handler from global window
delete window[jsonpHandlerName];
// remove the script tag from the html
removeScriptTag(jsonpHandlerName);
const response = new M.remote.Response();
response.parseProxy(proxyResponse);
...