summaryrefslogtreecommitdiff
path: root/js/codeq/init.js
blob: f34382ad28689952bef9e3cb25921cb8a126a230 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* CodeQ: an online programming tutor.
   Copyright (C) 2015,2016 UL FRI

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

(function () {
    "use strict";
    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.template.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.fire('layoutchange'); // ensure a layout is set
                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);
        // production
        //codeq.ajaxPrefix = 'http://codeq.si/';
        //codeq.eioHost= 'ws://codeq.si';
        // dev
        codeq.ajaxPrefix = 'http://212.235.189.51:22180/';
        codeq.eioHost= 'ws://212.235.189.51:22180';
    }

})();