Passed
Pull Request — master (#19)
by Matt
02:16 queued 01:04
created

get.js ➔ getElementsHandle   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 21
rs 8.6666
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) || !document.getElementById(id)) {
42
            continue;
43
        }
44
        elm = document.getElementById(id);
45
        if (elm.tagName.toLowerCase() === "input") {
46
            elm.value = data[id];
47
        } else {
48
            elm.innerText = data[id];
49
        }
50
        if ("createEvent" in document) {
51
            var evt = document.createEvent("HTMLEvents");
52
            evt.initEvent("change", false, true);
53
            elm.dispatchEvent(evt);
54
        } else {
55
            elm.fireEvent("onchange");
56
        }
57
    }
58
}
59
60
function getCallback(data) {
61
    if ("elements" in data) {
62
        getElementsHandle(data["elements"]);
63
    }
64
65
    getMessageHandle("success", "message", data);
66
    getMessageHandle("error", "error", data);
67
}
68
69
function get(url) {
70
    getRaw(url, getCallback);
71
}
72
73
(function () {
74
    var elms = [].slice.call(document.getElementsByTagName("a")); // NodeList to Array
75
    elms.forEach(function (elm) {
76
        elm.addEventListener("click", function (e) {
77
            e.preventDefault();
78
            if (elm.getAttribute("data-no-reload") !== null) {
79
                get(elm.getAttribute("href"));
80
            } else {
81
                window.location = elm.getAttribute("href");
82
            }
83
            return false;
84
        }, true);
85
    });
86
}());