cloud.js 21.4 KB
Newer Older
1 2 3
var toggleDetails;
$(function() {
    toggleDetails = cloud.throttle(function() {
Dányi Bence committed
4
        if ($(this).parent('.entry').hasClass('opened')) {
5 6 7 8 9 10 11
            $(this).parent('.entry').removeClass('opened');
            $(this).next('.details').slideUp(700);
        } else {
            $(this).parent('.entry').addClass('opened');
            $(this).next('.details').slideDown(700);
        }
    })
Dányi Bence committed
12
    $('a').click(function(e) {
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
        e.stopPropagation();
    });
    $('.delete-template').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        delete_template_confirm($(this).data('id'), $(this).data('name'));
    });
    $('.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,
                'url': '/ajax/key/delete/',
                'success': function() {
                    $('#key-' + id).slideUp(700);
                }
            });
        });
    });
Dányi Bence committed
35 36
    $('#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() {
37 38 39
            $.ajax({
                type: 'POST',
                url: '/ajax/key/reset/',
Dányi Bence committed
40
                success: function() {
41 42 43 44 45 46
                    window.location.reload();
                }
            })
        });
    });
    $('.entry .summary').click(toggleDetails);
Dányi Bence committed
47
    if (window.navigator.userAgent.indexOf('cloud-gui') < 0) {
48 49 50 51 52 53 54 55 56 57 58 59 60 61
        $('.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');
62
        var handler = arguments.callee;
63
        var oldName = $(this).data('name');
64 65 66 67 68 69 70 71 72
        var content = $('#vm-' + id + '-name').html();
        var self=this;
        $(this).unbind('click').click(function(e){
            e.preventDefault();
            e.stopPropagation();
            $(this).unbind('click').click(handler);
            $('#vm-' + id + '-name-details').show();
            $('#vm-' + id + '-name').html(content);
        })
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        $('#vm-' + id + '-name-details').hide();
        $('#vm-' + id + '-name').html('<input type="text" value="' + oldName + '" />\
<input type="submit" value="' + gettext('Rename') + '" />');
        $('#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',
                url: '/ajax/vm/rename/' + id + '/',
                success: function(data) {
                    $('#vm-' + id + '-name-details').removeAttr('style');
                    $('#vm-' + id + '-name').text(data.name);
92
                    $(self).click(handler);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 143 144 145 146 147 148 149 150 151 152 153 154
                }
            });
        })
    });
    $('.try-template').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        new_vm($(this).data('id'));
    });
    $('.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'));
    });
    $('.renew-suspend-vm').click(function(e) {
        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() {
        $.get('/ajax/templateWizard', function(data) {
            $('#modal-container').html(data);
        })
        $('#modal').show();
    });
    $('#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
155
    $('.template-unshare').click(function(e) {
156 157 158 159 160 161 162 163 164 165 166 167
        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
168
        if (parseInt($(this).css('width')) > 0) $(this).css('borderRight', function(w) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
            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);
        })

        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);
    });
    $('.hidden-password').click(function() {
Dányi Bence committed
197
        if ($(this).attr('type') == 'password') {
198 199
            $(this).attr('type', 'text');
            $(this).addClass('shown');
Dányi Bence committed
200
        } else if (this.selectionStart - this.selectionEnd == 0) {
201

202 203 204 205
            $(this).attr('type', 'password');
            $(this).removeClass('shown');
        }
    });
Dányi Bence committed
206 207 208 209 210 211
    $('.shares li').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        e = $(this);
        if (!e.hasClass('description')) {
            if (e.next().is(':hidden')) {
212 213 214 215 216 217 218
                e.next().slideDown(700);
            } else {
                e.next().slideUp(700);
            }
        } else {
            e.slideUp(700);
        }
219
    });
Dányi Bence committed
220 221 222
    $('.shares .edit').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
223 224
        $.ajax({
            type: 'GET',
Dányi Bence committed
225 226
            url: '/ajax/shareEdit/' + $(this).data('id') + '/',
            success: function(data) {
227 228 229 230
                $('#modal').show();
                $('#modal-container').html(data);
            }
        })
231
    })
232 233 234 235 236 237 238 239 240
    $('.selected-summary').next().show();
    /**
     * Connect button new window
     */

    function get_vm_details(id) {
        $.get('/vm/credentials/' + id, function(data) {
            $('#modal-container').html(data);
            $('#modal-container .hidden-password').click(function() {
Dányi Bence committed
241
                if ($(this).attr('type') == 'password') {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
                    $(this).attr('type', 'text');
                    $(this).addClass('shown');
                } else {
                    $(this).attr('type', 'password');
                    $(this).removeClass('shown');
                }
            })
        })
        $('#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();
        });
    }
    /**
     * 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) {
        manage_vm(id, "renew/suspend")
    }
    /**
     * Renew vm deletion time.
     */

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

    function manage_vm(id, state) {
        $.ajax({
            type: 'POST',
            url: '/vm/' + state + '/' + id + '/',
            success: function(data, b, c) {
Dányi Bence committed
327
                if (state == "resume") {
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
                    window.location.href = '/vm/show/' + id + "/";
                } else {
                    window.location.reload();
                }
            }
        })
    }
    /**
     * New VM
     */

    function new_vm(template_id) {
        $.ajax({
            type: 'POST',
            url: '/ajax/vm/new/' + template_id + '/',
            success: function(data, b, xhrRequest) {
                window.location.href = '/'; //xhrRequest.getResponseHeader("Location");
                //alert(xhrRequest.getResponseHeader("Location"));
                window.location.href = xhrRequest.getResponseHeader("Location");
            }
        })
    }

    /**
     * Template delete
     */

    function delete_template_confirm(id, name) {
        confirm_message = interpolate(gettext("Are you sure deleting this %s template?"), ["<strong>" + name + "</strong>"])
        vm_confirm_popup(confirm_message, gettext("Delete"), function() {
            delete_template(id)
        })
    }
    /**
     * Template delete
     */

    function delete_template(id) {
        $.ajax({
            type: 'POST',
            url: '/ajax/template/delete/',
            data: 'id=' + id,
            dataType: 'json',
            statusCode: {
                404: function(data) {
                    alert(data['responseText']);
                },
                200: function(data) {
                    $("#t" + id).remove()
                },

            }
        })
    }

    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
