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