From acb4e1b38493473fb329a2b961b9823c34e54f32 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 17 Jul 2018 09:40:13 +0200 Subject: [PATCH] Can scroll posts list to get older posts. --- assets/js/pages/groups/sections/posts.js | 37 +++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/assets/js/pages/groups/sections/posts.js b/assets/js/pages/groups/sections/posts.js index 767f3118..c985246b 100644 --- a/assets/js/pages/groups/sections/posts.js +++ b/assets/js/pages/groups/sections/posts.js @@ -16,6 +16,11 @@ ComunicWeb.pages.groups.sections.posts = { */ _loading_msg: null, + /** + * Load post lock + */ + _load_post_locked: false, + /** * Display the section * @@ -26,6 +31,7 @@ ComunicWeb.pages.groups.sections.posts = { //Reset posts counter this._oldest_post_id = 0; + this._load_post_locked = true; //Create posts target var postsBody = createElem2({ @@ -45,7 +51,31 @@ ComunicWeb.pages.groups.sections.posts = { "Loading", "Please wait while we load this group posts...", "info"); postsBody.appendChild(this._loading_msg); + //Refresh the list of posts this._refresh_list(info, postsBody); + + //Detect user scroll + $(window).scroll(function(){ + + //Cancel event if it came by error + if(!postsBody.isConnected) + return; + + //Cancel event if the page is locked + if(ComunicWeb.pages.groups.sections.posts._load_post_locked !== false) + return; + + //Check if we reached the bottom of the page + if($(window).scrollTop() + $(window).height() < $(document).height() - 50){ + return; + } + + //Lock the loading state + ComunicWeb.pages.groups.sections.posts._load_post_locked = true; + + //Load next posts + ComunicWeb.pages.groups.sections.posts._refresh_list(info, postsBody); + }); }, /** @@ -57,7 +87,8 @@ ComunicWeb.pages.groups.sections.posts = { _refresh_list: function(info, target){ //Get the posts of the group - ComunicWeb.components.posts.interface.get_group(info.id, 0, function(result){ + ComunicWeb.components.posts.interface.get_group(info.id, + ComunicWeb.pages.groups.sections.posts._oldest_post_id, function(result){ ComunicWeb.pages.groups.sections.posts._loading_msg.remove(); @@ -105,6 +136,10 @@ ComunicWeb.pages.groups.sections.posts = { if(this._oldest_post_id == 0 || this._oldest_post_id > oldest_id) this._oldest_post_id = oldest_id; + + //Unlock posts loading (only if we received more than one post) + if(list.length > 0) + this._load_post_locked = false; }, }; \ No newline at end of file