Fix: download the latest version regardless of the number, documentation & small refactor
This commit is contained in:
parent
e4dfb1b147
commit
eee4ba89d9
95
src/main.js
95
src/main.js
|
@ -9,7 +9,7 @@ var Conf = {
|
|||
,defaultModule: 'tpv'
|
||||
,defaultLocale: 'es'
|
||||
,dbName: 'vn2008'
|
||||
,maxSize: 500
|
||||
,maxCorruptSize: 600
|
||||
};
|
||||
|
||||
var Locale = {
|
||||
|
@ -208,12 +208,10 @@ var App = {
|
|||
|
||||
onEnterClick: function() {
|
||||
this.disableUi(true, _('Loading'));
|
||||
setTimeout(function() { App.login(); }, 0);
|
||||
setTimeout(function() { App.login(); });
|
||||
},
|
||||
|
||||
login: function() {
|
||||
var downloadVersion;
|
||||
|
||||
try {
|
||||
var user = this.$('user').value;
|
||||
var password = this.$('password').value;
|
||||
|
@ -226,13 +224,22 @@ var App = {
|
|||
this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ');
|
||||
this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ');
|
||||
|
||||
if (hasToUpdate.call(this)) {
|
||||
var version = this.fetchVersion();
|
||||
|
||||
if (version) {
|
||||
this.disableUi(true, _('Updating'));
|
||||
var remoteFile = Conf.remoteUrl;
|
||||
remoteFile += downloadVersion
|
||||
? '/.archive/'+ this.module +'/'+ downloadVersion
|
||||
: '/'+ this.module;
|
||||
remoteFile += '.7z';
|
||||
var remoteFile;
|
||||
|
||||
if (version.code == 'last') {
|
||||
remoteFile = this.module +'.7z';
|
||||
remoteFile += version.number
|
||||
? 'v'+ version.number
|
||||
: new Date().getTime();
|
||||
} else {
|
||||
remoteFile = '.archive/'+ this.module +'/'+ version.number;
|
||||
}
|
||||
|
||||
remoteFile = Conf.remoteUrl +'/'+ remoteFile;
|
||||
|
||||
var request = new ActiveXObject('MSXML2.XMLHTTP');
|
||||
request.open('GET', remoteFile, true);
|
||||
|
@ -245,8 +252,14 @@ var App = {
|
|||
} catch (err) {
|
||||
this.catchError(err);
|
||||
}
|
||||
},
|
||||
|
||||
function hasToUpdate() {
|
||||
/**
|
||||
* 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');
|
||||
|
@ -282,20 +295,27 @@ var App = {
|
|||
var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
|
||||
var rs = mysqlConn.execute(sql);
|
||||
|
||||
downloadVersion = rs.EOF ? 0 : parseInt(rs.fields(0).value);
|
||||
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 && downloadVersion > 1)
|
||||
downloadVersion -= 1;
|
||||
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 true;
|
||||
return version;
|
||||
} catch (e) {
|
||||
throw new Error(_('Application it\'s already open'));
|
||||
}
|
||||
|
@ -303,18 +323,18 @@ var App = {
|
|||
// Checks if MDB exists
|
||||
|
||||
if (!this.fso.fileExists(this.mdbFile))
|
||||
return true;
|
||||
return version;
|
||||
|
||||
// If it's abnormaly bigger, maybe is corrupted, so force download
|
||||
|
||||
var file = this.fso.getFile(this.mdbFile);
|
||||
|
||||
if (file.size > Conf.maxSize * 1024 * 1024)
|
||||
return true;
|
||||
if (file.size > Conf.maxCorruptSize * 1024 * 1024)
|
||||
return version;
|
||||
|
||||
// Obtains the local version number from the MDB file
|
||||
|
||||
var localVersion = -1;
|
||||
var localVersion;
|
||||
|
||||
try {
|
||||
var mdbConn = new ActiveXObject('ADODB.Connection');
|
||||
|
@ -330,8 +350,9 @@ var App = {
|
|||
|
||||
// Compares the local version with the requested version
|
||||
|
||||
return localVersion != downloadVersion;
|
||||
}
|
||||
return !localVersion || localVersion != version.number
|
||||
? version
|
||||
: null;
|
||||
},
|
||||
|
||||
onRequestReady: function(request) {
|
||||
|
@ -397,7 +418,19 @@ var App = {
|
|||
this.resetForm(clearPassword);
|
||||
},
|
||||
|
||||
/**
|
||||
* Displays a non-intrusive message.
|
||||
*
|
||||
* @param {String} message Message to display
|
||||
* @param {String<error|notice>} className Message type
|
||||
*/
|
||||
showMessage: function(message, className) {
|
||||
setTimeout(function() {
|
||||
App.showMessageAsync(message, className);
|
||||
});
|
||||
},
|
||||
|
||||
showMessageAsync: function(message, className) {
|
||||
if (this.messageTimeout)
|
||||
clearTimeout(this.messageTimeout);
|
||||
|
||||
|
@ -405,13 +438,18 @@ var App = {
|
|||
messageDiv.className = className;
|
||||
messageDiv.innerHTML = message;
|
||||
messageDiv.style.display = 'block';
|
||||
this.messageTimeout = setTimeout(function() { App.hideMessage(); }, 10000);
|
||||
this.messageTimeout = setTimeout(function() {
|
||||
App.hideMessage();
|
||||
}, 10000);
|
||||
},
|
||||
|
||||
onBodyClick: function() {
|
||||
this.hideMessage();
|
||||
},
|
||||
|
||||
/**
|
||||
* Hides the last displayed non-intrusive message.
|
||||
*/
|
||||
hideMessage: function() {
|
||||
if (this.messageTimeout) {
|
||||
this.$('message').style.display = 'none';
|
||||
|
@ -420,10 +458,11 @@ var App = {
|
|||
}
|
||||
},
|
||||
|
||||
onUnload: function() {
|
||||
this.disableUi(false);
|
||||
},
|
||||
|
||||
/**
|
||||
* Obtains a DOM element by it's identifier.
|
||||
*
|
||||
* @param {String} id The element id
|
||||
*/
|
||||
$: function(id) {
|
||||
return document.getElementById(id);
|
||||
},
|
||||
|
@ -456,6 +495,10 @@ var App = {
|
|||
regWrites: function(path, type, values) {
|
||||
for(var key in values)
|
||||
this.regWrite(path, key, values[key], type);
|
||||
},
|
||||
|
||||
onUnload: function() {
|
||||
this.disableUi(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue