diff --git a/js/Class.js b/js/Class.js deleted file mode 100644 index afa6f1c..0000000 --- a/js/Class.js +++ /dev/null @@ -1,64 +0,0 @@ -/* Simple JavaScript Inheritance - * By John Resig http://ejohn.org/ - * MIT Licensed. - */ -// Inspired by base2 and Prototype -(function(){ - var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/; - - // The base Class implementation (does nothing) - this.Class = function(){}; - - // Create a new Class that inherits from this class - Class.extend = function(prop) { - var _super = this.prototype; - - // Instantiate a base class (but only create the instance, - // don't run the init constructor) - initializing = true; - var prototype = new this(); - initializing = false; - - // Copy the properties over onto the new prototype - for (var name in prop) { - // Check if we're overwriting an existing function - prototype[name] = typeof prop[name] == "function" && - typeof _super[name] == "function" && fnTest.test(prop[name]) ? - (function(name, fn){ - return function() { - var tmp = this._super; - - // Add a new ._super() method that is the same method - // but on the super-class - this._super = _super[name]; - - // The method only need to be bound temporarily, so we - // remove it when we're done executing - var ret = fn.apply(this, arguments); - this._super = tmp; - - return ret; - }; - })(name, prop[name]) : - prop[name]; - } - - // The dummy class constructor - function Class() { - // All construction is actually done in the init method - if ( !initializing && this.init ) - this.init.apply(this, arguments); - } - - // Populate our constructed prototype object - Class.prototype = prototype; - - // Enforce the constructor to be what we expect - Class.prototype.constructor = Class; - - // And make this class extendable - Class.extend = arguments.callee; - - return Class; - }; -})(); \ No newline at end of file diff --git a/js/SettingsWindow.js b/js/SettingsWindow.js deleted file mode 100644 index 013dc8d..0000000 --- a/js/SettingsWindow.js +++ /dev/null @@ -1,791 +0,0 @@ -/* - * 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. - */ - -//these settings are defined in the firmware (conf_defaults.lua) and will be initialized in loadSettings() -var settings = {}; -var settingsPopup; -//wrapper to prevent scoping issues in showSettings() -function openSettingsWindow() { - settingsWindow.loadSettings(function() { // reload settings - settingsPopup.open(); - }); -} - -function SettingsWindow() { - this.wifiboxURL; - this.wifiboxCGIBinURL; - this.window; - this.btnOK; - this.form; - this.timeoutTime = 3000; - this.saveSettingsTimeoutTime = 8000; - this.retryDelay = 2000; // retry setTimout delay - this.retryRetrieveNetworkStatusDelayTime = 1000;// retry setTimout delay - - this.retryLoadSettingsDelay; // retry setTimout instance - this.retrySaveSettingsDelay; // retry setTimout instance - this.retryResetSettingsDelay; // retry setTimout instance - this.retryRetrieveNetworkStatusDelay;// retry setTimout instance - - this.apFieldSet; - this.clientFieldSet; - this.restoreStateField; - this.networks; - this.currentNetwork; // the ssid of the network the box is on - this.selectedNetwork; // the ssid of the selected network in the client mode settings - this.currentLocalIP = ""; - this.clientModeState = SettingsWindow.NOT_CONNECTED; - this.currentAP; - this.apModeState = SettingsWindow.NO_AP; - - // 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.retrieveNetworkStatusDelayTime = 1000; - - this.restoredStateHideDelayTime = 3000; - 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"; - - // 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; - - // network mode - SettingsWindow.NETWORK_MODE_NEITHER = "neither"; - SettingsWindow.NETWORK_MODE_CLIENT = "clientMode"; - SettingsWindow.NETWORK_MODE_ACCESS_POINT = "accessPointMode"; - - this.networkMode = SettingsWindow.NETWORK_MODE_NEITHER; - - this.updatePanel = new UpdatePanel(); - this.printerPanel = new PrinterPanel(); - - var self = this; - - this.init = function(wifiboxURL,wifiboxCGIBinURL) { - this.wifiboxURL = wifiboxURL; - this.wifiboxCGIBinURL = wifiboxCGIBinURL; - - this.window = $("#popupSettings"); - this.btnOK = this.window.find(".btnOK"); - settingsPopup = new Popup($("#popupSettings"), $("#popupMask")); - settingsPopup.setEnterEnabled(false); - settingsPopup.setAutoCloseEnabled(false); - - this.btnOK.on('touchstart mousedown',settingsPopup.commit); - $("#popupSettings").bind("onPopupCancel", function() { settingsPopup.close(); } ); - $("#popupSettings").bind("onPopupCommit", self.submitwindow); - - 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); }); - - $.ajax({ - url: self.wifiboxURL + "/printer/listall", - dataType: 'json', - timeout: self.timeoutTime, - success: function(response) { - 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; - } - networkSelector.append( - $("").val(element.ssid).html(element.ssid) - ); - self.networks[element.ssid] = element; - }); - 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") { - - } 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; - } - 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 - }); - } - }; - - 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(""); - } - }; - - this.showWiFiPassword = function() { - 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"); - - 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; - } - 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 - }; - - console.log(" postData: ",postData); - if (communicateWithWifibox) { - - // save network related settings and on complete, connect to network - self.saveSettings(self.readForm(),function(success) { - if(!success) return; - $.ajax({ - url: self.wifiboxCGIBinURL + "/network/associate", - type: "POST", - data: postData, - dataType: 'json', - timeout: self.timeoutTime, - success: function(response){ - console.log("Settings:connectToNetwork response: ",response); - } - }).fail(function() { - console.log("Settings:connectToNetwork: timeout (normal behavior)"); - //clearTimeout(self.retrySaveSettingsDelay); - //self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay - }); - }); - } - 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 - clearTimeout(self.retrieveNetworkStatusDelay); - self.retrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(true); },self.retrieveNetworkStatusDelayTime); - }; - - this.createAP = function() { - console.log("createAP"); - if (communicateWithWifibox) { - - // save network related settings and on complete, create access point - self.saveSettings(self.readForm(),function(success) { - if(!success) return; - self.setAPModeState(SettingsWindow.CREATING_AP); // get latest substituted ssid - $.ajax({ - url: self.wifiboxCGIBinURL + "/network/openap", - type: "POST", - dataType: 'json', - timeout: self.timeoutTime, - success: function(response){ - console.log("Settings:createAP response: ",response); - } - }).fail(function() { - console.log("Settings:createAP: timeout (normal behavior)"); - //clearTimeout(self.retrySaveSettingsDelay); - //self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay - }); - - self.setAPModeState(SettingsWindow.CREATING_AP,""); - - // 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); - }); - } - }; -} - -/************************* - * - * - * FROM DOODLE3D.INI - * - */ -//TODO: find all references to these variables, replace them and finally remove these. -var objectHeight = 20; -var layerHeight = .2; -//var wallThickness = .5; -//var hop = 0; -//var speed = 70; -//var travelSpeed = 200; -var enableTraveling = true; -//var filamentThickness = 2.89; -var minScale = .3; -var maxScale = 1; -var shape = "%"; -var twists = 0; -//var useSubLayers = true; -//var debug = false; // debug moved to main.js -var loglevel = 2; -//var zOffset = 0; -var serverport = 8888; -var autoLoadImage = "hand.txt"; -var loadOffset = [0, 0]; // x en y ? -var showWarmUp = true; -var loopAlways = false; -var firstLayerSlow = true; -var useSubpathColors = false; -var autoWarmUp = true; -//var maxObjectHeight = 150; -var maxScaleDifference = .1; -var frameRate = 60; -var quitOnEscape = true; -var screenToMillimeterScale = .3; // 0.3 -//var targetTemperature = 220; -//var simplifyiterations = 10; -//var simplifyminNumPoints = 15; -//var simplifyminDistance = 3; -//var retractionspeed = 50; -//var retractionminDistance = 5; -//var retractionamount = 3; -var sideis3D = true; -var sidevisible = true; -var sidebounds = [900, 210, 131, 390]; -var sideborder = [880, 169, 2, 471]; -var windowbounds = [0, 0, 800, 500]; -var windowcenter = true; -var windowfullscreen = false; -var autoWarmUpCommand = "M104 S230"; -//var checkTemperatureInterval = 3; -var autoWarmUpDelay = 3; diff --git a/js/api/ConfigAPI.js b/js/api/ConfigAPI.js new file mode 100644 index 0000000..50bb2ac --- /dev/null +++ b/js/api/ConfigAPI.js @@ -0,0 +1,81 @@ +/* + * 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. + */ + +function ConfigAPI() { + + var _wifiboxURL; + var _wifiboxCGIBinURL; + var _timeoutTime = 3000; + var _saveSettingsTimeoutTime = 8000; + var _retryDelay = 2000; // retry setTimout delay + + var _self = this; + + this.init = function(wifiboxURL,wifiboxCGIBinURL) { + console.log("ConfigAPI:init"); + + _wifiboxURL = wifiboxURL; + _wifiboxCGIBinURL = wifiboxCGIBinURL; + } + this.loadAll = function(completeHandler,failedHandler) { + console.log("ConfigAPI:loadAll"); + $.ajax({ + url: _wifiboxURL + "/config/all", + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + if(response.status == "error" || response.status == "fail") { + failedHandler(); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + failedHandler(); + }); + }; + this.save = function(newSettings,completeHandler,failedHandler) { + console.log("ConfigAPI:save"); + $.ajax({ + url: _wifiboxURL + "/config", + type: "POST", + data: newSettings, + dataType: 'json', + timeout: _saveSettingsTimeoutTime, + success: function(response){ + //console.log("ConfigAPI:save response: ",response); + if(response.status == "error" || response.status == "fail") { + failedHandler(); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + failedHandler(); + }); + }; + this.resetAll = function(completeHandler,failedHandler) { + console.log("ConfigAPI:resetAll"); + $.ajax({ + url: _wifiboxCGIBinURL + "/config/resetall", + type: "POST", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + if(response.status == "error" || response.status == "fail") { + failedHandler(); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + failedHandler(); + }); + }; +} \ No newline at end of file diff --git a/js/api/NetworkAPI.js b/js/api/NetworkAPI.js new file mode 100644 index 0000000..90549bf --- /dev/null +++ b/js/api/NetworkAPI.js @@ -0,0 +1,107 @@ +/* + * 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. + */ +function NetworkAPI() { + + NetworkAPI.STATUS = { + CONNECTING_FAILED: -1, + NOT_CONNECTED: 0, + CONNECTING: 1, + CONNECTED: 2, + CREATING: 3, + CREATED: 4 + }; + + var _wifiboxURL; + var _wifiboxCGIBinURL; + var _timeoutTime = 3000; + var _retryDelay = 2000; // retry setTimout delay + + var _self = this; + + this.init = function(wifiboxURL,wifiboxCGIBinURL) { + console.log("NetworkAPI:init"); + console.log(" wifiboxURL: ",wifiboxURL); + console.log(" wifiboxCGIBinURL: ",wifiboxCGIBinURL); + _wifiboxURL = wifiboxURL; + _wifiboxCGIBinURL = wifiboxCGIBinURL; + } + this.scan = function(completeHandler) { + console.log("NetworkAPI:scan"); + //console.log(" _wifiboxURL: ",_wifiboxURL); + $.ajax({ + url: _wifiboxURL + "/network/scan", + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + //console.log("NetworkAPI:scan response: ",response); + if(response.status == "error" || response.status == "fail") { + console.log("NetworkAPI:scan failed: ",response); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + console.log("NetworkAPI:scan failed"); + }); + }; + this.status = function(completeHandler,failedHandler) { + //console.log("NetworkAPI:status"); + $.ajax({ + url: _wifiboxURL + "/network/status", + type: "GET", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + //console.log("NetworkAPI:status response: ",response); + if(response.status == "error" || response.status == "fail") { + failedHandler(); + } else { + completeHandler(response.data); + } + } + }).fail(function() { + failedHandler(); + }); + }; + + this.associate = function(ssid,phrase,recreate) { + console.log("NetworkAPI:associate"); + var postData = { + ssid:ssid, + phrase:phrase, + recreate:recreate + }; + $.ajax({ + url: _wifiboxURL + "/network/associate", + type: "POST", + data: postData, + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + console.log("NetworkAPI:associate response: ",response); + } + }).fail(function() { + console.log("NetworkAPI:associate: timeout (normal behavior)"); + }); + }; + + this.signin = function() { + $.ajax({ + url: self.wifiboxCGIBinURL + "/network/signin", + type: "GET", + dataType: 'json', + timeout: self.timeoutTime, + success: function(response){ + console.log("Settings:signin response: ",response); + } + }).fail(function() { + console.log("Settings:signin: failed"); + }); + }; +} \ No newline at end of file diff --git a/js/main.js b/js/main.js index 9051cd1..8f8f9b1 100644 --- a/js/main.js +++ b/js/main.js @@ -35,7 +35,7 @@ var BUTTON_GROUP_SHOW_DURATION = 80; $(function() { console.log("ready"); - + if (getURLParameter("d") != "null") debugMode = (getURLParameter("d") == "1"); if (getURLParameter("p") != "null") sendPrintCommands = (getURLParameter("p") == "1"); if (getURLParameter("c") != "null") communicateWithWifibox = (getURLParameter("c") == "1"); diff --git a/js/settings/FormPanel.js b/js/settings/FormPanel.js new file mode 100644 index 0000000..56ecb40 --- /dev/null +++ b/js/settings/FormPanel.js @@ -0,0 +1,143 @@ +/* + * 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. + */ + +function FormPanel() { + + var _configAPI = new ConfigAPI(); + var _retryDelay = 2000; + var _retrySaveSettingsDelay; + // ui elements + var _element; + + var _self; + + FormPanel.prototype.init = function(wifiboxURL,wifiboxCGIBinURL,panelElement) { + console.log("FormPanel:init"); + // make _self the scope of which init was called? + // needed to have the subclass instance access the same counter + _self = this; + _element = panelElement; + //_configAPI.init(wifiboxURL,wifiboxCGIBinURL); + + console.log(" calling _self.readForm from FormPanel:init"); + _self.readForm(); + //console.log(" calling this.readForm from FormPanel:init"); + //this.readForm(); + //console.log(" calling _self2.readForm from FormPanel:init"); + //_self2.readForm(); + }; + + //this.readForm = function(form) { + FormPanel.prototype.readForm = function(form) { + console.log("FormPanel:readForm"); + /*if(!form) form = _element; // if no form specified, read whole panel form + //console.log("FormPanel"); + var settings = {}; + // Read all selects + var selects = form.find("select"); + selects.each( function(index,element) { + var elem = $(element); + //var fieldName = elem.attr('name'); + if(elem.attr('name') != "") { + settings[elem.attr('name')] = elem.val(); + } + }); + // Read all inputs + var inputs = form.find("input"); + inputs.each( function(index,element) { + var elem = $(element); + if(elem.attr('name') != "") { + switch(elem.attr("type")) { + case "text": + case "number": + settings[elem.attr('name')] = elem.val(); + break; + case "checkbox": + settings[elem.attr('name')] = elem.prop('checked'); + break; + } + } + }); + // Read all textareas + var textareas = form.find("textarea"); + textareas.each( function(index,element) { + var elem = $(element); + settings[elem.attr('name')] = elem.val(); + }); + console.log(" settings: ",settings); + return settings;*/ + }; + + /*this.fillForm = function(settings,form) { + console.log("FormPanel:fillForm"); + if(!form) form = _element; // if no form specified, fill whole panel form + console.log(" settings: ",settings); + console.log(" form: ",form); + //fill form with loaded settings + var selects = form.find("select"); + selects.each( function(index,element) { + var elem = $(element); + elem.val(settings[elem.attr('name')]); + }); + var inputs = form.find("input"); + inputs.each( function(index,element) { + var elem = $(element); + //console.log("printer setting input: ",index,element.attr("type"),element.attr('name')); //,element); + switch(elem.attr("type")) { + case "text": + case "number": + elem.val(settings[elem.attr('name')]); + break; + case "checkbox": + elem.prop('checked', settings[elem.attr('name')]); + break; + } + }); + var textareas = form.find("textarea"); + textareas.each( function(index,element) { + var elem = $(element); + var value = settings[elem.attr('name')]; + elem.val(value); + }); + }; + + this.saveSettings = function(newSettings,complete) { + console.log("FormPanel:saveSettings"); + console.log(" newSettings: ",newSettings); + console.log(" form: ",form); + _configAPI.save(newSettings,function(data) { + var validation = data.validation; + console.log(" validation: ",validation); + clearValidationErrors(); + var validated = true; + $.each(validation, function(key, val) { + if (val != "ok") { + console.log("ERROR: setting '" + key + "' not successfully set. Message: " + val); + displayValidationError(key,val); + validated = false; + } + }); + settings.substituted_ssid = data.substituted_ssid; + if(complete) complete(validated); + }, function() { + console.log("Settings:saveSettings: failed"); + clearTimeout(_retrySaveSettingsDelay); + _retrySaveSettingsDelay = setTimeout(function() { _self.saveSettings(newSettings,complete); },_retryDelay); // retry after delay + }); + }; + function displayValidationError(key,msg) { + var formElement = _element.find("[name|='"+key+"']"); + formElement.addClass("error"); + var errorMsg = "

