0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-11-05 07:03:24 +01:00
doodle3d-connect/js/BoxPage.js

162 lines
4.8 KiB
JavaScript
Raw Normal View History

/*
* 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;
2017-05-16 09:55:32 +02:00
var _settingsItem;
2014-04-25 16:24:25 +02:00
var _joinNetworkItem;
2017-07-13 15:34:30 +02:00
var _printItem;
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";
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-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");
2017-05-16 09:55:32 +02:00
_settingsItem = _list.find("#settingsItem");
2014-05-07 17:15:48 +02:00
_joinNetworkItem = _list.find("#joinNetworkItem");
2017-07-13 15:34:30 +02:00
_printItem = _list.find("#printItem");
// make sure draw link is opened in same WebApp (added to homescreen)
// and it doesn't start a browser
$.stayInWebApp("#box #drawItem a",true);
2017-07-13 15:34:30 +02:00
});
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);
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);
2017-07-13 15:34:30 +02:00
_printItem.hide();
2014-04-24 12:46:26 +02:00
_title.text(_boxData.wifiboxid);
2014-04-24 12:46:26 +02:00
var drawLink = (_boxData.link)? _boxData.link : boxURL;
_page.find("#drawItem a").attr("href",drawLink);
2014-04-24 12:46:26 +02:00
_networkAPI.init(boxURL);
2014-05-23 19:09:46 +02:00
_updateAPI.init(boxURL);
setNetworkStatus(NetworkAPI.STATUS.CONNECTED);
retrieveNetworkStatus();
2014-04-24 12:46:26 +02:00
});
$.mobile.document.on( "pagebeforehide", PAGE_ID, function( event, data ) {
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-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 = "";
2017-05-16 12:26:54 +02:00
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");
2017-05-16 09:55:32 +02:00
_drawItem.find("a").text("Draw / Sketch");
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
2017-05-16 12:26:54 +02:00
//update link
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
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
2017-05-16 12:26:54 +02:00
_drawItem.find("a").text("Draw / Sketch (local)");
2014-05-07 17:15:48 +02:00
// 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);
2017-05-16 12:26:54 +02:00
//joinLink
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-05-01 11:37:25 +02:00
_intro.text(introText);
_intro.toggleClass("ui-screen-hidden",(introText === ""));
//printLink
var printLink = _printItem.find("a").attr("href");
printLink = d3d.util.replaceURLParameters(printLink,_boxData);
_printItem.find("a").attr("href",printLink);
if (d3d && d3d.pageParams && d3d.pageParams.uuid) {
_drawItem.hide();
_printItem.show();
}
2017-05-16 12:26:54 +02:00
//settingsLink
// var settingsLink = _settingsItem.find("a").attr("href");
// settingsLink = d3d.util.replaceURLParameters(settingsLink,_boxData);
// _settingsItem.find("a").attr("href",settingsLink);
// _settingsItem.toggleClass("ui-screen-hidden",false);
2017-05-16 12:26:54 +02:00
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-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);