Ilovexm24/www/js/update.js

255 lines
11 KiB
JavaScript
Raw Permalink Normal View History

var REST_PARAMS = REST_PARAMS || {};
REST_PARAMS.api = "wp-json/wp/v2";
REST_PARAMS.posts = REST_PARAMS.api + "/posts?";
REST_PARAMS.pages = REST_PARAMS.api + "/pages?";
REST_PARAMS.max_page = 10;
REST_PARAMS.per_page = 'per_page=' + REST_PARAMS.max_page;
2017-03-18 15:43:44 +01:00
function aggiornaPages() {
var jsonurl = weburl + REST_PARAMS.pages;
2017-03-18 15:43:44 +01:00
$.ajax({
2017-03-25 16:32:18 +01:00
dataType: "json",
url: jsonurl,
success: function (data) {
2017-03-18 15:43:44 +01:00
$.each(data, function (key, val) {
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var content = val.content.rendered;
FONTE.dbHandler.executeSql('INSERT INTO xm24_pages VALUES (?,?,?,?)', [id, title, time, content], function (resultSet) {
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function (error) {
console.log('INSERT error: ' + error.message);
});
});
2017-03-25 16:32:18 +01:00
},
error: JSONErrorHandler
2017-03-18 15:43:44 +01:00
});
2017-03-25 16:32:18 +01:00
}
2017-03-18 15:43:44 +01:00
function aggiornaSingleCategory(category) {
//resetta la variabile d'errore per le chiamate ajax asincrone
FONTE.ajaxError = false;
var jsonurl = weburl + REST_PARAMS.posts + REST_PARAMS.per_page;
2017-03-18 15:43:44 +01:00
var storage = window.localStorage;
var lastUpdate = storage.getItem('db_update_date');
if (lastUpdate) jsonurl += "&filter[date_query][column]=post_modified_gmt&filter[date_query][after]=" + lastUpdate;
2017-03-25 16:32:18 +01:00
getSinglePageRecur(jsonurl, category, 1);
}
2017-03-18 15:43:44 +01:00
function resetAndUpdate() {
var storage = window.localStorage;
//resetto la data e droppo le tabelle
storage.setItem('db_update_date', null);
FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_posts;');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_posts (id INTEGER PRIMARY KEY, title, date, category, post)');
2017-03-25 16:32:18 +01:00
postsUpdate();
2017-03-18 15:43:44 +01:00
}
function update() {
FONTE.dbHandler.executeSql('SELECT COUNT(*) AS numPosts FROM xm24_posts', [], function (resultSet) {
var numPosts = resultSet.rows.item(0).numPosts;
if (numPosts < 300) { //questo numero e' scelto in maniera arbitraria, ricordati Cri.
2017-03-25 16:32:18 +01:00
resetAndUpdate();
2017-03-18 15:43:44 +01:00
} else {
2017-03-25 16:32:18 +01:00
postsUpdate();
2017-03-18 15:43:44 +01:00
}
});
}
2017-03-25 16:32:18 +01:00
function postsUpdate(categories) {
$(FONTE.contentListClass).empty();
2017-03-18 15:43:44 +01:00
var storage = window.localStorage;
2017-03-25 16:32:18 +01:00
var type = getObjKey(FONTE.categories, categories);
var jsonurl = weburl + REST_PARAMS.posts + REST_PARAMS.per_page + "&categories=" + categories.join(',');
2017-03-18 15:43:44 +01:00
FONTE.updatedCategoriesCount = 0;
FONTE.updatedPostsCount = 0;
FONTE.neededPostsCount = 0;
FONTE.ajaxError = false;
2017-03-25 16:32:18 +01:00
var lastUpdate = storage.getItem('db_update_' + type + '_date');
if (lastUpdate)
jsonurl += "&after=" + lastUpdate;
//jsonurl += "&filter[date_query][column]=post_modified_gmt&filter[date_query][after]=" + lastUpdate;
2017-03-25 16:32:18 +01:00
var firstTime = storage.getItem('db_last_' + type + '_firsttime');
if (firstTime === null) storage.setItem('db_last_' + type + '_firsttime', 'yes');
if (firstTime == 'yes') storage.setItem('db_last_' + type + '_firsttime', 'no');
getLastSinglePageRecur(jsonurl, 1, categories);
2017-03-25 16:32:18 +01:00
}
2017-03-18 15:43:44 +01:00
2017-03-25 16:32:18 +01:00
var updateLocalStorageDate = function(type){
var date = new Date();
var currentUpdate = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
currentUpdate += 'T' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ':' + ('0' + date.getSeconds()).slice(-2);
2017-03-25 16:32:18 +01:00
var lastUpdate = window.localStorage.getItem('db_update_' + type + '_date');
2017-03-25 16:32:18 +01:00
window.localStorage.setItem('db_update_' + type + '_date', currentUpdate);
window.localStorage.setItem('db_last_' + type + '_date', lastUpdate);
};
var getPrevNews = function(div, categories, page) {
var storage = window.localStorage;
var type = getObjKey(FONTE.categories, categories);
var jsonurl = weburl + REST_PARAMS.posts + REST_PARAMS.per_page + "&categories=" + FONTE.categories[type].join(',');
FONTE.updatedCategoriesCount = 0;
FONTE.updatedPostsCount = 0;
FONTE.neededPostsCount = 0;
var oldest_date = window.localStorage.getItem('db_oldest_' + type + '_date');
if (oldest_date)
jsonurl += "&before=" + oldest_date;
getPrevSinglePageRecur(jsonurl, 1, FONTE.categories[type]);
2017-03-25 16:32:18 +01:00
};
function loadMore(type) {
var categories = FONTE.categories[type];
2017-03-25 16:32:18 +01:00
renderPrevNews(FONTE.contentListClass, categories, FONTE.page_loaded[type], function(){
getPrevNews(FONTE.contentListClass, categories, FONTE.page_loaded[type]);
});
}
function getPrevSinglePageRecur(url, pagenum, categories) {
2017-03-26 19:03:01 +02:00
$.mobile.loading('show');
var type = getObjKey(FONTE.categories, categories);
console.log('url:'+url);
$.ajax({
2017-03-25 16:32:18 +01:00
dataType: "json",
url: url + "&page=" + pagenum,
success: function (data, textStatus, jqXHR) {
var totalPages = jqXHR.getResponseHeader('X-WP-TotalPages');
FONTE.neededPostsCount += data.length;
2017-03-25 16:32:18 +01:00
if (FONTE.neededPostsCount === 0) {
2017-03-26 19:03:01 +02:00
$.mobile.loading('hide');
toastr.info('Non ci sono altri alticoli da visualizzare in questa sezione');
2017-03-25 16:32:18 +01:00
updateLocalStorageDate(type);
}
2017-03-25 16:32:18 +01:00
var n_error = 0;
var oldest_date = window.localStorage.getItem('db_oldest_' + type + '_date');
$.each(data, function (key, val) {
var id = val.id;
2017-03-25 17:13:16 +01:00
var time = val.date;
var title = val.title.rendered;
var tags = val.tags;
var postCategories = val.categories;
var blob = JSON.stringify(val);
FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_posts VALUES (?,?,?,?)', [id, title, time, blob], function (resultSet) {
FONTE.updatedPostsCount++;
oldest_date = time;
if (n_error + FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
2017-03-25 16:32:18 +01:00
updateLocalStorageDate(type);
window.localStorage.setItem('db_oldest_' + type + '_date', oldest_date);
toastr.info('Caricati altri ' + FONTE.updatedPostsCount + ' articoli.');
loadMore(type);
} //manca il caso in cui sono stati cancellati dei post online, vanno cancellati anche dall'app.
}, function (error) {
n_error++;
if (n_error + FONTE.updatedPostsCount >= FONTE.neededPostsCount && FONTE.updatedPostsCount > 0 ) {
2017-03-25 16:32:18 +01:00
updateLocalStorageDate(type);
window.localStorage.setItem('db_oldest_' + type + '_date', oldest_date);
toastr.info('Caricati altri ' + FONTE.updatedPostsCount + ' articoli.');
loadMore(type);
}
});
$.each(postCategories, function (key, val) {
insertOrUpdateCatToPost(id, val);
});
});
2017-03-25 16:32:18 +01:00
},
error: JSONErrorHandler
});
2017-03-25 16:32:18 +01:00
}
function getLastSinglePageRecur(url, pagenum, categories) {
// resetto la paginazione di #info e #ilove
FONTE.page_loaded.info = 0;
FONTE.page_loaded.ilove = 0;
var type = getObjKey(FONTE.categories, categories);
console.log('url:'+url);
$.ajax({
2017-03-25 16:32:18 +01:00
dataType: "json",
url: url + "&page=" + pagenum,
success: function (data, textStatus, jqXHR) {
var totalPages = jqXHR.getResponseHeader('X-WP-TotalPages');
FONTE.neededPostsCount += data.length;
2017-03-25 16:32:18 +01:00
if (FONTE.neededPostsCount === 0) {
toastr.info('Nessun nuovo articolo.');
2017-03-25 16:32:18 +01:00
updateLocalStorageDate(type);
if(FONTE.page_loaded[type] === 0) {
renderLastNews(FONTE.contentListClass, categories);
}
}
2017-03-25 16:32:18 +01:00
var n_error = 0;
var oldest_date = window.localStorage.getItem('db_oldest_' + type + '_date');
var older_date = null;
$.each(data, function (key, val) {
var id = val.id;
2017-03-25 17:13:16 +01:00
var time = val.date;
var title = val.title.rendered;
var tags = val.tags;
var postCategories = val.categories;
var blob = JSON.stringify(val);
FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_posts VALUES (?,?,?,?)', [id, title, time, blob], function (resultSet) {
FONTE.updatedPostsCount++;
console.log('Voci aggiornate: ' + FONTE.updatedPostsCount);
older_date = time;
if (n_error + FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
2017-03-25 16:32:18 +01:00
updateLocalStorageDate(type);
if(! oldest_date)
window.localStorage.setItem('db_oldest_' + type + '_date', older_date);
toastr.info('Ci sono ' + FONTE.updatedPostsCount + ' nuovi articoli.');
2017-03-25 16:32:18 +01:00
renderLastNews(FONTE.contentListClass, categories);
} //manca il caso in cui sono stati cancellati dei post online, vanno cancellati anche dall'app.
}, function (error) {
n_error++;
if (n_error + FONTE.updatedPostsCount >= FONTE.neededPostsCount && FONTE.updatedPostsCount > 0 ) {
2017-03-25 16:32:18 +01:00
updateLocalStorageDate(type);
if(! oldest_date)
window.localStorage.setItem('db_oldest_' + type + '_date', older_date);
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' nuovi articoli.');
2017-03-25 16:32:18 +01:00
renderLastNews(FONTE.contentListClass, categories);
}
});
// $.each(tags, function (key, val) {
// aggiornaTagsToPosts(id, val);
// });
$.each(postCategories, function (key, val) {
insertOrUpdateCatToPost(id, val);
});
});
2017-04-19 17:42:11 +02:00
// if (data.length === 0) {
// renderLastNews(FONTE.contentListClass, categories);
// }
2017-03-25 16:32:18 +01:00
},
error: JSONErrorHandler
});
}