function getQueryParams(qs) { qs = qs.split('+').join(' '); var params = {}, tokens, re = /[?&]?([^=]+)=([^&]*)/g; while (tokens = re.exec(qs)) { params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); } return params; } function show_instances() { $.get($.cookie("base_location") + "api/v1/instances", function(data) { var admin = $.cookie("admin") == "1"; if (admin) { $("#main_content").html("

List of Spectrum 2 instances

NameStatusActions
"); } else { $("#main_content").html("

List of Spectrum 2 instances

NameStatus
"); } $.each(data.instances, function(i, instance) { if (instance.running) { if (admin) { var command = instance.running ? "stop" : "start"; } else { if (instance.registered) { instance.status += "
Registered as " + instance.username; } } } else if (admin) { var command = "start"; } else { var command = ""; } var row = '' row += '' + instance.name + '' row += '' + instance.status + '' if (admin) { if (command == "") { row += ''; $("#main_result > tbody:last-child").append(row); } else { row += ''; row += '' + command + ''; row += ''; $("#main_result > tbody:last-child").append(row); $(".button_command").click(function(e) { e.preventDefault(); $(this).parent().empty().progressbar( {value: false} ).css('height', '1em'); var url = $(this).attr('href'); $.get(url, function(data) { show_instances(); }); }) } } else { row += ''; $("#main_result > tbody:last-child").append(row); } }); }); } function show_users() { var admin = $.cookie("admin") == "1"; if (!admin) { $("#main_content").html("

List of Spectrum 2 users

Only administrator can list the users.

"); return; } $.get($.cookie("base_location") + "api/v1/users", function(data) { $("#main_content").html("

List of Spectrum 2 users

You can add new users here.

"); $.each(data.users, function(i, user) { var row = '' row += '' row += ''; $("#main_result > tbody:last-child").append(row); $(".button_command").click(function(e) { e.preventDefault(); $(this).parent().empty().progressbar( {value: false} ).css('height', '1em'); var url = $(this).attr('href'); $.get(url, function(data) { show_users(); }); }) }); }); } function fill_users_register_form() { $(".button").click(function(e) { e.preventDefault(); var postdata ={ "username": $("#username").val(), "password": $("#password").val() }; $.post(BaseLocation + "api/v1/users/add", postdata, function(data) { if (data.error) { $('#error').text(data.message); } else { var query = getQueryParams(document.location.search); if (query.back_to_list == "1") { window.location.replace("list.shtml"); } else { window.location.replace("../login/"); } } }); }) } function execute_command(instance, command) { $.get($.cookie("base_location") + 'api/v1/instances/command_args/' + instance + '?command=' + command, function(data) { var form = '
'; if (data.args.length != 0) { form += '
'; $.each(data.args, function(i, arg) { form += '
'; form += ''; form += '
'; if (arg.type == "password") { form += ''; } else { form += ''; } form += '
'; console.log('command_arg' + i ); }); } else { form += '
'; form += '
'; form += ''; form += '
'; } form += '
' bootbox.dialog({ title: "Command execution: " + command + ".", message: form, buttons: { cancel: { label: "Cancel", className: "btn-cancel" }, success: { label: "Execute", className: "btn-success", callback: function () { if (command == "register") { var postdata = {}; if ($("#command_arg0").val()) { postdata["jid"] = $("#command_arg0").val(); } if ($("#command_arg1").val()) { postdata["uin"] = $("#command_arg1").val(); } if ($("#command_arg2").val()) { postdata["password"] = $("#command_arg2").val(); } $.post($.cookie("base_location") + "api/v1/instances/register/" + instance, postdata, function(data) { if (data.oauth2_url) { window.location.replace(data.oauth2_url); } else { var dialog = bootbox.dialog({ title: "Command result: " + command + ".", message: "
" + data.message + "
", buttons: { success: { label: "OK", className: "btn-success", callback: function () { location.reload(); } } } }) dialog.find("div.modal-dialog").addClass("largeWidth"); dialog.find("div.modal-body").addClass("maxHeight"); } }); } else { if (command == "unregister") { var posturl = $.cookie("base_location") + "api/v1/instances/unregister/" + instance; } else { var posturl = $.cookie("base_location") + "api/v1/instances/execute/" + instance + "?command=" + command; } var postdata = {} for (i = 0; i < 10; i++) { var val = $('#command_arg' + i).val(); if (val) { postdata["command_arg" + i] = val; } } $.post(posturl, postdata, function(data) { if (data.table) { data.message = "
NameActions
' + user.username + 'remove
"; data.message += ""; for (var key in data.table[0]) { if (data.table[0].hasOwnProperty(key)) { data.message += ""; } } data.message += ""; $.each(data.table, function(i, line) { data.message += ""; for (var key in line) { if (line.hasOwnProperty(key)) { data.message += ""; } } data.message += ""; }) data.message += "
" + key + "
" + line[key] + "
"; } var dialog = bootbox.dialog({ title: "Command result: " + command + ".", message: "
" + data.message + "
", buttons: { success: { label: "OK", className: "btn-success", callback: function () { if (command == "unregister") { location.reload(); } } } } }) dialog.find("div.modal-dialog").addClass("largeWidth"); dialog.find("div.modal-body").addClass("maxHeight"); }); } } } } }) }); } function show_instance() { var query = getQueryParams(document.location.search); $("#main_content").html("

Instance: " + query.id + "

Available commands:

NameCategoryDescription

Available variables:

NameValueRead-onlyDesc
"); $.get($.cookie("base_location") + "api/v1/instances/commands/" + query.id, function(data) { $.each(data.commands, function(i, command) { var row = '' row += '' + command.label + ''; row += '' + command.category + ''; row += '' + command.desc + ''; row += ''; $("#commands > tbody:last-child").append(row); }); $(".button_command").click(function(e) { e.preventDefault(); var command = $(this).attr('command'); var instance = $(this).attr('instance'); execute_command(instance, command); }) }); $.get($.cookie("base_location") + "api/v1/instances/variables/" + query.id, function(data) { $.each(data.variables, function(i, variable) { var row = '' row += '' + variable.name + ''; row += '' + variable.value + ''; row += '' + variable.read_only + ''; row += '' + variable.desc + ''; row += ''; $("#variables > tbody:last-child").append(row); }); }); }