cloud.js 22.4 KB
Newer Older
1 2
var toggleDetails;
$(function() {
3
    toggleDetails = function() {
4 5 6 7
        try {
            if ($(this).parent('.entry').hasClass('opened')) {
                $(this).parent('.entry').removeClass('opened');
                $(this).next('.details').css('height', '0px');
Bence Dányi committed
8
            } else {
9 10 11 12 13 14 15
                $(this).parent('.entry').addClass('opened');
                if ($(this).data('fallback') == 'slide') {
                    $(this).next('.details').css('height', 'auto');
                } else {
                    $(this).next('.details').css('height',
                        $(this).next('.details').find('.details-container')[0].offsetHeight + 15 + 'px');
                }
Bence Dányi committed
16
            }
17 18
        } catch(ex) {
            //make sure we never fail :-)
19
        }
20
    }
21 22
    $('a[href=#]').click(function(e) {
        e.preventDefault();
23
    });
Bence Dányi committed
24
    $('.host-toggle').click(function(e) {
25
        e.preventDefault();
Bence Dányi committed
26
        if ($(this).find('.v4').is(':hidden')) {
27 28 29 30 31 32 33 34 35 36
            $(this).find('.v4').show();
            $(this).find('.v6').hide();
            $(this).parent().next().find('.host').show();
            $(this).parent().next().find('.ipv4host').hide();
        } else {
            $(this).find('.v6').show();
            $(this).find('.v4').hide();
            $(this).parent().next().find('.host').hide();
            $(this).parent().next().find('.ipv4host').show();
        }
37
    });
38 39 40
    $('a[href!=#]').click(function(e) {
        e.stopPropagation();
    });
41 42 43
    $('.delete-template').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
44
        delete_template_confirm($(this).data('url'), $(this).data('id'), $(this).data('name'));
45 46 47 48 49 50 51 52 53
    });
    $('.delete-key').click(function(e) {
        var id = $(this).data('id');
        e.preventDefault();
        e.stopPropagation();
        vm_confirm_popup(gettext('Are you sure deleting key?'), gettext('Delete'), function() {
            $.ajax({
                'type': 'POST',
                'data': 'id=' + id,
54
                'url': $(this).data('url'),
55 56 57 58 59 60
                'success': function() {
                    $('#key-' + id).slideUp(700);
                }
            });
        });
    });
Dányi Bence committed
61 62
    $('#reset-key').click(function(e) {
        vm_confirm_popup(gettext('Are you sure about reseting store credentials?<br /> You will lose your access to your store account on your existing virtual machines!'), gettext('Reset'), function() {
63 64
            $.ajax({
                type: 'POST',
65
                url: $(this).data('url'),
Dányi Bence committed
66
                success: function() {
67 68 69 70 71 72
                    window.location.reload();
                }
            })
        });
    });
    $('.entry .summary').click(toggleDetails);
Dányi Bence committed
73
    if (window.navigator.userAgent.indexOf('cloud-gui') < 0) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87
        $('.connect-vm').click(function(e) {
            e.preventDefault();
            e.stopPropagation();
            get_vm_details($(this).data('id'));
        });
    } else {
        $('.connect-vm').click(function(e) {
            e.stopPropagation();
        });
    }
    $('.rename-vm').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var id = $(this).data('id');
88
        var handler = arguments.callee;
89
        var oldName = $(this).data('name');
90
        var content = $('#vm-' + id + '-name').html();
Bence Dányi committed
91
        var self = this;
Bence Dányi committed
92
        var url = $(this).data('url');
Bence Dányi committed
93 94
        $('#vm-' + id).addClass('editing');
        $(this).unbind('click').click(function(e) {
95 96 97 98 99
            e.preventDefault();
            e.stopPropagation();
            $(this).unbind('click').click(handler);
            $('#vm-' + id + '-name-details').show();
            $('#vm-' + id + '-name').html(content);
Bence Dányi committed
100
            $('#vm-' + id).removeClass('editing');
101
        })
