function doAPIRequestWithCallback (mod, func, handler, errorHandler, args) {
'use strict';
let oReq = new XMLHttpRequest();
oReq.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE) {
if( this.status === 200 ) {
handler(this);
} else {
errorHandler(mod, func, this.status, this.responseText);
}
}
}
oReq.addEventListener("load", handler);
let argarr = [];
if( typeof args === 'Object' ) {
Object.keys(args).forEach( function(argument) {
argarr.push(`${argument}=${args[argument]}`);
});
}
oReq.open( "POST", "api.cgi", true );
oReq.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
oReq.send(argarr.join("&"));
return false;
}
function generalErrorHandler(mod, func, code, txt) {
console.log(txt);
alert(`API call to ${mod}::${func} failed with error code ${code}! Please see the JS console for details.`);
return false;
}
function safeParseJSON(txt) {
let obj = {};
try {
obj = JSON.parse(txt);
} catch(e) {
console.log(txt);
return { "error": e };
}
return obj;
}
function versionHandler (xhr) {
'use strict';
let obj = safeParseJSON(xhr.responseText);
if(obj.result === 1) {
console.log(obj);
// Construct version warning/display
let pgVersion = obj.data.installed_version.major + '.' + obj.data.installed_version.minor;
let elem = document.getElementById('psqlVersion');
let html = `${pgVersion}`;
if( parseFloat(pgVersion) < parseFloat(obj.data.minimum_supported_version) ) {
elem.classList.add('callout', 'callout-danger');
html += ' -- You are using a version of PostgreSQL Server that is no longer supported by ';
html += 'postgresql.org!
';
if( obj.data.eol_versions.hasOwnProperty(pgVersion) ) {
console.log(obj.data.eol_versions[pgVersion]);
console.log(obj.data.eol_versions[pgVersion].EOL);
html += "EOL -- " + new Date(obj.data.eol_versions[pgVersion].EOL * 1000).toLocaleString( undefined, { year: 'numeric', month: 'long', day: 'numeric' } ) + "
";
}
html += "Immediate upgrade is recommended.";
}
elem.innerHTML = html;
// Now let's build the table
let rows = '';
for ( var version of Object.keys(obj.data.available_versions).sort(function(a,b) { return parseFloat(b) - parseFloat(a) }) ) {
rows +=
`
Ensuring that the PostgreSQL Community repository is installed...\n'; doAPIRequestWithCallback('Postgres', 'enable_community_repositories', doInstallScroller, generalErrorHandler ); return false; } // Now kickoff the page load post bits document.getElementById('submit').disabled = true; doAPIRequestWithCallback( 'Postgres', 'get_postgresql_versions', versionHandler, generalErrorHandler );