From 21598c1afcd1ed503e895962705443fbb8cd1d6c Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 7 May 2014 15:18:08 +0200 Subject: [PATCH] Page leave prevention when appropriate --- js/BoxesPage.js | 10 ++++++++++ js/ConnectingToNetworkPage.js | 4 ++++ js/api/ConnectAPI.js | 14 +++++++------- js/main.js | 27 +++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/js/BoxesPage.js b/js/BoxesPage.js index f71a367..e177128 100644 --- a/js/BoxesPage.js +++ b/js/BoxesPage.js @@ -21,6 +21,8 @@ var BoxesPage = (function (w) { _findItem = _list.find("#findItem"); _connectAPI.refreshing = onRefreshing; + _connectAPI.listFailed = onListFailed; + _connectAPI.listSuccess = onlistSuccess; _connectAPI.listUpdated = onListUpdated; _connectAPI.boxAppeared = onBoxAppeared; _connectAPI.boxDisapeared = onBoxDisapeared; @@ -38,6 +40,14 @@ var BoxesPage = (function (w) { //console.log("onRefreshing"); d3d.util.showLoader(true); } + function onListFailed() { + d3d.util.enableRefreshPrevention(); + d3d.util.enableLeaveWarning("You're not connecting to the internet, leaving now will interrupt the connection proces"); + } + function onlistSuccess() { + d3d.util.disableRefreshPrevention(); + d3d.util.disableLeaveWarning(); + } function onListUpdated(boxesData) { //console.log("onListUpdated: ",boxesData); } diff --git a/js/ConnectingToNetworkPage.js b/js/ConnectingToNetworkPage.js index a66d327..ab79841 100644 --- a/js/ConnectingToNetworkPage.js +++ b/js/ConnectingToNetworkPage.js @@ -132,6 +132,10 @@ link = d3d.util.replaceURLParameters(link,linkParams); $.mobile.changePage(link); connectedBoxNetworkAPI.stopAutoRefresh(); + + // disable warnings that are enabled on boxes page + d3d.util.disableRefreshPrevention(); + d3d.util.disableLeaveWarning(); } }; connectedBoxNetworkAPI.startAutoRefresh(); diff --git a/js/api/ConnectAPI.js b/js/api/ConnectAPI.js index 4ca1688..ce5b311 100644 --- a/js/api/ConnectAPI.js +++ b/js/api/ConnectAPI.js @@ -9,6 +9,8 @@ function ConnectAPI() { // callbacks this.refreshing; // I'm refreshing my list + this.listFailed; // list retrieval from connect.doodle3d.com failed + this.listSuccess; // list retrieval from connect.doodle3d.com succeeded this.listUpdated; // the list of boxes is updated / changed this.boxAppeared; // a new box appeared this.boxDisapeared; // a box disappeared @@ -47,12 +49,14 @@ function ConnectAPI() { //console.log("ConnectAPI:list failed: ",response); if(failedHandler) failedHandler(response); } else { + if(_self.listSuccess) {_self.listSuccess(); } completeHandler(response.data); } } }).fail(function() { //console.log("ConnectAPI:list failed"); if(failedHandler) failedHandler(); + if(_self.listFailed) {_self.listFailed(); } }); }; @@ -71,12 +75,8 @@ function ConnectAPI() { clearTimeout(_refreshDelay); } this.refresh = function(listUpdated) { - if(listUpdated) { - _self.listUpdated = listUpdated; - } - if(_self.refreshing) { - _self.refreshing(); - } + if(listUpdated) { _self.listUpdated = listUpdated; } + if(_self.refreshing) { _self.refreshing(); } _self.list(function(foundBoxes) { //console.log(" foundBoxes: ",foundBoxes); @@ -86,8 +86,8 @@ function ConnectAPI() { clearTimeout(_refreshDelay); _refreshDelay = setTimeout(_self.refresh, _refreshInterval); } - //removeBox(_apBox.localip,true); // TODO: why again? }, function() { + console.log("ConnectAPI list retrieve failed"); // if web is not accessible try to find a box as an accesspoint // if not found, we look for a wired box _networkAPI.alive(_apBox.localip,_boxTimeoutTime,function() { diff --git a/js/main.js b/js/main.js index 68ca8ed..cda3550 100644 --- a/js/main.js +++ b/js/main.js @@ -64,6 +64,33 @@ d3d.util = { formData[field['name']] = field['value']; }); return formData; + }, + enableRefreshPrevention:function() { + $(document).on("keydown",d3d.util.preventRefresh); + }, + disableRefreshPrevention:function() { + $(document).off("keydown",d3d.util.preventRefresh); + }, + preventRefresh:function(event) { + if((event.which === 82 && (event.ctrlKey || event.metaKey)) || // ctrl+r + event.which === 116) { // F5 + console.log("d3d.util.preventRefresh"); + event.preventDefault(); + event.stopImmediatePropagation(); + return false; + } + }, + disableLeaveWarning:function() { + window.onbeforeunload = null; + }, + enableLeaveWarning:function(warning) { + if(warning === undefined) { + warning = "Are you sure you want to leave?"; + } + window.onbeforeunload = function() { + console.log("WARNING:"+warning); + return warning; + }; } };