102 103
        $('#vm-' + id + '-name-details').hide();
        $('#vm-' + id + '-name').html('<input type="text" value="' + oldName + '" />\
Bence Dányi committed
104
<input type="submit" value="' + gettext('Rename') + '" data-url="' + url + '"/>');
105 106 107 108 109 110 111 112 113 114 115 116
        $('#vm-' + id + '-name').find('input[type="text"]').click(function(f) {
            f.preventDefault();
            f.stopPropagation();
        }).focus();
        $('#vm-' + id + '-name').find('input[type=submit]').click(function(f) {
            f.preventDefault();
            f.stopPropagation();
            var newName = $(this).prev().val();
            $.ajax({
                type: 'POST',
                data: 'name=' + newName,
                dataType: 'json',
117
                url: $(this).data('url'),
118 119 120
                success: function(data) {
                    $('#vm-' + id + '-name-details').removeAttr('style');
                    $('#vm-' + id + '-name').text(data.name);
121
                    $(self).click(handler);
122
                    $(self).data('name', newName);
Bence Dányi committed
123
                    $('#vm-' + id).removeClass('editing');
124 125 126 127 128 129 130
                }
            });
        })
    });
    $('.try-template').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
131
        new_vm($(this).data('url'));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    });
    $('.stop-vm').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        stop_vm($(this).data('id'), $(this).data('name'));
    });
    $('.resume-vm').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        manage_vm($(this).data('id'), "resume");
    });
    $('.delete-vm').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        delete_vm($(this).data('id'), $(this).data('name'));
    });
    $('.restart-vm').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        restart_vm($(this).data('id'), $(this).data('name'));
    });
153
    $('.entry').on('click', '.renew-suspend-vm', function(e) {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        e.preventDefault();
        e.stopPropagation();
        renew_suspend_vm($(this).data('id'))
    });
    $('.renew-delete-vm').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        renew_delete_vm($(this).data('id'))
    });
    $('#new-vm-button').click(function() {
        $('#modal').show();
        $('#modal-container').html($('#new-vm').html());
        $('#modal-container .entry .summary').click(toggleDetails);
    });
    $('#new-template-button').click(function() {
169
        $.get($(this).data('url'), function(data) {
170 171 172 173
            $('#modal-container').html(data);
        })
        $('#modal').show();
    });
Bence Dányi committed
174
    $('.edit-template').click(function(e) {
175 176
        e.preventDefault();
        e.stopPropagation();
Bence Dányi committed
177
        var id = $(this).data('id');
178 179
        $.ajax({
            type: 'GET',
180
            url: $(this).data('url'),
Bence Dányi committed
181
            success: function(data) {
182 183 184 185
                $('#modal').show();
                $('#modal-container').html(data);
            }
        });
186
    });
187 188 189 190 191 192 193 194 195 196 197 198
    $('#shadow').click(function() {
        $('#modal').hide();
        $('#modal-container').html('');
    })
    $('.template-share').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        $.get('/ajax/share/' + $(this).data('id') + '/' + $(this).data('gid'), function(data) {
            $('#modal-container').html(data);
        })
        $('#modal').show();
    });
