function doAPIRequestWithCallback (meth, 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.responseText);
} else {
errorHandler(meth, mod, func, this.status, this.responseText);
}
}
}
let argarr = [ `module=${mod}`, `function=${func}` ];
if( typeof args === 'Object' ) {
Object.keys(args).forEach( function(argument) {
argarr.push(`${argument}=${args[argument]}`);
});
}
let argstr = argarr.join("&");
if( meth === 'GET' ) {
oReq.open( meth, `api.cgi?${argstr}`, true );
oReq.send();
} else if ( meth === 'POST' ) {
oReq.open( meth, "api.cgi", true );
oReq.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
oReq.send();
}
return false;
}
function generalErrorHandler(meth, mod, func, code, txt) {
console.log(txt);
alert(`${meth} call to Troglodyne::API::${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 (resp) {
'use strict';
let obj = safeParseJSON(resp);
if(obj.result === 1) {
// 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) ) {
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( 'GET', 'Postgres', 'enable_community_repositories', doInstallScroller, generalErrorHandler ); return false; } // Now kickoff the page load post bits document.getElementById('submit').disabled = true; doAPIRequestWithCallback( 'GET', 'Postgres', 'get_postgresql_versions', versionHandler, generalErrorHandler );