monkey/monkey_business/cc/admin/ui/js/monkeysb-admin.js

283 lines
8.4 KiB
JavaScript
Raw Normal View History

// The JSON must be fully loaded before onload() happens for calling draw() on 'monkeys'
$.ajaxSetup({
async: false
});
// Images/icons constants
const ICONS_DIR = "./css/img/objects/";
const ICONS_EXT = ".png";
// General options
// If variable from local storage != null, assign it, otherwise set it's default value.
var jobsTable = undefined;
2016-06-05 23:52:04 +08:00
var logsTable = undefined;
var jobCfg = undefined;
var conCfg = undefined;
2016-06-05 23:52:04 +08:00
var selectedJob = undefined;
JSONEditor.defaults.theme = 'bootstrap3';
function initAdmin() {
jobsTable = $("#jobs-table").DataTable({
"ordering": true,
"order": [[1, "desc"]],
});
2016-06-05 23:52:04 +08:00
logsTable = $("#logs-table").DataTable({
"ordering": false,
});
jobsTable.on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
jobsTable.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
jobdata = jobsTable.row(this).data();
2016-06-05 23:52:04 +08:00
selectedJob = jobdata[0];
createNewJob(selectedJob, jobdata[3]);
showLog(selectedJob);
} );
2016-06-06 01:01:33 +08:00
setInterval(updateJobs, 5000);
setInterval(showLog, 5000);
updateJobs();
}
2016-06-05 23:52:04 +08:00
function showLog() {
logsTable.clear();
if (!selectedJob) {
return;
}
$.getJSON('/job?action=log&id=' + selectedJob, function(json) {
var logsList = json.log;
for (var i = 0; i < logsList.length; i++) {
logsTable.row.add([logsList[i][0], logsList[i][1]]);
}
logsTable.draw();
});
}
function updateJobs() {
$.getJSON('/job', function(json) {
jobsTable.clear();
var jobsList = json.objects;
for (var i = 0; i < jobsList.length; i++) {
jobsTable.row.add([jobsList[i].id, jobsList[i].creation_time, jobsList[i].type,jobsList[i].execution.state, JSON.stringify(jobsList[i].properties)]);
}
jobsTable.draw();
});
}
function loadConnectorsConfig() {
elem = document.getElementById('connectors-config');
elem.innerHTML = ""
conCfg = new JSONEditor(elem,{
schema: {
type: "object",
title: "Connector",
properties: {
connector: {
title: "Type",
$ref: "/connector",
}
},
options: {
"collapsed": false
},
},
ajax: true,
disable_edit_json: false,
disable_collapse: true,
disable_properties: true,
no_additional_properties: true
});
conCfg.on('ready',function() {
document.getElementById("btnSaveConnectorConfig").style.visibility = "visible";
});
}
function updateConnectorConfig() {
var con_config = conCfg.getValue()
$.ajax({
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
url : '/connector',
type : 'POST',
data : JSON.stringify(con_config.connector),
success : function(response, textStatus, jqXhr) {
console.log("New vcenter config successfully updated!");
document.getElementById("btnSaveConnectorConfig").style.visibility = "hidden";
elem = document.getElementById('connectors-config');
elem.innerHTML = ""
},
error : function(jqXHR, textStatus, errorThrown) {
// log the error to the console
console.log("The following error occured: " + textStatus, errorThrown);
},
complete : function() {
console.log("Sending vcenter config update...");
}
});
}
2016-06-05 23:52:04 +08:00
function emptySelection() {
showLog();
selectedJob = undefined;
jobsTable.$('tr.selected').removeClass('selected');
}
function createNewJob(id, state) {
if (!id) {
2016-06-05 23:52:04 +08:00
emptySelection();
}
2016-05-29 16:45:07 +08:00
elem = document.getElementById('job-config');
elem.innerHTML = ""
jobCfg = new JSONEditor(elem,{
schema: {
type: "object",
title: "Job",
properties: {
job: {
title: "Type",
$ref: "/jobcreate" + ((id)?"?id="+id:""),
}
},
options: {
"collapsed": false
},
},
ajax: true,
disable_edit_json: false,
disable_collapse: true,
disable_properties: true,
no_additional_properties: true
});
jobCfg.on('ready',function() {
if (id && state != "pending") {
jobCfg.disable();
document.getElementById("btnSendJob").style.visibility = "hidden";
document.getElementById("btnDeleteJob").style.visibility = "hidden";
}
else {
jobCfg.enable();
document.getElementById("btnSendJob").style.visibility = "visible";
if (id) {
document.getElementById("btnDeleteJob").style.visibility = "visible";
}
else {
document.getElementById("btnDeleteJob").style.visibility = "hidden";
}
}
});
2016-05-29 16:45:07 +08:00
}
function sendJob() {
var job_config = jobCfg.getValue()
$.ajax({
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
url : '/jobcreate',
type : 'POST',
data : JSON.stringify(job_config.job),
success : function(response, textStatus, jqXhr) {
console.log("Job successfully updated!");
updateJobs();
},
error : function(jqXHR, textStatus, errorThrown) {
// log the error to the console
console.log("The following error occured: " + textStatus, errorThrown);
},
complete : function() {
console.log("Sending job config...");
}
});
}
function deleteJob() {
var job_config = jobCfg.getValue();
if (job_config.job.id) {
$.ajax({
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
url : '/jobcreate',
type : 'GET',
data : "action=delete&id=" + job_config.job.id,
success : function(response, textStatus, jqXhr) {
console.log("Job successfully updated!");
updateJobs();
},
error : function(jqXHR, textStatus, errorThrown) {
// log the error to the console
console.log("The following error occured: " + textStatus, errorThrown);
},
complete : function() {
console.log("Sending job config...");
}
});
}
}
function configSched() {
}
/**
* Clears the value in the local storage
*/
function clear(key) {
if (localStorage[key]) {
delete localStorage[key];
}
};
/** /.localStorage Section **/
/** **/
/** ----- **/
/** **/
/** Utilities Section **/
/**
* Returns the differences between two arrays
*/
Array.prototype.diff = function(other) {
var diff = [];
for (var i = 0; i < this.length; i++) {
var obj = this[i];
if (other.indexOf(obj) == -1) {
diff.push(obj);
}
}
for (var i = 0; i < other.length; i++) {
var obj = other[i];
if (this.indexOf(obj) == -1 && diff.indexOf(obj) == -1) {
diff.push(obj);
}
}
return diff;
};
/** /.Utilities Section **/
/** **/