vm-details.js 13 KB
Newer Older
1 2
var show_all = false;
var in_progress = false;
3
var activity_hash = 5;
4

5
$(function() {
6 7
  /* do we need to check for new activities */
  if(decideActivityRefresh()) {
8 9 10 11
    if(!in_progress) {
      checkNewActivity(1);
      in_progress = true;
    }
12
  }
13

14
  $('a[href="#activity"]').click(function(){
15
    $('a[href="#activity"] i').addClass('fa-spin');
16 17 18 19 20 21 22
    if(!in_progress) {
      checkNewActivity(1);
      in_progress = true;
    }
  });

  $("#activity-refresh").on("click", "#show-all-activities", function() {
23
    $(this).find("i").addClass("fa-spinner fa-spin");
24 25 26
    show_all = !show_all;
    $('a[href="#activity"]').trigger("click");
    return false;
27
  });
28 29 30

  /* save resources */
  $('#vm-details-resources-save').click(function() {
31 32 33 34 35 36 37 38 39
    var error = false;
    $(".cpu-count-input, .ram-input").each(function() {
      if(!$(this)[0].checkValidity()) {
        error = true;
      }
    });
    if(error) return true;


40
    $('i.fa-floppy-o', this).removeClass("fa-floppy-o").addClass("fa-refresh fa-spin");
41
    var vm = $(this).data("vm");
42 43
    $.ajax({
      type: 'POST',
44
      url: "/dashboard/vm/" + vm + "/op/resources_change/", 
45 46
      data: $('#vm-details-resources-form').serialize(),
      success: function(data, textStatus, xhr) {
47 48 49 50 51
        if(data.success) {
          $('a[href="#activity"]').trigger("click");
        } else {
          addMessage(data.messages.join("<br />"), "danger");
        }
52
        $("#vm-details-resources-save i").removeClass('fa-refresh fa-spin').addClass("fa-floppy-o");
53 54
      },
      error: function(xhr, textStatus, error) {
55
        $("#vm-details-resources-save i").removeClass('fa-refresh fa-spin').addClass("fa-floppy-o");
56
        if (xhr.status == 500) {
57
          addMessage("500 Internal Server Error", "danger");
58
        } else {
59 60
          addMessage(xhr.status + " Unknown Error", "danger");
        }  
61 62 63 64
      }
    });
    return false;
  });
65

Kálmán Viktor committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  /* remove tag */
  $('.vm-details-remove-tag').click(function() {
    var to_remove =  $.trim($(this).parent('div').text());
    var clicked = $(this);
    $.ajax({
      type: 'POST',
      url: location.href,
      headers: {"X-CSRFToken": getCookie('csrftoken')}, 
      data: {'to_remove': to_remove},
      success: function(re) {
        if(re['message'].toLowerCase() == "success") {
          $(clicked).closest(".label").fadeOut(500, function() {
            $(this).remove();
          });
        }
      },
      error: function() {
        addMessage(re['message'], 'danger');
      }

    });
    return false;
  });
89 90 91 92 93 94 95 96 97 98 99

  /* remove port */
  $('.vm-details-remove-port').click(function() {
    addModalConfirmation(removePort, 
      {
        'url': $(this).prop("href"),
        'data': [],
        'rule': $(this).data("rule")
      });
    return false;
  });
100 101 102 103 104 105 106 107 108 109

  /* for js fallback */
  $("#vm-details-pw-show").parent("div").children("input").prop("type", "password");
  
  /* show password */
  $("#vm-details-pw-show").click(function() {
    var input = $(this).parent("div").children("input");
    var eye = $(this).children("#vm-details-pw-eye");
    
    eye.tooltip("destroy")
110 111
    if(eye.hasClass("fa-eye")) {
      eye.removeClass("fa-eye").addClass("fa-eye-slash");
112 113 114 115
      input.prop("type", "text");
      input.focus();
      eye.prop("title", "Hide password");
    } else {
116
      eye.removeClass("fa-eye-slash").addClass("fa-eye");
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      input.prop("type", "password");
      eye.prop("title", "Show password");
    }
    eye.tooltip();
  });

  /* change password confirmation */
  $("#vm-details-pw-change").click(function() {
    $("#vm-details-pw-confirm").fadeIn();
    return false;
  });

  /* change password */
  $(".vm-details-pw-confirm-choice").click(function() {
    choice = $(this).data("choice");
    if(choice) {
      pk = $(this).data("vm");
      $.ajax({
        type: 'POST',
        url: "/dashboard/vm/" + pk + "/",
        data: {'change_password': 'true'},
        headers: {"X-CSRFToken": getCookie('csrftoken')},
        success: function(re, textStatus, xhr) {
          location.reload();
        },
        error: function(xhr, textStatus, error) {
Kálmán Viktor committed
143 144 145 146 147
          if (xhr.status == 500) {
            addMessage("Internal Server Error", "danger");
          } else {
            addMessage(xhr.status + " Unknown Error", "danger");
          }
148 149 150 151 152 153 154
        }
      });
    } else {
      $("#vm-details-pw-confirm").fadeOut(); 
    }
    return false;
  });
155 156 157

  /* add network button */
  $("#vm-details-network-add").click(function() {
158
    $("#vm-details-network-add-form").toggle();
159 160
    return false;
  });
161 162 163 164 165 166

  /* add disk button */
  $("#vm-details-disk-add").click(function() {
    $("#vm-details-disk-add-for-form").html($("#vm-details-disk-add-form").html());
    return false;
  });
167

168 169 170
  /* for interface remove buttons */
  $('.interface-remove').click(function() {
    var interface_pk = $(this).data('interface-pk');
171
    addModalConfirmation(removeInterface, 
172 173 174 175 176 177 178
      { 'url': '/dashboard/interface/' + interface_pk + '/delete/',
        'data': [],
        'pk': interface_pk,
	'type': "interface",
      });
    return false;
  });
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  /* removing interface post */
  function removeInterface(data) {
    $.ajax({
      type: 'POST',
      url: data['url'],
      headers: {"X-CSRFToken": getCookie('csrftoken')}, 
      success: function(re, textStatus, xhr) { 
        /* remove the html element */
        $('a[data-interface-pk="' + data.pk + '"]').closest("div").fadeOut();
        
        /* add the removed element to the list */
        network_select = $('select[name="new_network_vlan"]');
        name_html = (re.removed_network.managed ? "&#xf0ac;": "&#xf0c1;") + " " + re.removed_network.vlan;
        option_html = '<option value="' + re.removed_network.vlan_pk + '">' + name_html + '</option>';
        // if it's -1 then it's a dummy placeholder so we can use .html
        if($("option", network_select)[0].value === "-1") {
          network_select.html(option_html);
          network_select.next("div").children("button").prop("disabled", false); 
        } else {
          network_select.append(option_html);
        }
      },
      error: function(xhr, textStatus, error) {
        addMessage('Uh oh :(', 'danger')
      }
    });
  }

208 209 210 211 212
  /* rename */
  $("#vm-details-h1-name, .vm-details-rename-button").click(function() {
    $("#vm-details-h1-name").hide();
    $("#vm-details-rename").css('display', 'inline');
    $("#vm-details-rename-name").focus();
213
    return false;
214 215 216 217 218 219 220
  });

  /* rename in home tab */
  $(".vm-details-home-edit-name-click").click(function() {
    $(".vm-details-home-edit-name-click").hide();
    $("#vm-details-home-rename").show();
    $("input", $("#vm-details-home-rename")).focus();
221
    return false;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  });

  /* rename ajax */
  $('.vm-details-rename-submit').click(function() {
    var name = $(this).parent("span").prev("input").val();
    $.ajax({
      method: 'POST',
      url: location.href,
      data: {'new_name': name},
      headers: {"X-CSRFToken": getCookie('csrftoken')},
      success: function(data, textStatus, xhr) {
        $(".vm-details-home-edit-name").text(data['new_name']).show();
        $(".vm-details-home-edit-name").parent("div").show();
        $(".vm-details-home-edit-name-click").show();
        $(".vm-details-home-rename-form-div").hide();
        // update the inputs too
        $(".vm-details-rename-submit").parent("span").prev("input").val(data['new_name']);  
      },
      error: function(xhr, textStatus, error) {
        addMessage("Error during renaming!", "danger");
      }
    });
    return false;
  });
  
  /* update description click */
  $(".vm-details-home-edit-description-click").click(function() {
    $(".vm-details-home-edit-description-click").hide();
    $("#vm-details-home-description").show();
251 252 253 254 255
    var ta = $("#vm-details-home-description textarea");
    var tmp = ta.val();
    ta.val("");
    ta.focus();
    ta.val(tmp)
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    return false;
  });
  
  /* description update ajax */
  $('.vm-details-description-submit').click(function() {
    var description = $(this).prev("textarea").val();
    $.ajax({
      method: 'POST',
      url: location.href,
      data: {'new_description': description},
      headers: {"X-CSRFToken": getCookie('csrftoken')},
      success: function(data, textStatus, xhr) {
        var new_desc = data['new_description'];
        /* we can't simply use $.text, because we need new lines */ 
        var tagsToReplace = {
          '&': "&amp;",
          '<': "&lt;",
          '>': "&gt;",
        };
        
        new_desc = new_desc.replace(/[&<>]/g, function(tag) {
          return tagsToReplace[tag] || tag;
        });

        $(".vm-details-home-edit-description")
          .html(new_desc.replace(/\n/g, "<br />"));
        $(".vm-details-home-edit-description-click").show();
        $("#vm-details-home-description").hide();
        // update the textareia
        $("vm-details-home-description textarea").text(data['new_description']);  
      },
      error: function(xhr, textStatus, error) {
        addMessage("Error during renaming!", "danger");
      }
    });
    return false;
  });

294 295 296 297
  // screenshot
  $("#getScreenshotButton").click(function() {
    var vm = $(this).data("vm-pk");
    var ct = $("#vm-console-screenshot");
298
    $("i", this).addClass("fa-spinner fa-spin");
299
    $(this).prop("disabled", true);
300 301 302
    ct.slideDown();
    var img = $("img", ct).prop("src", '/dashboard/vm/' + vm + '/screenshot/');
  });
303 304 305 306 307 308

  // if the image is loaded remove the spinning stuff
  // note: this should not work if the image is cached, but it's not
  // see: http://stackoverflow.com/a/3877079/1112653
  $("#vm-console-screenshot img").load(function(e) {
    $("#getScreenshotButton").prop("disabled", false)
309
    .find("i").removeClass("fa-spinner fa-spin");
310 311 312

  });
    
313 314 315 316 317 318
  
  // screenshot close
  $("#vm-console-screenshot button").click(function() {
    $(this).parent("div").slideUp();
  });

319 320 321 322 323
  // select connection string
  $("#vm-details-connection-string-copy").click(function() {
    $("#vm-details-connection-string").focus();
  });

324 325 326 327
  $("a.operation-password_reset").click(function() {
    if(Boolean($(this).data("disabled"))) return false;
  });

328 329
});

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