Dányi Bence committed
199
    $('.template-unshare').click(function(e) {
200 201 202 203 204 205 206 207 208 209 210 211
        e.stopPropagation();
    });
    $('#old-upload').click(function(e) {
        e.preventDefault();
        $(this).parent().parent().hide().parent().find('#old-upload-form').show();
        return false;
    });
    $('.quota .used').each(function() {
        var s = this;
        $(this).css('backgroundColor', function(w) {
            return 'hsla(' + (120 - parseFloat(w) / 100 * 120).toFixed(0) + ', 100%, 50%, 0.2)';
        }($(this)[0].style.width));
Dányi Bence committed
212
        if (parseInt($(this).css('width')) > 0) $(this).css('borderRight', function(w) {
213 214 215 216 217 218 219 220 221 222 223 224 225
            return '1px solid hsla(' + (120 - parseFloat(w) / 100 * 120).toFixed(0) + ', 100%, 30%, 0.4)';
        }($(this)[0].style.width));
    });
    $('#new-folder').click(function() {
        $('#new-folder-form input')[0].focus();
    });
    $('#new-group').click(function() {
        var content = $('#new-group-wizard').html();
        $('#new-group-wizard').html('');
        $('#modal').show();
        $('#modal-container').html(content);
        $('#shadow').click(function() {
            $('#new-group-wizard').html(content);
226 227 228 229 230
        });
        $('#modal .prev').click(function() {
            $('#modal').hide();
            $('#new-group-wizard').html(content);
        });
231 232 233 234 235 236 237 238 239 240 241 242 243

        function updateSummary() {
            $('#new-group-summary-name').html($('#new-group-name').val());
            $('#new-group-summary-count').html(function(text) {
                var m = text.match(/\s*[a-z][0-9a-z]{5}\s*(\r|\n|$)+/gi);
                return m ? m.length : 0;
            }($('#new-group-members').val()));
            $('#new-group-summary-semester').html($('#new-group-semester')[0].options[$('#new-group-semester')[0].selectedIndex].innerHTML)
        }
        $('#new-group-name').change(updateSummary);
        $('#new-group-semester').change(updateSummary);
        $('#new-group-members').change(updateSummary);
    });
Dányi Bence committed
244 245 246 247 248 249
    $('.shares li').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        e = $(this);
        if (!e.hasClass('description')) {
            if (e.next().is(':hidden')) {
250 251 252 253 254 255 256
                e.next().slideDown(700);
            } else {
                e.next().slideUp(700);
            }
        } else {
            e.slideUp(700);
        }
257
    });
Dányi Bence committed
258 259 260
    $('.shares .edit').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
261 262
        $.ajax({
            type: 'GET',
263
            url: $(this).data('url'),
Dányi Bence committed
264
            success: function(data) {
265 266 267 268
                $('#modal').show();
                $('#modal-container').html(data);
            }
        })
269
    })
270 271 272 273

    // manually open details of the selected vm
    $('.selected-summary').click();

274 275 276 277 278 279 280
    /**
     * Connect button new window
     */

    function get_vm_details(id) {
        $.get('/vm/credentials/' + id, function(data) {
            $('#modal-container').html(data);
Bence Dányi committed
281
            $('#modal-container .host-toggle').click(function(e) {
282
                e.preventDefault();
Bence Dányi committed
283
                if ($(this).find('.v4').is(':hidden')) {
284 285 286 287 288 289 290 291 292 293 294
                    $(this).find('.v4').show();
                    $(this).find('.v6').hide();
                    $(this).parent().next().find('.host').show();
                    $(this).parent().next().find('.ipv4host').hide();
                } else {
                    $(this).find('.v6').show();
                    $(this).find('.v4').hide();
                    $(this).parent().next().find('.host').hide();
                    $(this).parent().next().find('.ipv4host').show();
                }
            });
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        })
        $('#modal').show();
    };
    /**
     * Confirm pop-up window
     */

    function vm_confirm_popup(confirm_message, button_text, success) {
        $('#modal').show();
        $('#modal-container').html(confirm_message + '<br /><input class="ok" type="button" value="' + button_text + '" style="float: left"/><input class="cancel" type="button" value="' + gettext('Cancel') + '" style="float: right" />');
        $('#modal-container .ok').click(function() {
            $('#modal').hide();
            success();
        });
        $('#modal-container .cancel').click(function() {
            $('#modal').hide();
        });
    }
313

