Passed
Push — web-timed ( 1d9e3e...80b983 )
by Matt
01:14
created

get.js ➔ ... ➔ get_callback   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 31
rs 7.952
c 0
b 0
f 0
cc 7
nop 1
nc 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 getMessageHandle(idBase, dataBase, data) {
7
    var elm = document.getElementById("message-" + idBase);
8
    if (elm) {
9
        clearTimeout(parseInt(elm.getAttribute("data-tm"), 10));
10
        elm.remove();
11
    }
12
    if (dataBase in data) {
13
        elm = document.createElement("h3");
14
        elm.id = "message-" + idBase;
15
        elm.className = "alert";
16
        elm.style.color = (idBase === "error" ? "#DA4453" : "#37BC9B");
17
        elm.innerText = data[dataBase];
18
        var tm = setTimeout(function () {
19
            document.getElementById("message-" + idBase).remove();
20
        }, 15 * 1000);
21
        elm.setAttribute("data-tm", tm.toString(10));
22
        document.body.insertBefore(elm, document.body.children[1]);
23
    }
24
}
25
26
function get_raw(url, callback) {
27
    var http = new XMLHttpRequest();
28
    http.onreadystatechange = function () {
29
        if (http.readyState === 4) {
30
            var data = JSON.parse(http.responseText);
31
            callback(data);
32
        }
33
    };
34
    http.open("GET", url);
35
    http.send();
36
}
37
38
function get(url) {
39
    function get_callback(data) {
40
        var elm;
41
42
        if ("elements" in data) {
43
            for (var id in data["elements"]) {
44
                if (!data["elements"].hasOwnProperty(id)) {
45
                    continue;
46
                }
47
                var text = data["elements"][id];
48
                elm = document.getElementById(id);
49
                if (!elm) {
50
                    continue;
51
                }
52
                if (elm.tagName.toLowerCase() === "input") {
53
                    elm.value = text;
54
                } else {
55
                    elm.innerText = text;
56
                }
57
                if ("createEvent" in document) {
58
                    var evt = document.createEvent("HTMLEvents");
59
                    evt.initEvent("change", false, true);
60
                    elm.dispatchEvent(evt);
61
                } else {
62
                    elm.fireEvent("onchange");
63
                }
64
            }
65
        }
66
67
        getMessageHandle("success", "message", data);
68
        getMessageHandle("error", "error", data);
69
    }
70
71
    get_raw(url, get_callback);
72
}
73
74
(function () {
75
    var elms = [].slice.call(document.getElementsByTagName("a")); // NodeList to Array
76
    elms.forEach(function (elm) {
77
        elm.addEventListener("click", function (e) {
78
            e.preventDefault();
79
            if (elm.getAttribute("data-no-reload") !== null) {
80
                get(elm.getAttribute("href"));
81
            } else {
82
                window.location = elm.getAttribute("href");
83
            }
84
            return false;
85
        }, true);
86
    });
87
}());