2017-12-29 07:58:22 +00:00
|
|
|
/**
|
|
|
|
* Posts function
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.pages.userPage.posts = {
|
|
|
|
|
2018-02-03 16:58:00 +00:00
|
|
|
/**
|
|
|
|
* Last displayed post ID
|
|
|
|
*/
|
|
|
|
_last_post_id: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify if post loading is locked
|
|
|
|
*/
|
|
|
|
_load_post_locked: false,
|
|
|
|
|
2017-12-29 07:58:22 +00:00
|
|
|
/**
|
|
|
|
* Display the posts
|
|
|
|
*
|
|
|
|
* @param {Object} userInfos Informations about the user
|
|
|
|
* @param {Object} params Additionnal parametres passed with the request
|
|
|
|
* @param {HTMLElement} target The target where the posts will be applied
|
|
|
|
*/
|
|
|
|
display: function(userInfos, params, target){
|
|
|
|
|
2018-02-03 16:58:00 +00:00
|
|
|
//Reset last post id
|
|
|
|
this._last_post_id = 0;
|
|
|
|
this._load_post_locked = true;
|
|
|
|
|
2017-12-29 07:58:22 +00:00
|
|
|
//Create posts blocks
|
|
|
|
var postsBlock = createElem2({
|
|
|
|
appendTo: target,
|
|
|
|
type: "div",
|
|
|
|
class: "box box-primary"
|
|
|
|
});
|
|
|
|
|
2017-12-31 17:51:46 +00:00
|
|
|
var postsBody = createElem2({
|
|
|
|
appendTo: postsBlock,
|
|
|
|
type: "div",
|
2018-01-03 08:28:40 +00:00
|
|
|
class: "box-body",
|
2017-12-31 17:51:46 +00:00
|
|
|
});
|
|
|
|
|
2018-01-03 08:28:40 +00:00
|
|
|
//Display loading message
|
|
|
|
var loadingMsg = createElem2({
|
|
|
|
appendTo: postsBody,
|
|
|
|
type: "p",
|
|
|
|
innerHTML: "Loading posts..."
|
|
|
|
});
|
|
|
|
loadingMsg.style.textAlign = "center";
|
|
|
|
|
2018-02-03 16:58:00 +00:00
|
|
|
//Trigger an event the the user reach the end of the page
|
|
|
|
$(window).scroll(function(){
|
|
|
|
|
|
|
|
//Cancel event if it came by error
|
|
|
|
if(!postsBlock.isConnected)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Cancel event if the page is locked
|
|
|
|
if(ComunicWeb.pages.userPage.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.userPage.posts._load_post_locked = true;
|
|
|
|
|
|
|
|
//Load next posts
|
|
|
|
ComunicWeb.pages.userPage.posts._load_posts(userInfos, postsBody);
|
|
|
|
});
|
|
|
|
|
|
|
|
//Load the posts
|
|
|
|
this._load_posts(userInfos, postsBody, function(){
|
|
|
|
loadingMsg.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the posts for the user
|
|
|
|
*
|
|
|
|
* @param {Object} userInfos Informations about the user to load
|
|
|
|
* @param {HTMLElement} target The target for the posts
|
|
|
|
* @param {function} callback What to do once the posts have been loaded
|
|
|
|
*/
|
2018-04-29 19:21:01 +00:00
|
|
|
_load_posts: function(userInfos, target, callback){
|
2018-02-03 16:58:00 +00:00
|
|
|
|
2017-12-31 17:51:46 +00:00
|
|
|
//Get the posts from the API
|
2018-02-03 16:58:00 +00:00
|
|
|
ComunicWeb.components.posts.interface.get_user(userInfos.userID, this._last_post_id, function(result){
|
2017-12-31 17:51:46 +00:00
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
//Display notification
|
|
|
|
ComunicWeb.common.notificationSystem.showNotification("Couldn't get user posts!", "danger", 4, "Error");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
//Show the posts
|
2018-02-03 16:58:00 +00:00
|
|
|
ComunicWeb.pages.userPage.posts._show(result, target);
|
2017-12-31 17:51:46 +00:00
|
|
|
|
|
|
|
}
|
2018-02-03 16:58:00 +00:00
|
|
|
|
2018-05-26 14:21:18 +00:00
|
|
|
//Unlock posts loading, if required
|
|
|
|
if(result.length != 0)
|
|
|
|
ComunicWeb.pages.userPage.posts._load_post_locked = false;
|
2018-05-26 14:16:32 +00:00
|
|
|
|
2018-02-03 16:58:00 +00:00
|
|
|
//Call callback (if any)
|
|
|
|
if(callback)
|
|
|
|
callback();
|
|
|
|
|
2017-12-31 17:51:46 +00:00
|
|
|
});
|
2018-02-03 16:58:00 +00:00
|
|
|
|
2017-12-31 17:51:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show user posts
|
|
|
|
*
|
|
|
|
* @param {Object} posts The list of posts to display
|
2018-01-03 08:28:40 +00:00
|
|
|
* @param {HTMLElement} target The rendering target
|
2017-12-31 17:51:46 +00:00
|
|
|
*/
|
|
|
|
_show: function(posts, target){
|
2018-01-03 08:28:40 +00:00
|
|
|
|
2017-12-31 17:51:46 +00:00
|
|
|
//Process each post
|
|
|
|
var i;
|
|
|
|
for(i in posts){
|
|
|
|
|
|
|
|
//Display post
|
|
|
|
ComunicWeb.components.posts.ui.display_post(posts[i], target);
|
|
|
|
|
2018-02-03 16:58:00 +00:00
|
|
|
//Update last post ID (if small than previous one or if it has not been initialized yet)
|
|
|
|
this._last_post_id = posts[i].ID;
|
|
|
|
|
2017-12-31 17:51:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 08:28:40 +00:00
|
|
|
//Check if there is not any posts
|
2018-02-03 17:00:32 +00:00
|
|
|
if(this._last_post_id == 0){
|
2018-01-03 08:28:40 +00:00
|
|
|
this._no_posts_msg(target);
|
|
|
|
}
|
2017-12-31 17:51:46 +00:00
|
|
|
},
|
2017-12-29 07:58:22 +00:00
|
|
|
|
2018-01-03 08:28:40 +00:00
|
|
|
/**
|
|
|
|
* Display a message to inform user that there is not any post
|
|
|
|
* on the page he is visiting
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} target The target for the message
|
|
|
|
*/
|
2018-02-03 17:04:47 +00:00
|
|
|
_no_posts_msg: function(target){
|
2018-01-03 08:28:40 +00:00
|
|
|
|
2018-03-25 07:43:39 +00:00
|
|
|
var msgContainer = createElem2({
|
2018-01-03 08:28:40 +00:00
|
|
|
appendTo: target,
|
|
|
|
type: "div"
|
|
|
|
});
|
|
|
|
|
|
|
|
var msgTitle = createElem2({
|
2018-03-25 07:43:39 +00:00
|
|
|
appendTo: msgContainer,
|
2018-01-03 08:28:40 +00:00
|
|
|
type: "h2",
|
|
|
|
innerHTML: "No post yet"
|
|
|
|
});
|
|
|
|
msgTitle.style.textAlign = "center";
|
|
|
|
|
|
|
|
var msgContent = createElem2({
|
2018-03-25 07:43:39 +00:00
|
|
|
appendTo: msgContainer,
|
2018-01-03 08:28:40 +00:00
|
|
|
type: "p",
|
|
|
|
innerHTML: "Nobody has posted a message on this page yet."
|
|
|
|
}).style.textAlign = "center";
|
|
|
|
}
|
|
|
|
|
2017-12-29 07:58:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
};
|