summaryrefslogtreecommitdiff
path: root/js/codeq/init.js
blob: 811d781ea96a94fa2c50d04279f5fff6aa4b35c1 (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
99
100
101
(function () {


    var loadGuiTranslations = function () {
        var langs = codeq.availableLangs,
            loaders = [],
            loaderLangs = [],
            i, lang;
        for (i = langs.length - 1; i >= 0; i--) {
            lang = langs[i];
            loaders.push(codeq.comms.getGuiTranslation(lang));
            loaderLangs.push(lang);
        }
        return Q.all(loaders)
            .then(function (results) {
                // convert translations into something that the translation module can use
                var dictionary = {},
                    i, json, key, lang, translations;
                for (i = results.length - 1; i >= 0; i--) {
                    json = results[i];
                    lang = loaderLangs[i];
                    for (key in json) {
                        if (!json.hasOwnProperty(key)) continue;
                        translations = dictionary[key];
                        if (translations) translations[lang] = json[key];
                        else {
                            translations = {};
                            dictionary[key] = translations;
                            translations[lang] = json[key];
                        }
                    }
                }
                // ensure each key contains all translations
                for (key in dictionary) {
                    if (!dictionary.hasOwnProperty(key)) continue;
                    translations = dictionary[key];
                    for (i = langs.length - 1; i >= 0; i--) {
                        lang = langs[i];
                        if (!(lang in translations)) translations[lang] = "(Untranslated: " + lang + ")";
                    }
                }
                // register with the system
                codeq.tr.registerDictionary('gui', dictionary);
            });
    };

    // ================================================================================
    // The boot sequence
    // ================================================================================

    var Boot = function () {
        // set the language
        var navigatorLang = navigator.language || navigator.browserLanguage, // language reported by browser
            lang = null, // the translation language that will be chosen
            key;
        if (typeof navigatorLang === 'string') {
            navigatorLang = navigatorLang.split('-')[0]; // truncate the language variant, in eg. en-US
        }
        else navigatorLang = 'en';
        for (key in codeq.supportedLangs) {
            if (!codeq.supportedLangs.hasOwnProperty(key)) continue;
            if (key === navigatorLang) lang = key; // we support the browser's language
            codeq.availableLangs.push(key);
        }

        // the boot chain of async handlers: must be a sequence of .then() terminated with a .fail().done()
        loadGuiTranslations()
            .then(codeq.comms.getResourceTree)
            .then(function (resourceTree) {
                codeq.setResources(resourceTree); // save the loaded resource tree

                codeq.fire('init'); // tell any interested modules that we are now initialized, perhaps they want to initialize too
                codeq.setLang(lang || 'en'); // initial language setting, this also translates the GUI
                // go to login
                codeq.globalStateMachine.transition('login');

                //For performance reasons, the Tooltip and Popover data-apis are opt-in, meaning you must initialize them yourself.
                $('[data-toggle="popover"]').popover()
            })
            .fail(function (e) {
                codeq.log.error('CodeQ failed to start: ' + e, e);
                alert('CodeQ failed to start: ' + e);
            })
            .done();
    };

    if (codeq.isWebApp) {
        // we are a web app
        $(window).on('load', Boot);
        codeq.ajaxPrefix = location.pathname;
        if (location.protocol == 'https:') codeq.eioHost= 'wss://' + location.host;
        else codeq.eioHost= 'ws://' + location.host;
    }
    else {
        // we are a phonegap native app
        document.addEventListener("deviceready", Boot, false);
        codeq.ajaxPrefix = 'http://codeq.si/';
        codeq.eioHost= 'wss://codeq.si';
    }

})();