vn-access/src/main.js

409 lines
9.9 KiB
JavaScript
Raw Normal View History

2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
var Conf = {
appName: 'Verdnatura'
,dsName: 'verdnatura'
,dsPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura'
,regPath: 'HKCU\\Software\\Verdnatura\\vn-access'
,remoteUrl: 'https://verdnatura.es/vn-access'
,dbHost: 'db.verdnatura.es'
,defaultModule: 'tpv'
,defaultLocale: 'es'
};
2017-10-06 10:55:13 +00:00
var Locale = {
es: {
"Enter a user name":
"Introduce un nombre de usuario"
,"Enter a password":
"Introduce una contraseña"
,"Server can't be reached":
"No se ha podido conectar con el servidor"
,"Updating":
"Actualizando"
,"Bad login":
"Usuario o contraseña incorrectos"
,"Loading":
"Cargando"
,"Error while updating":
"Error al actualizar"
,"Microsoft Access 2003 is not installed":
"Microsoft Access 2003 no está instalado en el sistema"
,"MDB file not found":
"No se encontro el fichero MDB"
}
};
2017-10-06 10:55:13 +00:00
var App = {
shell: new ActiveXObject('WScript.Shell'),
fso: new ActiveXObject('scripting.filesystemobject'),
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
init: function() {
2016-03-14 08:23:40 +00:00
var width = 420;
var height = 360;
2017-10-06 10:55:13 +00:00
window.resizeTo(width, height);
window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
_onLoad: function() {
2017-09-22 11:11:55 +00:00
// Initializes the global variables
2016-03-14 08:23:40 +00:00
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (split.length > 1)
2017-10-06 10:55:13 +00:00
this.module = split[1].replace(/^"+|"+$/g, '');
if (!this.module)
this.module = Conf.defaultModule;
2017-10-06 10:55:13 +00:00
this.appDir = this.getEnv('ProgramFiles') +'\\'+ Conf.appName;
this.moduleDir = this.shell.SpecialFolders('AppData') +'\\'+ Conf.appName;
this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z';
2017-10-06 10:55:13 +00:00
this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb';
2017-09-22 11:11:55 +00:00
// Creates the necessary registry entries
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
var configured = this.regRead(Conf.regPath, 'configured');
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
if (!configured) {
2016-03-14 08:23:40 +00:00
var path;
2017-09-22 11:11:55 +00:00
// Creates the Access configuration entries
2016-03-14 08:23:40 +00:00
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Settings';
2017-10-06 10:55:13 +00:00
this.regWrites(path, 'REG_DWORD', {
2016-03-14 08:23:40 +00:00
'Confirm Document Deletions' : 0,
'Confirm Action Queries' : 0,
'Confirm Record Changes' : 0
2016-03-14 08:23:40 +00:00
});
2016-03-14 11:01:40 +00:00
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security';
2017-10-06 10:55:13 +00:00
this.regWrite(path, 'Level', 1, 'REG_DWORD');
2016-03-14 11:01:40 +00:00
2017-09-22 11:11:55 +00:00
// Creates the MySQL ODBC connection
2017-10-06 10:55:13 +00:00
var driverPath = this.getEnv('ProgramFiles')
2016-03-14 08:23:40 +00:00
+'\\MySQL\\Connector ODBC 5.1\\myodbc5.dll';
var params = {
'Driver' : driverPath,
'DESCRIPTION' : Conf.appName,
'SERVER' : Conf.dbHost,
2016-03-14 08:23:40 +00:00
'DATABASE' : 'vn2008',
'SSLCA' : this.appDir +'\\cacert.pem',
'SSLVERIFY' : 1,
'AUTO_RECONNECT' : 1
};
2017-10-06 10:55:13 +00:00
this.createOdbc(
Conf.dsName,
'Mysql ODBC 5.1 Driver',
params
);
2017-09-22 11:11:55 +00:00
// Marks the application as configured
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
this.regWrite(Conf.regPath, 'configured', 1, 'REG_DWORD');
2016-03-14 08:23:40 +00:00
}
2017-09-22 11:11:55 +00:00
// Loads the form data
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
var user = this.regRead(Conf.dsPath, 'UID');
var password = this.regRead(Conf.dsPath, 'PWD');
var remember = this.regRead(Conf.regPath, 'remember');
2016-03-14 08:23:40 +00:00
if (user)
this.$('user').value = user;
2017-10-06 10:55:13 +00:00
if (remember && password) {
2016-03-14 08:23:40 +00:00
this.$('password').value = password;
this.$('remember').checked = true;
2017-10-06 10:55:13 +00:00
this._onEnterClick();
} else
this.resetForm(true);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
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\\';
2017-10-06 10:55:13 +00:00
this.regWrites(odbcPath + dsName, 'REG_SZ', params);
this.regWrite(odbcPath + 'ODBC Data Sources',
dsName, driverName, 'REG_SZ');
},
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
_disableUi: function(disabled, loadMessage) {
2016-03-14 08:23:40 +00:00
if (disabled)
2017-10-06 10:55:13 +00:00
this._hideMessage();
2016-03-14 08:23:40 +00:00
else
loadMessage = '';
this.$('loading-message').innerHTML = loadMessage;
this.$('user').disabled = disabled;
this.$('password').disabled = disabled;
this.$('remember').disabled = disabled;
this.$('enter').disabled = disabled;
var display = disabled ? 'block' : 'none';
this.$('background').style.display = display;
this.$('spinner').style.display = display;
},
2017-10-06 10:55:13 +00:00
_onKeyPress: function(e) {
switch (e.keyCode) {
2016-03-14 08:23:40 +00:00
case 13: // Enter
2017-10-06 10:55:13 +00:00
this._onEnterPress(e);
2016-03-14 08:23:40 +00:00
break;
case 27: // Esc
2017-10-06 10:55:13 +00:00
window.close();
2016-03-14 08:23:40 +00:00
break;
}
},
2017-09-11 14:25:54 +00:00
2017-10-06 10:55:13 +00:00
_onEnterPress: function(event) {
var target = event.target || event.srcElement;
2017-10-06 10:55:13 +00:00
if (target && target.id == 'user' && this.$('password').value == '') {
this.$('password').focus();
2017-09-11 14:25:54 +00:00
return;
}
2017-10-06 10:55:13 +00:00
this._onEnterClick();
2017-09-11 14:25:54 +00:00
},
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
_onEnterClick: function() {
2017-10-06 10:59:18 +00:00
this._disableUi(true, _('Loading'));
setTimeout(function() {App._login();}, 0);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:59:18 +00:00
_login: function() {
var clearPassword;
2017-10-06 10:55:13 +00:00
2016-03-14 08:23:40 +00:00
try {
var user = this.$('user').value;
var password = this.$('password').value;
2016-03-14 08:23:40 +00:00
if (!user || user === '')
2017-10-06 10:55:13 +00:00
throw new Error(_('Enter a user name'));
2016-03-14 08:23:40 +00:00
if (!password || password === '')
2017-10-06 10:55:13 +00:00
throw new Error(_('Enter a password'));
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ');
this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ');
2016-03-14 08:23:40 +00:00
2017-09-22 21:24:21 +00:00
// Gets the last version number using the MySQL ODBC connection
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
var mysqlConn = new ActiveXObject('ADODB.Connection');
2017-09-22 11:11:55 +00:00
2016-03-14 08:23:40 +00:00
try {
2017-10-06 10:55:13 +00:00
mysqlConn.open(Conf.dsName);
} catch (e) {
2017-09-22 21:24:21 +00:00
var dbErrors = mysqlConn.errors;
2017-10-06 10:55:13 +00:00
if (dbErrors.count > 0) {
2017-09-22 11:11:55 +00:00
var errorMsg;
var dbError = dbErrors.item(0);
2017-10-06 10:55:13 +00:00
switch (dbError.NativeError) {
2017-09-22 11:11:55 +00:00
case 1045: // Access denied
clearPassword = true;
errorMsg = _('Bad login');
2017-09-22 11:11:55 +00:00
break;
case 2003: // Can't connect
errorMsg = _('Server can\'t be reached');
2017-09-22 11:11:55 +00:00
break;
default:
errorMsg = dbError.description;
}
dbErrors.clear();
2017-10-06 10:55:13 +00:00
throw new Error(errorMsg);
} else
2017-09-22 11:11:55 +00:00
throw e;
2016-03-14 08:23:40 +00:00
}
var sql = 'SELECT version FROM versiones WHERE programa = \''+ this.module +'\'';
2017-10-06 10:55:13 +00:00
var rs = mysqlConn.execute(sql);
var lastVersion = rs.EOF ? 0 : parseInt(rs.fields(0).value);
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
rs.close();
mysqlConn.close();
2016-03-14 08:23:40 +00:00
2017-09-22 11:11:55 +00:00
// Obtains the local version number from the MDB file
2016-03-14 08:23:40 +00:00
var localVersion = -1;
2017-10-06 10:55:13 +00:00
if (this.fso.fileExists(this.mdbFile))
try {
2017-10-06 10:55:13 +00:00
var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';');
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
var oRs = new ActiveXObject('ADODB.Recordset');
oRs.Open('SELECT Version FROM tblVariables', mdbConn);
localVersion = oRs.EOF ? -1 : parseInt(oRs('Version'));
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
oRs.close();
mdbConn.close();
} catch (e) {}
2016-03-14 08:23:40 +00:00
2017-09-22 11:11:55 +00:00
// Compares the local version with the las version
2017-10-06 10:55:13 +00:00
if (localVersion < lastVersion) {
this._disableUi(true, _('Updating'));
var queryString = lastVersion
? 'v'+ lastVersion
: new Date().getTime();
2017-10-06 10:55:13 +00:00
var request = new ActiveXObject('MSXML2.XMLHTTP');
request.open('GET', this.remoteFile +'?'+ queryString, true);
2017-10-06 10:55:13 +00:00
request.onreadystatechange = function() {
App._onRequestReady(request);
2017-09-22 11:11:55 +00:00
};
2017-10-06 10:55:13 +00:00
request.send();
} else
this.openMdb();
} catch (e) {
this._catchError(e, clearPassword);
2016-03-14 08:23:40 +00:00
}
},
2017-10-06 10:55:13 +00:00
_onRequestReady: function(request)
2016-03-14 08:23:40 +00:00
{
if (request.readyState !== 4)
return;
try {
if (request.status !== 200)
2017-10-06 10:55:13 +00:00
throw new Error('HTTP: '+ request.statusText);
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
if (this.fso.fileExists(this.compressFile))
this.fso.deleteFile(this.compressFile);
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
var stream = new ActiveXObject('ADODB.Stream');
stream.open();
2016-03-14 08:23:40 +00:00
stream.Type = 1; //adTypeBinary
2017-10-06 10:55:13 +00:00
stream.write(request.responseBody);
2016-03-14 08:23:40 +00:00
stream.Position = 0;
2017-10-06 10:55:13 +00:00
stream.saveToFile(this.compressFile, 2);
stream.close();
2016-03-14 08:23:40 +00:00
2017-10-06 10:55:13 +00:00
if (this.fso.fileExists(this.mdbFile))
this.fso.deleteFile(this.mdbFile);
2017-10-06 10:55:13 +00:00
this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true);
this.fso.deleteFile(this.compressFile);
} catch (e) {
alert(_('Error while updating') +': '+ e.message);
2016-03-14 08:23:40 +00:00
}
2017-09-25 11:45:26 +00:00
try {
2017-10-06 10:55:13 +00:00
if (!this.fso.fileExists(this.mdbFile))
throw new Error(_('MDB file not found'));
2017-10-06 10:55:13 +00:00
this.openMdb();
} catch (e) {
this._catchError(e);
2017-09-25 11:45:26 +00:00
}
},
2017-10-06 10:55:13 +00:00
openMdb: function() {
2016-03-14 08:23:40 +00:00
var remember = this.$('remember').checked ? 1 : 0;
2017-10-06 10:55:13 +00:00
this.regWrite(Conf.regPath, 'remember', remember, 'REG_DWORD');
2017-10-06 10:55:13 +00:00
var programFiles = this.getEnv('ProgramFiles');
var accessBin = programFiles +'\\Microsoft Office\\OFFICE11\\MSACCESS.EXE';
2017-10-06 10:55:13 +00:00
if (!this.fso.fileExists(accessBin))
throw new Error(_('Microsoft Access 2003 is not installed'));
2017-10-06 10:55:13 +00:00
this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile +'"');
window.close();
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
_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) {
2016-03-14 08:23:40 +00:00
if (this.messageTimeout)
2017-10-06 10:55:13 +00:00
clearTimeout(this.messageTimeout);
2016-03-14 08:23:40 +00:00
var messageDiv = this.$('message');
messageDiv.innerHTML = message;
messageDiv.style.display = 'block';
2017-10-06 10:55:13 +00:00
this.messageTimeout = setTimeout(function() {App._hideMessage();}, 10000);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
_onBodyClick: function() {
this._hideMessage();
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
_hideMessage: function() {
if (this.messageTimeout) {
2016-03-14 08:23:40 +00:00
this.$('message').style.display = 'none';
2017-10-06 10:55:13 +00:00
clearTimeout(this.messageTimeout);
2016-03-14 08:23:40 +00:00
this.messageTimeout = null;
}
},
2017-10-06 10:55:13 +00:00
_onUnload: function() {
this._disableUi(false);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
$: function(id) {
return document.getElementById(id);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
run: function(command, wait) {
2016-03-14 08:23:40 +00:00
if (!wait)
wait = false;
2017-10-06 10:55:13 +00:00
this.shell.run(command, 0, wait);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
getEnv: function(varName) {
return this.shell.expandEnvironmentStrings('%'+ varName +'%');
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
regRead: function(path, key) {
2016-03-14 08:23:40 +00:00
try {
2017-10-06 10:55:13 +00:00
var value = this.shell.regRead(path +'\\'+ key);
} catch (e) {
2016-03-14 08:23:40 +00:00
var value = null;
}
return value;
},
2017-10-06 10:55:13 +00:00
regWrite: function(path, key, value, type) {
this.shell.regWrite(path +'\\'+ key, value, type);
2016-03-14 08:23:40 +00:00
},
2017-10-06 10:55:13 +00:00
regWrites: function(path, type, values) {
for(var key in values)
this.regWrite(path, key, values[key], type);
2016-03-14 08:23:40 +00:00
}
};
2017-10-06 10:55:13 +00:00
App.init();
2017-10-06 10:55:13 +00:00
function _(string) {
var translation = Locale[Conf.defaultLocale][string];
return translation ? translation : string;
}