Beautify JS, bugs solved

This commit is contained in:
Juan Ferrer Toribio 2017-10-06 12:55:13 +02:00
parent 1c6ca3ef62
commit 66e42d1808
1 changed files with 152 additions and 200 deletions

View File

@ -1,6 +1,5 @@
var Conf = var Conf = {
{
appName: 'Verdnatura' appName: 'Verdnatura'
,dsName: 'verdnatura' ,dsName: 'verdnatura'
,dsPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura' ,dsPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura'
@ -34,14 +33,11 @@ var Locale ={
} }
}; };
var App = var App = {
{
shell: new ActiveXObject('WScript.Shell'), shell: new ActiveXObject('WScript.Shell'),
fso: new ActiveXObject('scripting.filesystemobject'), fso: new ActiveXObject('scripting.filesystemobject'),
init: function () init: function() {
{
var width = 420; var width = 420;
var height = 360; var height = 360;
@ -49,8 +45,7 @@ var App =
window.moveTo((screen.width - width) / 2, (screen.height - height) / 2); window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
}, },
_onLoad: function () _onLoad: function() {
{
// Initializes the global variables // Initializes the global variables
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g); var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
@ -70,8 +65,7 @@ var App =
var configured = this.regRead(Conf.regPath, 'configured'); var configured = this.regRead(Conf.regPath, 'configured');
if (!configured) if (!configured) {
{
var path; var path;
// Creates the Access configuration entries // Creates the Access configuration entries
@ -142,18 +136,23 @@ var App =
if (user) if (user)
this.$('user').value = user; this.$('user').value = user;
if (remember && password) if (remember && password) {
{
this.$('password').value = password; this.$('password').value = password;
this.$('remember').checked = true; this.$('remember').checked = true;
this._onEnterClick(); this._onEnterClick();
} } else
else this.resetForm(true);
this.resetForm ();
}, },
createOdbc: function (dsName, driverName, params) resetForm: function(clearPassword) {
{ if (clearPassword)
this.$('password').value = '';
this.$('user').focus();
this.$('user').select();
},
createOdbc: function(dsName, driverName, params) {
var odbcPath = 'HKCU\\Software\\ODBC\\ODBC.INI\\'; var odbcPath = 'HKCU\\Software\\ODBC\\ODBC.INI\\';
this.regWrites(odbcPath + dsName, 'REG_SZ', params); this.regWrites(odbcPath + dsName, 'REG_SZ', params);
@ -161,15 +160,7 @@ var App =
dsName, driverName, 'REG_SZ'); dsName, driverName, 'REG_SZ');
}, },
resetForm: function () _disableUi: function(disabled, loadMessage) {
{
this.$('user').focus ();
this.$('user').select ();
this.$('password').value = '';
},
_disableUi: function (disabled, loadMessage)
{
if (disabled) if (disabled)
this._hideMessage(); this._hideMessage();
else else
@ -187,10 +178,8 @@ var App =
this.$('spinner').style.display = display; this.$('spinner').style.display = display;
}, },
_onKeyPress: function (e) _onKeyPress: function(e) {
{ switch (e.keyCode) {
switch (e.keyCode)
{
case 13: // Enter case 13: // Enter
this._onEnterPress(e); this._onEnterPress(e);
break; break;
@ -200,12 +189,10 @@ var App =
} }
}, },
_onEnterPress: function (event) _onEnterPress: function(event) {
{
var target = event.target || event.srcElement; var target = event.target || event.srcElement;
if (target && target.id == 'user' && this.$('password').value == '') if (target && target.id == 'user' && this.$('password').value == '') {
{
this.$('password').focus(); this.$('password').focus();
return; return;
} }
@ -213,16 +200,15 @@ var App =
this._onEnterClick(); this._onEnterClick();
}, },
_onEnterClick: function () _onEnterClick: function() {
{
this._disableUi (true, _('Loading'));
setTimeout(function() {App.login();}, 0); setTimeout(function() {App.login();}, 0);
}, },
login: function () login: function() {
{
var clearPassword; var clearPassword;
this._disableUi(true, _('Loading'));
try { try {
var user = this.$('user').value; var user = this.$('user').value;
var password = this.$('password').value; var password = this.$('password').value;
@ -241,18 +227,14 @@ var App =
try { try {
mysqlConn.open(Conf.dsName); mysqlConn.open(Conf.dsName);
} } catch (e) {
catch (e)
{
var dbErrors = mysqlConn.errors; var dbErrors = mysqlConn.errors;
if (dbErrors.count > 0) if (dbErrors.count > 0) {
{
var errorMsg; var errorMsg;
var dbError = dbErrors.item(0); var dbError = dbErrors.item(0);
switch (dbError.NativeError) switch (dbError.NativeError) {
{
case 1045: // Access denied case 1045: // Access denied
clearPassword = true; clearPassword = true;
errorMsg = _('Bad login'); errorMsg = _('Bad login');
@ -266,8 +248,7 @@ var App =
dbErrors.clear(); dbErrors.clear();
throw new Error(errorMsg); throw new Error(errorMsg);
} } else
else
throw e; throw e;
} }
@ -293,13 +274,11 @@ var App =
oRs.close(); oRs.close();
mdbConn.close(); mdbConn.close();
} } catch (e) {}
catch (e) {}
// Compares the local version with the las version // Compares the local version with the las version
if (localVersion < lastVersion) if (localVersion < lastVersion) {
{
this._disableUi(true, _('Updating')); this._disableUi(true, _('Updating'));
var request = new ActiveXObject('MSXML2.XMLHTTP'); var request = new ActiveXObject('MSXML2.XMLHTTP');
request.open('GET', this.remoteFile +'?'+ new Date().getTime(), true); request.open('GET', this.remoteFile +'?'+ new Date().getTime(), true);
@ -307,12 +286,9 @@ var App =
App._onRequestReady(request); App._onRequestReady(request);
}; };
request.send(); request.send();
} } else
else
this.openMdb(); this.openMdb();
} } catch (e) {
catch (e)
{
this._catchError(e, clearPassword); this._catchError(e, clearPassword);
} }
}, },
@ -342,9 +318,7 @@ var App =
this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true); this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true);
this.fso.deleteFile(this.compressFile); this.fso.deleteFile(this.compressFile);
} } catch (e) {
catch (e)
{
alert(_('Error while updating') +': '+ e.message); alert(_('Error while updating') +': '+ e.message);
} }
@ -353,30 +327,12 @@ var App =
throw new Error(_('MDB file not found')); throw new Error(_('MDB file not found'));
this.openMdb(); this.openMdb();
} } catch (e) {
catch (e)
{
this._catchError(e); this._catchError(e);
} }
}, },
_catchError: function (error, clearPassword) openMdb: function() {
{
if (error)
{
if (!this.$('remember').checked || clearPassword)
this.regWrite (Conf.dsPath, 'PWD', '', 'REG_SZ');
this.showMessage (error.message);
}
this._disableUi (false);
this.$('user').focus ();
this.$('user').select ();
},
openMdb: function ()
{
var remember = this.$('remember').checked ? 1 : 0; var remember = this.$('remember').checked ? 1 : 0;
this.regWrite(Conf.regPath, 'remember', remember, 'REG_DWORD'); this.regWrite(Conf.regPath, 'remember', remember, 'REG_DWORD');
@ -390,8 +346,16 @@ var App =
window.close(); window.close();
}, },
showMessage: function (message) _catchError: function(error, clearPassword) {
{ if (!this.$('remember').checked || clearPassword)
this.regWrite(Conf.dsPath, 'PWD', '', 'REG_SZ');
this._disableUi(false);
this.showMessage(error.message);
this.resetForm(clearPassword);
},
showMessage: function(message) {
if (this.messageTimeout) if (this.messageTimeout)
clearTimeout(this.messageTimeout); clearTimeout(this.messageTimeout);
@ -401,63 +365,52 @@ var App =
this.messageTimeout = setTimeout(function() {App._hideMessage();}, 10000); this.messageTimeout = setTimeout(function() {App._hideMessage();}, 10000);
}, },
_onBodyClick: function () _onBodyClick: function() {
{
this._hideMessage(); this._hideMessage();
}, },
_hideMessage: function () _hideMessage: function() {
{ if (this.messageTimeout) {
if (this.messageTimeout)
{
this.$('message').style.display = 'none'; this.$('message').style.display = 'none';
clearTimeout(this.messageTimeout); clearTimeout(this.messageTimeout);
this.messageTimeout = null; this.messageTimeout = null;
} }
}, },
_onUnload: function () _onUnload: function() {
{
this._disableUi(false); this._disableUi(false);
}, },
$: function (id) $: function(id) {
{
return document.getElementById(id); return document.getElementById(id);
}, },
run: function (command, wait) run: function(command, wait) {
{
if (!wait) if (!wait)
wait = false; wait = false;
this.shell.run(command, 0, wait); this.shell.run(command, 0, wait);
}, },
getEnv: function (varName) getEnv: function(varName) {
{
return this.shell.expandEnvironmentStrings('%'+ varName +'%'); return this.shell.expandEnvironmentStrings('%'+ varName +'%');
}, },
regRead: function (path, key) regRead: function(path, key) {
{
try { try {
var value = this.shell.regRead(path +'\\'+ key); var value = this.shell.regRead(path +'\\'+ key);
} } catch (e) {
catch (e) {
var value = null; var value = null;
} }
return value; return value;
}, },
regWrite: function (path, key, value, type) regWrite: function(path, key, value, type) {
{
this.shell.regWrite(path +'\\'+ key, value, type); this.shell.regWrite(path +'\\'+ key, value, type);
}, },
regWrites: function (path, type, values) regWrites: function(path, type, values) {
{
for(var key in values) for(var key in values)
this.regWrite(path, key, values[key], type); this.regWrite(path, key, values[key], type);
} }
@ -465,8 +418,7 @@ var App =
App.init(); App.init();
function _(string) function _(string) {
{
var translation = Locale[Conf.defaultLocale][string]; var translation = Locale[Conf.defaultLocale][string];
return translation ? translation : string; return translation ? translation : string;
} }