JS code to handle the add ssh key functionality

This commit is contained in:
PCoder 2019-05-12 19:19:40 +02:00
parent 61127e56ca
commit c9c91b1ecb
1 changed files with 59 additions and 0 deletions

View File

@ -81,6 +81,65 @@ $(document).ready(function() {
})
});
$('#modal-add-ssh-key').on('hidden.bs.modal', function () {
location.reload();
});
haveResponse = false;
$('#modal-add-ssh-key').on('click', '.btn-ok', function(e) {
if(haveResponse) {
console.log("We had response, so reload");
location.reload();
}
var url = $('#add_ssh_key_to_vm_form').attr('action');
console.log("Url to POST " + url);
// Declare a checkbox array
var chkArray = [];
var encoded_data ="";
// Look for all checkboxes that have a specific class and was checked
$(".chk-ssh-key:checked").each(function() {
chkArray.push($(this).val());
});
encoded_data = encodeURIComponent(chkArray.join(","));
console.log("Encoded data = " + encoded_data);
fa_icon = $('#ssh-key-modal-icon');
modal_btn = $('#ssh-key-modal-button');
modal_btn.prop("disabled", true);
modal_btn.html('<i class="fa fa-cog fa-spin" aria-hidden="true"></i>');
$.post(url, {selected_key: encoded_data})
.done(function(data) {
console.log("Request Done");
modal_btn.prop("disabled", false);
modal_btn.html("OK");
if (data.status === true) {
fa_icon.html('<i class="fa fa-check" aria-hidden="true"></i>');
} else {
fa_icon.html('<i class="fa fa-close" aria-hidden="true"></i>');
modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide');
}
console.log("title = " + data.msg_title);
console.log("desc = " + data.msg_body);
$('#ssh-key-modal-title').text(data.msg_title);
$('#ssh-key-modal-description').html(data.msg_body);
console.log("Request Done end");
})
.fail(function(data) {
console.log("Request Failed");
console.log("title " + data.msg_title);
console.log("body " + data.msg_body);
modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide');
$('#ssh-key-modal-title').text(data.msg_title);
$('#ssh-key-modal-description').html(data.msg_body);
})
.always(function () {
console.log("changing href to location: " + location);
haveResponse = true;
})
});
var hash = window.location.hash;
hash && $('ul.nav a[href="' + hash + '"]').tab('show');