2018-02-02 05:56:52 +00:00
/ * *
* Latest posts page main script
*
* @ author Pierre HUBERT
* /
ComunicWeb . pages . latestPosts . main = {
2018-02-03 17:21:26 +00:00
/ * *
* Last loaded post id
* /
_last _post _id : 0 ,
/ * *
* Specify if post loading is locked or not
* /
_load _post _locked : false ,
2018-02-02 05:56:52 +00:00
/ * *
* Open latest posts page
*
* @ param { Object } params Parametres required to open the page
* @ param { HTMLElement } target The target for the user page
* /
open : function ( params , target ) {
2018-02-03 17:21:26 +00:00
//Reset variables
this . _last _post _id = 0 ;
this . _load _post _locked = true ;
2018-02-03 14:58:28 +00:00
//Create post list box
//Create row
var pageRow = createElem2 ( {
appendTo : target ,
type : "div" ,
class : "row latest-posts-row"
} ) ;
//Post column
var column = createElem2 ( {
appendTo : pageRow ,
type : "div" ,
2018-02-03 17:25:21 +00:00
class : "col-md-5"
2018-02-03 14:58:28 +00:00
} ) ;
//Create post box
var postBox = createElem2 ( {
appendTo : column ,
type : "div" ,
class : "box box-primary"
} ) ;
//Create box body
var boxBody = createElem2 ( {
appendTo : postBox ,
type : "div" ,
class : "box-body"
} ) ;
2018-02-03 17:21:26 +00:00
//Load the list
this . _load _list ( boxBody ) ;
//Catch scroll event
$ ( window ) . scroll ( function ( ) {
//Cancel event if it came by error
if ( ! boxBody . isConnected )
return ;
//Cancel event if the page is locked
if ( ComunicWeb . pages . latestPosts . main . _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 . latestPosts . main . _load _post _locked = true ;
//Load next posts
ComunicWeb . pages . latestPosts . main . _load _list ( boxBody ) ;
} ) ;
} ,
/ * *
* Load the list of post
*
* @ param { HTMLElement } target The target
* /
_load _list : function ( target ) {
//Perform a request on the server to get the list of latest posts
ComunicWeb . components . posts . interface . get _latest ( this . _last _post _id , function ( response ) {
//Check for errors - display a modal
if ( response . error ) {
//Display modal error
var error = ComunicWeb . common . messages . createCalloutElem ( "Error" , "Could not get the list of the latest posts ! Please try to refresh the page..." , "danger" ) ;
error . className += " latestPostsError" ;
target . appendChild ( error ) ;
}
else
//Display the list of posts
ComunicWeb . pages . latestPosts . main . _display _list ( response , target ) ;
2018-05-26 14:19:24 +00:00
//Unlock posts loading (if required)
if ( response . length != 0 )
ComunicWeb . pages . latestPosts . main . _load _post _locked = false ;
2018-02-03 17:21:26 +00:00
} ) ;
} ,
/ * *
* Display the list of latest post
*
* @ param { Object } list The list of posts to display
* @ param { HTMLElement } target The target for the posts
* /
_display _list : function ( list , target ) {
2018-02-03 14:58:28 +00:00
//Process the list of posts
2018-04-24 18:19:00 +00:00
for ( var index = 0 ; index < list . length ; index ++ ) {
2018-02-03 17:21:26 +00:00
2018-02-03 14:58:28 +00:00
//Display the post
2018-02-03 17:21:26 +00:00
ComunicWeb . components . posts . ui . display _post ( list [ index ] , target ) ;
//Save its ID
this . _last _post _id = list [ index ] . ID ;
2018-02-03 15:04:53 +00:00
}
//Check if there aren't any post to display
if ( list . length == 0 ) {
2018-02-03 15:09:16 +00:00
//Display message
2018-02-03 15:04:53 +00:00
var message = ComunicWeb . common . messages . createCalloutElem ( "No post to display" , "Posts from you and your friend will appear here..." , "info" ) ;
message . className += " noLatestPosts" ;
2018-04-11 06:54:22 +00:00
target . appendChild ( message ) ;
2018-02-03 14:58:28 +00:00
}
}
2018-02-02 05:56:52 +00:00
}