ComunicWeb/assets/js/pages/groups/pages/main.js

168 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-07-02 06:07:59 +00:00
/**
* Groups main page
*
* @author Pierre HUBERT
*/
ComunicWeb.pages.groups.pages.main = {
/**
* Open the page
*
* @param {HTMLElement} target The target for the page
*/
open: function(target){
//Create page container
var pageContainer = createElem2({
appendTo: target,
type: "div",
class: "groups-main-page"
});
//Add a button to offer to create a group
var createGroupBtn = createElem2({
appendTo: pageContainer,
type: "div",
class: "btn btn-primary btn-create-group",
innerHTML: "Create a group"
});
createGroupBtn.addEventListener("click", function(e){
openPage("groups/create");
});
2018-07-10 12:35:44 +00:00
//Add loading message
var message = ComunicWeb.common.messages.createCalloutElem(
"Loading",
"Please wait while we retrieve the list of your groups...",
"info");
pageContainer.appendChild(message);
2018-09-02 12:14:21 +00:00
/**
* This function is used if an error occurs while retrieving the list
* of groups of the user
*/
var getListError = function(){
message.remove();
pageContainer.appendChild(
ComunicWeb.common.messages.createCalloutElem(
"Error",
"An error occurred while retrieving the list of groups of the user!",
"danger"
)
);
}
2018-07-10 12:35:44 +00:00
//Get the list of groups of the user
ComunicWeb.components.groups.interface.getListUser(function(list){
//Check for errors
if(list.error)
2018-09-02 12:14:21 +00:00
return getListError();
2018-07-10 12:35:44 +00:00
2018-09-02 12:14:21 +00:00
//Get information about the groups
getInfoMultipleGroups(list, function(info){
if(info.error)
return getListError();
message.remove();
//Display the list of the groups of the user
ComunicWeb.pages.groups.pages.main._display_list(pageContainer, info);
}, true);
2018-07-10 12:35:44 +00:00
});
2018-07-02 06:07:59 +00:00
},
2018-07-10 12:35:44 +00:00
/**
* Display the list of groups of the user
*
* @param {HTMLElement} target The target for the lsit
* @param {Object} list The list to apply
*/
_display_list: function(target, list){
2018-09-02 12:14:21 +00:00
for (const i in list) {
if (list.hasOwnProperty(i)) {
const group = list[i];
ComunicWeb.pages.groups.pages.main._display_group(group, target);
}
}
2018-07-10 12:35:44 +00:00
2018-09-02 12:14:21 +00:00
},
2018-07-10 12:35:44 +00:00
2018-09-02 12:14:21 +00:00
/**
* Display single group entry
*
* @param {Object} group Information about the group
* @param {HTMLElement} target The target to display
* information about the group
*/
_display_group: function(group, target){
2018-07-10 12:35:44 +00:00
2018-09-02 12:14:21 +00:00
//Create group item
var groupItem = createElem2({
appendTo: target,
type: "div",
class: "group-item"
});
2018-07-10 12:35:44 +00:00
2018-09-02 12:14:21 +00:00
//Display group information
createElem2({
appendTo: groupItem,
type: "img",
class: "group-icon",
src: group.icon_url
});
var groupName = createElem2({
appendTo: groupItem,
type: "div",
class: "group-name a",
innerHTML: group.name
});
2018-07-10 12:35:44 +00:00
2018-09-02 12:14:21 +00:00
groupName.addEventListener("click", function(e){
openGroupPage(group);
});
2018-09-02 12:14:21 +00:00
//Offer the user to delete its membership
var deleteButton = createElem2({
appendTo: groupItem,
type: "div",
class: "buttons-area a",
innerHTML: "<i class='fa fa-trash'></i>"
});
2018-09-02 12:14:21 +00:00
deleteButton.addEventListener("click", function(e){
2018-09-02 12:14:21 +00:00
//Ask user confirmation
ComunicWeb.common.messages.confirm("Do you really want to delete your membership of this group ?", function(r){
if(!r) return;
2018-09-02 12:14:21 +00:00
groupItem.style.visibility = "hidden";
2018-09-02 12:14:21 +00:00
ComunicWeb.components.groups.interface.removeMembership(group.id, function(result){
2018-09-02 12:14:21 +00:00
groupItem.style.visibility = "visible";
2018-09-02 12:14:21 +00:00
if(result.error)
return notify("Could not delete your membership to this group!", "error");
groupItem.remove();
2018-09-02 12:14:21 +00:00
});
});
2018-07-10 12:35:44 +00:00
});
2018-09-02 12:14:21 +00:00
//Display membership status
ComunicWeb.pages.groups.sections.membershipBlock.display(group, groupItem);
2018-07-10 12:35:44 +00:00
}
2018-07-02 06:07:59 +00:00
}