function removePort(data) {
  $.ajax({
    type: 'POST',
    url: data['url'],
    headers: {"X-CSRFToken": getCookie('csrftoken')},
    success: function(re, textStatus, xhr) {
      $("a[data-rule=" + data['rule'] + "]").each(function() {
        $(this).closest("tr").fadeOut(500, function() {
          $(this).remove();
        });
      });
      addMessage(re['message'], "success");
    },
    error: function(xhr, textStatus, error) {

    }
  });
348

349 350
}

351 352 353
function decideActivityRefresh() {
  var check = false;
  /* if something is still spinning */
354
  if($('.timeline .activity i').hasClass('fa-spin'))
355 356 357 358
    check = true;
  /* if there is only one activity */
  if($('#activity-timeline div[class="activity"]').length < 2)
    check = true;
359
  
360 361 362
  return check;
}

363
function checkNewActivity(runs) {
364 365 366
  var instance = location.href.split('/'); instance = instance[instance.length - 2];

  $.ajax({
367
    type: 'GET',
368
    url: '/dashboard/vm/' + instance + '/activity/',
369
    data: {'show_all': show_all},
370
    success: function(data) {
371 372
      var new_activity_hash = (data['activities'] + "").hashCode();
      if(new_activity_hash != activity_hash) {
373
        $("#activity-refresh").html(data['activities']);
374
      }
375 376
      activity_hash = new_activity_hash;

377 378 379
      $("#ops").html(data['ops']);
      $("#disk-ops").html(data['disk_ops']);
      $("[title]").tooltip();
380

381 382 383 384 385 386 387 388
      /* changing the status text */
      var icon = $("#vm-details-state i");
      if(data['is_new_state']) {
        if(!icon.hasClass("fa-spin"))
          icon.prop("class", "fa fa-spinner fa-spin");
      } else {
        icon.prop("class", "fa " + data['icon']);
      }
389 390
      $("#vm-details-state span").html(data['human_readable_status'].toUpperCase());
      if(data['status'] == "RUNNING") {
391
	$("#dashboard-vm-details-connect-button").prop("disabled", false);
392
        $("[data-target=#_console]").attr("data-toggle", "pill").attr("href", "#console").parent("li").removeClass("disabled");
393
      } else {
394
	$("#dashboard-vm-details-connect-button").prop("disabled", true);
395
        $("[data-target=#_console]").attr("data-toggle", "_pill").attr("href", "#").parent("li").addClass("disabled");
396
      }
397

398 399 400
      if(data['status'] == "STOPPED" || data['status'] == "PENDING") {
        $(".change-resources-button").prop("disabled", false);
        $(".change-resources-help").hide();
401
      } else {
402 403
        $(".change-resources-button").prop("disabled", true);
        $(".change-resources-help").show();
404 405
      }

406
      if(runs > 0 && decideActivityRefresh()) {
407
        setTimeout(
408
          function() {checkNewActivity(runs + 1)}, 
409
          1000 + Math.exp(runs * 0.05)
410
        );
411 412
      } else {
        in_progress = false;
413
      }
414
      $('a[href="#activity"] i').removeClass('fa-spin');
415 416
    },
    error: function() {
417
      in_progress = false;
418 419 420
    }
  });
}
421 422 423 424 425 426 427 428 429 430 431

String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length == 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
};