diviso il js in piu' file

This commit is contained in:
cek@posta.indivia.net 2017-03-18 15:43:44 +01:00
parent fa9a02e619
commit a169a5d66e
4 changed files with 723 additions and 460 deletions

View file

@ -36,6 +36,8 @@
<!--script src="js/swipe-page.js"></script-->
<script src="js/main.js"></script>
<script src="js/update.js"></script>
<script src="js/query.js"></script>
<script src="js/vendor/jquery.mobile-1.4.5.min.js"></script>
<script src="js/vendor/xml2json.js"></script>
<script src="js/vendor/toastr.js"></script>
@ -420,4 +422,4 @@
<!-- chiude pagina -->
</body>
</html>
</html>

View file

@ -118,9 +118,9 @@ function benvenuta() {
$("#benvenuta").popup("open");
};
function aggiornaora() {
$("#aggiornaora").popup("open");
};
//function aggiornaora() {
// $("#aggiornaora").popup("open");
//};
function setFirstRun() {
initStorage();
@ -231,243 +231,243 @@ function postInListTag(div) {
});
};
function initTag(div) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_tags ORDER BY date COLLATE NOCASE DESC;", [], function (resultSet) {
var items = [];
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).tag);
var name = val.name;
var slug = val.slug;
var id = val.id;
items.push("<li><img src='./image/virgolette_1.png' class='epuntato'/><a href='#posttag' data-post='" + id + "' >" + name + "</a><br/></li>");
}
$(div).empty();
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
}, function (error) {
console.log(error);
});
$.mobile.loading('hide');
}
//function initTag(div) {
// $.mobile.loading('show');
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_tags ORDER BY date COLLATE NOCASE DESC;", [], function (resultSet) {
// var items = [];
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = JSON.parse(resultSet.rows.item(i).tag);
// var name = val.name;
// var slug = val.slug;
// var id = val.id;
// items.push("<li><img src='./image/virgolette_1.png' class='epuntato'/><a href='#posttag' data-post='" + id + "' >" + name + "</a><br/></li>");
// }
// $(div).empty();
// $("<ul/>", {
// "class": "my-new-list"
// , html: items.join("")
// }).prependTo(div);
// }, function (error) {
// console.log(error);
// });
// $.mobile.loading('hide');
//}
function myJsonPost(Qid, div_title, div_content) {
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE id = ?;", [Qid], function (resultSet) {
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var content = val.content.rendered;
content = findImg(content);
//function myJsonPost(Qid, div_title, div_content) {
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE id = ?;", [Qid], function (resultSet) {
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = JSON.parse(resultSet.rows.item(i).post);
// var id = val.id;
// var time = val.modified;
// var title = val.title.rendered;
// var url = val.guid.rendered;
// var content = val.content.rendered;
// content = findImg(content);
//
// $(div_title).html(title);
// //key mi dice l'indice'
// $(div_content).html("<p>" + content + "</p>");
// }
// }, function (error) {
// console.log(error);
// });
//};
//
//function myJsonPage(Qid, div_title, div_content) {
// $.mobile.loading('show');
// var pageId = parseInt(Qid);
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_pages WHERE id = ?;", [pageId], function (resultSet) {
// for (var i = 0; i < resultSet.rows.length; i++) {
// var data = resultSet.rows.item(i);
// var id = data.id;
// var time = data.time;
// var title = data.title;
// var content = data.page;
// content = findImg(content);
// $(div_title).html(title);
// //key mi dice l'indice'
// $(div_content).html("<p>" + content + "</p>");
// }
// });
// $.mobile.loading('hide');
//};
//
//function myJsonRandomPost(div_title, div_content) {
// $.mobile.loading('show');
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts ORDER BY RANDOM() LIMIT 1;", [], function (resultSet) {
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = JSON.parse(resultSet.rows.item(i).post);
// var id = val.id;
// var time = val.modified;
// var title = val.title.rendered;
// var url = val.guid.rendered;
// var content = val.content.rendered;
// content = findImg(content);
// $(div_title).html(title);
// //key mi dice l'indice'
// $(div_content).html("<p>" + content + "</p>");
// }
// });
// $.mobile.loading('hide');
//};
//
//function myJsonPostByTagList(idTag, div) {
// $.mobile.loading('show');
// var tagid = parseInt(idTag);
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_tags_to_posts WHERE tagid = ?;", [tagid], function (resultSet) {
// var items = [];
// var finishCounter = 0;
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = resultSet.rows.item(i);
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE id = ? ORDER BY title COLLATE NOCASE ASC;", [val.id], function (resultSetPost) {
// finishCounter++;
// for (var n = 0; n < resultSetPost.rows.length; n++) {
// var val = JSON.parse(resultSetPost.rows.item(n).post);
// var id = val.id;
// var time = val.modified;
// var title = val.title.rendered;
// var url = val.guid.rendered;
// var splitTime = time.split("T");
// var cleanTime = splitTime[0];
// //key mi dice l'indice'
// items.push({
// item: "<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</li>"
// , title: title
// });
// }
// // fallo solo quando items è stato riempito
// if (finishCounter >= resultSet.rows.length - 1) {
// $(div).empty();
// $("<ul/>", {
// "class": "my-new-list"
// , html: function () {
// function compare(a, b) {
// if (a.title < b.title) return -1;
// if (a.title > b.title) return 1;
// return 0;
// }
// items.sort(compare);
// var finalHtml = "";
// for (var i = 0; i < items.length; i++) {
// finalHtml += items[i].item;
// }
// return finalHtml;
// }()
// }).prependTo(div);
// }
// });
// }
// }, function (error) {
// console.log(error);
// });
// $.mobile.loading('hide');
//}
$(div_title).html(title);
//key mi dice l'indice'
$(div_content).html("<p>" + content + "</p>");
}
}, function (error) {
console.log(error);
});
};
function myJsonPage(Qid, div_title, div_content) {
$.mobile.loading('show');
var pageId = parseInt(Qid);
FONTE.dbHandler.executeSql("SELECT * FROM xm24_pages WHERE id = ?;", [pageId], function (resultSet) {
for (var i = 0; i < resultSet.rows.length; i++) {
var data = resultSet.rows.item(i);
var id = data.id;
var time = data.time;
var title = data.title;
var content = data.page;
content = findImg(content);
$(div_title).html(title);
//key mi dice l'indice'
$(div_content).html("<p>" + content + "</p>");
}
});
$.mobile.loading('hide');
};
function myJsonRandomPost(div_title, div_content) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts ORDER BY RANDOM() LIMIT 1;", [], function (resultSet) {
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var content = val.content.rendered;
content = findImg(content);
$(div_title).html(title);
//key mi dice l'indice'
$(div_content).html("<p>" + content + "</p>");
}
});
$.mobile.loading('hide');
};
function myJsonPostByTagList(idTag, div) {
$.mobile.loading('show');
var tagid = parseInt(idTag);
FONTE.dbHandler.executeSql("SELECT * FROM xm24_tags_to_posts WHERE tagid = ?;", [tagid], function (resultSet) {
var items = [];
var finishCounter = 0;
for (var i = 0; i < resultSet.rows.length; i++) {
var val = resultSet.rows.item(i);
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE id = ? ORDER BY title COLLATE NOCASE ASC;", [val.id], function (resultSetPost) {
finishCounter++;
for (var n = 0; n < resultSetPost.rows.length; n++) {
var val = JSON.parse(resultSetPost.rows.item(n).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var splitTime = time.split("T");
var cleanTime = splitTime[0];
//key mi dice l'indice'
items.push({
item: "<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</li>"
, title: title
});
}
// fallo solo quando items è stato riempito
if (finishCounter >= resultSet.rows.length - 1) {
$(div).empty();
$("<ul/>", {
"class": "my-new-list"
, html: function () {
function compare(a, b) {
if (a.title < b.title) return -1;
if (a.title > b.title) return 1;
return 0;
}
items.sort(compare);
var finalHtml = "";
for (var i = 0; i < items.length; i++) {
finalHtml += items[i].item;
}
return finalHtml;
}()
}).prependTo(div);
}
});
}
}, function (error) {
console.log(error);
});
$.mobile.loading('hide');
}
function myJsonCategory(Qcategory, div, addTagCategory) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) {
var items = [];
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var splitTime = time.split("T");
var cleanTime = splitTime[0];
var tag = '';
var cattag = '';
/* if (addTagCategory) {
$.each(val.tags, function (index, value) {
if (value == 43) {
}
if (value == 36) {
}
});
//key mi dice l'indice'
items.push("<li class='"+ cattag +"'><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|<span>" + tag + "</span>|</li>");
}
else {*/
items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|</li>");
}
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
});
$.mobile.loading('hide');
};
function myJsonLastPage(div) {
var storage = window.localStorage;
var lastnewsDate = storage.getItem('db_lastnews_date');
var firstTime = storage.getItem('db_lastnews_firsttime');
if (firstTime != 'yes') {
if (lastnewsDate) {
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE date > ? ORDER BY date COLLATE NOCASE DESC LIMIT 40;", [lastnewsDate], function (resultSet) {
var items = [];
$(div).empty();
if (resultSet.rows.length > 0) {
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
// console.log(Url);
var splitTime = time.split("T");
var cleanTime = splitTime[0];
//key mi dice l'indice'
items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|");
}
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
} else {
$(div).empty();
$(div).append('<span>Non ci sono nuovi contenuti.</span>')
}
});
} else {
$(div).empty();
$(div).append('<span>Tutti i contenuti sono nuovi.</span>')
}
} else {
$(div).empty();
$(div).append('<span>Tutti i contenuti sono nuovi.</span>')
}
};
function myJsonCategoryPaginated(Qcategory, div, pageNumber) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) {
var items = [];
$(div).empty();
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var tag = val.tags;
var splitTime = time.split("T");
var cleanTime = splitTime[0];
//key mi dice l'indice'
items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|</li>");
}
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
}, function (error) {
console.log('errore myJsonCategoryPaginated: ' + error);
});
$.mobile.loading('hide');
};
//function myJsonCategory(Qcategory, div, addTagCategory) {
// $.mobile.loading('show');
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) {
// var items = [];
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = JSON.parse(resultSet.rows.item(i).post);
// var id = val.id;
// var time = val.modified;
// var title = val.title.rendered;
// var url = val.guid.rendered;
// var splitTime = time.split("T");
// var cleanTime = splitTime[0];
// var tag = '';
// var cattag = '';
// /* if (addTagCategory) {
// $.each(val.tags, function (index, value) {
// if (value == 43) {
//
// }
// if (value == 36) {
//
// }
// });
// //key mi dice l'indice'
// items.push("<li class='"+ cattag +"'><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|<span>" + tag + "</span>|</li>");
// }
// else {*/
// items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|</li>");
//
// }
// $("<ul/>", {
// "class": "my-new-list"
// , html: items.join("")
// }).prependTo(div);
// });
// $.mobile.loading('hide');
//};
//
//function myJsonLastPage(div) {
// var storage = window.localStorage;
// var lastnewsDate = storage.getItem('db_lastnews_date');
// var firstTime = storage.getItem('db_lastnews_firsttime');
// if (firstTime != 'yes') {
// if (lastnewsDate) {
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE date > ? ORDER BY date COLLATE NOCASE DESC LIMIT 40;", [lastnewsDate], function (resultSet) {
// var items = [];
// $(div).empty();
// if (resultSet.rows.length > 0) {
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = JSON.parse(resultSet.rows.item(i).post);
// var id = val.id;
// var time = val.modified;
// var title = val.title.rendered;
// var url = val.guid.rendered;
// // console.log(Url);
// var splitTime = time.split("T");
// var cleanTime = splitTime[0];
// //key mi dice l'indice'
// items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|");
// }
// $("<ul/>", {
// "class": "my-new-list"
// , html: items.join("")
// }).prependTo(div);
// } else {
// $(div).empty();
// $(div).append('<span>Non ci sono nuovi contenuti.</span>')
// }
// });
// } else {
// $(div).empty();
// $(div).append('<span>Tutti i contenuti sono nuovi.</span>')
// }
// } else {
// $(div).empty();
// $(div).append('<span>Tutti i contenuti sono nuovi.</span>')
// }
//};
//
//function myJsonCategoryPaginated(Qcategory, div, pageNumber) {
// $.mobile.loading('show');
// FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) {
// var items = [];
// $(div).empty();
// for (var i = 0; i < resultSet.rows.length; i++) {
// var val = JSON.parse(resultSet.rows.item(i).post);
// var id = val.id;
// var time = val.modified;
// var title = val.title.rendered;
// var url = val.guid.rendered;
// var tag = val.tags;
// var splitTime = time.split("T");
// var cleanTime = splitTime[0];
// //key mi dice l'indice'
// items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|</li>");
// }
// $("<ul/>", {
// "class": "my-new-list"
// , html: items.join("")
// }).prependTo(div);
// }, function (error) {
// console.log('errore myJsonCategoryPaginated: ' + error);
// });
// $.mobile.loading('hide');
//};
function cercaMe() {
var input_text = $("input.cerca").val();
@ -515,139 +515,6 @@ function cercaMe() {
}
}
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)');
partialUpdate();
}
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.
resetAndUpdate()
} else {
partialUpdate();
}
});
}
function partialUpdate() {
//feedback
$("#aggiorna").attr("src", "./image/icona_aggiorna.gif");
var storage = window.localStorage;
var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=100&categories=";
FONTE.updatedCategoriesCount = 0;
FONTE.updatedPostsCount = 0;
FONTE.neededPostsCount = 0;
for (var index in FONTE.categoriesList) {
aggiornaSingleCategory(FONTE.categoriesList[index])
}
FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_tags;');
// FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_tags_to_posts;');
FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_pages;');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags (id INTEGER PRIMARY KEY, title, tag)');
// FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_posts (id, tagid)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_pages (id INTEGER PRIMARY KEY, title, date, page)');
aggiornaTags(1);
aggiornaPages();
var firstTime = storage.getItem('db_lastnews_firsttime');
if (firstTime == null) storage.setItem('db_lastnews_firsttime', 'yes');
if (firstTime == 'yes') storage.setItem('db_lastnews_firsttime', 'no');
$("#benvenuta").popup("close");
checkloader();
};
function getSinglePageRecur(url, category, pagenum) {
$.ajax({
dataType: "json"
, url: url + "&categories=" + category + "&page=" + pagenum
, success: function (data, textStatus, jqXHR) {
var totalPages = jqXHR.getResponseHeader('X-WP-TotalPages');
if (pagenum == totalPages || totalPages == 0) FONTE.updatedCategoriesCount += 1;
FONTE.neededPostsCount += data.length;
if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.neededPostsCount == 0) {
toastr.info('Nessun nuovo articolo.');
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);
var lastUpdate = window.localStorage.getItem('db_update_date')
window.localStorage.setItem('db_update_date', currentUpdate);
window.localStorage.setItem('db_lastnews_date', lastUpdate);
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
}
$.each(data, function (key, val) {
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var tags = val.tags;
var blob = JSON.stringify(val);
FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_posts VALUES (?,?,?,?,?)', [id, title, time, category, blob], function (resultSet) {
FONTE.updatedPostsCount += 1;
console.log('Voci aggiornate: ' + FONTE.updatedPostsCount);
if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
window.localStorage.setItem('has_run', 'true');
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);
var lastUpdate = window.localStorage.getItem('db_update_date')
window.localStorage.setItem('db_update_date', currentUpdate);
window.localStorage.setItem('db_lastnews_date', lastUpdate);
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.');
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
ultimoAggiornamento();
} //manca il caso in cui sono stati cancellati dei post online, vanno cancellati anche dall'app.
}, function (error) {
FONTE.updatedPostsCount += 1;
if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.');
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
}
});
$.each(tags, function (key, val) {
aggiornaTagsToPosts(id, val);
});
});
if (pagenum < totalPages) {
pagenum += 1;
getSinglePageRecur(url, category, pagenum)
}
}
, error: JSONErrorHandler
});
};
function aggiornaSingleCategory(category) {
//resetta la variabile d'errore per le chiamate ajax asincrone
FONTE.ajaxError = false;
var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=100";
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;
getSinglePageRecur(jsonurl, category, 1)
};
function checkloader() {
myJsonLastPage('#mostralistaultime');
var mieultime = $("#mostralistaultime").html();
@ -663,93 +530,6 @@ function checkloader() {
return false
}
function aggiornaTagsToPosts(id, tagId) {
FONTE.dbHandler.executeSql('INSERT INTO xm24_tags_to_posts VALUES (?,?)', [id, tagId], function (resultSet) {
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function (error) {
console.log('INSERT error: ' + error.message);
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
});
}
function aggiornaTags(pagenum) {
var jsonurl = weburl + "wp-json/wp/v2/tags?per_page=100&page=" + pagenum;
$.ajax({
dataType: "json"
, url: jsonurl
, success: function (data, status, xhr) {
var totalPages = parseInt(xhr.getResponseHeader("X-WP-TotalPages"));
$.each(data, function (key, val) {
var name = val.name;
var slug = val.slug;
var id = val.id;
var blob = JSON.stringify(val);
FONTE.dbHandler.executeSql('INSERT INTO xm24_tags VALUES (?,?,?)', [id, slug, blob], function (resultSet) {
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function (error) {
console.log('INSERT error: ' + error.message);
});
});
pagenum += 1
if (pagenum <= totalPages) aggiornaTags(pagenum)
}
, error: JSONErrorHandler
});
};
function aggiornaPages() {
var jsonurl = weburl + "wp-json/wp/v2/pages/";
$.ajax({
dataType: "json"
, url: jsonurl
, success: function (data) {
$.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);
});
});
}
, error: JSONErrorHandler
});
};
function createDB() {
//crea il databse per i posts se non esiste
FONTE.dbHandler = window.sqlitePlugin.openDatabase({
name: FONTE.dbName
, location: 'default'
}, function () {
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_posts (id INTEGER PRIMARY KEY, title, date, category, post)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags (id INTEGER PRIMARY KEY, title, tag)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_posts (id, tagid)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_pages (id INTEGER PRIMARY KEY, title, date, page)');
}, function () {
console.log('error');
console.log(arguments);
});
};
function deleteDB() {
window.sqlitePlugin.deleteDatabase({
name: FONTE.dbName
, location: 'default'
});
var storage = window.localStorage;
storage.setItem('db_update_date', null);
storage.setItem('db_lastnews_date', null);
createDB();
}
function initTagsPage() {
if (!FONTE.tagsPageInitiated) {
initTag("#mostralistaTag"); // questo mostra i tag
@ -832,4 +612,4 @@ function checkAndReset() {
}
return ret;
};
};

