0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-22 09:17:56 +01:00

Formatting of SettingsWindow.js.

This commit is contained in:
Wouter R 2014-01-17 16:28:29 +01:00
parent 5eb129d2a2
commit 74b9149175

View File

@ -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;
@ -49,7 +49,7 @@ function SettingsWindow() {
this.retrieveNetworkStatusDelayTime = 1000;
this.restoredStateHideDelayTime = 3000;
this.restoredStateHideDelay // setTimout instance
this.restoredStateHideDelay; // setTimout instance
// Events
SettingsWindow.SETTINGS_LOADED = "settingsLoaded";
@ -58,19 +58,19 @@ function SettingsWindow() {
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.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";
@ -98,7 +98,7 @@ function SettingsWindow() {
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",
@ -147,15 +147,17 @@ function SettingsWindow() {
});
}); //this.window.find
} //this.init
}; //this.init
this.openSettings = function() {
self.loadSettings(function() { // reload settings
settingsPopup.open();
});
}
};
this.closeSettings = function(complete) {
settingsPopup.close(complete);
}
};
this.submitwindow = function(e) {
disableButton(self.btnOK,self.submitwindow);
@ -173,11 +175,11 @@ function SettingsWindow() {
});
clearTimeout(self.retryRetrieveNetworkStatusDelay);
}
};
this.loadSettings = function(complete) {
if (!communicateWithWifibox) {
console.log(" communicateWithWifibox is false: settings aren't being loaded from wifibox...")
console.log(" communicateWithWifibox is false: settings aren't being loaded from wifibox...");
return;
}
console.log("Settings:loadSettings() >> getting new data...");
@ -197,42 +199,43 @@ function SettingsWindow() {
}).fail(function() {
console.log("Settings:loadSettings: failed");
clearTimeout(self.retryLoadSettingsDelay);
self.retryLoadSettingsDelay = setTimeout(function() { self.loadSettings() },self.retryDelay); // retry after delay
self.retryLoadSettingsDelay = setTimeout(function() { self.loadSettings(); },self.retryDelay); // retry after delay
});
this.refreshNetworks();
this.retrieveNetworkStatus(false);
}
};
this.fillForm = function(settings,form) {
if(!form) form = this.form; // if no form specified, fill whole form
//fill form with loaded settings
var selects = form.find("select");
selects.each( function(index,element) {
var element = $(element);
element.val(settings[element.attr('name')]);
var elem = $(element);
elem.val(settings[elem.attr('name')]);
});
var inputs = form.find("input");
inputs.each( function(index,element) {
var element = $(element);
var elem = $(element);
//console.log("printer setting input: ",index,element.attr("type"),element.attr('name')); //,element);
switch(element.attr("type")) {
switch(elem.attr("type")) {
case "text":
case "number":
element.val(settings[element.attr('name')]);
elem.val(settings[elem.attr('name')]);
break;
case "checkbox":
element.prop('checked', settings[element.attr('name')]);
elem.prop('checked', settings[elem.attr('name')]);
break;
}
});
var textareas = form.find("textarea");
textareas.each( function(index,element) {
var element = $(element);
var value = settings[element.attr('name')];
element.val(value);
var elem = $(element);
var value = settings[elem.attr('name')];
elem.val(value);
});
}
};
this.saveSettings = function(newSettings,complete) {
settings = newSettings; // store new settings in global settings
@ -247,7 +250,7 @@ function SettingsWindow() {
console.log("Settings:saveSettings response: ",response);
if(response.status == "error") {
clearTimeout(self.retrySaveSettingsDelay);
self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings(settings,complete) },self.retryDelay); // retry after delay
self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings(settings,complete); },self.retryDelay); // retry after delay
} else {
var data = response.data;
var validation = data.validation;
@ -267,10 +270,11 @@ function SettingsWindow() {
}).fail(function() {
console.log("Settings:saveSettings: failed");
clearTimeout(self.retrySaveSettingsDelay);
self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings(settings,complete) },self.retryDelay); // retry after delay
self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings(settings,complete); },self.retryDelay); // retry after delay
});
}
}
};
this.resetSettings = function() {
console.log("resetSettings");
//$("#restoreSettings").addClass("disabled");
@ -292,7 +296,7 @@ function SettingsWindow() {
console.log("Settings:resetSettings response: ",response);
if(response.status == "error") {
clearTimeout(self.retryResetSettingsDelay);
self.retryResetSettingsDelay = setTimeout(function() { self.resetSettings() },self.retryDelay); // retry after delay
self.retryResetSettingsDelay = setTimeout(function() { self.resetSettings(); },self.retryDelay); // retry after delay
} else {
settings = response.data;
console.log(" settings: ",settings);
@ -309,47 +313,50 @@ function SettingsWindow() {
}).fail(function() {
console.log("Settings:resetSettings: failed");
clearTimeout(self.retryResetSettingsDelay);
self.retryResetSettingsDelay = setTimeout(function() { self.resetSettings() },self.retryDelay); // retry after delay
self.retryResetSettingsDelay = setTimeout(function() { self.resetSettings(); },self.retryDelay); // retry after delay
});
}
}
};
this.setRestoreState = function(text) {
self.restoreStateField.html(text);
}
};
this.displayValidationError = function(key,msg) {
var formElement = self.form.find("[name|='"+key+"']");
formElement.addClass("error");
var errorMsg = "<p class='errorMsg'>"+msg+"</p>"
var errorMsg = "<p class='errorMsg'>"+msg+"</p>";
formElement.after(errorMsg);
}
};
this.clearValidationErrors = function() {
self.form.find(".errorMsg").remove();
self.form.find(".error").removeClass("error");
}
};
this.readForm = function() {
//console.log("SettingsWindow:readForm");
var settings = {};
var selects = self.form.find("select");
selects.each( function(index,element) {
var element = $(element);
var fieldName = element.attr('name');
if(element.attr('name') != "") {
settings[element.attr('name')] = element.val();
var elem = $(element);
//var fieldName = elem.attr('name');
if(elem.attr('name') != "") {
settings[elem.attr('name')] = elem.val();
}
});
var inputs = self.form.find("input");
inputs.each( function(index,element) {
var element = $(element);
if(element.attr('name') != "") {
switch(element.attr("type")) {
var elem = $(element);
if(elem.attr('name') != "") {
switch(elem.attr("type")) {
case "text":
case "number":
settings[element.attr('name')] = element.val();
settings[elem.attr('name')] = elem.val();
break;
case "checkbox":
settings[element.attr('name')] = element.prop('checked')
settings[elem.attr('name')] = elem.prop('checked');
break;
}
}
@ -357,12 +364,12 @@ function SettingsWindow() {
var textareas = self.form.find("textarea");
textareas.each( function(index,element) {
var element = $(element);
settings[element.attr('name')] = element.val();
var elem = $(element);
settings[elem.attr('name')] = elem.val();
});
//console.log(settings);
return settings;
}
};
this.signin = function() {
$.ajax({
@ -376,11 +383,11 @@ function SettingsWindow() {
}).fail(function() {
console.log("Settings:signin: failed");
});
}
};
this.downloadlogs = function() {
window.location.href = self.wifiboxURL + "/info/logfiles"
}
window.location.href = self.wifiboxURL + "/info/logfiles";
};
this.downloadGcode = function() {
var gcode = generate_gcode();
@ -388,7 +395,7 @@ function SettingsWindow() {
var blob = new Blob([gcode.join("\n")], {type: "text/plain;charset=utf-8"});
saveAs(blob, "doodle3d.gcode");
}
}
};
this.downloadSvg = function() {
var svg = saveToSvg();
@ -396,7 +403,7 @@ function SettingsWindow() {
var blob = new Blob([svg], {type: "text/plain;charset=utf-8"});
saveAs(blob, "doodle3d.svg");
}
}
};
/*
* Networks ui
@ -404,11 +411,13 @@ function SettingsWindow() {
this.showAPSettings = function() {
self.apFieldSet.show();
self.clientFieldSet.hide();
}
};
this.showClientSettings = function() {
self.clientFieldSet.show();
self.apFieldSet.hide();
}
};
this.refreshNetworks = function() {
console.log("Settings:refreshNetworks");
@ -424,7 +433,7 @@ function SettingsWindow() {
//clearTimeout(self.retrySaveSettingsDelay);
//self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay
} else {
var networks = response.data.networks
var networks = response.data.networks;
self.networks = {};
var foundCurrentNetwork = false;
var networkSelector = self.form.find("#network");
@ -451,7 +460,7 @@ function SettingsWindow() {
});
}
}
};
this.retrieveNetworkStatus = function(connecting) {
//console.log("Settings:retrieveNetworkStatus");
@ -499,7 +508,7 @@ function SettingsWindow() {
self.currentLocalIP = data.localip;
self.selectNetwork(data.ssid);
} else {
self.currentLocalIP = ""
self.currentLocalIP = "";
}
self.networkMode = SettingsWindow.NETWORK_MODE_CLIENT;
break;
@ -558,7 +567,7 @@ function SettingsWindow() {
case SettingsWindow.API_CONNECTING:
case SettingsWindow.API_CREATING:
clearTimeout(self.retryRetrieveNetworkStatusDelay);
self.retryRetrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(connecting) },self.retryRetrieveNetworkStatusDelayTime); // retry after delay
self.retryRetrieveNetworkStatusDelay = setTimeout(function() { self.retrieveNetworkStatus(connecting); },self.retryRetrieveNetworkStatusDelayTime); // retry after delay
break;
}
}
@ -567,15 +576,15 @@ function SettingsWindow() {
}).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);
@ -593,15 +602,17 @@ function SettingsWindow() {
}
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");
@ -631,7 +642,8 @@ function SettingsWindow() {
break;
}
this.clientModeState = state;
}
};
this.setAPModeState = function(state,msg) {
var field = this.form.find("#apModeState");
var btnCreate = this.form.find("#createAP");
@ -650,7 +662,7 @@ function SettingsWindow() {
break;
}
this.apModeState = state;
}
};
this.connectToNetwork = function() {
console.log("connectToNetwork");
@ -659,7 +671,8 @@ function SettingsWindow() {
ssid:self.selectedNetwork,
phrase:self.form.find("#password").val(),
recreate:true
}
};
console.log(" postData: ",postData);
if (communicateWithWifibox) {
@ -687,8 +700,8 @@ 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);
};
this.createAP = function() {
console.log("createAP");
@ -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);
});
}
}
};
}
/*************************