From 5740a77ed52f3e8d1ba7493a7faf8d831b660942 Mon Sep 17 00:00:00 2001 From: gine Date: Fri, 24 Mar 2017 02:51:21 +0100 Subject: [PATCH] Load more funzionante per le info, preparato per il tag ilovexm24. --- www/index.html | 57 ++++++------- www/js/main.js | 38 ++++----- www/js/query.js | 78 ++++++++++++------ www/js/update.js | 204 ++++++++++++++++++++++++++--------------------- 4 files changed, 207 insertions(+), 170 deletions(-) diff --git a/www/index.html b/www/index.html index 27471b2..1a814c5 100644 --- a/www/index.html +++ b/www/index.html @@ -40,10 +40,11 @@ - - + + + @@ -99,12 +100,26 @@ Non è stato attalmente caricato alcun contenuto

- + + - - - + + +
+
+
+

I love Xm24

+

+ + Non è stato attalmente caricato alcun contenuto + +

+ + +
+
+
@@ -151,23 +166,6 @@
- -
- -
- -
-

IO AMO XM

-

- -

-
- -
- -
- -
@@ -183,25 +181,18 @@
-
-

-

- - + + Non è stato attalmente caricato alcun contenuto +

- -
-
diff --git a/www/js/main.js b/www/js/main.js index c01ea11..d61ac1c 100644 --- a/www/js/main.js +++ b/www/js/main.js @@ -22,15 +22,21 @@ FONTE.dbName = "xm24"; FONTE.dbHandler; FONTE.tagsPageInitiated = false; FONTE.categoriesList = [8, 497]; -FONTE.categoriesILove = [497,647,590]; //ilove, 4mar, solid -FONTE.categoriesInfo = [8,42,68,3]; +FONTE.categories = { + ilove : [497,647,590], //ilove, 4mar, solid + info : [8,42,68,3], //ilove, 4mar, solid +} + +FONTE.page_loaded = { + ilove : 0, + info : 0, +}; + FONTE.updatedCategoriesCount; FONTE.updatedPostsCount; FONTE.neededPostsCount; FONTE.hrefseguente = ""; -FONTE.info_page_loaded = 0; -FONTE.ilove_page_loaded = 0; - +FONTE.contentListId = '#mostralistainfo'; FONTE.ajaxError = false; /* * data ultimo aggiornamento @@ -131,9 +137,9 @@ function run() { initStorage(); overlayblock(); if (checkConnection()) { - partialUpdate(); + partialUpdate(FONTE.categories['info']); } else { - + renderLastNews(FONTE.contentListId, FONTE.categories['info']); } }; @@ -311,7 +317,7 @@ function postInListTag(div) { function cercaMe() { var input_text = $("input.cerca").val(); input_text = encodeURIComponent(input_text); - var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=100&search=" + input_text + ""; + var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=10&search=" + input_text + ""; $(".listresult").empty(); $("body").pagecontainer("change", "#ricerca"); $.mobile.loading('show'); @@ -354,22 +360,6 @@ function cercaMe() { } } -function checkloader(categories) { - var div = '#mostralistainfo'; - - displayLastUpdateDate(categories); - renderLastNews(div, categories); - - if ($(div).html() == "Non ci sono nuovi contenuti.") { - $("#aggiorna").attr("src", "./image/aggiorna.png"); - - return true - //console.log("ultime2"); - } - // console.log("ultime3"); - return false -} - function initTagsPage() { if (!FONTE.tagsPageInitiated) { initTag("#mostralistaTag"); // questo mostra i tag diff --git a/www/js/query.js b/www/js/query.js index 30886aa..f0e15b6 100644 --- a/www/js/query.js +++ b/www/js/query.js @@ -36,26 +36,51 @@ function myJsonCategory(Qcategory, div, addTagCategory) { $.mobile.loading('hide'); }; -function drawPostsList(div, resultSet, categories, fallback) { +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.modified; + var title = val.title.rendered; + var url = val.guid.rendered; + var cleanTime = EVENTS.formatDate(date, EVENTS.dateFormat.display.date); + //key mi dice l'indice' + items.push("
  • " + title + "
    " + cleanTime + ""); + } + + var ul = $("
      ", { + "class": "my-new-list", + html: items.join("") + }) + + if(position == 'append'){ + ul.appendTo(div); + } else if(position == 'prepend'){ + ul.prependTo(div); + } +} + +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 { + 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) { - 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("
    • " + title + "
      " + cleanTime + ""); - } - $("
        ", { - "class": "my-new-list" - , html: items.join("") - }).prependTo(div); + FONTE.page_loaded[type]++; + drawListPost(div, resultSet, 'prepend'); } else if (fallback) { fallback(div, categories); } else { @@ -92,15 +117,15 @@ var getCategoryWhere = function(categories) { var POST = POST || {}; POST.sql = { init: "SELECT * FROM xm24_posts JOIN xm24_cats_to_posts USING(id) WHERE ", - end : "ORDER BY date COLLATE NOCASE DESC LIMIT 10" + end : "ORDER BY date COLLATE NOCASE DESC LIMIT ?" } var selectLastNews = function(div, categories) { var catsWhere = getCategoryWhere(categories); var sql = POST.sql.init + catsWhere + POST.sql.end; - FONTE.dbHandler.executeSql(sql, [], function (resultSet) { - drawPostsList(div, resultSet); + FONTE.dbHandler.executeSql(sql, [REST_PARAMS.max_page], function (resultSet) { + prependPostsList(div, resultSet, categories); }, sqlErrHandler); } @@ -114,8 +139,8 @@ function renderLastNews(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], function (resultSet) { - drawPostsList(div, resultSet, categories, selectLastNews); + FONTE.dbHandler.executeSql(sql, [lastnewsDate, REST_PARAMS.max_page], function (resultSet) { + prependPostsList(div, resultSet, categories, selectLastNews); }, sqlErrHandler); } else { selectLastNews(div, categories); @@ -126,6 +151,13 @@ function renderLastNews(div, categories) { } }; +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'); @@ -366,7 +398,7 @@ function createDB() { FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_posts (id INTEGER PRIMARY KEY, title, date, post)'); FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_cats (catid INTEGER PRIMARY KEY, title, cat)'); - FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_cats_to_posts (id, catid)'); + FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_cats_to_posts (id, catid, PRIMARY KEY (id, catid))'); 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)'); diff --git a/www/js/update.js b/www/js/update.js index 9695009..fbb8df7 100644 --- a/www/js/update.js +++ b/www/js/update.js @@ -1,6 +1,6 @@ var REST_PARAMS = REST_PARAMS || {}; -REST_PARAMS.per_page = 'per_page=10'; -FONTE.page_loaded = 0; +REST_PARAMS.max_page = 10; +REST_PARAMS.per_page = 'per_page=' + REST_PARAMS.max_page; function aggiornaPages() { var jsonurl = weburl + "wp-json/wp/v2/pages/"; @@ -38,43 +38,6 @@ function aggiornaSingleCategory(category) { 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"); }; @@ -99,9 +62,21 @@ function update() { }); } -function partialUpdate() { +var getObjKey = function(obj, val){ + var key = null; + for(var k in obj){ + if(obj[k] == val){ + key = k; + break; + } + } + + return key; +} + +function partialUpdate(categories) { var storage = window.localStorage; - var jsonurl = weburl + "wp-json/wp/v2/posts?" + REST_PARAMS.per_page + "&categories=" + FONTE.categoriesInfo.join(','); + var jsonurl = weburl + "wp-json/wp/v2/posts?" + REST_PARAMS.per_page + "&categories=" + categories.join(','); FONTE.updatedCategoriesCount = 0; FONTE.updatedPostsCount = 0; @@ -109,9 +84,6 @@ function partialUpdate() { FONTE.ajaxError = false; - //feedback - $("#aggiorna").attr("src", "./image/icona_aggiorna.gif"); - var lastUpdate = storage.getItem('db_update_date'); if (lastUpdate) jsonurl += "&after=" + lastUpdate; @@ -121,7 +93,7 @@ function partialUpdate() { if (firstTime == null) storage.setItem('db_lastnews_firsttime', 'yes'); if (firstTime == 'yes') storage.setItem('db_lastnews_firsttime', 'no'); - mygetSinglePageRecur(jsonurl, 1, FONTE.categoriesInfo); + getLastSinglePageRecur(jsonurl, 1, categories); }; var updateLocalStorageDate = function(){ @@ -136,27 +108,106 @@ var updateLocalStorageDate = function(){ window.localStorage.setItem('db_lastnews_date', lastUpdate); } -function mygetSinglePageRecur(url, pagenum, categories) { +var getPrevNews = function(div, categories, page) { + var storage = window.localStorage; + var type = getObjKey(FONTE.categories, categories); + var jsonurl = weburl + "wp-json/wp/v2/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]); +} + +function loadMore(type) { + var categories = FONTE.categories[type]; + + renderPrevNews(FONTE.contentListId, categories, FONTE.page_loaded[type], function(){ + getPrevNews(FONTE.contentListId, categories, FONTE.page_loaded[type]) + }); +} + +function getPrevSinglePageRecur(url, pagenum, categories) { + var type = getObjKey(FONTE.categories, categories); + console.log('url:'+url); $.ajax({ dataType: "json" , url: url + "&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.neededPostsCount == 0) { + toastr.info('Non ci sono altri alticoli da visualizzare in questa sezione'); + updateLocalStorageDate(); + } + + var n_error = 0 + var oldest_date = window.localStorage.getItem('db_oldest_' + type + '_date'); + $.each(data, function (key, val) { + var id = val.id; + var time = val.modified; + 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) { + updateLocalStorageDate(); + 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 ) { + updateLocalStorageDate(); + 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); + }); + }); + } + , error: JSONErrorHandler + }); +}; + +function getLastSinglePageRecur(url, pagenum, categories) { + var type = getObjKey(FONTE.categories, categories); + + console.log('url:'+url); + $.ajax({ + dataType: "json" + , url: url + "&page=" + pagenum + , success: function (data, textStatus, jqXHR) { + var totalPages = jqXHR.getResponseHeader('X-WP-TotalPages'); + FONTE.neededPostsCount += data.length; if (FONTE.neededPostsCount == 0) { toastr.info('Nessun nuovo articolo.'); updateLocalStorageDate(); - $("#aggiorna").attr("src", "./image/aggiorna.png"); - if(FONTE.info_page_loaded == 0) { - checkloader(categories); + if(FONTE.page_loaded[type] == 0) { + renderLastNews(FONTE.contentListId, categories); } } 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; var time = val.modified; @@ -168,18 +219,24 @@ function mygetSinglePageRecur(url, pagenum, categories) { 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) { updateLocalStorageDate(); - toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.'); - checkloader(categories); + if(! oldest_date) + window.localStorage.setItem('db_oldest_' + type + '_date', older_date); + + toastr.info('Ci sono ' + FONTE.updatedPostsCount + ' nuovi articoli.'); + renderLastNews(FONTE.contentListId, 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 ) { updateLocalStorageDate(); - toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.'); - checkloader(categories); + if(! oldest_date) + window.localStorage.setItem('db_oldest_' + type + '_date', older_date); + + toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' nuovi articoli.'); + renderLastNews(FONTE.contentListId, categories); } }); @@ -191,40 +248,7 @@ function mygetSinglePageRecur(url, pagenum, categories) { insertOrUpdateCatToPost(id, val); }); }); - // if (pagenum < totalPages) { - // pagenum += 1; - // getSinglePageRecur(url, category, pagenum) - // } } , error: JSONErrorHandler }); -}; - -// function partialUpdate() { -// //feedback -// $("#aggiorna").attr("src", "./image/icona_aggiorna.gif"); -// var storage = window.localStorage; -// var jsonurl = weburl + "wp-json/wp/v2/posts?" + REST_PARAMS.per_page + "&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(); -// -// }; +}; \ No newline at end of file