ComunicWeb/assets/js/components/like/button.js

102 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2017-12-29 07:58:22 +00:00
/**
* Like button
*
* @author Pierre HUBERT
*/
ComunicWeb.components.like.button = {
/**
* Display like button
*
* @param {String} kind The kind of like component
* @param {int} id The ID of the component
* @param {int} count The current number of likes
* @param {Boolean} liking Specify wether user is liking the content or not
* @param {HTMLElement} target The target for the button
*/
display: function(kind, id, count, liking, target){
//Empty target
emptyElem(target);
//Display like button
var likeRoot = createElem2({
type: "div",
appendTo: target,
});
var likeLink = createElem2({
appendTo: likeRoot,
type: "a",
class: "link-black text-sm"
});
createElem2({
appendTo: likeLink,
type: "i",
class: "fa fa-thumbs-o-up margin-r-5"
});
var likeMsg = createElem2({
appendTo: likeLink,
type: "span",
});
//Check if user can like or not the component
if(!signed_in()){
//Remove link effect
likeLink.style.cursor = "default";
if(count == 0){
//Hide like component
likeRoot.style.visibility = "hidden";
}
else {
//Update message
if(count == 1){
2018-08-04 07:35:28 +00:00
likeMsg.innerHTML = lang("like_btn_one_like");
2017-12-29 07:58:22 +00:00
}
else
2018-08-04 07:35:28 +00:00
likeMsg.innerHTML = lang("like_btn_x_likes", [count]);
2017-12-29 07:58:22 +00:00
}
}
else {
//Update the message
if(liking == true){
2018-08-04 07:35:28 +00:00
likeMsg.innerHTML = lang("like_btn_liking")
2017-12-29 07:58:22 +00:00
}
else {
2018-08-04 07:35:28 +00:00
likeMsg.innerHTML = lang("like_btn_like");
2017-12-29 07:58:22 +00:00
}
//Add total count if possible
if(count > 0)
likeMsg.innerHTML += " (" + count + ")";
//Set onclick behaviour
likeLink.onclick = function(){
//Get the new status
var newliking = liking == true ? false : true;
count = newliking ? count + 1 : count - 1;
//Update liking status on the API
2017-12-29 08:58:40 +00:00
ComunicWeb.components.like.interface.update(kind, id, newliking);
2017-12-29 07:58:22 +00:00
//Display liking element again
ComunicWeb.components.like.button.display(kind, id, count, newliking, target);
}
}
},
};