vn-access/src/main.js

511 lines
12 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'
,dbName: 'vn2008'
,maxCorruptSize: 600
};
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"
,"Application it's already open":
"La aplicación ya está abierta"
,"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"
2020-02-28 16:29:07 +00:00
,"Cache files have been deleted":
"Se han borrado todos los ficheros cacheados"
}
};
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;
2020-02-28 16:05:09 +00:00
var height = 420;
2016-03-14 08:23:40 +00:00
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
},
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.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb';
this.lockFile = this.moduleDir +'\\'+ this.module +'.ldb';
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,
'DATABASE' : Conf.dbName,
2016-03-14 08:23:40 +00:00
'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;
this.onEnterClick();
2017-10-06 10:55:13 +00:00
} 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
disableUi: function(disabled, loadMessage) {
2016-03-14 08:23:40 +00:00
if (disabled)
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;
},
onCleanCacheClick: function() {
2020-02-28 16:05:09 +00:00
setTimeout(function() { App.cleanCache(); });
},
cleanCache: function() {
if (this.fso.folderExists(this.moduleDir)) {
var folder = this.fso.getFolder(this.moduleDir);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext()) {
var file = files.item();
if (/\.mdb$/.test(file.name) && file.name != 'config.mdb')
try {
file.Delete();
} catch (e) {}
}
}
2020-02-28 16:29:07 +00:00
this.showMessage(_('Cache files have been deleted'), 'notice');
},
onKeyPress: function(event) {
switch (event.keyCode) {
2016-03-14 08:23:40 +00:00
case 13: // Enter
this.onEnterPress(event);
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
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;
}
this.onEnterClick();
2017-09-11 14:25:54 +00:00
},
2016-03-14 08:23:40 +00:00
onEnterClick: function() {
this.disableUi(true, _('Loading'));
setTimeout(function() { App.login(); });
2016-03-14 08:23:40 +00:00
},
login: function() {
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
var version = this.fetchVersion();
if (version) {
this.disableUi(true, _('Updating'));
var remoteFile;
if (version.code == 'last') {
2020-03-03 11:17:28 +00:00
remoteFile = this.module +'.7z?';
remoteFile += version.number
? 'v'+ version.number
: new Date().getTime();
} else {
remoteFile = '.archive/'+ this.module +'/'+ version.number;
}
remoteFile = Conf.remoteUrl +'/'+ remoteFile;
2017-10-06 10:55:13 +00:00
var request = new ActiveXObject('MSXML2.XMLHTTP');
2020-02-28 16:05:09 +00:00
request.open('GET', remoteFile, 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 (err) {
this.catchError(err);
}
},
/**
* Gets information about the version to download.
*
* @return {Object} Version information or %null if local is up-to-date
*/
fetchVersion: function() {
// Gets the last version number using the MySQL ODBC connection
var mysqlConn = new ActiveXObject('ADODB.Connection');
try {
mysqlConn.open(Conf.dsName);
} catch (err) {
var dbErrors = mysqlConn.errors;
if (dbErrors.count > 0) {
var newErr;
var dbError = dbErrors.item(0);
switch (dbError.NativeError) {
case 1045: // Access denied
clearPassword = true;
newErr = new Error(_('Bad login'));
newErr.code = 'BadLogin';
break;
case 2003: // Can't connect
newErr = new Error(_('Server can\'t be reached'));
break;
default:
errorMsg = dbError.description;
}
dbErrors.clear();
throw newErr;
} else
throw err;
}
2020-02-28 16:05:09 +00:00
var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
var rs = mysqlConn.execute(sql);
var lastVersion = rs.EOF ? null : parseInt(rs.fields(0).value);
var version = {
code: 'last',
number: lastVersion
};
rs.close();
mysqlConn.close();
if (this.$('previous-version').checked && lastVersion > 1)
version = {
code: 'previous',
number: lastVersion - 1
};
// Checks if it's already open
if (this.fso.fileExists(this.lockFile))
try {
this.fso.deleteFile(this.lockFile);
return version;
} catch (e) {
throw new Error(_('Application it\'s already open'));
2020-02-28 16:05:09 +00:00
}
// Checks if MDB exists
if (!this.fso.fileExists(this.mdbFile))
return version;
// If it's abnormaly bigger, maybe is corrupted, so force download
var file = this.fso.getFile(this.mdbFile);
if (file.size > Conf.maxCorruptSize * 1024 * 1024)
return version;
// Obtains the local version number from the MDB file
var localVersion;
try {
var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';');
var oRs = new ActiveXObject('ADODB.Recordset');
oRs.Open('SELECT Version FROM tblVariables', mdbConn);
localVersion = oRs.EOF ? -1 : parseInt(oRs('Version'));
oRs.close();
mdbConn.close();
} catch (e) {}
// Compares the local version with the requested version
return !localVersion || localVersion != version.number
? version
: null;
2016-03-14 08:23:40 +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
},
catchError: function(err) {
var clearPassword = err.code == 'BadLogin';
2017-10-06 10:55:13 +00:00
if (!this.$('remember').checked || clearPassword)
this.regWrite(Conf.dsPath, 'PWD', '', 'REG_SZ');
this.disableUi(false);
2020-02-28 16:29:07 +00:00
this.showMessage(err.message, 'error');
2017-10-06 10:55:13 +00:00
this.resetForm(clearPassword);
},
/**
* Displays a non-intrusive message.
*
* @param {String} message Message to display
* @param {String<error|notice>} className Message type
*/
2020-02-28 16:29:07 +00:00
showMessage: function(message, className) {
setTimeout(function() {
App.showMessageAsync(message, className);
});
},
showMessageAsync: function(message, className) {
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');
2020-02-28 16:29:07 +00:00
messageDiv.className = className;
2016-03-14 08:23:40 +00:00
messageDiv.innerHTML = message;
messageDiv.style.display = 'block';
this.messageTimeout = setTimeout(function() {
App.hideMessage();
}, 10000);
2016-03-14 08:23:40 +00:00
},
onBodyClick: function() {
this.hideMessage();
2016-03-14 08:23:40 +00:00
},
/**
* Hides the last displayed non-intrusive message.
*/
hideMessage: function() {
2017-10-06 10:55:13 +00:00
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;
}
},
/**
* Obtains a DOM element by it's identifier.
*
* @param {String} id The element id
*/
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);
},
onUnload: function() {
this.disableUi(false);
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;
}