Passed
Push — web-timed ( 80b983...b83f75 )
by Matt
01:13
created

get.js ➔ getRaw   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 2
nc 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A get.js ➔ ... ➔ http.onreadystatechange 0 6 2
1
/*
2
 *  PyDMXControl: A Python 3 module to control DMX via Python. Featuring fixture profiles and working with uDMX.
3
 *  <https://github.com/MattIPv4/PyDMXControl/>
4
 *  Copyright (C) 2018 Matt Cowley (MattIPv4) ([email protected])
5
 */
6
function getRaw(url, callback) {
7
    var http = new XMLHttpRequest();
8
    http.onreadystatechange = function () {
9
        if (http.readyState === 4) {
10
            var data = JSON.parse(http.responseText);
11
            callback(data);
12
        }
13
    };
14
    http.open("GET", url);
15
    http.send();
16
}
17
18
function getMessageHandle(idBase, dataBase, data) {
19
    var elm = document.getElementById("message-" + idBase);
20
    if (elm) {
21
        clearTimeout(parseInt(elm.getAttribute("data-tm"), 10));
22
        elm.remove();
23
    }
24
    if (dataBase in data) {
25
        elm = document.createElement("h3");
26
        elm.id = "message-" + idBase;
27
        elm.className = "alert";
28
        elm.style.color = (idBase === "error" ? "#DA4453" : "#37BC9B");
29
        elm.innerText = data[dataBase];
30
        var tm = setTimeout(function () {
31
            document.getElementById("message-" + idBase).remove();
32
        }, 15 * 1000);
33
        elm.setAttribute("data-tm", tm.toString(10));
34
        document.body.insertBefore(elm, document.body.children[1]);
35
    }
36
}
37
38
function getElementsHandle(data) {
39
    var elm;
40
    for (var id in data) {
41
        if (!data.hasOwnProperty(id)) {
42
            continue;
43
        }
44
        var text = data[id];
45
        elm = document.getElementById(id);
46
        if (!elm) {
47
            continue;
48
        }
49
        if (elm.tagName.toLowerCase() === "input") {
50
            elm.value = text;
51
        } else {
52
            elm.innerText = text;
53
        }
54
        if ("createEvent" in document) {
55
            var evt = document.createEvent("HTMLEvents");
56
            evt.initEvent("change", false, true);
57
            elm.dispatchEvent(evt);
58
        } else {
59
            elm.fireEvent("onchange");
60
        }
61
    }
62
}
63
64
function getCallback(data) {
65
    if ("elements" in data) {
66
        getElementsHandle(data["elements"]);
67
    }
68
69
    getMessageHandle("success", "message", data);
70
    getMessageHandle("error", "error", data);
71
}
72
73
function get(url) {
74
    getRaw(url, getCallback);
75
}
76
77
(function () {
78
    var elms = [].slice.call(document.getElementsByTagName("a")); // NodeList to Array
79
    elms.forEach(function (elm) {
80
        elm.addEventListener("click", function (e) {
81
            e.preventDefault();
82
            if (elm.getAttribute("data-no-reload") !== null) {
83
                get(elm.getAttribute("href"));
84
            } else {
85
                window.location = elm.getAttribute("href");
86
            }
87
            return false;
88
        }, true);
89
    });
90
}());