335
www/js/query.js Normal file
View file

@ -0,0 +1,335 @@
function myJsonCategory(Qcategory, div, addTagCategory) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) {
var items = [];
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var splitTime = time.split("T");
var cleanTime = splitTime[0];
var tag = '';
var cattag = '';
/* if (addTagCategory) {
$.each(val.tags, function (index, value) {
if (value == 43) {
}
if (value == 36) {
}
});
//key mi dice l'indice'
items.push("<li class='"+ cattag +"'><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|<span>" + tag + "</span>|</li>");
}
else {*/
items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|</li>");
}
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
});
$.mobile.loading('hide');
};
function myJsonLastPage(div) {
var storage = window.localStorage;
var lastnewsDate = storage.getItem('db_lastnews_date');
var firstTime = storage.getItem('db_lastnews_firsttime');
if (firstTime != 'yes') {
if (lastnewsDate) {
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE date > ? ORDER BY date COLLATE NOCASE DESC LIMIT 40;", [lastnewsDate], function (resultSet) {
var items = [];
$(div).empty();
if (resultSet.rows.length > 0) {
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
// console.log(Url);
var splitTime = time.split("T");
var cleanTime = splitTime[0];
//key mi dice l'indice'
items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|");
}
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
} else {
$(div).empty();
$(div).append('<span>Non ci sono nuovi contenuti.</span>')
}
});
} else {
$(div).empty();
$(div).append('<span>Tutti i contenuti sono nuovi.</span>')
}
} else {
$(div).empty();
$(div).append('<span>Tutti i contenuti sono nuovi.</span>')
}
};
function myJsonCategoryPaginated(Qcategory, div, pageNumber) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) {
var items = [];
$(div).empty();
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var tag = val.tags;
var splitTime = time.split("T");
var cleanTime = splitTime[0];
//key mi dice l'indice'
items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small>|</li>");
}
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
}, function (error) {
console.log('errore myJsonCategoryPaginated: ' + error);
});
$.mobile.loading('hide');
};
function myJsonPost(Qid, div_title, div_content) {
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE id = ?;", [Qid], function (resultSet) {
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var content = val.content.rendered;
content = findImg(content);
$(div_title).html(title);
//key mi dice l'indice'
$(div_content).html("<p>" + content + "</p>");
}
}, function (error) {
console.log(error);
});
};
function myJsonPage(Qid, div_title, div_content) {
$.mobile.loading('show');
var pageId = parseInt(Qid);
FONTE.dbHandler.executeSql("SELECT * FROM xm24_pages WHERE id = ?;", [pageId], function (resultSet) {
for (var i = 0; i < resultSet.rows.length; i++) {
var data = resultSet.rows.item(i);
var id = data.id;
var time = data.time;
var title = data.title;
var content = data.page;
content = findImg(content);
$(div_title).html(title);
//key mi dice l'indice'
$(div_content).html("<p>" + content + "</p>");
}
});
$.mobile.loading('hide');
};
function myJsonRandomPost(div_title, div_content) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts ORDER BY RANDOM() LIMIT 1;", [], function (resultSet) {
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var content = val.content.rendered;
content = findImg(content);
$(div_title).html(title);
//key mi dice l'indice'
$(div_content).html("<p>" + content + "</p>");
}
});
$.mobile.loading('hide');
};
function myJsonPostByTagList(idTag, div) {
$.mobile.loading('show');
var tagid = parseInt(idTag);
FONTE.dbHandler.executeSql("SELECT * FROM xm24_tags_to_posts WHERE tagid = ?;", [tagid], function (resultSet) {
var items = [];
var finishCounter = 0;
for (var i = 0; i < resultSet.rows.length; i++) {
var val = resultSet.rows.item(i);
FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE id = ? ORDER BY title COLLATE NOCASE ASC;", [val.id], function (resultSetPost) {
finishCounter++;
for (var n = 0; n < resultSetPost.rows.length; n++) {
var val = JSON.parse(resultSetPost.rows.item(n).post);
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var url = val.guid.rendered;
var splitTime = time.split("T");
var cleanTime = splitTime[0];
//key mi dice l'indice'
items.push({
item: "<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</li>"
, title: title
});
}
// fallo solo quando items è stato riempito
if (finishCounter >= resultSet.rows.length - 1) {
$(div).empty();
$("<ul/>", {
"class": "my-new-list"
, html: function () {
function compare(a, b) {
if (a.title < b.title) return -1;
if (a.title > b.title) return 1;
return 0;
}
items.sort(compare);
var finalHtml = "";
for (var i = 0; i < items.length; i++) {
finalHtml += items[i].item;
}
return finalHtml;
}()
}).prependTo(div);
}
});
}
}, function (error) {
console.log(error);
});
$.mobile.loading('hide');
};
function initTag(div) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT * FROM xm24_tags ORDER BY date COLLATE NOCASE DESC;", [], function (resultSet) {
var items = [];
for (var i = 0; i < resultSet.rows.length; i++) {
var val = JSON.parse(resultSet.rows.item(i).tag);
var name = val.name;
var slug = val.slug;
var id = val.id;
items.push("<li><img src='./image/virgolette_1.png' class='epuntato'/><a href='#posttag' data-post='" + id + "' >" + name + "</a><br/></li>");
}
$(div).empty();
$("<ul/>", {
"class": "my-new-list"
, html: items.join("")
}).prependTo(div);
}, function (error) {
console.log(error);
});
$.mobile.loading('hide');
};
function createDB() {
//crea il databse per i posts se non esiste
FONTE.dbHandler = window.sqlitePlugin.openDatabase({
name: FONTE.dbName
, location: 'default'
}, function () {
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_posts (id INTEGER PRIMARY KEY, title, date, category, post)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags (id INTEGER PRIMARY KEY, title, tag)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_posts (id, tagid)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_pages (id INTEGER PRIMARY KEY, title, date, page)');
}, function () {
console.log('error');
console.log(arguments);
});
};
function deleteDB() {
window.sqlitePlugin.deleteDatabase({
name: FONTE.dbName
, location: 'default'
});
var storage = window.localStorage;
storage.setItem('db_update_date', null);
storage.setItem('db_lastnews_date', null);
createDB();
};
function getSinglePageRecur(url, category, pagenum) {
$.ajax({
dataType: "json"
, url: url + "&categories=" + category + "&page=" + pagenum
, success: function (data, textStatus, jqXHR) {
var totalPages = jqXHR.getResponseHeader('X-WP-TotalPages');
if (pagenum == totalPages || totalPages == 0) FONTE.updatedCategoriesCount += 1;
FONTE.neededPostsCount += data.length;
if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.neededPostsCount == 0) {
toastr.info('Nessun nuovo articolo.');
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);
var lastUpdate = window.localStorage.getItem('db_update_date')
window.localStorage.setItem('db_update_date', currentUpdate);
window.localStorage.setItem('db_lastnews_date', lastUpdate);
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
}
$.each(data, function (key, val) {
var id = val.id;
var time = val.modified;
var title = val.title.rendered;
var tags = val.tags;
var blob = JSON.stringify(val);
FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_posts VALUES (?,?,?,?,?)', [id, title, time, category, blob], function (resultSet) {
FONTE.updatedPostsCount += 1;
console.log('Voci aggiornate: ' + FONTE.updatedPostsCount);
if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
window.localStorage.setItem('has_run', 'true');
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);
var lastUpdate = window.localStorage.getItem('db_update_date')
window.localStorage.setItem('db_update_date', currentUpdate);
window.localStorage.setItem('db_lastnews_date', lastUpdate);
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.');
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
ultimoAggiornamento();
} //manca il caso in cui sono stati cancellati dei post online, vanno cancellati anche dall'app.
}, function (error) {
FONTE.updatedPostsCount += 1;
if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.');
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
}
});
$.each(tags, function (key, val) {
aggiornaTagsToPosts(id, val);
});
});
if (pagenum < totalPages) {
pagenum += 1;
getSinglePageRecur(url, category, pagenum)
}
}
, error: JSONErrorHandler
});
};