Bence Dányi committed
314
    cloud.confirm = vm_confirm_popup;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    /**
     * Manage VM State (STOP)
     */

    function stop_vm(id, name) {
        confirm_message = interpolate(gettext("Are you sure stopping %s?"), ["<strong>" + name + "</strong>"])
        vm_confirm_popup(confirm_message, gettext("Stop"), function() {
            manage_vm(id, "stop")
        });
    }
    /**
     * Manage VM State (DELETE)
     */

    function delete_vm(id, name) {
        confirm_message = interpolate(gettext("Are you sure deleting %s?"), ["<strong>" + name + "</strong>"])
        vm_confirm_popup(confirm_message, gettext("Delete"), function() {
            manage_vm(id, "delete")
        })
    }
    /**
     * Manage VM State (RESET)
     */

    function restart_vm(id, name) {
        confirm_message = interpolate(gettext("Are you sure restarting %s?"), ["<strong>" + name + "</strong>"])
        vm_confirm_popup(confirm_message, gettext("Restart"), function() {
            manage_vm(id, "restart")
        })
    }
    /**
     * Manage VM State (RESUME)
     */

    function resume_vm(id, name) {
        manage_vm(id, "resume")
    }
    /**
     * Renew vm suspend time.
     */

    function renew_suspend_vm(id) {
357 358
        manage_vm(id, "renew/suspend", function(data) {
            //workaround for some strange jquery parse error :o
Bence Dányi committed
359 360
            var foo = $('<div />').append(data);
            $('#vm-' + id + ' .details-container').replaceWith(foo.find('.details-container'));
361
        });
362 363 364 365 366 367 368 369 370 371 372 373
    }
    /**
     * Renew vm deletion time.
     */

    function renew_delete_vm(id) {
        manage_vm(id, "renew/delete")
    }
    /**
     * Manage VM State generic
     */

374
    function manage_vm(id, state, f) {
375 376 377 378
        $.ajax({
            type: 'POST',
            url: '/vm/' + state + '/' + id + '/',
            success: function(data, b, c) {
Bence Dányi committed
379
                if (f) {
380 381
                    f(data);
                } else if (state == "resume") {
382 383 384 385 386 387 388 389 390 391 392
                    window.location.href = '/vm/show/' + id + "/";
                } else {
                    window.location.reload();
                }
            }
        })
    }
    /**
     * New VM
     */

393
    function new_vm(url) {
394 395
        $.ajax({
            type: 'POST',
396
            url: url,
397 398 399 400 401 402 403 404 405 406
            success: function(data, b, xhrRequest) {
                window.location.href = xhrRequest.getResponseHeader("Location");
            }
        })
    }

    /**
     * Template delete
     */

407
    function delete_template_confirm(url, id, name) {
408 409
        confirm_message = interpolate(gettext("Are you sure deleting this %s template?"), ["<strong>" + name + "</strong>"])
        vm_confirm_popup(confirm_message, gettext("Delete"), function() {
410
            delete_template(url, id)
411 412 413 414 415 416
        })
    }
    /**
     * Template delete
     */

417
    function delete_template(url, id) {
418 419
        $.ajax({
            type: 'POST',
420
            url: url,
421 422 423 424 425 426 427
            data: 'id=' + id,
            dataType: 'json',
            statusCode: {
                404: function(data) {
                    alert(data['responseText']);
                },
                200: function(data) {
428 429
                    $("#t" + id).remove();
                    alert(gettext('Template deletion successful!'));
430 431 432 433 434 435 436 437 438
                },

            }
        })
    }

    function hide_group(id) {
        var hidden_groups = JSON.parse(window.localStorage.getItem('hidden_groups')) || {};
        var hidden_groups_for_user = hidden_groups[current_user] || [];
Dányi Bence committed
439
        for (var i in hidden_groups_for_user) {
440
            var hide = hidden_groups_for_user[i];
Dányi Bence committed
441
            if (hide == id) return false;
442 443 444 445 446 447 448 449 450 451
        }
        hidden_groups_for_user.push(id);
        hidden_groups[current_user] = hidden_groups_for_user;
        window.localStorage.setItem('hidden_groups', JSON.stringify(hidden_groups));
        $('#group-' + id).slideUp(700);
    }

    function hide_groups() {
        var hidden_groups = JSON.parse(window.localStorage.getItem('hidden_groups')) || {};
        var hidden_groups_for_user = hidden_groups[current_user] || [];
Dányi Bence committed
452
        for (var i in hidden_groups_for_user) {
453 454 455 456 457 458 459 460 461 462 463 464 465 466
            var hide = hidden_groups_for_user[i];
            $('#group-' + hide).hide();
        }
    }

    function show_hidden_groups() {
        var hidden_groups = JSON.parse(window.localStorage.getItem('hidden_groups')) || {};
        hidden_groups[current_user] = [];
        window.localStorage.setItem('hidden_groups', JSON.stringify(hidden_groups));
    }
    hide_groups();

    function hidden_group_count() {
        var hidden_groups = JSON.parse(window.localStorage.getItem('hidden_groups')) || {};
Dányi Bence committed
467
        return (hidden_groups[current_user] || []).length;
468
    }
Dányi Bence committed
469 470 471 472 473 474 475 476 477 478

    function toggle_box(id) {
        var boxes = JSON.parse(window.localStorage.getItem('hidden_boxes')) || {};
        var user_boxes = boxes[current_user] || [];
        for (var i in user_boxes) {
            var box = user_boxes[i];
            if (box == id) {
                user_boxes.splice(i, 1);
                boxes[current_user] = user_boxes;
                window.localStorage.setItem('hidden_boxes', JSON.stringify(boxes));
Bence Dányi committed
479 480
                $('#toggle-box-' + id).attr('src', '/static/icons/eye-half.png');
                $('#toggle-box-' + id).parent().parent().parent().next().slideDown(700);
Dányi Bence committed
481 482 483 484 485
                return;
            }
        }
        user_boxes.push(id);
        boxes[current_user] = user_boxes;
Bence Dányi committed
486 487 488
        $('#toggle-box-' + id).attr('src', '/static/icons/eye.png');
        $('#toggle-box-' + id).parent().parent().parent().next().slideUp(700);
        console.log($('#toggle-box-' + id).parent().parent().parent().next()[0])
Dányi Bence committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
        window.localStorage.setItem('hidden_boxes', JSON.stringify(boxes));
    }

    function box_hidden(id) {
        var boxes = JSON.parse(window.localStorage.getItem('hidden_boxes')) || {};
        var user_boxes = boxes[current_user] || [];
        for (var i in user_boxes) {
            var box = user_boxes[i];
            if (box == id) {
                return true;
            }
        }
        return false;
    }

Bence Dányi committed
504 505 506
    $('.toggle-box').each(function() {
        var id = $(this).data('id');
        $(this).click(function() {
Dányi Bence committed
507 508
            toggle_box(id);
        });
Bence Dányi committed
509 510
        if (box_hidden(id)) {
            $(this).attr('src', '/static/icons/eye.png');
Dányi Bence committed
511 512
            $(this).parent().parent().parent().next().hide();
        } else {
Bence Dányi committed
513
            $(this).attr('src', '/static/icons/eye-half.png');
Dányi Bence committed
514 515 516 517
        }
    })

    if (hidden_group_count() == 0) {
518 519 520 521 522 523 524
        $('#show-hidden-groups').hide();
    }

    $('.hide-group').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        hide_group($(this).data('id'));
