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'
@ -11,7 +10,7 @@ var Conf =
,defaultLocale: 'es' ,defaultLocale: 'es'
}; };
var Locale ={ var Locale = {
es: { es: {
"Enter a user name": "Enter a user name":
"Introduce un nombre de usuario" "Introduce un nombre de usuario"
@ -34,61 +33,56 @@ var Locale ={
} }
}; };
var App = var App = {
{ shell: new ActiveXObject('WScript.Shell'),
fso: new ActiveXObject('scripting.filesystemobject'),
shell: new ActiveXObject ('WScript.Shell'), init: function() {
fso: new ActiveXObject ('scripting.filesystemobject'),
init: function ()
{
var width = 420; var width = 420;
var height = 360; var height = 360;
window.resizeTo (width, height); window.resizeTo(width, height);
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);
if (split.length > 1) if (split.length > 1)
this.module = split[1].replace (/^"+|"+$/g, ''); this.module = split[1].replace(/^"+|"+$/g, '');
if (!this.module) if (!this.module)
this.module = Conf.defaultModule; this.module = Conf.defaultModule;
this.appDir = this.getEnv ('ProgramFiles') +'\\'+ Conf.appName; this.appDir = this.getEnv('ProgramFiles') +'\\'+ Conf.appName;
this.moduleDir = this.shell.SpecialFolders ('AppData') +'\\'+ Conf.appName; this.moduleDir = this.shell.SpecialFolders('AppData') +'\\'+ Conf.appName;
this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z'; this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z';
this.compressFile = this.getEnv ('TEMP') +'\\'+ this.module +'.7z'; this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb'; this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb';
// Creates the necessary registry entries // Creates the necessary registry entries
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
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Settings'; path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Settings';
this.regWrites (path, 'REG_DWORD', { this.regWrites(path, 'REG_DWORD', {
'Confirm Document Deletions' : 0, 'Confirm Document Deletions' : 0,
'Confirm Action Queries' : 0, 'Confirm Action Queries' : 0,
'Confirm Record Changes' : 0 'Confirm Record Changes' : 0
}); });
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security'; path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security';
this.regWrite (path, 'Level', 1, 'REG_DWORD'); this.regWrite(path, 'Level', 1, 'REG_DWORD');
// Creates the MySQL ODBC connection // Creates the MySQL ODBC connection
var driverPath = this.getEnv ('ProgramFiles') var driverPath = this.getEnv('ProgramFiles')
+'\\MySQL\\Connector ODBC 5.1\\myodbc5.dll'; +'\\MySQL\\Connector ODBC 5.1\\myodbc5.dll';
var params = { var params = {
@ -101,7 +95,7 @@ var App =
'AUTO_RECONNECT' : 1 'AUTO_RECONNECT' : 1
}; };
this.createOdbc ( this.createOdbc(
Conf.dsName, Conf.dsName,
'Mysql ODBC 5.1 Driver', 'Mysql ODBC 5.1 Driver',
params params
@ -109,7 +103,7 @@ var App =
// Creates the PosgreSQL ODBC connection // Creates the PosgreSQL ODBC connection
var driverPath = this.getEnv ('ProgramFiles') var driverPath = this.getEnv('ProgramFiles')
+'\\psqlODBC\\0804\\bin\\psqlodbc35w.dll'; +'\\psqlODBC\\0804\\bin\\psqlodbc35w.dll';
var params = { var params = {
@ -122,7 +116,7 @@ var App =
'PASSWORD' : '' 'PASSWORD' : ''
}; };
this.createOdbc ( this.createOdbc(
'verdnaturapg', 'verdnaturapg',
'PostgreSQL Unicode', 'PostgreSQL Unicode',
params params
@ -130,48 +124,45 @@ var App =
// Marks the application as configured // Marks the application as configured
this.regWrite (Conf.regPath, 'configured', 1, 'REG_DWORD'); this.regWrite(Conf.regPath, 'configured', 1, 'REG_DWORD');
} }
// Loads the form data // Loads the form data
var user = this.regRead (Conf.dsPath, 'UID'); var user = this.regRead(Conf.dsPath, 'UID');
var password = this.regRead (Conf.dsPath, 'PWD'); var password = this.regRead(Conf.dsPath, 'PWD');
var remember = this.regRead (Conf.regPath, 'remember'); var remember = this.regRead(Conf.regPath, 'remember');
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);
this.regWrite (odbcPath + 'ODBC Data Sources', this.regWrite(odbcPath + 'ODBC Data Sources',
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
loadMessage = ''; loadMessage = '';
@ -187,72 +178,63 @@ 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;
case 27: // Esc case 27: // Esc
window.close (); window.close();
break; break;
} }
}, },
_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;
} }
this._onEnterClick (); this._onEnterClick();
}, },
_onEnterClick: function () _onEnterClick: function() {
{ setTimeout(function() {App.login();}, 0);
this._disableUi (true, _('Loading'));
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;
if (!user || user === '') if (!user || user === '')
throw new Error (_('Enter a user name')); throw new Error(_('Enter a user name'));
if (!password || password === '') if (!password || password === '')
throw new Error (_('Enter a password')); throw new Error(_('Enter a password'));
this.regWrite (Conf.dsPath, 'UID', user, 'REG_SZ'); this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ');
this.regWrite (Conf.dsPath, 'PWD', password, 'REG_SZ'); this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ');
// Gets the last version number using the MySQL ODBC connection // Gets the last version number using the MySQL ODBC connection
var mysqlConn = new ActiveXObject ('ADODB.Connection'); var mysqlConn = new ActiveXObject('ADODB.Connection');
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');
@ -265,208 +247,178 @@ var App =
} }
dbErrors.clear(); dbErrors.clear();
throw new Error (errorMsg); throw new Error(errorMsg);
} } else
else
throw e; throw e;
} }
var sql = 'SELECT version FROM versiones WHERE programa = \''+ this.module +'\''; var sql = 'SELECT version FROM versiones WHERE programa = \''+ this.module +'\'';
var rs = mysqlConn.execute (sql); var rs = mysqlConn.execute(sql);
var lastVersion = parseInt (rs.fields(0).value); var lastVersion = parseInt(rs.fields(0).value);
rs.close (); rs.close();
mysqlConn.close (); mysqlConn.close();
// Obtains the local version number from the MDB file // Obtains the local version number from the MDB file
var localVersion = -1; var localVersion = -1;
if (this.fso.fileExists (this.mdbFile)) if (this.fso.fileExists(this.mdbFile))
try { try {
var mdbConn = new ActiveXObject ('ADODB.Connection'); var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open ('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';'); mdbConn.open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';');
var oRs = new ActiveXObject ('ADODB.Recordset'); var oRs = new ActiveXObject('ADODB.Recordset');
oRs.Open ('SELECT Version FROM tblVariables', mdbConn); oRs.Open('SELECT Version FROM tblVariables', mdbConn);
localVersion = oRs.EOF ? -1 : parseInt (oRs('Version')); localVersion = oRs.EOF ? -1 : parseInt(oRs('Version'));
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); request.onreadystatechange = function() {
request.onreadystatechange = function () { App._onRequestReady(request);
App._onRequestReady (request);
}; };
request.send (); request.send();
} } else
else this.openMdb();
this.openMdb (); } catch (e) {
} this._catchError(e, clearPassword);
catch (e)
{
this._catchError (e, clearPassword);
} }
}, },
_onRequestReady: function (request) _onRequestReady: function(request)
{ {
if (request.readyState !== 4) if (request.readyState !== 4)
return; return;
try { try {
if (request.status !== 200) if (request.status !== 200)
throw new Error ('HTTP: '+ request.statusText); throw new Error('HTTP: '+ request.statusText);
if (this.fso.fileExists (this.compressFile)) if (this.fso.fileExists(this.compressFile))
this.fso.deleteFile (this.compressFile); this.fso.deleteFile(this.compressFile);
var stream = new ActiveXObject ('ADODB.Stream'); var stream = new ActiveXObject('ADODB.Stream');
stream.open (); stream.open();
stream.Type = 1; //adTypeBinary stream.Type = 1; //adTypeBinary
stream.write (request.responseBody); stream.write(request.responseBody);
stream.Position = 0; stream.Position = 0;
stream.saveToFile (this.compressFile, 2); stream.saveToFile(this.compressFile, 2);
stream.close (); stream.close();
if (this.fso.fileExists (this.mdbFile)) if (this.fso.fileExists(this.mdbFile))
this.fso.deleteFile (this.mdbFile); this.fso.deleteFile(this.mdbFile);
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);
} }
try { try {
if (!this.fso.fileExists (this.mdbFile)) if (!this.fso.fileExists(this.mdbFile))
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');
var programFiles = this.getEnv ('ProgramFiles'); var programFiles = this.getEnv('ProgramFiles');
var accessBin = programFiles +'\\Microsoft Office\\OFFICE11\\MSACCESS.EXE'; var accessBin = programFiles +'\\Microsoft Office\\OFFICE11\\MSACCESS.EXE';
if (!this.fso.fileExists (accessBin)) if (!this.fso.fileExists(accessBin))
throw new Error (_('Microsoft Access 2003 is not installed')); throw new Error(_('Microsoft Access 2003 is not installed'));
this.shell.exec ('"'+ accessBin +'" "'+ this.mdbFile +'"'); this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile +'"');
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);
var messageDiv = this.$('message'); var messageDiv = this.$('message');
messageDiv.innerHTML = message; messageDiv.innerHTML = message;
messageDiv.style.display = 'block'; messageDiv.style.display = 'block';
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);
} }
}; };
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;
} }