Ilovexm24/www/js/query.js

338 行
15 KiB
JavaScript

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)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_events_single (id PRIMARY KEY, title, description, initDate, endDate, url, geo)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_events (id, tagid)');
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_events_repeat (id PRIMARY KEY, title, description, initDate, endDate, url, geo)');
}, 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
});
};