From c9c91b1ecba2e67f5d0ce7b6ea5477b1245e1a1a Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 12 May 2019 19:19:40 +0200 Subject: [PATCH] JS code to handle the add ssh key functionality --- .../hosting/js/virtual_machine_detail.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/hosting/static/hosting/js/virtual_machine_detail.js b/hosting/static/hosting/js/virtual_machine_detail.js index 8f90933b..32ae3f53 100644 --- a/hosting/static/hosting/js/virtual_machine_detail.js +++ b/hosting/static/hosting/js/virtual_machine_detail.js @@ -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(''); + $.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(''); + } else { + fa_icon.html(''); + 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');