diff --git a/js/SettingsWindow.js b/js/SettingsWindow.js index 4c663b8..d98c8ba 100644 --- a/js/SettingsWindow.js +++ b/js/SettingsWindow.js @@ -7,7 +7,7 @@ */ //these settings are defined in the firmware (conf_defaults.lua) and will be initialized in loadSettings() -var settings = { } +var settings = {}; var settingsPopup; //wrapper to prevent scoping issues in showSettings() function openSettingsWindow() { @@ -18,7 +18,7 @@ function openSettingsWindow() { function SettingsWindow() { this.wifiboxURL; - this.wifiboxCGIBinURL + this.wifiboxCGIBinURL; this.window; this.btnOK; this.form; @@ -29,7 +29,7 @@ function SettingsWindow() { this.retryLoadSettingsDelay; // retry setTimout instance this.retrySaveSettingsDelay; // retry setTimout instance - this.retryResetSettingsDelay // retry setTimout instance + this.retryResetSettingsDelay; // retry setTimout instance this.retryRetrieveNetworkStatusDelay;// retry setTimout instance this.apFieldSet; @@ -45,37 +45,37 @@ function SettingsWindow() { // after switching wifi network or creating a access point we delay the status retrieval // because the webserver needs time to switch - this.retrieveNetworkStatusDelay; // setTimout delay + this.retrieveNetworkStatusDelay; // setTimout delay this.retrieveNetworkStatusDelayTime = 1000; this.restoredStateHideDelayTime = 3000; - this.restoredStateHideDelay // setTimout instance - + this.restoredStateHideDelay; // setTimout instance + // Events SettingsWindow.SETTINGS_LOADED = "settingsLoaded"; // network client mode states - SettingsWindow.NOT_CONNECTED = "not connected"; // also used as first item in networks list - SettingsWindow.CONNECTED = "connected"; - SettingsWindow.CONNECTING = "connecting"; - SettingsWindow.CONNECTING_FAILED = "connecting failed" + SettingsWindow.NOT_CONNECTED = "not connected"; // also used as first item in networks list + SettingsWindow.CONNECTED = "connected"; + SettingsWindow.CONNECTING = "connecting"; + SettingsWindow.CONNECTING_FAILED = "connecting failed"; // network access point mode states SettingsWindow.NO_AP = "no ap"; SettingsWindow.AP = "ap"; SettingsWindow.CREATING_AP = "creating ap"; - SettingsWindow.API_CONNECTING_FAILED = -1 - SettingsWindow.API_NOT_CONNECTED = 0 - SettingsWindow.API_CONNECTING = 1 - SettingsWindow.API_CONNECTED = 2 - SettingsWindow.API_CREATING = 3 - SettingsWindow.API_CREATED = 4 + SettingsWindow.API_CONNECTING_FAILED = -1; + SettingsWindow.API_NOT_CONNECTED = 0; + SettingsWindow.API_CONNECTING = 1; + SettingsWindow.API_CONNECTED = 2; + SettingsWindow.API_CREATING = 3; + SettingsWindow.API_CREATED = 4; // network mode - SettingsWindow.NETWORK_MODE_NEITHER = "neither"; - SettingsWindow.NETWORK_MODE_CLIENT = "clientMode"; - SettingsWindow.NETWORK_MODE_ACCESS_POINT = "accessPointMode"; + SettingsWindow.NETWORK_MODE_NEITHER = "neither"; + SettingsWindow.NETWORK_MODE_CLIENT = "clientMode"; + SettingsWindow.NETWORK_MODE_ACCESS_POINT = "accessPointMode"; this.networkMode = SettingsWindow.NETWORK_MODE_NEITHER; @@ -91,14 +91,14 @@ function SettingsWindow() { this.window = $("#popupSettings"); this.btnOK = this.window.find(".btnOK"); enableButton(this.btnOK,this.submitwindow); - + settingsPopup = new Popup($("#popupSettings"), $("#popupMask")); this.window.find("#settingsContainer").load("settings.html", function() { console.log("Settings:finished loading settings.html, now loading settings..."); self.form = self.window.find("form"); - self.form.submit(function (e) { self.submitwindow(e) }); + self.form.submit(function (e) { self.submitwindow(e); }); $.ajax({ url: self.wifiboxURL + "/printer/listall", @@ -108,23 +108,23 @@ function SettingsWindow() { console.log("Settings:printer/listall response: ",response.data.printers); $.each(response.data.printers, function(key, value) { - // console.log(key,value); - $('#printerType').append($('").val(SettingsWindow.NOT_CONNECTED).html("not connected") - ); - $.each(networks, function(index,element) { - if(element.ssid == self.currentNetwork) { - foundCurrentNetwork = true; - } + ); + $.each(networks, function(index,element) { + if(element.ssid == self.currentNetwork) { + foundCurrentNetwork = true; + } networkSelector.append( - $("").val(element.ssid).html(element.ssid) + $("").val(element.ssid).html(element.ssid) ); self.networks[element.ssid] = element; }); - if(foundCurrentNetwork) { - networkSelector.val(self.currentNetwork); - self.selectNetwork(self.currentNetwork); - } - } + if(foundCurrentNetwork) { + networkSelector.val(self.currentNetwork); + self.selectNetwork(self.currentNetwork); + } + } } }).fail(function() { }); - } - } + } + }; - this.retrieveNetworkStatus = function(connecting) { - //console.log("Settings:retrieveNetworkStatus"); - if (communicateWithWifibox) { - $.ajax({ - url: self.wifiboxURL + "/network/status", - type: "GET", - dataType: 'json', - timeout: self.timeoutTime, - success: function(response){ - console.log("Settings:retrieveNetworkStatus response: ",response); - if(response.status == "error") { + this.retrieveNetworkStatus = function(connecting) { + //console.log("Settings:retrieveNetworkStatus"); + if (communicateWithWifibox) { + $.ajax({ + url: self.wifiboxURL + "/network/status", + type: "GET", + dataType: 'json', + timeout: self.timeoutTime, + success: function(response){ + console.log("Settings:retrieveNetworkStatus response: ",response); + if(response.status == "error") { - } else { - var data = response.data; + } else { + var data = response.data; - if(typeof data.status === 'string') { - data.status = parseInt(data.status); - } - //console.log(" data.status: ",data.status,data.statusMessage); - - // Determine which network settings to show - switch(data.status) { - case SettingsWindow.API_NOT_CONNECTED: - //console.log(" not connected & not a access point"); - self.apFieldSet.show(); - self.clientFieldSet.show(); - - self.networkMode = SettingsWindow.NETWORK_MODE_NEITHER; - break; - case SettingsWindow.API_CONNECTING_FAILED: - case SettingsWindow.API_CONNECTING: - case SettingsWindow.API_CONNECTED: - //console.log(" client mode"); - self.form.find("#client").prop('checked',true); - - self.apFieldSet.hide(); - self.clientFieldSet.show(); - - if(data.status == SettingsWindow.API_CONNECTED) { - var networkSelector = self.form.find("#network"); - networkSelector.val(data.ssid); - - self.currentNetwork = data.ssid; - self.currentLocalIP = data.localip; - self.selectNetwork(data.ssid); - } else { - self.currentLocalIP = "" - } - self.networkMode = SettingsWindow.NETWORK_MODE_CLIENT; - break; - case SettingsWindow.API_CREATING: - case SettingsWindow.API_CREATED: - //console.log(" access point mode"); - self.form.find("#ap").prop('checked',true); - - self.apFieldSet.show(); - self.clientFieldSet.hide(); - - self.currentNetwork = undefined; - self.selectNetwork(SettingsWindow.NOT_CONNECTED); - var networkSelector = self.form.find("#network"); - networkSelector.val(SettingsWindow.NOT_CONNECTED); - - if(data.ssid && data.status == SettingsWindow.API_CREATED) { - self.currentAP = data.ssid; - } - self.networkMode = SettingsWindow.NETWORK_MODE_ACCESS_POINT; - break; + if(typeof data.status === 'string') { + data.status = parseInt(data.status); } - self.updatePanel.setNetworkMode(self.networkMode); + //console.log(" data.status: ",data.status,data.statusMessage); - // update status message - switch(data.status) { - case SettingsWindow.API_CONNECTING_FAILED: - self.setClientModeState(SettingsWindow.CONNECTING_FAILED,data.statusMessage); - self.setAPModeState(SettingsWindow.NO_AP,""); - break; - case SettingsWindow.API_NOT_CONNECTED: - self.setClientModeState(SettingsWindow.NOT_CONNECTED,""); - self.setAPModeState(SettingsWindow.NO_AP,""); - break; - case SettingsWindow.API_CONNECTING: - self.setClientModeState(SettingsWindow.CONNECTING,""); - self.setAPModeState(SettingsWindow.NO_AP,""); - break; - case SettingsWindow.API_CONNECTED: - self.setClientModeState(SettingsWindow.CONNECTED,""); - self.setAPModeState(SettingsWindow.NO_AP,""); - break; - case SettingsWindow.API_CREATING: - self.setClientModeState(SettingsWindow.NOT_CONNECTED,""); - self.setAPModeState(SettingsWindow.CREATING_AP,""); - break; - case SettingsWindow.API_CREATED: - self.setClientModeState(SettingsWindow.NOT_CONNECTED,""); - self.setAPModeState(SettingsWindow.AP,""); - break; - } + // Determine which network settings to show + switch(data.status) { + case SettingsWindow.API_NOT_CONNECTED: + //console.log(" not connected & not a access point"); + self.apFieldSet.show(); + self.clientFieldSet.show(); - // Keep checking for updates? - if(connecting) { - switch(data.status) { - case SettingsWindow.API_CONNECTING: - case SettingsWindow.API_CREATING: - clearTimeout(self.retryRetrieveNetworkStatusDelay); - self.retryRetrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(connecting) },self.retryRetrieveNetworkStatusDelayTime); // retry after delay - break; + self.networkMode = SettingsWindow.NETWORK_MODE_NEITHER; + break; + case SettingsWindow.API_CONNECTING_FAILED: + case SettingsWindow.API_CONNECTING: + case SettingsWindow.API_CONNECTED: + //console.log(" client mode"); + self.form.find("#client").prop('checked',true); + + self.apFieldSet.hide(); + self.clientFieldSet.show(); + + if(data.status == SettingsWindow.API_CONNECTED) { + var networkSelector = self.form.find("#network"); + networkSelector.val(data.ssid); + + self.currentNetwork = data.ssid; + self.currentLocalIP = data.localip; + self.selectNetwork(data.ssid); + } else { + self.currentLocalIP = ""; } - } - } + self.networkMode = SettingsWindow.NETWORK_MODE_CLIENT; + break; + case SettingsWindow.API_CREATING: + case SettingsWindow.API_CREATED: + //console.log(" access point mode"); + self.form.find("#ap").prop('checked',true); + + self.apFieldSet.show(); + self.clientFieldSet.hide(); + + self.currentNetwork = undefined; + self.selectNetwork(SettingsWindow.NOT_CONNECTED); + var networkSelector = self.form.find("#network"); + networkSelector.val(SettingsWindow.NOT_CONNECTED); + + if(data.ssid && data.status == SettingsWindow.API_CREATED) { + self.currentAP = data.ssid; + } + self.networkMode = SettingsWindow.NETWORK_MODE_ACCESS_POINT; + break; + } + self.updatePanel.setNetworkMode(self.networkMode); + + // update status message + switch(data.status) { + case SettingsWindow.API_CONNECTING_FAILED: + self.setClientModeState(SettingsWindow.CONNECTING_FAILED,data.statusMessage); + self.setAPModeState(SettingsWindow.NO_AP,""); + break; + case SettingsWindow.API_NOT_CONNECTED: + self.setClientModeState(SettingsWindow.NOT_CONNECTED,""); + self.setAPModeState(SettingsWindow.NO_AP,""); + break; + case SettingsWindow.API_CONNECTING: + self.setClientModeState(SettingsWindow.CONNECTING,""); + self.setAPModeState(SettingsWindow.NO_AP,""); + break; + case SettingsWindow.API_CONNECTED: + self.setClientModeState(SettingsWindow.CONNECTED,""); + self.setAPModeState(SettingsWindow.NO_AP,""); + break; + case SettingsWindow.API_CREATING: + self.setClientModeState(SettingsWindow.NOT_CONNECTED,""); + self.setAPModeState(SettingsWindow.CREATING_AP,""); + break; + case SettingsWindow.API_CREATED: + self.setClientModeState(SettingsWindow.NOT_CONNECTED,""); + self.setAPModeState(SettingsWindow.AP,""); + break; + } + + // Keep checking for updates? + if(connecting) { + switch(data.status) { + case SettingsWindow.API_CONNECTING: + case SettingsWindow.API_CREATING: + clearTimeout(self.retryRetrieveNetworkStatusDelay); + self.retryRetrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(connecting); },self.retryRetrieveNetworkStatusDelayTime); // retry after delay + break; + } + } + } } }).fail(function() { console.log("Settings:retrieveNetworkStatus: failed"); clearTimeout(self.retryRetrieveNetworkStatusDelay); - self.retryRetrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(connecting) },self.retryDelay); // retry after delay + self.retryRetrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(connecting); },self.retryDelay); // retry after delay }); - } - } + } + }; this.networkSelectorChanged = function(e) { var selectedOption = $(this).find("option:selected"); self.selectNetwork(selectedOption.val()); - } + }; this.selectNetwork = function(ssid) { console.log("select network: ",ssid); if(ssid == "") return; console.log(" checked"); this.selectedNetwork = ssid; - if(this.networks == undefined || ssid == SettingsWindow.NOT_CONNECTED) { - this.hideWiFiPassword(); - } else { - var network = this.networks[ssid]; - if(network.encryption == "none") { - this.hideWiFiPassword(); - } else { - this.showWiFiPassword(); - } - this.form.find("#password").val(""); - } - } + if(this.networks == undefined || ssid == SettingsWindow.NOT_CONNECTED) { + this.hideWiFiPassword(); + } else { + var network = this.networks[ssid]; + if(network.encryption == "none") { + this.hideWiFiPassword(); + } else { + this.showWiFiPassword(); + } + this.form.find("#password").val(""); + } + }; + this.showWiFiPassword = function() { - this.form.find("#passwordLabel").show(); + this.form.find("#passwordLabel").show(); this.form.find("#password").show(); - } + }; + this.hideWiFiPassword = function() { this.form.find("#passwordLabel").hide(); this.form.find("#password").hide(); - } + }; - this.setClientModeState = function(state,msg) { - var field = this.form.find("#clientModeState"); - var btnConnect = self.form.find("#connectToNetwork"); - switch(state) { - case SettingsWindow.NOT_CONNECTED: - btnConnect.removeAttr("disabled"); - field.html("Not connected"); - break; - case SettingsWindow.CONNECTED: - btnConnect.removeAttr("disabled"); + this.setClientModeState = function(state,msg) { + var field = this.form.find("#clientModeState"); + var btnConnect = self.form.find("#connectToNetwork"); + switch(state) { + case SettingsWindow.NOT_CONNECTED: + btnConnect.removeAttr("disabled"); + field.html("Not connected"); + break; + case SettingsWindow.CONNECTED: + btnConnect.removeAttr("disabled"); - var fieldText = "Connected to: "+this.currentNetwork+"."; - if(this.currentLocalIP != undefined && this.currentLocalIP != "") { - var a = ""+this.currentLocalIP+""; - fieldText += " (IP: "+a+")"; - } - field.html(fieldText); - break; - case SettingsWindow.CONNECTING: - btnConnect.attr("disabled", true); - field.html("Connecting... Reconnect by connecting your device to "+this.selectedNetwork+" and going to connect.doodle3d.com"); - break; - case SettingsWindow.CONNECTING_FAILED: - btnConnect.removeAttr("disabled"); - field.html(msg); - break; - } - this.clientModeState = state; - } - this.setAPModeState = function(state,msg) { + var fieldText = "Connected to: "+this.currentNetwork+"."; + if(this.currentLocalIP != undefined && this.currentLocalIP != "") { + var a = ""+this.currentLocalIP+""; + fieldText += " (IP: "+a+")"; + } + field.html(fieldText); + break; + case SettingsWindow.CONNECTING: + btnConnect.attr("disabled", true); + field.html("Connecting... Reconnect by connecting your device to "+this.selectedNetwork+" and going to connect.doodle3d.com"); + break; + case SettingsWindow.CONNECTING_FAILED: + btnConnect.removeAttr("disabled"); + field.html(msg); + break; + } + this.clientModeState = state; + }; + + this.setAPModeState = function(state,msg) { var field = this.form.find("#apModeState"); var btnCreate = this.form.find("#createAP"); switch(state) { - case SettingsWindow.NO_AP: - btnCreate.removeAttr("disabled"); - field.html("Not currently a access point"); - break; - case SettingsWindow.AP: - btnCreate.removeAttr("disabled"); - field.html("Is access point: "+this.currentAP+""); - break; - case SettingsWindow.CREATING_AP: - btnCreate.attr("disabled", true); - field.html("Creating access point... Reconnect by connecting your device to "+settings.substituted_ssid+" and going to draw.doodle3d.com"); - break; + case SettingsWindow.NO_AP: + btnCreate.removeAttr("disabled"); + field.html("Not currently a access point"); + break; + case SettingsWindow.AP: + btnCreate.removeAttr("disabled"); + field.html("Is access point: "+this.currentAP+""); + break; + case SettingsWindow.CREATING_AP: + btnCreate.attr("disabled", true); + field.html("Creating access point... Reconnect by connecting your device to "+settings.substituted_ssid+" and going to draw.doodle3d.com"); + break; } this.apModeState = state; - } + }; this.connectToNetwork = function() { console.log("connectToNetwork"); if(self.selectedNetwork == undefined) return; var postData = { - ssid:self.selectedNetwork, - phrase:self.form.find("#password").val(), - recreate:true - } + ssid:self.selectedNetwork, + phrase:self.form.find("#password").val(), + recreate:true + }; + console.log(" postData: ",postData); if (communicateWithWifibox) { @@ -681,17 +694,17 @@ function SettingsWindow() { //self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay }); }); - } - self.setClientModeState(SettingsWindow.CONNECTING,""); + } + self.setClientModeState(SettingsWindow.CONNECTING,""); - // after switching wifi network or creating a access point we delay the status retrieval - // because the webserver needs time to switch + // after switching wifi network or creating a access point we delay the status retrieval + // because the webserver needs time to switch clearTimeout(self.retrieveNetworkStatusDelay); - self.retrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(true) },self.retrieveNetworkStatusDelayTime); - } + self.retrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(true); },self.retrieveNetworkStatusDelayTime); + }; - this.createAP = function() { - console.log("createAP"); + this.createAP = function() { + console.log("createAP"); if (communicateWithWifibox) { // save network related settings and on complete, create access point @@ -717,10 +730,10 @@ function SettingsWindow() { // after switching wifi network or creating a access point we delay the status retrieval // because the webserver needs time to switch clearTimeout(self.retrieveNetworkStatusDelay); - self.retrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(true) },self.retrieveNetworkStatusDelayTime); + self.retrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(true); },self.retrieveNetworkStatusDelayTime); }); - } - } + } + }; } /*************************