aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/mist/assets/muted/muted.js
blob: 72e858d7ae29f57f87300f5aa9317131a06a545b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Helper function for generating pseudo callbacks and sending data to the QML part of the application
function postData(data, cb) {
    data._seed = Math.floor(Math.random() * 1000000)
    if(cb) {
        Muted._callbacks[data._seed] = cb;
    }

    if(data.args === undefined) {
        data.args = [];
    }

    navigator.qt.postMessage(JSON.stringify(data));
}

window.Muted = {
    prototype: Object(),
}

window.Muted._callbacks = {}
window.Muted._onCallbacks = {}

function debug(/**/) {
    console.log("hello world")

    var args = arguments;
    var msg = ""
    for(var i = 0; i < args.length; i++){
        if(typeof args[i] == "object") {
            msg += " " + JSON.stringify(args[i])
        } else {
            msg += args[i]
        }
    }

    document.querySelector("#debugger").innerHTML += "<div class='line'><div class='col1'></div><div class='col2'>"+msg+"</div></div>";
}
console.log = function() {
    var args = []
    for(var i = 0; i < arguments.length; i++) {
        args.push(arguments[i]);
    }
    postData({call:"log", args:args})
}

navigator.qt.onmessage = function(ev) {
    var data = JSON.parse(ev.data)

    if(data._event !== undefined) {
        Muted.trigger(data._event, data.data);
    } else {
        if(data._seed) {
            var cb = Muted._callbacks[data._seed];
            if(cb) {
                // Call the callback
                cb(data.data);
                // Remove the "trigger" callback
                delete Muted._callbacks[ev._seed];
            }
        }
    }
}