Passed
Push — master ( cc7767...b0feda )
by Stefan
11:56
created

popup_redirect.js ➔ createMsgbox   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 34
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 16
nop 2
dl 0
loc 34
rs 8.2266
c 0
b 0
f 0
1
/* 
2
 *******************************************************************************
3
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
4
 * and GN4-2 consortia
5
 *
6
 * License: see the web/copyright.php file in the file structure
7
 *******************************************************************************
8
 */
9
10
/****************************************************************
11
 *                                                              *
12
 *                      Notification Handling                   *
13
 *                                                              *
14
 ****************************************************************/
15
16
17
/* Get Height of the Browser Window */
18
19
function getWindowHeight() {
20
    var windowHeight = 0;
21
    if (typeof (window.innerHeight) === 'number') {
22
        windowHeight = window.innerHeight;
23
    } else {
24
        if (document.documentElement && document.documentElement.clientHeight) {
25
            windowHeight = document.documentElement.clientHeight;
26
        } else {
27
            if (document.body && document.body.clientHeight) {
28
                windowHeight = document.body.clientHeight;
29
            }
30
        }
31
    }
32
    return windowHeight;
33
}
34
35
36
/* Center an element in the browser window */
37
38
function centerElement(el) {
39
    if (document.getElementById) {
40
        var windowHeight = getWindowHeight();
41
        if (windowHeight > 0) {
42
            var contentHeight = el.offsetHeight;
43
            if (windowHeight - contentHeight > 0) {
44
                el.parentNode.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
45
            }
46
        }
47
    }
48
}
49
50
51
/* Display errors/warnings/infos in an overlay box, */
52
53
function createMsgbox(type, onclick) {
54
    var body = document.getElementsByTagName("body")[0];
55
    if (document.getElementById("overlay") === null) {
56
        var overlay = document.createElement("div");
57
        overlay.setAttribute("id", "overlay");
58
        body.appendChild(overlay);
59
    } else {
60
        body.removeChild(document.getElementById("msgbox"));
61
    }
62
    var msgbox = document.createElement("div");
63
    msgbox.setAttribute("id", "msgbox");
64
    var div = document.createElement("div");
65
    var msg = document.createElement("div");
66
    if (type === "err") {
67
        msg.setAttribute("id", "errorbox");
68
    } else if (type === "warn") {
69
        msg.setAttribute("id", "warnbox");
70
    } else if (type === "info") {
71
        msg.setAttribute("class", "graybox");
72
    }
73
    var img = document.createElement("img");
74
    img.setAttribute("src", "../resources/images/icons/button_cancel.png");
75
    img.setAttribute("alt", "cancel");
76
    if (onclick) {
77
        img.setAttribute("onclick", "removeMsgbox(); " + onclick);
78
    } else {
79
        img.setAttribute("onclick", "removeMsgbox()");
80
    }
81
    msg.appendChild(img);
82
    div.appendChild(msg);
83
    msgbox.appendChild(div);
84
    body.appendChild(msgbox);
85
    return msg;
86
}
87
88
89
function removeMsgbox() {
90
    var body = document.getElementsByTagName("body")[0];
91
    body.removeChild(document.getElementById("overlay"));
92
    body.removeChild(document.getElementById("msgbox"));
93
}
94
95
96
function addEvent(elem, type, eventHandle) {
97
    if (elem === null || elem === undefined) {
98
        return;
99
    }
100
    if (elem.addEventListener) {
101
        elem.addEventListener(type, eventHandle, false);
102
    } else if (elem.attachEvent) {
103
        elem.attachEvent("on" + type, eventHandle);
104
    }
105
}
106
107
108
function overlay_resize() {
109
    var el = document.getElementById("msgbox");
110
    if (!el || !el.firstChild || !el.firstChild.firstChild) {
111
        return;
112
    }
113
    centerElement(el.firstChild.firstChild);
114
}
115
116
addEvent(window, "resize", overlay_resize);
117
118
function popupRedirectWindow(form) {
119
    postXML(createWindow, form);
120
}
121
122
function createWindow() {
123
    if (this.readyState === 4 && this.status === 200) {
124
        var infoBox;
125
        infoBox = createMsgbox("info");
126
        infoBox.innerHTML += this.responseText;
127
        centerElement(infoBox);
128
    }
129
}