common.js 3.13 KB
Newer Older
Marco Mariani's avatar
Marco Mariani committed
1 2 3 4
/*jslint undef: true */
/*global $, window, $SCRIPT_ROOT */
/* vim: set et sts=4: */

5
/*Common javascript function*/
Marco Mariani's avatar
Marco Mariani committed
6 7 8 9 10 11 12 13 14 15 16
String.prototype.toHtmlChar = function () {
    "use strict";
    var c = {
        '<': '&lt;',
        '>': '&gt;',
        '&': '&amp;',
        '"': '&quot;',
        "'": '&#039;',
        '#': '&#035;'
    };
    return this.replace(/[<&>'"#]/g, function (s) { return c[s]; });
17
};
18
String.prototype.trim = function () {
Marco Mariani's avatar
Marco Mariani committed
19
    "use strict";
20
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
21
};
Alain Takoudjou's avatar
Alain Takoudjou committed
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36
String.prototype.hashCode = function(){
    if (Array.prototype.reduce){
        return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
    }
    var hash = 0;
    if (this.length === 0) return hash;
    for (var i = 0; i < this.length; i++) {
        var character  = this.charCodeAt(i);
        hash  = ((hash<<5)-hash)+character;
        hash = hash & hash; // Convert to 32bit integer
    }
    return hash;
}

37 38
/****************************************/
function setInput($elt) {
Marco Mariani's avatar
Marco Mariani committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    "use strict";
    if (!$elt) {
        $elt = $('input[type="text"], input[type="password"]');
    }
    $elt.addClass("idleField");
    $elt.focus(function () {
        $(this).removeClass("idleField").addClass("focusField");
        if (this.value === this.defaultValue) {
            this.value = '';
        }
        if (this.value !== this.defaultValue) {
            this.select();
        }
    });
    $elt.blur(function () {
        $(this).removeClass("focusField").addClass("idleField");
        if ($.trim(this.value) === '') {
            this.value = (this.defaultValue || '');
        }
    });
59 60
}

61
/*******************Bind remove all button*******************/
Marco Mariani's avatar
Marco Mariani committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
function bindRemove() {
    "use strict";
    $("a#removeSr").click(function () {
        if (!window.confirm("Do you really want to remove all software release?")) {
            return false;
        }
        window.location.href = $SCRIPT_ROOT + '/removeSoftware';
    });
    $("a#removeIst").click(function () {
        if (!window.confirm("Do you really want to remove all computer partition?")) {
            return false;
        }
        window.location.href = $SCRIPT_ROOT + '/removeInstance';
    });
76 77
}

78 79
/**************************/
(function ($, document, window) {
Marco Mariani's avatar
Marco Mariani committed
80
    "use strict";
81
    $.extend($.fn, {
Marco Mariani's avatar
Marco Mariani committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        slideBox: function (state) {
            if (!state) {
                state = "hide";
            }
            var header = $("#" + $(this).attr('id') + ">h2"),
                box = $("#" + $(this).attr('id') + ">div");
            header.addClass(state);
            if (state === "hide") {
                box.css('display', 'none');
            }
            header.click(function () {
                var state = box.css("display");
                if (state === "none") {
                    box.slideDown("normal");
                    header.removeClass("hide");
                    header.addClass("show");
                } else {
                    box.slideUp("normal");
                    header.removeClass("show");
                    header.addClass("hide");
                }
            });
        }
105
    });
Marco Mariani's avatar
Marco Mariani committed
106
}(jQuery, document, this));
107