From 16e584cc8dc057290e14f829b507fbbef3cdf60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Pu=C5=A1nik?= Date: Mon, 5 Oct 2015 15:57:43 +0200 Subject: handle phonegap native mobile app --- config.xml | 177 ++++++++++++++++++++++++++++++++++++++---------------- cordova.js | 3 + index.html | 6 +- js/codeq/comms.js | 4 +- js/codeq/core.js | 84 +------------------------- js/codeq/init.js | 100 ++++++++++++++++++++++++++++++ 6 files changed, 236 insertions(+), 138 deletions(-) create mode 100644 cordova.js create mode 100644 js/codeq/init.js diff --git a/config.xml b/config.xml index a22f584..2664448 100644 --- a/config.xml +++ b/config.xml @@ -3,8 +3,8 @@ + id = "si.uni-lj.fri.gameteam.codeq" + version = "0.1.0"> - + + + - + + - - - - - - - - - - - - - - - - - - - + + @@ -76,32 +91,90 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 100 @@ -121,9 +194,11 @@ --> - - + + + + diff --git a/cordova.js b/cordova.js new file mode 100644 index 0000000..1aa58fd --- /dev/null +++ b/cordova.js @@ -0,0 +1,3 @@ +// differentiates between a native Phonegap/Cordova app, and a web app +// a Phonegap/Cordova build injects a native version of this file +codeq.isWebApp = true; \ No newline at end of file diff --git a/index.html b/index.html index 6918bfa..bbba5f5 100644 --- a/index.html +++ b/index.html @@ -468,8 +468,6 @@ - - @@ -478,6 +476,10 @@ + + + + diff --git a/js/codeq/comms.js b/js/codeq/comms.js index ad2376e..3671e95 100644 --- a/js/codeq/comms.js +++ b/js/codeq/comms.js @@ -198,7 +198,7 @@ }, ajaxPrefix, ajaxDataPrefix, ajaxResPrefix; - ajaxPrefix = location.pathname; + ajaxPrefix = codeq.ajaxPrefix; if (!ajaxPrefix) ajaxPrefix = '/'; else if (ajaxPrefix[ajaxPrefix.length - 1] !== '/') { ajaxPrefix = ajaxPrefix.split('/'); @@ -233,7 +233,7 @@ else { // create a new connection connectPromise = deferred; - socket = eio('ws://' + location.host, { + socket = eio(codeq.eioHost, { 'upgrade': true, 'path': '/ws', 'transports': ['polling', 'websocket'] diff --git a/js/codeq/core.js b/js/codeq/core.js index 2d88f18..02bc2f0 100644 --- a/js/codeq/core.js +++ b/js/codeq/core.js @@ -393,6 +393,7 @@ 'en': 'English', 'sl': 'Slovenščina' }, + 'isWebApp': false, // this is a PhoneGap/Cordova build, will be overridden in cordova.js for webapp 'getLang': function () { return lang; }, @@ -570,87 +571,4 @@ } } }; - - 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 - // ================================================================================ - - $(document).ready(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) { - resources = resourceTree; // save the loaded resource tree to the internal variable - - 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(); - }); })(); diff --git a/js/codeq/init.js b/js/codeq/init.js new file mode 100644 index 0000000..b7e903c --- /dev/null +++ b/js/codeq/init.js @@ -0,0 +1,100 @@ +(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) { + resources = resourceTree; // save the loaded resource tree to the internal variable + + 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; + codeq.eioHost= 'ws://' + location.host; + } + else { + // we are a phonegap native app + document.addEventListener("deviceready", Boot, false); + codeq.ajaxPrefix = 'http://codeq.si/'; + codeq.eioHost= 'ws://codeq.si'; + } + +})(); -- cgit v1.2.1