Parametrizzata la funzione EVENTS.query(). Definiti i formati delle date in store/display. Migliorato il template degli eventi

This commit is contained in:
gine 2017-03-19 12:03:58 +01:00
parent 394a58ed18
commit 61f0542497

View file

@ -1,48 +1,74 @@
var EVENTS = EVENTS || {}; var EVENTS = EVENTS || {};
//var date = moment();
EVENTS.dateFormat = {
store : {
date: "YYYY-MM-DD",
hour: "HH:mm"
},
display : {
date: "DD-MM-YYYY",
hour: "HH:mm"
}
}
EVENTS.storeDateFormat = EVENTS.dateFormat.store.date + " " + EVENTS.dateFormat.store.hour;
EVENTS.displayDateFormat = EVENTS.dateFormat.display.date + " " + EVENTS.dateFormat.display.hour;
EVENTS.render = function() { EVENTS.render = function() {
var ret = checkConnection('ERR_EVENT'); var ret = checkConnection('ERR_EVENT');
if (ret) { if (ret) {
EVENTS.querySingle(); EVENTS.query(ai1ec.cats.single);
} else { } else {
EVENTS.loadFromDb('#mostralista', ai1ec.cats.single); EVENTS.loadFromDb('#mostralista', ai1ec.cats.single);
} }
} }
EVENTS.querySingle = function () { EVENTS.formatDate = function(date, format) {
var mdate = moment(date);
return mdate.isValid() ? mdate.format(format) : "";
}
EVENTS.query = function (type) {
type = type || ai1ec.cats.single;
var table = type == ai1ec.cats.repeat ? 'xm24_events_repeat' : 'xm24_events_single';
$.mobile.loading('show'); $.mobile.loading('show');
ai1ec.remote.getLastEvents(ai1ec.cats.single, function(events) { ai1ec.remote.getLastEvents(type, function(events) {
var n_evs = events.length, var n_evs = events.length,
n_evs_ins = 0, n_evs_ins = 0,
n_evs_err = 0; n_evs_err = 0;
if(n_evs > 0) { if(n_evs > 0) {
FONTE.dbHandler.executeSql('DELETE FROM xm24_events_single', [], function() { FONTE.dbHandler.executeSql('DELETE FROM ' + table , [], function() {
for (var i = 0; i < events.length; i++) { for (var i = 0; i < events.length; i++) {
var event = events[i]; var event = events[i];
var evId = ai1ec.event.getId(event); var evId = ai1ec.event.getId(event);
var evTitle = ai1ec.event.getTitle(event); var evTitle = ai1ec.event.getTitle(event);
var evDesc = ai1ec.event.getDescription(event); var evDesc = ai1ec.event.getDescription(event);
var evInitDate = ai1ec.event.getInitDate(event);
var evEndDate = ai1ec.event.getEndDate(event);
var evUrl = ai1ec.event.getUrl(event); var evUrl = ai1ec.event.getUrl(event);
var evGeo = ai1ec.event.getGeo(event); var evGeo = ai1ec.event.getGeo(event);
FONTE.dbHandler.executeSql('INSERT INTO xm24_events_single VALUES (?,?,?,?,?,?,?)', [evId, evTitle, evDesc, evInitDate, evEndDate, evUrl, evGeo], function (resultSet) { var evInitDate = EVENTS.formatDate(ai1ec.event.getInitDate(event), EVENTS.storeDateFormat);
var evEndDate = EVENTS.formatDate(ai1ec.event.getEndDate(event), EVENTS.storeDateFormat);
FONTE.dbHandler.executeSql('INSERT INTO ' + table + ' VALUES (?,?,?,?,?,?,?)', [evId, evTitle, evDesc, evInitDate, evEndDate, evUrl, evGeo],
//onSuccess
function (resultSet) {
n_evs_ins++; n_evs_ins++;
console.log('resultSet.insertId: ' + resultSet.insertId); console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected); console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
if(n_evs_ins + n_evs_err == n_evs - 1) { if(n_evs_ins + n_evs_err == n_evs - 1) {
if(n_evs_ins > 0) { if(n_evs_ins > 0) {
EVENTS.loadFromDb('#mostralista', ai1ec.cats.single); EVENTS.loadFromDb('#mostralista', type);
} }
} }
}, function (error) { },
//onError
function (error) {
n_evs_err++; n_evs_err++;
if(n_evs_ins + n_evs_err == n_evs - 1) { if(n_evs_ins + n_evs_err == n_evs - 1) {
if(n_evs_ins > 0) { if(n_evs_ins > 0) {
EVENTS.loadFromDb('#mostralista', ai1ec.cats.single); EVENTS.loadFromDb('#mostralista', type);
} else { } else {
toastr.info("Non ci sono nuovi eventi."); toastr.info("Non ci sono nuovi eventi.");
$.mobile.loading('hide'); $.mobile.loading('hide');
@ -52,7 +78,8 @@ EVENTS.querySingle = function () {
}); });
} }
}, function(error){ }, function(error){
toastr.error("Err: errore nel TRUNCATE della tabella degli eventi single"); console.error("EVENTS.query() : ", error + " type: " + type);
toastr.error("Err: errore nel TRUNCATE della tabella degli eventi");
}); });
} else { } else {
toastr.info("Non ci sono nuovi eventi."); toastr.info("Non ci sono nuovi eventi.");
@ -60,42 +87,20 @@ EVENTS.querySingle = function () {
}); });
}; };
EVENTS.queryRepeat = function () {
$.mobile.loading('show');
ai1ec.remote.getLastEvents(ai1ec.cats.single, function(events) {
for (var i = 0; i < events.length; i++) {
var event = events[i];
var evId = ai1ec.event.getId(event);
var evTitle = ai1ec.event.getTitle(event);
var evDesc = ai1ec.event.getDescription(event);
var evInitDate = ai1ec.event.getInitDate(event);
var evEndDate = ai1ec.event.getEndDate(event);
var evUrl = ai1ec.event.getUrl(event);
var evGeo = ai1ec.event.getGeo(event);
FONTE.dbHandler.executeSql('TRUNCATE TABLE xm24_events_repeat', [], function() {
FONTE.dbHandler.executeSql('INSERT INTO xm24_events_repeat VALUES (?,?,?,?,?,?,?)', [evId, evTitle, evDesc, evInitDate, evEndDate, evUrl, evGeo], function (resultSet) {
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
$.mobile.loading('hide');
}, function (error) {
console.error("EVENTS.query() : ", error);
});
});
}
});
};
EVENTS.loadFromDb = function (containerId, type) { EVENTS.loadFromDb = function (containerId, type) {
var table = type == ai1ec.cats.single ? 'xm24_events_single' : 'xm24_events_repeat'; var table = type == ai1ec.cats.repeat ? 'xm24_events_repeat' : 'xm24_events_single';
FONTE.dbHandler.executeSql("SELECT * FROM " + table + " ORDER BY initDate COLLATE NOCASE ASC;", [], function (resultSet) { FONTE.dbHandler.executeSql("SELECT * FROM " + table + " ORDER BY initDate COLLATE NOCASE ASC;", [], function (resultSet) {
var items = []; var items = [];
for (var i = 0; i < resultSet.rows.length; i++) { for (var i = 0; i < resultSet.rows.length; i++) {
var item = resultSet.rows.item(i); var item = resultSet.rows.item(i);
var period = "Da: " + item.initDate; var evInitDate = EVENTS.formatDate(item.initDate, EVENTS.displayDateFormat);
if(item.endDate) { var evEndDate = EVENTS.formatDate(item.endDate, EVENTS.displayDateFormat);
period += " A: " + item.endDate;
var evFrom = "Da: " + evInitDate;
var evTo = "";
if(evEndDate != "") {
evTo = "<br/>" + " A: " + evEndDate;
} }
var li = "<li>" + var li = "<li>" +
@ -104,8 +109,10 @@ EVENTS.loadFromDb = function (containerId, type) {
+ item.title + + item.title +
"</a>" + "</a>" +
"<br/>" + "<br/>" +
period + evFrom +
"</li>"; evTo
"</li>";
items.push(li); items.push(li);
} }
$(containerId).empty(); $(containerId).empty();