386
        for (var i in hidden_groups_for_user) {
387
            var hide = hidden_groups_for_user[i];
Dányi Bence committed
388
            if (hide == id) return false;
389 390 391 392 393 394 395 396 397 398
        }
        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
399
        for (var i in hidden_groups_for_user) {
400 401 402 403 404 405 406 407 408 409 410 411 412 413
            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
414
        return (hidden_groups[current_user] || []).length;
415
    }
Dányi Bence committed
416 417 418 419 420 421 422 423 424 425

    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
426 427
                $('#toggle-box-' + id).attr('src', '/static/icons/eye-half.png');
                $('#toggle-box-' + id).parent().parent().parent().next().slideDown(700);
Dányi Bence committed
428 429 430 431 432
                return;
            }
        }
        user_boxes.push(id);
        boxes[current_user] = user_boxes;
Bence Dányi committed
433 434 435
        $('#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
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
        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
451 452 453
    $('.toggle-box').each(function() {
        var id = $(this).data('id');
        $(this).click(function() {
Dányi Bence committed
454 455
            toggle_box(id);
        });
Bence Dányi committed
456 457
        if (box_hidden(id)) {
            $(this).attr('src', '/static/icons/eye.png');
Dányi Bence committed
458 459
            $(this).parent().parent().parent().next().hide();
        } else {
Bence Dányi committed
460
            $(this).attr('src', '/static/icons/eye-half.png');
Dányi Bence committed
461 462 463 464
        }
    })

    if (hidden_group_count() == 0) {
465 466 467 468 469 470 471
        $('#show-hidden-groups').hide();
    }

    $('.hide-group').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        hide_group($(this).data('id'));
Dányi Bence committed
472
        if ($('#show-hidden-groups').is(':hidden')) {
473 474 475 476 477 478 479 480 481 482
            $('#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
483
            if ($(this).is(':hidden')) {
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
                $(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',
            url: '/ajax/group/' + $(this).data('id') + '/add/',
            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
518 519
        return function(e) {
            var val = $(this).val().split(' ')[0];
520
            clearTimeout(timer);
Dányi Bence committed
521 522
            timer = setTimeout(function() {
                if (val.length < 1) return;
523 524
                $.ajax({
                    type: 'POST',
Dányi Bence committed
525
                    data: 'q=' + val,
526 527
                    url: '/ajax/group/autocomplete/',
                    dataType: 'json',
Dányi Bence committed
528
                    success: function(data) {
529
                        console.log(data);
Dányi Bence committed
530 531 532 533 534
                        $('#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>';
535
                        }
Dányi Bence committed
536 537
                        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>';
538
                        }
Dányi Bence committed
539 540 541 542
                        el.innerHTML += '</ul>';
                        $(el).find('input').each(function() {
                            var self = this;
                            $(this).click(function(e) {
543 544 545
                                e.stopPropagation();
                                $.ajax({
                                    type: 'POST',
Dányi Bence committed
546 547
                                    data: 'neptun=' + $(self).data('neptun'),
                                    url: '/ajax/group/' + $('#new-owner').data('gid') + '/addOwner/',
548
                                    dataType: 'json',
Dányi Bence committed
549
                                    success: function(data) {
550 551 552 553 554 555 556
                                        window.location.reload();
                                    }
                                })
                            })
                        })
                    }
                });
Dányi Bence committed
557
            }, 1000);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
            e.stopPropagation();
        }
    }());
    $('#new-owner-form input[type=submit]').click(function() {
        var neptun = $(this).prev().val();
        $.ajax({
            type: 'POST',
            url: '/ajax/group/' + $(this).data('id') + '/add/',
            data: 'neptun=' + neptun,
            dataType: 'json',
            success: function(data) {
                window.location.reload();
            }
        }).error(function(data) {
            //TODO: fancy modal alert
            alert(JSON.parse(data.responseText).status);
        })
    });

    $('#group-members .remove').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var neptun = $(this).data('neptun');
        $.ajax({
            type: 'POST',
            url: '/ajax/group/' + $(this).data('gid') + '/remove/',
            data: 'neptun=' + neptun,
            success: function(data) {
                $('#member-' + neptun).slideUp(700);
            }
        });
    });
    /*$('#group-owners .remove').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        var neptun = $(this).data('neptun');
        $.ajax({
            type: 'POST',
            url: '/ajax/group/' + $(this).data('gid') + '/remove/',
            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');
        vm_confirm_popup(
        interpolate(
        gettext('Are you sure deleting <strong>%s</strong>'), [name]), gettext('Delete'), function() {
            $.ajax({
                type: 'POST',
                url: '/ajax/group/delete/',
                data: 'gid=' + gid,
                success: function() {
                    $('#group-' + gid).slideUp(700);
                }
            }).error(function() {
                window.location.reload();
            })
        })
    })
})