function myJsonCategory(Qcategory, div, addTagCategory) {
FONTE.dbHandler.executeSql("SELECT DISTINCT * 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 date = val.date;
var title = val.title.rendered;
var url = val.guid.rendered;
var cleanTime = EVENTS.formatDate(date, EVENTS.dateFormat.display.date);
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("
";
return li;
};
var drawListPost = function(div, resultSet, position) {
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 date = val.date;
var title = val.title.rendered;
var url = val.guid.rendered;
var cleanTime = EVENTS.formatDate(date, EVENTS.dateFormat.display.date);
items.push(getLiPost(id, title, cleanTime));
}
var ul = $("
", {
html: items.join("")
});
if(position == 'append'){
ul.appendTo(div);
} else if(position == 'prepend'){
ul.prependTo(div);
}
setTimeout(function () {
$.mobile.loading('hide');
}, 1000);
};
var appendPostList = function(div, resultSet, categories, fallback) {
var type = getObjKey(FONTE.categories, categories);
if (resultSet.rows.length > 0) {
FONTE.page_loaded[type]++;
drawListPost(div, resultSet, 'append');
} else if (fallback) {
fallback();
} else {
$.mobile.loading('hide');
toastr.info("Non ci sono altri articoli da visualizzare");
}
};
var prependPostsList = function(div, resultSet, categories, fallback) {
var type = getObjKey(FONTE.categories, categories);
//$(div).empty();
if (resultSet.rows.length > 0) {
FONTE.page_loaded[type]++;
drawListPost(div, resultSet, 'prepend');
} else if (fallback) {
fallback(div, categories);
} else {
drawNoPostsFound(div);
}
};
function drawNoPostsFound(div) {
$(div).empty();
$(div).append('Non ci sono nuovi contenuti.');
$.mobile.loading('hide');
}
function sqlErrHandler(error){
$.mobile.loading('hide');
console.log(error.message);
}
var getCategoryWhere = function(categories) {
var catsWhere = "";
for(var i = 0, len = categories.length; i < len; i++) {
var cat = categories[i];
var c = 'catid = '+ cat + ' ';
if(i === 0) {
catsWhere = '(' + c;
} else if(i == len -1) {
catsWhere += 'OR ' + c + ')';
} else {
catsWhere += 'OR ' + c;
}
}
return catsWhere;
};
var POST = POST || {};
POST.sql = {
init: "SELECT DISTINCT * FROM xm24_posts JOIN xm24_cats_to_posts USING(id) WHERE ",
end : "ORDER BY date COLLATE NOCASE DESC LIMIT ?"
};
POST.table = "xm24_posts";
var selectLastNews = function(div, categories) {
var catsWhere = getCategoryWhere(categories);
var sql = POST.sql.init + catsWhere + POST.sql.end;
FONTE.dbHandler.executeSql(sql, [REST_PARAMS.max_page], function (resultSet) {
prependPostsList(div, resultSet, categories);
}, sqlErrHandler);
};
function renderLastNews(div, categories) {
var storage = window.localStorage;
var type = getObjKey(FONTE.categories, categories);
var lastnewsDate = storage.getItem('db_last_' + type + '_date');
var firstTime = storage.getItem('db_last_' + type + '_firsttime');
if (firstTime == 'yes') {
selectLastNews(div, categories);
} else if (firstTime == 'no') {
if (lastnewsDate) {
var sql = POST.sql.init + "date > ? AND " + getCategoryWhere(categories) + POST.sql.end;
FONTE.dbHandler.executeSql(sql, [lastnewsDate, REST_PARAMS.max_page], function (resultSet) {
prependPostsList(div, resultSet, categories, selectLastNews);
}, sqlErrHandler);
} else {
selectLastNews(div, categories);
toastr.info("Non ci sono nuovi contenuti");
}
} else {
drawNoPostsFound(div);
}
}
function renderPrevNews(div, categories, page, callback){
var sql = POST.sql.init + getCategoryWhere(categories) + POST.sql.end + ', ?';
FONTE.dbHandler.executeSql(sql, [page * REST_PARAMS.max_page, (page+1) * REST_PARAMS.max_page], function (resultSet) {
appendPostList(div, resultSet, categories, callback);
}, sqlErrHandler);
}
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 DISTINCT * 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("
", {
"class": "my-new-list",
html: items.join("")
}).prependTo(div);
} else {
$(div).empty();
$(div).append('Non ci sono nuovi contenuti.');
}
});
} else {
$(div).empty();
$(div).append('Tutti i contenuti sono nuovi.');
}
} else {
$(div).empty();
$(div).append('Tutti i contenuti sono nuovi.');
}
}
function myJsonCategoryPaginated(Qcategory, div, pageNumber) {
$.mobile.loading('show');
FONTE.dbHandler.executeSql("SELECT DISTINCT * 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("