Passed
Push — update-web-timed-events ( 845708...ee023e )
by Matt
01:19
created

get.js ➔ getElementsHandle   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
/*
2
 *  PyDMXControl: A Python 3 module to control DMX using uDMX.
3
 *                Featuring fixture profiles, built-in effects and a web control panel.
4
 *  <https://github.com/MattIPv4/PyDMXControl/>
5
 *  Copyright (C) 2019 Matt Cowley (MattIPv4) ([email protected])
6
 */
7
function getRaw(url, callback) {
8
    var http = new XMLHttpRequest();
9
    http.onreadystatechange = function () {
10
        if (http.readyState === 4) {
11
            var data = JSON.parse(http.responseText);
12
            callback(data);
13
        }
14
    };
15
    http.open("GET", url);
16
    http.send();
17
}
18
19
function getMessageHandle(idBase, dataBase, data) {
20
    var elm = document.getElementById("message-" + idBase);
21
    if (elm) {
22
        clearTimeout(parseInt(elm.getAttribute("data-tm"), 10));
23
        elm.remove();
24
    }
25
    if (dataBase in data) {
26
        elm = document.createElement("h3");
27
        elm.id = "message-" + idBase;
28
        elm.className = "alert";
29
        elm.style.color = (idBase === "error" ? "#DA4453" : "#37BC9B");
30
        elm.innerText = data[dataBase];
31
        var tm = setTimeout(function () {
32
            document.getElementById("message-" + idBase).remove();
33
        }, 15 * 1000);
34
        elm.setAttribute("data-tm", tm.toString(10));
35
        document.body.insertBefore(elm, document.body.children[1]);
36
    }
37
}
38
39
function getElementHandle(id, val) {
40
    var elm = document.getElementById(id);
41
    if (elm.tagName.toLowerCase() === "input") {
42
        elm.value = val;
43
    } else {
44
        elm.innerText = val;
45
    }
46
    if ("createEvent" in document) {
47
        var evt = document.createEvent("HTMLEvents");
48
        evt.initEvent("change", false, true);
49
        elm.dispatchEvent(evt);
50
    } else {
51
        elm.fireEvent("onchange");
52
    }
53
}
54
55
function getElementsHandle(data) {
56
    for (var id in data) {
57
        if (!data.hasOwnProperty(id) || !document.getElementById(id)) {
58
            continue;
59
        }
60
        getElementHandle(id, data[id]);
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
}());