2014-02-24 11:26:30 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, Doodle3D
|
|
|
|
* This software is licensed under the terms of the GNU GPL v2 or later.
|
|
|
|
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
|
|
|
|
*/
|
2014-04-24 12:46:26 +02:00
|
|
|
|
2014-05-12 14:44:21 +02:00
|
|
|
(function (w) {
|
2014-04-24 12:46:26 +02:00
|
|
|
var _page;
|
2014-04-25 16:24:25 +02:00
|
|
|
var _list;
|
2014-04-24 12:46:26 +02:00
|
|
|
var _title;
|
|
|
|
var _intro;
|
2014-04-25 16:24:25 +02:00
|
|
|
var _drawItem;
|
2014-05-23 19:09:46 +02:00
|
|
|
var _updateItem;
|
2014-04-25 16:24:25 +02:00
|
|
|
var _joinNetworkItem;
|
2014-05-07 17:15:48 +02:00
|
|
|
var _defaultItems;
|
2014-04-25 16:24:25 +02:00
|
|
|
|
2014-04-24 12:46:26 +02:00
|
|
|
var _networkStatus;
|
|
|
|
var _networkAPI = new NetworkAPI();
|
2014-05-23 19:09:46 +02:00
|
|
|
var _updateAPI = new UpdateAPI();
|
2014-04-24 12:46:26 +02:00
|
|
|
var _boxData = {};
|
|
|
|
var _retryRetrieveStatusDelay;
|
|
|
|
var _retryRetrieveStatusDelayTime = 3000;
|
|
|
|
var PAGE_ID = "#box";
|
2014-02-24 11:26:30 +01:00
|
|
|
|
|
|
|
var _self = this;
|
2014-04-24 12:46:26 +02:00
|
|
|
|
|
|
|
$.mobile.document.on( "pageinit", PAGE_ID, function( event, data ) {
|
|
|
|
//console.log("Box page pageinit");
|
|
|
|
_page = $(this);
|
2014-04-25 16:24:25 +02:00
|
|
|
_list = _page.find("ul[data-role=listview]");
|
2014-04-24 12:46:26 +02:00
|
|
|
_title = _page.find(".ui-title");
|
|
|
|
_intro = _page.find(".intro");
|
2014-02-24 11:26:30 +01:00
|
|
|
|
2014-05-07 17:15:48 +02:00
|
|
|
_defaultItems = _list.children();
|
|
|
|
_drawItem = _list.find("#drawItem");
|
2014-05-23 19:09:46 +02:00
|
|
|
_updateItem = _list.find("#updateItem");
|
2014-05-07 17:15:48 +02:00
|
|
|
_joinNetworkItem = _list.find("#joinNetworkItem");
|
2014-05-13 14:46:35 +02:00
|
|
|
|
|
|
|
// make sure draw link is opened in same WebApp (added to homescreen)
|
|
|
|
// and it doesn't start a browser
|
|
|
|
$.stayInWebApp("#box #drawItem a",true);
|
2014-04-24 12:46:26 +02:00
|
|
|
});
|
|
|
|
$.mobile.document.on( "pagebeforeshow", PAGE_ID, function( event, data ) {
|
|
|
|
console.log("Box page pagebeforeshow");
|
|
|
|
_boxData = d3d.util.getPageParams(PAGE_ID);
|
2014-05-14 12:00:02 +02:00
|
|
|
if(_boxData === undefined) {
|
|
|
|
$.mobile.changePage("#boxes");
|
|
|
|
return;
|
|
|
|
}
|
2014-04-24 12:46:26 +02:00
|
|
|
var boxURL = "http://"+_boxData.localip;
|
2014-05-07 23:36:31 +02:00
|
|
|
//console.log(" _boxData: ",_boxData);
|
2014-02-24 11:26:30 +01:00
|
|
|
|
2014-04-24 12:46:26 +02:00
|
|
|
_title.text(_boxData.wifiboxid);
|
2014-02-24 11:26:30 +01:00
|
|
|
|
2014-04-24 12:46:26 +02:00
|
|
|
var drawLink = (_boxData.link)? _boxData.link : boxURL;
|
|
|
|
_page.find("#drawItem a").attr("href",drawLink);
|
2014-02-24 11:26:30 +01:00
|
|
|
|
2014-04-24 12:46:26 +02:00
|
|
|
_networkAPI.init(boxURL);
|
2014-05-23 19:09:46 +02:00
|
|
|
_updateAPI.init(boxURL);
|
2015-06-17 10:31:19 +02:00
|
|
|
|
|
|
|
setNetworkStatus(NetworkAPI.STATUS.CONNECTED);
|
|
|
|
retrieveNetworkStatus();
|
2014-04-24 12:46:26 +02:00
|
|
|
});
|
2014-05-12 14:53:01 +02:00
|
|
|
$.mobile.document.on( "pagebeforehide", PAGE_ID, function( event, data ) {
|
2014-05-12 14:43:59 +02:00
|
|
|
clearTimeout(_retryRetrieveStatusDelay);
|
|
|
|
});
|
|
|
|
|
2014-04-24 12:46:26 +02:00
|
|
|
|
|
|
|
function retrieveNetworkStatus() {
|
|
|
|
_networkAPI.status(function(data) {
|
2014-04-25 16:24:25 +02:00
|
|
|
data.status = parseInt(data.status,10);
|
2014-05-01 11:37:25 +02:00
|
|
|
setNetworkStatus(data.status);
|
2014-04-24 12:46:26 +02:00
|
|
|
}, function() {
|
|
|
|
clearTimeout(_retryRetrieveStatusDelay);
|
|
|
|
_retryRetrieveStatusDelay = setTimeout(_self.retrieveStatus, _retryRetrieveStatusDelayTime); // retry after delay
|
|
|
|
});
|
2014-02-24 11:26:30 +01:00
|
|
|
}
|
2014-05-01 11:37:25 +02:00
|
|
|
function setNetworkStatus(status) {
|
2014-05-07 17:15:48 +02:00
|
|
|
console.log(PAGE_ID+":setNetworkStatus: ",status);
|
2014-05-01 11:37:25 +02:00
|
|
|
var introText = "";
|
2014-04-24 12:46:26 +02:00
|
|
|
if(status === NetworkAPI.STATUS.CONNECTED) { // online
|
2014-05-01 18:37:29 +02:00
|
|
|
//console.log("online");
|
2014-04-28 16:55:57 +02:00
|
|
|
_drawItem.find("a").text("Draw");
|
2014-04-24 12:46:26 +02:00
|
|
|
|
2014-05-07 17:15:48 +02:00
|
|
|
// display the right buttons
|
|
|
|
_defaultItems.toggleClass("ui-screen-hidden",false);
|
2014-05-01 11:37:25 +02:00
|
|
|
_joinNetworkItem.toggleClass("ui-screen-hidden",true);
|
2014-05-23 19:09:46 +02:00
|
|
|
|
|
|
|
var updateLink = _updateItem.find("a").attr("href");
|
|
|
|
updateLink = d3d.util.replaceURLParameters(updateLink,_boxData);
|
|
|
|
_updateItem.find("a").attr("href",updateLink);
|
2014-05-01 11:37:25 +02:00
|
|
|
|
2014-05-23 22:59:38 +02:00
|
|
|
retrieveUpdateStatus();
|
|
|
|
|
2014-04-24 12:46:26 +02:00
|
|
|
} else { // offline
|
2014-05-01 18:37:29 +02:00
|
|
|
//console.log("offline");
|
2014-05-01 11:37:25 +02:00
|
|
|
introText = "Please connect your WiFi-Box to the internet. You can also use it offline, but then you won't be able to update.";
|
2014-04-24 12:46:26 +02:00
|
|
|
|
2014-05-07 17:15:48 +02:00
|
|
|
_drawItem.find("a").text("Draw (offline)");
|
|
|
|
|
|
|
|
// display the right buttons
|
|
|
|
_defaultItems.toggleClass("ui-screen-hidden",true);
|
|
|
|
_drawItem.toggleClass("ui-screen-hidden",false);
|
2014-04-25 16:24:25 +02:00
|
|
|
_joinNetworkItem.toggleClass("ui-screen-hidden",false);
|
2014-04-24 12:46:26 +02:00
|
|
|
|
2014-04-25 16:24:25 +02:00
|
|
|
var joinLink = _joinNetworkItem.find("a").attr("href");
|
2014-04-24 12:46:26 +02:00
|
|
|
joinLink = d3d.util.replaceURLParameters(joinLink,_boxData);
|
2014-04-25 16:24:25 +02:00
|
|
|
_joinNetworkItem.find("a").attr("href",joinLink);
|
2014-02-24 11:26:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-01 11:37:25 +02:00
|
|
|
_intro.text(introText);
|
|
|
|
_intro.toggleClass("ui-screen-hidden",(introText === ""));
|
|
|
|
|
2014-04-25 16:24:25 +02:00
|
|
|
// ToDo: update footer with network info
|
|
|
|
|
|
|
|
_list.listview('refresh'); // jQuery mobile enhance content
|
2014-05-01 11:37:25 +02:00
|
|
|
_networkStatus = status;
|
2014-02-24 11:26:30 +01:00
|
|
|
}
|
2014-05-23 19:09:46 +02:00
|
|
|
|
|
|
|
function retrieveUpdateStatus() {
|
|
|
|
console.log(PAGE_ID+":retrieveUpdateStatus");
|
|
|
|
var updateCounter = _list.find("#updateItem .ui-li-count");
|
|
|
|
updateCounter.hide();
|
|
|
|
_updateAPI.status(function(data) { // completed
|
|
|
|
console.log("UpdateAPI:refresh:completed");
|
|
|
|
var canUpdate = data.can_update;
|
|
|
|
updateCounter.text(canUpdate? 1 : 0);
|
|
|
|
updateCounter.show();
|
|
|
|
});
|
|
|
|
}
|
2014-04-24 12:46:26 +02:00
|
|
|
})(window);
|