2017-12-31 17:51:46 +00:00
/ * *
* Posts UI
*
* @ author Pierre HUBERT
* /
ComunicWeb . components . posts . ui = {
/ * *
* Show a single post
*
* @ param { Object } infos Informations about the post
* @ param { HTMLElement } target The target for the post
* /
2020-04-03 17:38:21 +00:00
display _post : async function ( info , target ) {
2020-04-14 16:03:49 +00:00
// Safari strange bug
if ( target === undefined )
target = arguments [ 1 ]
2017-12-31 17:51:46 +00:00
2018-01-16 17:51:35 +00:00
//Check if it is required to create a post root element or not
if ( target . className . includes ( "post" ) )
postRoot = target ;
else
//Create post root element
var postRoot = createElem2 ( {
appendTo : target ,
type : "div" ,
class : "post"
} ) ;
2017-12-31 17:51:46 +00:00
//Display user block
var userBlock = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
class : "user-block"
} ) ;
//Display user account image
var userAccountImage = createElem2 ( {
appendTo : userBlock ,
type : "img" ,
class : "img-circle img-bordered-sm" ,
src : ComunicWeb . _ _config . assetsURL + "img/defaultAvatar.png"
} ) ;
//Add user name
var userNameBlock = createElem2 ( {
appendTo : userBlock ,
type : "span" ,
class : "username" ,
} ) ;
var userName = createElem2 ( {
appendTo : userNameBlock ,
type : "a" ,
innerHTML : "Loading"
} ) ;
2018-07-20 12:22:27 +00:00
//Second user area
var secondUserArea = createElem2 ( {
appendTo : userNameBlock ,
type : "span" ,
class : "second-user-area"
} ) ;
2017-12-31 17:51:46 +00:00
//Add post description
var postDescription = createElem2 ( {
appendTo : userBlock ,
type : "span" ,
class : "description"
} ) ;
//Show the age of the post
2018-08-05 14:40:14 +00:00
postDescription . innerHTML = lang ( "dates_ago" , [ ComunicWeb . common . date . timeDiffToStr ( info . post _time ) ] ) ;
2017-12-31 17:51:46 +00:00
2018-01-04 10:29:52 +00:00
2018-07-20 12:22:27 +00:00
/ * *
* Apply post creator information
*
* @ param { Object } info Information about the user
* /
var applyUserInfo = function ( info ) {
userAccountImage . src = info . accountImage ;
userName . innerHTML = info . firstName + " " + info . lastName ;
userName . onclick = function ( ) {
openUserPage ( info ) ;
2017-12-31 17:51:46 +00:00
}
2018-07-20 12:22:27 +00:00
}
/ * *
* Add a separator between to name ( user / group ) in name header
* /
var addSeparatorForUsers = function ( ) {
add _space ( secondUserArea ) ;
createElem2 ( {
appendTo : secondUserArea ,
type : "span" ,
class : "fa fa-caret-right"
} ) ;
add _space ( secondUserArea ) ;
}
//Determine the source of the post
//User page
if ( info . user _page _id != 0 ) {
//Determine which users to get information about
var usersToFetch = Array ( ) ;
usersToFetch . push ( info . userID ) ;
if ( info . user _page _id != info . userID )
usersToFetch . push ( info . user _page _id )
2019-05-16 13:07:58 +00:00
getMultipleUsersInfo ( usersToFetch , function ( result ) {
2018-07-20 12:22:27 +00:00
if ( result . error ) {
ComunicWeb . debug . logMessage ( "Could not get some users info!" ) ;
2018-08-05 14:13:48 +00:00
userName . innerHTML = lang ( "posts_ui_error" ) ;
2018-07-20 12:22:27 +00:00
return ;
}
//Apply main user information
applyUserInfo ( result [ "user-" + info . userID ] ) ;
//Add second user (if required)
if ( info . user _page _id != info . userID ) {
//Add separator
addSeparatorForUsers ( ) ;
//Add second user information
var infoSecondUser = result [ "user-" + info . user _page _id ] ;
var secondUser = createElem2 ( {
appendTo : secondUserArea ,
type : "a" ,
innerHTML : userFullName ( infoSecondUser )
} ) ;
secondUser . addEventListener ( "click" , function ( e ) {
openUserPage ( infoSecondUser ) ;
} ) ;
}
} ) ;
}
//Group page
if ( info . group _id != 0 ) {
//Get information about the user who created the post
ComunicWeb . user . userInfos . getUserInfos ( info . userID , function ( result ) {
if ( result . firstName )
applyUserInfo ( result ) ;
} ) ;
//Get information about the related group
addSeparatorForUsers ( ) ;
getInfoGroup ( info . group _id , function ( info ) {
ComunicWeb . debug . logMessage ( "Could not get a group info!" ) ;
//Add group information
var groupLink = createElem2 ( {
appendTo : secondUserArea ,
type : "a" ,
innerHTML : info . name
} ) ;
groupLink . addEventListener ( "click" , function ( e ) {
openGroupPage ( info ) ;
} ) ;
} ) ;
}
2017-12-31 17:51:46 +00:00
2018-01-10 18:16:28 +00:00
//Create top right area
var topRightArea = createElem2 ( {
insertAsFirstChild : userBlock ,
type : "div" ,
2018-01-10 19:29:34 +00:00
class : "pull-right top-right-buttons" ,
} ) ;
2018-01-10 18:16:28 +00:00
//Load informations about visibility
var visibilityTarget = createElem2 ( {
appendTo : topRightArea ,
type : "div" ,
class : "visibility"
} ) ;
//Get informations about the current visibility level
2018-07-20 11:55:58 +00:00
var visibilityInfos = ComunicWeb . components . posts . visibilityLevels [ info . visibility _level ] ;
2018-01-10 18:16:28 +00:00
//Check user level access
2018-07-20 11:55:58 +00:00
if ( info . user _access != "full" ) {
2018-01-10 18:16:28 +00:00
//The user can't change the visibility level of the post
//Display visibility level as a simple icon
createElem2 ( {
appendTo : visibilityTarget ,
type : "i" ,
class : "read-only fa " + visibilityInfos . icon
} ) ;
}
else {
//The user can change the visibility level of the post
//Create button gropu
var visibilityButtonGroup = createElem2 ( {
appendTo : visibilityTarget ,
type : "div" ,
class : "btn-group"
} ) ;
//Visibility choose button
var visibilityChooseButton = createElem2 ( {
appendTo : visibilityButtonGroup ,
type : "button" ,
class : "btn btn-default dropdown-toggle" ,
elemType : "button" ,
} ) ;
visibilityChooseButton . setAttribute ( "data-toggle" , "dropdown" ) ;
//Set the current value of the button
visibilityChooseButton . innerHTML = "<i class='fa " + visibilityInfos . icon + "'></i>" ;
//Add dropdown menu
var visibilityDropdown = createElem2 ( {
appendTo : visibilityButtonGroup ,
type : "ul" ,
class : "dropdown-menu"
} ) ;
//Process all visibility levels
2018-07-17 07:58:04 +00:00
//For pages only
2018-07-20 11:55:58 +00:00
if ( info . user _page _id != 0 ) {
2018-07-17 07:58:04 +00:00
var privateChoice = this . _add _visibility _menu _item ( visibilityDropdown , "private" ) ;
var friendsChoice = this . _add _visibility _menu _item ( visibilityDropdown , "friends" ) ;
}
//For groups only
2018-07-20 11:55:58 +00:00
if ( info . group _id != 0 ) {
2018-07-17 07:58:04 +00:00
var membersChoice = this . _add _visibility _menu _item ( visibilityDropdown , "members" ) ;
}
//Public
2018-01-10 18:16:28 +00:00
var publicChoice = this . _add _visibility _menu _item ( visibilityDropdown , "public" ) ;
var onVisibilityLevelChoice = function ( ) {
//Get the new visibility level
var new _level = this . getAttribute ( "data-level" ) ;
//Lock button
visibilityChooseButton . disabled = true ;
//Make a request on the server to update the level
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . interface . set _visibility _level ( info . ID , new _level , function ( response ) {
2018-01-10 18:16:28 +00:00
//Unlock button
visibilityChooseButton . disabled = false ;
//Check for errors
if ( response . error ) {
2018-08-05 14:13:48 +00:00
ComunicWeb . common . notificationSystem . showNotification ( lang ( "posts_ui_err_update_visibility" ) , "danger" ) ;
2018-01-10 18:16:28 +00:00
return ;
}
//Change the level on the button
visibilityChooseButton . innerHTML = "<i class='fa " + ComunicWeb . components . posts . visibilityLevels [ new _level ] . icon + "'></i>" ;
} ) ;
}
//Set the items lives
2018-07-20 11:55:58 +00:00
if ( info . user _page _id != 0 ) {
2018-07-17 07:58:04 +00:00
privateChoice . onclick = onVisibilityLevelChoice ;
friendsChoice . onclick = onVisibilityLevelChoice ;
}
2018-07-20 11:55:58 +00:00
if ( info . group _id != 0 )
2018-07-17 07:58:04 +00:00
membersChoice . onclick = onVisibilityLevelChoice ;
2018-01-10 18:16:28 +00:00
publicChoice . onclick = onVisibilityLevelChoice ;
}
2017-12-31 17:51:46 +00:00
2018-01-14 07:09:04 +00:00
//Add a button to edit the post if the user is allowed
2018-07-20 11:55:58 +00:00
if ( info . user _access == "full" ) {
2018-01-14 07:09:04 +00:00
var editButtonDiv = createElem2 ( {
appendTo : topRightArea ,
type : "div" ,
class : "edit-post-div"
} ) ;
var editButtonLink = createElem2 ( {
appendTo : editButtonDiv ,
type : "a" ,
innerHTML : "<i class='fa fa-pencil'></i>"
} ) ;
2018-01-14 07:11:36 +00:00
//Make buttons lives
editButtonLink . onclick = function ( ) {
2018-01-14 07:56:17 +00:00
//Open post editor
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . edit . open ( info , postRoot ) ;
2018-01-14 07:11:36 +00:00
} ;
2018-01-14 07:09:04 +00:00
}
2018-01-10 19:29:34 +00:00
//Add a button to delete the post if the user is allowed
2018-07-20 11:55:58 +00:00
if ( info . user _access == "full" || info . user _access == "intermediate" ) {
2018-01-10 19:29:34 +00:00
var deleteButtonDiv = createElem2 ( {
appendTo : topRightArea ,
type : "div" ,
class : "del-post-div"
} ) ;
var deleteButtonLink = createElem2 ( {
appendTo : deleteButtonDiv ,
type : "a" ,
innerHTML : "<i class='fa fa-trash'></i>"
} ) ;
2018-01-11 05:59:30 +00:00
//Make delete button lives
deleteButtonLink . onclick = function ( ) {
//Create a confirmation dialog
2018-08-05 14:13:48 +00:00
ComunicWeb . common . messages . confirm ( lang ( "posts_ui_confirm_delete" ) , function ( accept ) {
2018-01-11 05:59:30 +00:00
//Check if the user cancelled the operation
if ( ! accept )
return ;
postRoot . style . visibility = "hidden" ;
//Delete the post
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . interface . delete ( info . ID , function ( response ) {
2018-01-11 05:59:30 +00:00
2018-01-12 05:39:58 +00:00
//Check for error
if ( response . error ) {
//Display an error
2018-08-05 14:13:48 +00:00
ComunicWeb . common . notificationSystem . showNotification ( lang ( "posts_ui_err_delete_post" ) , "danger" ) ;
2018-01-12 05:39:58 +00:00
//Make the post visible
postRoot . style . visibility = "visible" ;
return ;
}
//Delete the post
emptyElem ( postRoot ) ;
postRoot . remove ( ) ;
2018-01-11 05:59:30 +00:00
} ) ;
} ) ;
}
2018-01-10 19:29:34 +00:00
}
2017-12-31 17:51:46 +00:00
//Add post attachement (if any)
2018-07-20 11:55:58 +00:00
if ( info . kind == "text" ) {
2017-12-31 17:51:46 +00:00
//Do nothing
}
//In case of image
2018-07-20 11:55:58 +00:00
else if ( info . kind == "image" ) {
2017-12-31 17:51:46 +00:00
//Image link
var imageLink = createElem2 ( {
appendTo : postRoot ,
type : "a" ,
2018-07-20 11:55:58 +00:00
href : info . file _path _url ,
2017-12-31 17:51:46 +00:00
} ) ;
//Image element
createElem2 ( {
appendTo : imageLink ,
type : "img" ,
2018-07-20 11:55:58 +00:00
src : info . file _path _url ,
2017-12-31 17:51:46 +00:00
class : "post-image"
} ) ;
//Enable lightbox
imageLink . onclick = function ( ) {
$ ( this ) . ekkoLightbox ( {
alwaysShowClose : true ,
} ) ;
return false ;
}
}
2018-01-03 09:37:46 +00:00
//In case of video
2018-07-20 11:55:58 +00:00
else if ( info . kind == "movie" ) {
2018-01-03 09:37:46 +00:00
2018-03-25 07:43:39 +00:00
var videoContainer = createElem2 ( {
2018-01-04 16:16:27 +00:00
appendTo : postRoot ,
type : "div" ,
class : "post-video"
} ) ;
2018-01-03 09:37:46 +00:00
//Create video element
var video = createElem2 ( {
2018-03-25 07:43:39 +00:00
appendTo : videoContainer ,
2018-01-03 09:37:46 +00:00
type : "video" ,
2018-01-04 16:16:27 +00:00
class : "video-js vjs-default-skin"
2018-01-03 09:37:46 +00:00
} ) ;
video . setAttribute ( "controls" , "" ) ;
//Add source
var video _src = createElem2 ( {
appendTo : video ,
type : "source" ,
2018-07-20 11:55:58 +00:00
src : info . video _info . url
2018-01-03 09:37:46 +00:00
} ) ;
2018-07-20 11:55:58 +00:00
video _src . type = info . video _info . file _type ;
2018-01-03 09:37:46 +00:00
//Enable videoJS
2018-01-03 10:17:47 +00:00
//videojs(video);
2018-01-03 09:37:46 +00:00
}
2018-01-03 10:11:24 +00:00
//In case of YouTube video
2018-07-20 11:55:58 +00:00
else if ( info . kind == "youtube" ) {
2018-01-03 10:11:24 +00:00
2019-02-23 18:08:34 +00:00
//Create frame placeholder
var youtube _placeholder = createElem2 ( {
2018-01-03 10:11:24 +00:00
appendTo : postRoot ,
2019-02-23 18:08:34 +00:00
type : "div" ,
class : "post-youtube post-youtube-placeholder"
} ) ;
//Title
createElem2 ( {
appendTo : youtube _placeholder ,
type : "div" ,
class : "title" ,
innerHTML : "<i class='fa fa-youtube-play'></i> YouTube Movie"
} ) ;
createElem2 ( {
appendTo : youtube _placeholder ,
type : "a" ,
class : "btn btn-default" ,
innerHTML : "Open on YouTube" ,
href : "https://youtube.com/watch?v=" + info . file _path ,
} ) . target = "_blank" ;
var openHere = createElem2 ( {
appendTo : youtube _placeholder ,
type : "div" ,
class : "cursor-pointer" ,
innerHTML : "Open here"
} ) ;
openHere . addEventListener ( "click" , function ( ) {
//Create iframe
var youtube _iframe = createElem2 ( {
insertBefore : youtube _placeholder ,
type : "iframe" ,
class : "post-youtube" ,
src : "https://www.youtube-nocookie.com/embed/" + info . file _path + "?rel=0"
} ) ;
youtube _iframe . setAttribute ( "frameborder" , 0 ) ;
youtube _iframe . setAttribute ( "gesture" , "media" ) ;
youtube _iframe . setAttribute ( "allow" , "encrypted-media" ) ;
youtube _iframe . setAttribute ( "allowfullscreen" , "" ) ;
youtube _placeholder . remove ( ) ;
2018-01-03 10:11:24 +00:00
} ) ;
}
2018-01-03 09:53:33 +00:00
//In case of PDF
2018-07-20 11:55:58 +00:00
else if ( info . kind == "pdf" ) {
2018-01-03 09:53:33 +00:00
//Create PDF button
var buttonContainer = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
class : "post-pdf" ,
} ) ;
var button = createElem2 ( {
appendTo : buttonContainer ,
type : "a" ,
class : "btn btn-app" ,
2018-07-20 11:55:58 +00:00
href : info . file _path _url ,
2018-01-03 09:53:33 +00:00
} ) ;
button . target = "_blank" ;
createElem2 ( {
appendTo : button ,
type : "i" ,
class : "fa fa-file-pdf-o"
} ) ;
createElem2 ( {
appendTo : button ,
type : "span" ,
innerHTML : "PDF"
} ) ;
}
2018-01-03 10:33:50 +00:00
//In case of weblink
2018-07-20 11:55:58 +00:00
else if ( info . kind == "weblink" ) {
2018-01-03 10:33:50 +00:00
var linkContainer = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
class : "attachment-block clearfix"
} ) ;
//Link image
var link _img = createElem2 ( {
appendTo : linkContainer ,
type : "img" ,
2018-07-20 11:55:58 +00:00
src : ( info . link _image != null ? info . link _image : ComunicWeb . _ _config . assetsURL + "img/world.png" ) ,
2018-01-03 10:33:50 +00:00
class : "attachment-img" ,
} ) ;
//Link heading
var link _heading = createElem2 ( {
appendTo : linkContainer ,
type : "h4" ,
2018-01-03 10:34:18 +00:00
class : "attachment-heading" ,
2018-07-20 11:55:58 +00:00
innerHTML : ( info . link _title != null ? info . link _title : "Web page" )
2018-01-03 10:33:50 +00:00
} ) ;
2018-01-03 10:34:18 +00:00
2018-01-03 10:33:50 +00:00
//Add attachement text
var link _attachment _text = createElem2 ( {
appendTo : linkContainer ,
type : "div" ,
class : "attachment_text" ,
} ) ;
var link _a _url = createElem2 ( {
appendTo : link _attachment _text ,
type : "a" ,
2018-07-20 11:55:58 +00:00
href : info . link _url ,
innerHTML : info . link _url
2018-01-03 10:33:50 +00:00
} ) ;
link _a _url . target = "_blank" ;
//Add description (if any)
2018-07-20 11:55:58 +00:00
if ( info . link _description != null ) {
2018-01-03 10:33:50 +00:00
var link _description = createElem2 ( {
appendTo : link _attachment _text ,
type : "p" ,
2018-07-20 11:55:58 +00:00
innerHTML : info . link _description
2018-01-03 10:33:50 +00:00
} ) ;
}
}
2018-01-03 11:28:46 +00:00
//In case of countdown timer
2018-07-20 11:55:58 +00:00
else if ( info . kind == "countdown" ) {
2018-01-03 11:28:46 +00:00
//Create countdown target
var target = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
class : "post-countdown"
} ) ;
2020-05-04 18:43:49 +00:00
// Manon's birthday is a sacred day, like the end of the world
// nobody must know when it will happen !
//
// Psst: it is on May 25, 11h10m00s
//
// => Easter egg
//
const endDate = new Date ( info . time _end * 1000 ) ;
if ( endDate . getMonth ( ) == 4 && endDate . getDate ( ) == 25 && endDate . getHours ( ) == 11 && endDate . getMinutes ( ) == 10 ) {
target . appendChild ( ComunicWeb . common . messages . createCalloutElem ( "Hold up!" , "You MAY NOT KNOW when this countdown will end!" , "danger" ) ) ;
createElem2 ( {
appendTo : target ,
type : "p" ,
innerHTML : "<strong>Ends in ∞ days ∞ hours ∞ minutes ∞ seconds</strong>"
} )
createElem2 ( {
appendTo : target ,
type : "p" ,
innerHTML : "This is not a common behavior of the Countdown timer. If you want this message to disappear, just create a new post with another end date..."
} )
add _p ( target ) ;
add _p ( target ) ;
}
2018-01-03 11:28:46 +00:00
2020-05-04 18:43:49 +00:00
else
//Initialize countdown timer
ComunicWeb . components . countdown . init ( info . time _end , target ) ;
2018-01-03 11:28:46 +00:00
}
2018-01-04 10:22:41 +00:00
//In case of survey
2018-07-20 11:55:58 +00:00
else if ( info . kind == "survey" ) {
2018-01-04 10:22:41 +00:00
//Add survey question
var surveyQuestion = createElem2 ( {
appendTo : postRoot ,
type : "h4" ,
2018-07-20 11:55:58 +00:00
innerHTML : info . data _survey . question ,
2018-01-04 10:22:41 +00:00
class : "post-survey-question"
} ) ;
2018-03-25 07:43:39 +00:00
//Answer container
2018-01-16 18:08:45 +00:00
var surveyResponse = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
} ) ;
2018-01-04 10:22:41 +00:00
//Create row
var row = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
2018-03-25 07:43:39 +00:00
class : "row post-survey-chart-container"
2018-01-04 10:22:41 +00:00
} ) ;
//Create canvas column
var leftColumn = createElem2 ( {
appendTo : row ,
type : "div" ,
class : "col-md-8"
} ) ;
2018-03-25 07:43:39 +00:00
//Chart container
var chartContainer = createElem2 ( {
2018-01-04 10:22:41 +00:00
appendTo : leftColumn ,
type : "div" ,
class : "chart-responsive"
} ) ;
//Create canvas
var canvas = createElem2 ( {
2018-03-25 07:43:39 +00:00
appendTo : chartContainer ,
2018-01-04 10:22:41 +00:00
type : "canvas" ,
} ) ;
canvas . style . height = "150px" ;
//Create data column
var rightColumn = createElem2 ( {
appendTo : row ,
type : "div" ,
class : "col-md-4"
} ) ;
//Initialize legend
var charLegend = createElem2 ( {
appendTo : rightColumn ,
type : "ul" ,
class : "chart-legend clearfix"
} ) ;
//Define chart options
var pieOptions = {
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke : true ,
//String - The colour of each segment stroke
segmentStrokeColor : "#fff" ,
//Number - The width of each segment stroke
segmentStrokeWidth : 1 ,
//Number - The percentage of the chart that we cut out of the middle
percentageInnerCutout : 50 , // This is 0 for Pie charts
//Number - Amount of animation steps
animationSteps : 100 ,
//String - Animation easing effect
animationEasing : "easeOutBounce" ,
//Boolean - Whether we animate the rotation of the Doughnut
animateRotate : true ,
//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale : false ,
//Boolean - whether to make the chart responsive to window resizing
responsive : true ,
// Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
maintainAspectRatio : false ,
//String - A legend template
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>" ,
//String - A tooltip template
tooltipTemplate : "<%=value %> <%=label%>"
} ;
//Generate survey data
var colors = [
{ fg : "#f56954" , bg : "#f56954" } ,
{ fg : "#00a65a" , bg : "#00a65a" } ,
{ fg : "#f39c12" , bg : "#f39c12" } ,
{ fg : "#00c0ef" , bg : "#00c0ef" } ,
{ fg : "#3c8dbc" , bg : "#3c8dbc" } ,
{ fg : "#d2d6de" , bg : "#d2d6de" }
] ;
var surveyData = [ ] ;
2018-07-20 11:55:58 +00:00
var survey _choices = info . data _survey . choices ;
2018-01-04 10:22:41 +00:00
var color _id = 0 ;
var i ;
for ( i in survey _choices ) {
//Get the color
if ( ! colors [ color _id ] )
color _id = 0 ;
var curr _color = colors [ color _id ] ;
//Generate choice informations
var choiceInfos = {
value : survey _choices [ i ] . responses ,
label : survey _choices [ i ] . name ,
color : curr _color . fg ,
highlight : curr _color . bg ,
}
//Add the choice to the list
surveyData . push ( choiceInfos ) ;
//Increment color
color _id ++ ;
}
//Initialie chart
var pieChart = new Chart ( canvas . getContext ( "2d" ) ) ;
pieChart . Doughnut ( surveyData , pieOptions ) ;
//Fill legend
var i ;
for ( i in surveyData ) {
//Legend list elem
var lengendLi = createElem2 ( {
appendTo : charLegend ,
type : "li"
} ) ;
createElem2 ( {
appendTo : lengendLi ,
type : "i" ,
class : "fa fa-circle-o"
} ) . style . color = surveyData [ i ] . color ;
createElem2 ( {
appendTo : lengendLi ,
type : "span" ,
2018-08-26 13:10:03 +00:00
innerHTML : " " + ( surveyData [ i ] . value > 0 ? "(" + surveyData [ i ] . value + ") " : "" ) + surveyData [ i ] . label
2018-01-04 10:22:41 +00:00
} ) ;
}
2018-01-16 18:08:45 +00:00
//Display survey response options if the user is signed in
if ( signed _in ( ) ) {
//Check if the user gave a response to the survey
2018-07-20 11:55:58 +00:00
if ( info . data _survey . user _choice != 0 ) {
2018-01-16 18:08:45 +00:00
//Create a text to display user choice
var choosedResponseElem = createElem2 ( {
appendTo : surveyResponse ,
class : "survey-given-response" ,
type : "p" ,
2018-08-05 14:13:48 +00:00
innerHTML : lang ( "posts_ui_survey_your_response" , [ info . data _survey . choices [ info . data _survey . user _choice ] . name ] )
2018-01-16 18:08:45 +00:00
} ) ;
//Offer the user to cancel his choice
var cancelReponseLink = createElem2 ( {
appendTo : choosedResponseElem ,
type : "a" ,
2018-08-15 05:44:34 +00:00
innerHTML : lang ( "posts_ui_cancel_response_survey" )
2018-01-16 18:08:45 +00:00
} ) ;
//Make cancel button lives
cancelReponseLink . onclick = function ( ) {
2018-08-05 14:13:48 +00:00
ComunicWeb . common . messages . confirm ( lang ( "posts_ui_confirm_cancel_survey_response" ) , function ( confirm ) {
2018-01-16 18:08:45 +00:00
//Check if the user cancelled
if ( ! confirm )
return ;
//Make a request on the server
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . interface . cancel _survey _response ( info . ID , function ( response ) {
2018-01-16 18:08:45 +00:00
//Check for errors
if ( response . error ) {
2018-08-05 14:13:48 +00:00
ComunicWeb . common . notificationSystem . showNotification ( lang ( "posts_ui_err_cancel_response_survey" ) , "danger" ) ;
2018-01-16 18:08:45 +00:00
return ;
}
//Reload post
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . actions . reload _post ( info . ID , postRoot ) ;
2018-01-16 18:08:45 +00:00
} ) ;
} ) ;
}
}
2018-01-17 05:48:03 +00:00
else {
//Offer the user the possibility to respond the survey
var surveyResponseForm = createElem2 ( {
appendTo : surveyResponse ,
type : "div" ,
class : "input-group"
} ) ;
//Create response chooser
var surveyResponseChooser = createElem2 ( {
appendTo : surveyResponseForm ,
type : "select" ,
class : "form-control"
} ) ;
//Display options
for ( j in survey _choices ) {
//Create an element for the choice
option = createElem2 ( {
appendTo : surveyResponseChooser ,
type : "option" ,
innerHTML : survey _choices [ j ] . name ,
value : survey _choices [ j ] . choiceID ,
} ) ;
}
//Add confirm button
var chooseButtonSpan = createElem2 ( {
appendTo : surveyResponseForm ,
type : "span" ,
class : "input-group-btn"
} ) ;
var chooseButton = createElem2 ( {
appendTo : chooseButtonSpan ,
type : "button" ,
class : "btn btn-default" ,
2018-08-05 14:13:48 +00:00
innerHTML : lang ( "posts_ui_send_survey_response" )
2018-01-17 05:48:03 +00:00
} ) ;
//Make confirm button lives
chooseButton . onclick = function ( ) {
//Get selected answer ID
2018-01-17 05:55:26 +00:00
var choice _id = surveyResponseChooser . value ;
2018-01-17 05:48:03 +00:00
//Lock send button
chooseButton . disabled = true ;
//Perform a request on the server
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . interface . survey _send _response ( info . ID , choice _id , function ( response ) {
2018-01-17 05:48:03 +00:00
//Unlock button
chooseButton . disabled = false ;
//Check for errors
if ( response . error ) {
2018-03-03 13:42:06 +00:00
ComunicWeb . common . notificationSystem . showNotification ( "Could not send response to survey !" , "danger" ) ;
2018-01-17 05:48:03 +00:00
return ;
}
//Reload post
2018-07-20 11:55:58 +00:00
ComunicWeb . components . posts . actions . reload _post ( info . ID , postRoot ) ;
2018-01-17 05:48:03 +00:00
} ) ;
}
}
2018-01-16 18:08:45 +00:00
}
2018-01-04 10:22:41 +00:00
}
2018-01-03 10:33:50 +00:00
//If the kind of post was not implemented
2017-12-31 17:51:46 +00:00
else {
//Log error
2018-07-20 11:55:58 +00:00
ComunicWeb . debug . logMessage ( "Not implemented kind of post: " + info . kind ) ;
ComunicWeb . common . error . submitError ( "notice" , "Unimplemented kind of post: " + info . kind , 0 , { } ) ;
2017-12-31 17:51:46 +00:00
}
//Add post content
var postContent = createElem2 ( {
appendTo : postRoot ,
type : "div" ,
class : "post_content" ,
2018-12-27 13:02:01 +00:00
innerHTML : lineBreakToPTags ( BBCodeParser . process ( removeHtmlTags ( info . content ) ) )
2017-12-31 17:51:46 +00:00
} ) ;
2018-03-03 13:52:52 +00:00
//Parse emojies
2018-05-03 20:05:06 +00:00
ComunicWeb . components . textParser . parse ( {
2020-04-03 17:38:21 +00:00
element : postContent ,
user : await userInfo ( info . userID )
2018-03-03 13:52:52 +00:00
} ) ;
2017-12-31 17:51:46 +00:00
//Add bottom elements container
var bottomArea = createElem2 ( {
appendTo : postRoot ,
type : "ul" ,
class : "list-inline post-buttons"
} ) ;
//Load likes
var likesTarget = createElem2 ( {
appendTo : bottomArea ,
type : "li" ,
} ) ;
var userLiking = null ;
if ( signed _in ( ) ) {
2018-07-20 11:55:58 +00:00
userLiking = info . userlike ;
2017-12-31 17:51:46 +00:00
}
//Call component
ComunicWeb . components . like . button . display (
"post" ,
2018-07-20 11:55:58 +00:00
info . ID ,
info . likes ,
2017-12-31 17:51:46 +00:00
userLiking ,
likesTarget
) ;
2018-01-18 05:53:00 +00:00
//Load comments (if possible)
2018-07-20 11:55:58 +00:00
if ( info . comments != null )
ComunicWeb . components . comments . ui . display ( info . comments , info . ID , postRoot ) ;
2020-04-01 16:45:29 +00:00
// Register for post updates
2020-04-11 06:50:46 +00:00
if ( UserWebSocket . IsConnected )
PostsInterface . register ( info . ID ) ;
2020-04-01 16:45:29 +00:00
// Auto-unregister when the post goes out of scope
const ev = async ( e ) => {
if ( postRoot . isConnected )
return ;
document . removeEventListener ( "openPage" , ev ) ;
PostsInterface . unregister ( info . ID ) ;
}
document . addEventListener ( "openPage" , ev ) ;
2018-01-10 18:16:28 +00:00
} ,
/ * *
* Add a visibility level choice to a dropodown menu
*
* @ param { HTMLElement } target The target menu
* @ param { Object } name The name of the visibility level
* @ return { HTMLElement } The created element container
* /
2018-01-14 07:11:36 +00:00
_add _visibility _menu _item : function ( target , name ) {
2018-01-10 18:16:28 +00:00
//Create container
var itemContainer = createElem2 ( {
appendTo : target ,
type : "li" ,
} ) ;
//Create link container
var itemLink = createElem2 ( {
appendTo : itemContainer ,
type : "a"
} ) ;
itemLink . setAttribute ( "data-level" , name ) ;
//Add visibility icon
createElem2 ( {
appendTo : itemLink ,
type : "i" ,
class : "fa " + ComunicWeb . components . posts . visibilityLevels [ name ] . icon
} ) ;
//Add visibility label
createElem2 ( {
appendTo : itemLink ,
type : "span" ,
innerHTML : ComunicWeb . components . posts . visibilityLevels [ name ] . name
} ) ;
return itemLink ;
} ,
2017-12-31 17:51:46 +00:00
}