"+msg+"

"; + formElement.after(errorMsg); + }; + function clearValidationErrors() { + _element.find(".errorMsg").remove(); + _element.find(".error").removeClass("error"); + };*/ +} diff --git a/js/settings/NetworkPanel.js b/js/settings/NetworkPanel.js new file mode 100644 index 0000000..ced4599 --- /dev/null +++ b/js/settings/NetworkPanel.js @@ -0,0 +1,389 @@ +/* + * 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. + */ + +// prototype inheritance +// http://robertnyman.com/2008/10/06/javascript-inheritance-how-and-why/ +NetworkPanel.prototype = new FormPanel(); +function NetworkPanel() { + + // network client mode states + /*var CLIENT_MODE_STATE = { + NOT_CONNECTED: "not connected", // also used as first item in networks list + CONNECTED: "connected", + CONNECTING: "connecting", + CONNECTING_FAILED: "connecting failed" + }; + var _clientModeState = CLIENT_MODE_STATE.NOT_CONNECTED; + + // network access point mode states + var AP_MODE_STATE = { + NO_AP: "no ap", // also used as first item in networks list + AP: "ap", + CREATING_AP: "creating ap" + }; + var _apModeState = AP_MODE_STATE.NO_AP; + // network mode + var NETWORK_MODE = { + NEITHER: "neither", + CLIENT: "clientMode", + ACCESS_POINT: "accessPointMode" + }; + var _networkMode = NETWORK_MODE.NETWORK_MODE_NEITHER; + + var _api = new NetworkAPI(); + var _networks = {}; + var _currentNetwork; // the ssid of the network the box is on + var _selectedNetwork; // the ssid of the selected network in the client mode settings + var _currentLocalIP = ""; + + var _currentAP; + + var _retryRetrieveStatusDelayTime = 1000; + var _retryRetrieveStatusDelay; + // after switching wifi network or creating a access point we delay the status retrieval + // because the webserver needs time to switch + var _retrieveNetworkStatusDelayTime = 1000; + var _retrieveNetworkStatusDelay; + + // ui elements + var _element; + var _networkSelector; + var _apFieldSet; + var _clientFieldSet; + var _apRadioButton; + var _clientRadioButton; + var _btnRefresh + var _btnConnect; + var _btnCreate; + var _passwordField; + var _passwordLabel; + var _clientStateDisplay; + var _apModeStateDisplay;*/ + + var _self = this; + + this.init = function(wifiboxURL,wifiboxCGIBinURL,panelElement) { + console.log("NetworkPanel:init"); + // super call: + _self.constructor.prototype.init.call(_self,wifiboxURL,wifiboxCGIBinURL,panelElement); + + console.log(" calling readForm from NetworkPanel:init"); + _self.readForm(); + + /*_api.init(wifiboxURL,wifiboxCGIBinURL); + + _element = panelElement; + _apRadioButton = _element.find("#ap"); + _clientRadioButton = _element.find("#client"); + _btnRefresh = _element.find("#refreshNetworks"); + _btnConnect = _element.find("#connectToNetwork"); + _btnCreate = _element.find("#createAP"); + _networkSelector = _element.find("#network"); + _apFieldSet = _element.find("#apSettings"); + _clientFieldSet = _element.find("#clientSettings"); + _passwordField = _element.find("#password"); + _passwordLabel = _element.find("#passwordLabel"); + _clientStateDisplay = _element.find("#clientModeState"); + _apModeStateDisplay = _element.find("#apModeState"); + + _apRadioButton.parent().on('touchstart mousedown',showAPSettings); + _clientRadioButton.parent().on('touchstart mousedown',showClientSettings); + _btnRefresh.on('touchstart mousedown',onRefreshClick); + _btnConnect.on('touchstart mousedown',_self.connectToNetwork); + _btnCreate.on('touchstart mousedown',_self.createAP); + _networkSelector.change(networkSelectorChanged);*/ + } + /* + * Handlers + */ + /* + function showAPSettings() { + _apFieldSet.show(); + _clientFieldSet.hide(); + }; + function showClientSettings() { + _clientFieldSet.show(); + _apFieldSet.hide(); + }; + function onRefreshClick() { + _btnRefresh.attr("disabled", true); + _self.refreshNetworks(function() { + _btnRefresh.removeAttr("disabled"); + }) + } + function networkSelectorChanged(e) { + var selectedOption = $(this).find("option:selected"); + _self.selectNetwork(selectedOption.val()); + }; + + this.update = function() { + console.log("NetworkPanel:update"); + _self.refreshNetworks(); + _self.retrieveStatus(false); + } + this.refreshNetworks = function(completeHandler) { + console.log("NetworkPanel:refreshNetworks"); + _api.scan(function(data) { + //console.log("NetworkPanel:scanned"); + _networks = {}; + var foundCurrentNetwork = false; + // fill network selector + _networkSelector.empty(); + _networkSelector.append( + $("").val(CLIENT_MODE_STATE.NOT_CONNECTED).html(CLIENT_MODE_STATE.NOT_CONNECTED) + ); + $.each(data.networks, function(index,element) { + if(element.ssid == _currentNetwork) { + foundCurrentNetwork = true; + } + _networkSelector.append( + $("").val(element.ssid).html(element.ssid) + ); + _networks[element.ssid] = element; + }); + if(foundCurrentNetwork) { + _networkSelector.val(_currentNetwork); + _self.selectNetwork(_currentNetwork); + } + if(completeHandler) completeHandler(); + }); + }; + + this.retrieveStatus = function(connecting) { + //console.log("NetworkPanel:retrieveStatus"); + _api.status(function(data) { + if(typeof data.status === 'string') { + data.status = parseInt(data.status); + } + console.log("NetworkPanel:retrievedStatus status: ",data.status,data.statusMessage); + + // Determine which network settings to show + switch(data.status) { + case NetworkAPI.STATUS.NOT_CONNECTED: + setNetworkMode(NETWORK_MODE.NEITHER); + break; + case NetworkAPI.STATUS.CONNECTING_FAILED: + case NetworkAPI.STATUS.CONNECTING: + case NetworkAPI.STATUS.CONNECTED: + setNetworkMode(NETWORK_MODE.CLIENT); + + if(data.status == SettingsWindow.API_CONNECTED) { + _networkSelector.val(data.ssid); + + _currentNetwork = data.ssid; + _currentLocalIP = data.localip; + _self.selectNetwork(data.ssid); + } else { + _currentLocalIP = ""; + } + break; + case NetworkAPI.STATUS.CREATING: + case NetworkAPI.STATUS.CREATED: + setNetworkMode(NETWORK_MODE.ACCESS_POINT); + + _currentNetwork = undefined; + _self.selectNetwork(CLIENT_MODE_STATE.NOT_CONNECTED); + _networkSelector.val(CLIENT_MODE_STATE.NOT_CONNECTED); + + if(data.ssid && data.status == SettingsWindow.API_CREATED) { + _currentAP = data.ssid; + } + break; + } + + // update status message + switch(data.status) { + case NetworkAPI.STATUS.CONNECTING_FAILED: + setClientModeState(CLIENT_MODE_STATE.CONNECTING_FAILED,data.statusMessage); + setAPModeState(AP_MODE_STATE.NO_AP,""); + break; + case NetworkAPI.STATUS.NOT_CONNECTED: + setClientModeState(CLIENT_MODE_STATE.NOT_CONNECTED,""); + setAPModeState(AP_MODE_STATE.NO_AP,""); + break; + case NetworkAPI.STATUS.CONNECTING: + setClientModeState(CLIENT_MODE_STATE.CONNECTING,""); + setAPModeState(AP_MODE_STATE.NO_AP,""); + break; + case NetworkAPI.STATUS.CONNECTED: + setClientModeState(CLIENT_MODE_STATE.CONNECTED,""); + setAPModeState(AP_MODE_STATE.NO_AP,""); + break; + case NetworkAPI.STATUS.CREATING: + setClientModeState(CLIENT_MODE_STATE.NOT_CONNECTED,""); + setAPModeState(AP_MODE_STATE.CREATING_AP,""); + break; + case NetworkAPI.STATUS.CREATED: + setClientModeState(CLIENT_MODE_STATE.NOT_CONNECTED,""); + setAPModeState(AP_MODE_STATE.AP,""); + break; + } + + // Keep checking for updates? + if(connecting) { + switch(data.status) { + case NetworkAPI.STATUS.CONNECTING: + case NetworkAPI.STATUS.CREATING: + clearTimeout(_retryRetrieveStatusDelay); + _retryRetrieveStatusDelay = setTimeout(function() { _self.retrieveStatus(connecting); },_retryRetrieveStatusDelayTime); // retry after delay + break; + } + } + }, function() { + console.log("NetworkPanel:retrieveStatus failed"); + clearTimeout(_retryRetrieveStatusDelay); + _retryRetrieveStatusDelay = setTimeout(function() { _self.retrieveStatus(connecting); }, _retryRetrieveStatusDelayTime); // retry after delay + }); + }; + function setNetworkMode(mode) { + //console.log("NetworkPanel:setNetworkMode: ",mode); + if(mode == _networkMode) return; + switch(mode) { + case NETWORK_MODE.NEITHER: + _apFieldSet.show(); + _clientFieldSet.show(); + break; + case NETWORK_MODE.CLIENT: + _clientRadioButton.prop('checked',true); + _apFieldSet.hide(); + _clientFieldSet.show(); + break; + case NETWORK_MODE.ACCESS_POINT: + _apRadioButton.prop('checked',true); + _apFieldSet.show(); + _clientFieldSet.hide(); + break; + } + // TODO + //self.updatePanel.setNetworkMode(mode); + _networkMode = mode; + } + + this.selectNetwork = function(ssid) { + console.log("select network: ",ssid); + if(ssid == "") return; + //console.log(" checked"); + _selectedNetwork = ssid; + if(_networks == undefined || ssid == CLIENT_MODE_STATE.NOT_CONNECTED) { + hideWiFiPassword(); + } else { + var network = _networks[ssid]; + if(network.encryption == "none") { + hideWiFiPassword(); + } else { + showWiFiPassword(); + } + _passwordField.val(""); + } + }; + function showWiFiPassword() { + _passwordLabel.show(); + _passwordField.show(); + }; + function hideWiFiPassword() { + _passwordLabel.hide(); + _passwordField.hide(); + }; + + function setClientModeState(state,statusMessage) { + var msg = ""; + switch(state) { + case SettingsWindow.NOT_CONNECTED: + _btnConnect.removeAttr("disabled"); + msg = "Not connected"; + break; + case SettingsWindow.CONNECTED: + _btnConnect.removeAttr("disabled"); + + msg = "Connected to: "+_currentNetwork+"."; + if(_currentLocalIP != undefined && _currentLocalIP != "") { + var a = ""+_currentLocalIP+""; + msg += " (IP: "+a+")"; + } + break; + case SettingsWindow.CONNECTING: + _btnConnect.attr("disabled", true); + msg = "Connecting... Reconnect by connecting your device to "+_selectedNetwork+" and going to connect.doodle3d.com"; + break; + case SettingsWindow.CONNECTING_FAILED: + _btnConnect.removeAttr("disabled"); + msg = statusMessage; + break; + } + _clientStateDisplay.html(msg); + _clientModeState = state; + }; + function setAPModeState(state,statusMessage) { + var msg = ""; + switch(state) { + case SettingsWindow.NO_AP: + _btnCreate.removeAttr("disabled"); + msg = "Not currently a access point"; + break; + case SettingsWindow.AP: + _btnCreate.removeAttr("disabled"); + msg = "Is access point: "+_currentAP+""; + break; + case SettingsWindow.CREATING_AP: + _btnCreate.attr("disabled", true); + msg = "Creating access point... Reconnect by connecting your device to "+settings.substituted_ssid+" and going to draw.doodle3d.com"; + break; + } + _apModeStateDisplay.html(msg); + _apModeState = state; + }; + + this.connectToNetwork = function() { + console.log("NetworkPanel:connectToNetwork"); + if(_selectedNetwork == undefined) return; + + // save network related settings and on complete, connect to network + _self.saveSettings(_self.readForm(),function(validated) { + if(!validated) return; + _api.associate(_selectedNetwork,_passwordField.val(),true); + }); + setClientModeState(CLIENT_MODE_STATE.CONNECTING,""); + + // after switching wifi network or creating a access point we delay the status retrieval + // because the webserver needs time to switch + clearTimeout(_retrieveNetworkStatusDelay); + _retrieveNetworkStatusDelay = setTimeout(function() { _self.retrieveNetworkStatus(true); }, _retrieveNetworkStatusDelayTime); + }; + + this.createAP = function() { + console.log("createAP"); + if (communicateWithWifibox) { + + // save network related settings and on complete, create access point + _self.saveSettings(_self.readForm(),function(success) { + if(!success) return; + setAPModeState(AP_MODE_STATE.CREATING_AP); // get latest substituted ssid + $.ajax({ + url: _wifiboxCGIBinURL + "/network/openap", + type: "POST", + dataType: 'json', + timeout: _timeoutTime, + success: function(response){ + console.log("Settings:createAP response: ",response); + } + }).fail(function() { + console.log("Settings:createAP: timeout (normal behavior)"); + //clearTimeout(self.retrySaveSettingsDelay); + //self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay + }); + + setAPModeState(AP_MODE_STATE.CREATING_AP,""); + + // after switching wifi network or creating a access point we delay the status retrieval + // because the webserver needs time to switch + clearTimeout(_retrieveNetworkStatusDelay); + _retrieveNetworkStatusDelay = setTimeout(function() { _self.retrieveNetworkStatus(true); }, _retrieveNetworkStatusDelayTime); + }); + } + };*/ +} diff --git a/js/PrinterPanel.js b/js/settings/PrinterPanel.js similarity index 100% rename from js/PrinterPanel.js rename to js/settings/PrinterPanel.js diff --git a/js/settings/SettingsWindow.js b/js/settings/SettingsWindow.js new file mode 100644 index 0000000..ecb562f --- /dev/null +++ b/js/settings/SettingsWindow.js @@ -0,0 +1,409 @@ +/* + * 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. + */ + +//these settings are defined in the firmware (conf_defaults.lua) and will be initialized in loadSettings() +var settings = {}; +var settingsPopup; +//wrapper to prevent scoping issues in showSettings() +function openSettingsWindow() { + settingsWindow.loadSettings(function() { // reload settings + settingsPopup.open(); + }); +} + +function SettingsWindow() { + this.wifiboxURL; + this.wifiboxCGIBinURL; + this.window; + this.btnOK; + this.form; + this.timeoutTime = 3000; + this.saveSettingsTimeoutTime = 8000; + this.retryDelay = 2000; // retry setTimout delay + + this.retryLoadSettingsDelay; // retry setTimout instance + this.retrySaveSettingsDelay; // retry setTimout instance + this.retryResetSettingsDelay; // retry setTimout instance + + this.restoreStateField + this.restoredStateHideDelayTime = 3000; + this.restoredStateHideDelay; // setTimout instance + + // Events + SettingsWindow.SETTINGS_LOADED = "settingsLoaded"; + + this.updatePanel = new UpdatePanel(); + this.printerPanel = new PrinterPanel(); + var _networkPanel = new NetworkPanel(); + + var self = this; + + this.init = function(wifiboxURL,wifiboxCGIBinURL) { + + console.log("EARLY initialize network panel"); + //var _networkPanel = new NetworkPanel(); + _networkPanel.init(wifiboxURL,wifiboxCGIBinURL); + + + this.wifiboxURL = wifiboxURL; + this.wifiboxCGIBinURL = wifiboxCGIBinURL; + + this.window = $("#popupSettings"); + this.btnOK = this.window.find(".btnOK"); + settingsPopup = new Popup($("#popupSettings"), $("#popupMask")); + settingsPopup.setEnterEnabled(false); + settingsPopup.setAutoCloseEnabled(false); + + this.btnOK.on('touchstart mousedown',settingsPopup.commit); + $("#popupSettings").bind("onPopupCancel", function() { settingsPopup.close(); } ); + $("#popupSettings").bind("onPopupCommit", self.submitwindow); + + //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); }); + + $.ajax({ + url: self.wifiboxURL + "/printer/listall", + dataType: 'json', + timeout: self.timeoutTime, + success: function(response) { + console.log("Settings:printer/listall response: ",response.data.printers); + console.log(" this: ",this); + // network panel + console.log("initialize network panel"); + var $networkPanelElement = self.form.find("#networkPanel"); + _networkPanel.init(wifiboxURL,wifiboxCGIBinURL,$networkPanelElement); + + $.each(response.data.printers, function(key, value) { + // console.log(key,value); + $('#printerType').append($('