Dányi Bence committed
525
        if ($('#show-hidden-groups').is(':hidden')) {
526 527 528 529 530 531 532 533 534 535
            $('#show-hidden-groups').slideDown(700);
        }
        return false;
    });
    $('#show-hidden-groups').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        show_hidden_groups();
        $('#show-hidden-groups').slideUp(700);
        $('#groups > li').each(function() {
Dányi Bence committed
536
            if ($(this).is(':hidden')) {
537 538 539 540 541 542 543 544 545 546 547 548 549 550
                $(this).slideDown(700);
            }
        })
    })
    $('#new-member').click(function() {
        $('#new-member-form').toggle();
    });
    $('#new-member-form input').click(function(e) {
        e.stopPropagation();
    });
    $('#new-member-form input[type=submit]').click(function() {
        var neptun = $(this).prev().val();
        $.ajax({
            type: 'POST',
551
            url: $(this).data('url'),
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
            data: 'neptun=' + neptun,
            dataType: 'json',
            success: function(data) {
                window.location.reload();
            }
        }).error(function(data) {
            //TODO: fancy modal alert
            alert(JSON.parse(data.responseText).status);
        })
    });

    $('#new-owner').click(function() {
        $('#new-owner-form input[type=text]').focus();
    });
    $('#new-owner-form input').click(function(e) {
        e.stopPropagation();
    });
    $('#new-owner-form input').keyup(function() {
        var timer;
Dányi Bence committed
571 572
        return function(e) {
            var val = $(this).val().split(' ')[0];
573
            var that = this;
574
            clearTimeout(timer);
Dányi Bence committed
575 576
            timer = setTimeout(function() {
                if (val.length < 1) return;
577 578
                $.ajax({
                    type: 'POST',
Dányi Bence committed
579
                    data: 'q=' + val,
580
                    url: $(that).data('url'),
581
                    dataType: 'json',
Dányi Bence committed
582
                    success: function(data) {
583
                        console.log(data);
Dányi Bence committed
584 585 586 587 588
                        $('#new-owner-autocomplete')[0].innerHTML = '<ul>';
                        var el = $('#new-owner-autocomplete')[0];
                        for (var i in data) {
                            var d = data[i];
                            el.innerHTML += '<li>' + d.name + ': ' + d.neptun + ' <input type="button" value="' + gettext('Add owner') + '" data-neptun="' + d.neptun + '" />' + '<div class="clear"></div></li>';
589
                        }
Dányi Bence committed
590 591
                        if (data.length == 0) {
                            el.innerHTML += '<li>' + gettext('Unknown') + ': ' + val + ' <input type="button" value="' + gettext('Add owner') + '" data-neptun="' + val + '" />' + '<div class="clear"></div></li>';
592
                        }
Dányi Bence committed
593 594 595 596
                        el.innerHTML += '</ul>';
                        $(el).find('input').each(function() {
                            var self = this;
                            $(this).click(function(e) {
597 598 599
                                e.stopPropagation();
                                $.ajax({
                                    type: 'POST',
Dányi Bence committed
600 601
                                    data: 'neptun=' + $(self).data('neptun'),
                                    url: '/ajax/group/' + $('#new-owner').data('gid') + '/addOwner/',
602
                                    dataType: 'json',
Dányi Bence committed
603
                                    success: function(data) {
604 605 606 607 608 609 610
                                        window.location.reload();
                                    }
                                })
                            })
                        })
                    }
                });
Dányi Bence committed
611
            }, 1000);
612 613 614 615 616 617 618 619 620 621
            e.stopPropagation();
        }
    }());

    $('#group-members .remove').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var neptun = $(this).data('neptun');
        $.ajax({
            type: 'POST',
622
            url: $(this).data('url'),
623 624 625 626 627 628 629 630 631 632 633
            data: 'neptun=' + neptun,
            success: function(data) {
                $('#member-' + neptun).slideUp(700);
            }
        });
    });
    $('#groups .delete').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var gid = $(this).data('id');
        var name = $(this).data('name');
634
        var url = $(this).data('url');
635 636 637 638 639
        vm_confirm_popup(
        interpolate(
        gettext('Are you sure deleting <strong>%s</strong>'), [name]), gettext('Delete'), function() {
            $.ajax({
                type: 'POST',
640
                url: url,
641 642 643 644 645 646 647 648 649 650
                data: 'gid=' + gid,
                success: function() {
                    $('#group-' + gid).slideUp(700);
                }
            }).error(function() {
                window.location.reload();
            })
        })
    })
})