summaryrefslogtreecommitdiffstats
path: root/browser/welcome/welcome.js
blob: 89ab11e3f4bd6bc921f2f9666d5801b7dbbb1fdd (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
window.onload = onLoaded;

function onLoaded() {
    var elem;
    window.addEventListener('message', onMessage, false);

    elem = document.getElementById('slide-1-button');
    if (elem)
	elem.onclick = function() {
            onSlideClick('slide-2-indicator', true);
	};

    elem = document.getElementById('slide-2-button');
    if (elem)
	elem.onclick = function() {
            onSlideClick('slide-3-indicator', true);
	};

    elem = document.getElementById('slide-3-button');
    if (elem)
	elem.onclick = function() {
            onClose();
	};

    elem = document.getElementById('slide-1-indicator');
    if (elem)
	elem.onclick = function() {
            onSlideClick('slide-1-indicator');
	};

    elem = document.getElementById('slide-2-indicator');
    if (elem)
	elem.onclick = function() {
            onSlideClick('slide-2-indicator');
	};

    elem = document.getElementById('slide-3-indicator');
    if (elem)
	elem.onclick = function() {
            onSlideClick('slide-3-indicator');
	};

    if (window.parent !== window.self) {
        var message = {
            MessageId: 'welcome-translate',
            strings: {}
        };
        getTranslatable(document.body, message.strings);
        window.parent.postMessage(JSON.stringify(message), '*');
    }
}

function onClose() {
    if (window.parent !== window.self) {
        window.parent.postMessage('{"MessageId":"welcome-close"}', '*');
    }
}

function onSlideClick(e, isButton = false) {
    for (var i = 1; i < 4; i++)
        document.getElementById('slide-' + i + '-indicator').classList.remove("active");

    document.getElementById(e).classList.add("active");
    if (isButton)
        document.location = '#' + e.replace('-indicator', '');
}

function onMessage(e) {
    try {
        var msg = JSON.parse(e.data);
        if (window.parent !== window.self && msg.MessageId === 'welcome-translate') {
            setTranslatable(document.body, msg.strings);
            window.parent.postMessage('{"MessageId":"welcome-show"}', '*');
        }
    } catch (err) {
        return;
    }
}

function getTranslatable(root, strings) {
    var children = root.children;
    for (var i = 0; i < children.length; ++i) {
        if (children[i].dataset.translate === 'true') {
            strings[children[i].innerHTML.trim().replace('\n', '')] = '';
        }
        getTranslatable(children[i], strings);
    }
}

function setTranslatable(root, strings) {
    var children = root.children;
    for (var i = 0; i < children.length; ++i) {
        if (children[i].dataset.translate === 'true') {
            children[i].innerHTML = strings[children[i].innerHTML.trim().replace('\n', '')];
        }
        setTranslatable(children[i], strings);
    }
}