146
www/js/update.js Normal file
View file

@ -0,0 +1,146 @@
function aggiornaPages() {
var jsonurl = weburl + "wp-json/wp/v2/pages/";
$.ajax({
dataType: "json"
, url: jsonurl
, success: function (data) {
$.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);
});
});
}
, error: JSONErrorHandler
});
};
function aggiornaSingleCategory(category) {
//resetta la variabile d'errore per le chiamate ajax asincrone
FONTE.ajaxError = false;
var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=100";
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;
getSinglePageRecur(jsonurl, category, 1)
};
function aggiornaTagsToPosts(id, tagId) {
FONTE.dbHandler.executeSql('INSERT INTO xm24_tags_to_posts VALUES (?,?)', [id, tagId], function (resultSet) {
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function (error) {
console.log('INSERT error: ' + error.message);
//feedback
$("#aggiorna").attr("src", "./image/aggiorna.png");
});
}
function aggiornaTags(pagenum) {
var jsonurl = weburl + "wp-json/wp/v2/tags?per_page=100&page=" + pagenum;
$.ajax({
dataType: "json"
, url: jsonurl
, success: function (data, status, xhr) {
var totalPages = parseInt(xhr.getResponseHeader("X-WP-TotalPages"));
$.each(data, function (key, val) {
var name = val.name;
var slug = val.slug;
var id = val.id;
var blob = JSON.stringify(val);
FONTE.dbHandler.executeSql('INSERT INTO xm24_tags VALUES (?,?,?)', [id, slug, blob], function (resultSet) {
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function (error) {
console.log('INSERT error: ' + error.message);
});
});
pagenum += 1
if (pagenum <= totalPages) aggiornaTags(pagenum)
}
, error: JSONErrorHandler
});
};
function aggiornaora() {
$("#aggiornaora").popup("open");
};
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)');
partialUpdate();
}
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.
resetAndUpdate()
} else {
partialUpdate();
}
});
}
function partialUpdate() {
//feedback
$("#aggiorna").attr("src", "./image/icona_aggiorna.gif");
var storage = window.localStorage;
var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=100&categories=";
FONTE.updatedCategoriesCount = 0;
FONTE.updatedPostsCount = 0;
FONTE.neededPostsCount = 0;
for (var index in FONTE.categoriesList) {
aggiornaSingleCategory(FONTE.categoriesList[index])
}
FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_tags;');
// FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_tags_to_posts;');
FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_pages;');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags (id INTEGER PRIMARY KEY, title, tag)');
// FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_posts (id, tagid)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_pages (id INTEGER PRIMARY KEY, title, date, page)');
aggiornaTags(1);
aggiornaPages();
var firstTime = storage.getItem('db_lastnews_firsttime');
if (firstTime == null) storage.setItem('db_lastnews_firsttime', 'yes');
if (firstTime == 'yes') storage.setItem('db_lastnews_firsttime', 'no');
$("#benvenuta").popup("close");
checkloader();
};
/*
* data ultimo aggiornamento
*/
function ultimoAggiornamento() {
var lastUpdate = window.localStorage.getItem('db_update_date');
if (lastUpdate) {
var data = lastUpdate.split('T');
var datagiorno = data[0];
var dataora = data[1];
$("#datadiritti").empty();
$("#datadirittiora").empty();
display_results("#datadiritti", datagiorno)
display_results("#datadirittiora", dataora)
if (data) {
$("#boxdata").css("display", "block");
}
} else {
toastr.warning('Warn: manca la data del tuo ultimo aggiornamento. Aggiorna al più presto.');
}
};