SMMP/public_html/view/js/users.js

1145 lines
32 KiB
JavaScript
Raw Permalink Normal View History

// On referencie toutes les sections
var section = {
view: {
text: '#CONTAINER > section[data-sublink="view"] ',
element: document.querySelector('#CONTAINER > section[data-sublink="view"]'),
search: {
bar: document.querySelector('#CONTAINER > section[data-sublink="view"] > .searchbar'),
func: null,
time: null
}
},
create: {
text: '#CONTAINER > section[data-sublink="create"] ',
element: document.querySelector('#CONTAINER > section[data-sublink="create"]')
},
remove: {
text: '#CONTAINER > section[data-sublink="remove"] ',
element: document.querySelector('#CONTAINER > section[data-sublink="remove"]')
},
edit: {
text: '#CONTAINER > section[data-sublink="edit"] ',
element: document.querySelector('#CONTAINER > section[data-sublink="edit"]')
},
groups: {
text: '#CONTAINER > section[data-sublink="groups"] ',
element: document.querySelector('#CONTAINER > section[data-sublink="groups"]'),
search: {
bar: document.querySelector('#CONTAINER > section[data-sublink="groups"] > .searchbar'),
func: null,
time: null
}
}
};
/* [1] view -> AFFICHAGE DES UTILISATEURS
=========================================================*/
if( section.view.element != null ){
/* (0) On gère le zoom sur un utilisateur via URL */
if( pageManager.vars.length > 1 && !isNaN(pageManager.vars[1]) ){
document.location = '#'+pageManager.vars[1];
if( document.getElementById(pageManager.vars[1]) != null )
document.getElementById(pageManager.vars[1]).addClass('selected');
2016-07-12 15:40:40 +00:00
}
/* (1) On recupere tous les liens */
section.view.link = {
edit: document.querySelectorAll(section.view.text + '.link_edit[data-user]'),
remove: document.querySelectorAll(section.view.text + '.link_remove[data-user]')
};
/* (2) On récupère toutes les actions de groupes */
section.view.group = {
rem: document.querySelectorAll(section.view.text + '.rem-group[data-user][data-group]'),
add: document.querySelectorAll(section.view.text + '.add-group[data-user]')
};
/* (3) Gestion de la recherche instantannee */
section.view.search.func = function(){
// if no keyword -> show all
if( section.view.search.bar.value.length == 0 ){
// On recupere la liste des elements correspondants aux utilisateurs
var user_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]');
// Affiche chaque carte
for( var i = 0 ; i < user_list.length ; i++ )
user_list[i].remClass('hidden');
return;
}
var search = {
path: 'userDefault/search',
keywords: section.view.search.bar.value
};
// On envoie la requete
api.send(search, function(result){
if( result.error != 0 ) return;
console.log(result.users.length);
if( result.error == 0 ){ // si aucune erreur
// On enregistre tous les UID dans un tableau
var uid_list = [];
for( var i = 0 ; i < result.users.length ; i++ )
uid_list.push( result.users[i].id_user );
// On recupere la liste des elements correspondants aux utilisateurs
var user_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]');
// Pour chaque utilisateur
for( var i = 0 ; i < user_list.length ; i++ ){
// Si doit etre visible
if( uid_list.indexOf(parseInt(user_list[i].id)) > -1 )
user_list[i].remClass('hidden');
// Si ne doit pas etre visible
else
user_list[i].addClass('hidden');
}
}
});
}
section.view.search.bar.addEventListener('keyup', function(e){
if( section.view.search.time != null )
clearTimeout(section.view.search.time);
section.view.search.time = setTimeout(section.view.search.func, 500);
}, false);
/* (4) On gere la "redirection" vers la modification */
for( var i = 0 ; i < section.view.link.edit.length ; i++ ){
section.view.link.edit[i].addEventListener('click', function(e){
pageManager.vars = [ 'edit', e.target.getData('user') ];
pageManager.refresh();
}, false);
}
/* (5) On gere la "redirection" vers la suppression */
for( var i = 0 ; i < section.view.link.remove.length ; i++ ){
section.view.link.remove[i].addEventListener('click', function(e){
pageManager.vars = [ 'remove', e.target.getData('user') ];
pageManager.refresh();
}, false);
}
/* (6) On gere la "suppression" d'un groupe */
section.view.group.remfunc = function(e){
// {1} On rédige la requête //
var request = {
path: 'userDefault/unlink',
id_user: e.target.getData('user'),
id_cluster: e.target.getData('group')
};
// {2} On envoie la requête //
api.send(request, function(response){
// {3} Si erreur, on ne fait rien //
if( response.error != 0 )
return false;
// {4} Si tout s'est bien passé //
e.target.parentNode.parentNode.removeChild( e.target.parentNode );
});
};
for( var i = 0 ; i < section.view.group.rem.length ; i++ )
section.view.group.rem[i].addEventListener('click', section.view.group.remfunc, false);
/* (6) On gere la "modification" (ajout) des groupes */
for( var i = 0 ; i < section.view.group.add.length ; i++ ){
section.view.group.add[i].addEventListener('mouseup', function(e){
/* (1) Check if already opened */
var opened = e.target.innerHTML != '+';
/* (2) Hide all dropdowns */
for( j = 0 ; j < section.view.group.add.length ; j++ )
section.view.group.add[j].innerHTML = '+';
/* (3) Try to show dropdown if not already */
if( !opened ){
/* (1) Get the group id/type */
if( !/^\d+$/.test(e.target.getData('user')) )
return;
var user_id = parseInt(e.target.getData('user'));
/* (2) Get already members ids */
var brothers = e.target.parentNode.children[1].children;
var already = []; // will contain already ids
for( j = 0 ; j < brothers.length ; j++ )
already += parseInt( brothers[j].children[0].getData('group') );
/* (3) Call list of members */
api.send({path: 'clusterDefault/getAll', class: 0}, function(response){
// Manage error
if( response.error != 0 ){
console.warn('error loading list of members');
return;
}
// remove already members
var members = response.clusters;
/* (4) Build dropdown content without already members */
dropdown = "<div class='dropdown'>";
for( j = 0 ; j < members.length ; j++ ){
if( already.indexOf(members[j].id_user_cluster) > -1 )
continue;
dropdown += "<span data-groupid='"+members[j].id_user_cluster+"'>"+members[j].name+"</span>";
}
dropdown += "</div>";
e.target.innerHTML = '+'+dropdown;
/* (5) Attach events to dropdown elements */
var dropdownElements = e.target.children[0].children;
for( j = 0 ; j < dropdownElements.length ; j++ )
dropdownElements[j].addEventListener('mousedown', function(e2){
// {1} Get member id/username //
var groupid = e2.target.getData('groupid');
var groupname = e2.target.innerHTML;
// manage error
if( groupid === false )
return;
// {2} Request : add member to group //
var request = {
path: 'userDefault/link',
id_user: user_id,
id_cluster: groupid
};
// {3} Call link //
api.send(request, function(response2){
// manage error
if( response2.error != 0 ){
console.warn('cannot link user to group');
return;
}
// Remove dropdown (in order to be reloaded with new members);
e.target.innerHTML = '+';
// {4} Update display //
var newMemberRemoveIcon = document.createElement('span');
newMemberRemoveIcon.setAttribute('class', 'rem-group');
newMemberRemoveIcon.setAttribute('data-group', groupid);
newMemberRemoveIcon.setAttribute('data-user', e.target.getData('user'));
newMemberRemoveIcon.addEventListener('click', section.view.group.remfunc, false);
var newMember = document.createElement('span');
newMember.appendChild( document.createTextNode(groupname) );
newMember.appendChild(newMemberRemoveIcon);
e.target.parentNode.children[1].appendChild(newMember);
});
}, false);
});
}
}, false);
}
2016-07-12 15:40:40 +00:00
};
/* [2] create -> Creation d'utilisateur
=========================================================*/
if( section.create.element != null ){
// On recupere tous les champs
section.create.input = {
code: document.querySelector(section.create.text + '#create_code'),
username: document.querySelector(section.create.text + '#create_username'),
firstname: document.querySelector(section.create.text + '#create_firstname'),
lastname: document.querySelector(section.create.text + '#create_lastname'),
mail: document.querySelector(section.create.text + '#create_mail'),
submit: document.querySelector(section.create.text + '#create_submit')
2017-09-03 17:26:59 +00:00
};
// On recupere tous les messages d'erreur
section.create.errmsg = {
code: document.querySelector(section.create.text + '.error-msg.create_code'),
username: document.querySelector(section.create.text + '.error-msg.create_username'),
firstname: document.querySelector(section.create.text + '.error-msg.create_firstname'),
lastname: document.querySelector(section.create.text + '.error-msg.create_lastname'),
mail: document.querySelector(section.create.text + '.error-msg.create_mail')
};
// On initialise le gestionnaire des saisies
var createChecker = new inputChecker();
/* (1) Gestion du code RFID */
createChecker.append( section.create.input.code, format_code, '01-AB-CD-23');
section.create.input.code.addEventListener('keyup', function(e){
e.target.value = e.target.value.toUpperCase();
if( !createChecker.check(e.target) )
createChecker.correct(e.target, false);
}, false);
/* (n) Gestion de l'envoi du formulaire */
section.create.input.submit.addEventListener('click', function(e){
// On annule l'envoi de base (PHP)
e.preventDefault();
// On vide les messages erreurs
2017-09-03 17:26:59 +00:00
section.create.errmsg.code.innerHTML =
section.create.errmsg.username.innerHTML =
section.create.errmsg.firstname.innerHTML =
section.create.errmsg.lastname.innerHTML =
section.create.errmsg.mail.innerHTML = '';
var request = {
path: 'userDefault/create', // On veut creer un utilisateur
code: section.create.input.code.value,
username: section.create.input.username.value,
firstname: section.create.input.firstname.value,
lastname: section.create.input.lastname.value,
mail: section.create.input.mail.value
};
api.send(request, function(answer){
if( answer.error == 0 ){ // Tout s'est bien deroule
console.log('Utilisateur cree!');
section.create.input.submit.anim('active', 1500);
// on vide les champs
section.create.input.code.value = '';
section.create.input.username.value = '';
section.create.input.firstname.value = '';
section.create.input.lastname.value = '';
section.create.input.mail.value = '';
2017-09-03 17:26:59 +00:00
}else{ // Erreur
console.error('ModuleError::'+answer.error);
// if missing or incorrect param
if( answer.error == 16 || answer.error == 17 ){
if( section.create.errmsg[answer.ErrorArguments[0]] != null )
section.create.errmsg[answer.ErrorArguments[0]].innerHTML = 'Le champ est manquant ou incorrect !';
}
2017-09-03 17:26:59 +00:00
}
});
}, false);
}
/* [3] remove -> Suppression d'utilisateur
=========================================================*/
var lastkeywords_remove = '';
var found_remove = [];
var searchindex_remove = 0;
if( section.remove.element != null ){
/* (1) Definition des champs */
section.remove.input = {
search: {
view: document.querySelector(section.remove.text + '.remove_search_view'),
num: document.querySelector(section.remove.text + '.remove_search_num'),
sum: document.querySelector(section.remove.text + '.remove_search_sum'),
keyword: document.querySelector(section.remove.text + '#remove_search_keyword'),
id: document.querySelector(section.remove.text + '#remove_search_id'),
submit: document.querySelector(section.remove.text + '#remove_search_submit')
},
code: document.querySelector(section.remove.text + '#remove_code'),
username: document.querySelector(section.remove.text + '#remove_username'),
submit: document.querySelector(section.remove.text + '#remove_submit')
};
/* (2) Gestion de l'auto-remplissage par lien
---------------------------------------------------------*/
function autofill_remove(){
/* (0) Valeurs par défaut */
var user_data = {
id_user: '',
username: '',
code: '',
disable_input: true
};
// {1} Si aucun résultat //
if( found_remove[searchindex_remove] == null ){
// Mise a jour des indices des resultats
section.remove.input.search.num.innerHTML = 0;
searchindex_remove = 0;
// {2} Si au moins 1 résultat //
}else{
// On recupere les données de la user selectionnee
user_data = {
id_user: found_remove[searchindex_remove]['id_user'],
username: found_remove[searchindex_remove]['username'],
code: found_remove[searchindex_remove]['code'],
disable_input: false
};
// Mise a jour des indices des resultats
section.remove.input.search.num.innerHTML = searchindex_remove+1;
}
// {3} On auto-remplit le formulaire //
// On enregistre l'UID de la user
section.remove.input.search.id.value = user_data.id_user;
// Auto-remplissage du formulaire
section.remove.input.username.value = user_data.username;
section.remove.input.code.value = user_data.code;
// On gere le caractere "inactif" du submit
section.remove.input.submit.disabled = user_data.disable_input;
}
/* (3) Gestion de la recherche
---------------------------------------------------------*/
section.remove.input.search.submit.addEventListener('click', function(e){
// On annule l'envoi de base (PHP)
e.preventDefault();
/* (1) Si le champ n'est pas vide */
if( section.remove.input.search.keyword.value != '' ){
/* (2) Si même mots-clés, on passe au prochain résultat */
if( section.remove.input.search.keyword.value == lastkeywords_remove ){
// on affiche le prochain (decalage cyclique)
searchindex_remove = (searchindex_remove+1) % found_remove.length;
// On met à jour l'auto-remplissage
autofill_remove();
/* (3) Nouveaux mots-clés, on lance la recherche */
}else{
/* (4) Redaction de la requete */
var getUsers = {
path: 'userDefault/search',
keywords: section.remove.input.search.keyword.value
};
/* (5) On envoie la requête */
api.send(getUsers, function(getUsersResponse){
if( getUsersResponse.error == 0 ){ // si pas d'erreur
// On notifie que la recherche est terminée
section.remove.input.search.submit.anim('active', 1000);
/* (6) On met à jour les données des users, et le compteur */
searchindex_remove = 0;
found_remove = getUsersResponse.users;
section.remove.input.search.sum.innerHTML = found_remove.length;
// On met à jour l'auto-remplissage
autofill_remove();
}
// On met à jour la dernière recherche
lastkeywords_remove = getUsers.keywords;
});
}
}
}, false);
/* (n) Gestion de l'envoi du formulaire */
section.remove.input.submit.addEventListener('click', function(e){
// On annule l'envoi de base (PHP)
e.preventDefault();
// Si l'id n'est pas defini -> abort
if( section.remove.input.search.id.value == '' )
return;
/* (1) Create popup */
var PopupManager = new Popup();
var confirm_content = {
title: "Suppression d'utilisateur",
content: "L'utilisateur <b>"+section.remove.input.username.value+"</b> sera définitivement supprimé de la plateforme. Cette opération ne peut être annulée.",
type: "invalid",
action: "Supprimer"
};
/* (2) Ask for confirmation */
PopupManager.ask(confirm_content, function(is_confirmed){
// {2.1} If cancelled -> abort //
if( !is_confirmed )
return;
/* (2) On supprime l'utilisateur */
var request = {
path: 'userDefault/delete', // On veut supprimer cet utilisateur
id_user: section.remove.input.search.id.value
};
api.send(request, function(answer){
if( answer.error == 0 && answer.status == true ){ // Tout s'est bien deroule
console.log('Utilisateur supprime!');
section.remove.input.submit.anim('active', 1500);
/* (1) On actualise la recherche
---------------------------------------------------------*/
if( section.remove.input.search.keyword.value.length == 0 ){ // Si aucun champ de recherche, on efface juste l'entrée
// On supprime toutes les valeurs
found_remove = [];
searchindex_remove = 0;
}else{
var getUsers = {
path: 'userDefault/search',
keywords: section.remove.input.search.keyword.value
};
api.send(getUsers, function(getUsersResponse){
if( getUsersResponse.error == 0 ){ // si pas d'erreur
searchindex_remove = 0;
found_remove = getUsersResponse.users;
section.remove.input.search.sum.innerHTML = found_remove.length;
}
});
}
autofill_remove();
}else // Erreur
console.error('ModuleError::'+answer.error);
});
});
}, false);
}
/* [4] edit -> Modification d'utilisateur
=========================================================*/
var lastkeywords_edit = '';
var found_edit = [];
var searchindex_edit = 0;
if( section.edit.element != null ){
// Definition des champs
section.edit.input = {
search: {
view: document.querySelector(section.edit.text + '.edit_search_view'),
num: document.querySelector(section.edit.text + '.edit_search_num'),
sum: document.querySelector(section.edit.text + '.edit_search_sum'),
keyword: document.querySelector(section.edit.text + '#edit_search_keyword'),
id: document.querySelector(section.edit.text + '#edit_search_id'),
submit: document.querySelector(section.edit.text + '#edit_search_submit')
},
code: document.querySelector(section.edit.text + '#edit_code'),
username: document.querySelector(section.edit.text + '#edit_username'),
firstname: document.querySelector(section.edit.text + '#edit_firstname'),
lastname: document.querySelector(section.edit.text + '#edit_lastname'),
mail: document.querySelector(section.edit.text + '#edit_mail'),
submit: document.querySelector(section.edit.text + '#edit_submit')
};
// On recupere tous les messages d'erreur
section.edit.errmsg = {
code: document.querySelector(section.edit.text + '.error-msg.edit_code'),
username: document.querySelector(section.edit.text + '.error-msg.edit_username'),
firstname: document.querySelector(section.edit.text + '.error-msg.edit_firstname'),
lastname: document.querySelector(section.edit.text + '.error-msg.edit_lastname'),
mail: document.querySelector(section.edit.text + '.error-msg.edit_mail')
};
/* (1) Gestion du code RFID */
createChecker.append( section.edit.input.code, format_code, '01-AB-CD-23');
section.edit.input.code.addEventListener('keyup', function(e){
e.target.value = e.target.value.toUpperCase();
if( !createChecker.check(e.target) )
createChecker.correct(e.target, false);
}, false);
/* (2) Gestion de l'auto-remplissage par lien
---------------------------------------------------------*/
function autofill_edit(){
/* (0) Valeurs par défaut */
var user_data = {
id_user: '',
code: '',
username: '',
firstname: '',
lastname: '',
mail: '',
disable_input: true
};
// {1} Si aucun résultat //
if( found_edit[searchindex_edit] == null ){
// Mise a jour des indices des resultats
section.edit.input.search.num.innerHTML = 0;
searchindex_edit = 0;
// {2} Si au moins 1 résultat //
}else{
// On recupere les données de la user selectionnee
user_data = {
id_user: found_edit[searchindex_edit]['id_user'],
code: found_edit[searchindex_edit]['code'],
username: found_edit[searchindex_edit]['username'],
firstname: found_edit[searchindex_edit]['firstname'],
lastname: found_edit[searchindex_edit]['lastname'],
mail: found_edit[searchindex_edit]['mail'],
disable_input: false
};
// Mise a jour des indices des resultats
section.edit.input.search.num.innerHTML = searchindex_edit+1;
}
// {3} On auto-remplit le formulaire //
// On enregistre l'UID de la user
section.edit.input.search.id.value = user_data.id_user;
// Auto-remplissage du formulaire
section.edit.input.code.value = user_data.code;
section.edit.input.username.value = user_data.username;
section.edit.input.firstname.value = user_data.firstname;
section.edit.input.lastname.value = user_data.lastname;
section.edit.input.mail.value = user_data.mail;
// On gere le caractere "inactif" du submit
section.edit.input.submit.disabled = user_data.disable_input;
}
/* (3) Gestion de la recherche
---------------------------------------------------------*/
section.edit.input.search.submit.addEventListener('click', function(e){
// On annule l'envoi de base (PHP)
e.preventDefault();
/* (1) Si le champ n'est pas vide */
if( section.edit.input.search.keyword.value != '' ){
/* (2) Si même mots-clés, on passe au prochain résultat */
if( section.edit.input.search.keyword.value == lastkeywords_edit ){
// on affiche le prochain (decalage cyclique)
searchindex_edit = (searchindex_edit+1) % found_edit.length;
// On met à jour l'auto-remplissage
autofill_edit();
/* (3) Nouveaux mots-clés, on lance la recherche */
}else{
/* (4) Redaction de la requete */
var getUsers = {
path: 'userDefault/search',
keywords: section.edit.input.search.keyword.value
};
/* (5) On envoie la requête */
api.send(getUsers, function(getUsersResponse){
if( getUsersResponse.error == 0 ){ // si pas d'erreur
// On notifie que la recherche est terminée
section.edit.input.search.submit.anim('active', 1000);
/* (6) On met à jour les données des users, et le compteur */
searchindex_edit = 0;
found_edit = getUsersResponse.users;
section.edit.input.search.sum.innerHTML = found_edit.length;
// On met à jour l'auto-remplissage
autofill_edit();
}
// On met à jour la dernière recherche
lastkeywords_edit = getUsers.keywords;
});
}
}
}, false);
/* (n) Gestion de l'envoi du formulaire */
section.edit.input.submit.addEventListener('click', function(e){
// On annule l'envoi de base (PHP)
e.preventDefault();
// On vide les messages erreurs
section.edit.errmsg.code.innerHTML =
section.edit.errmsg.username.innerHTML =
section.edit.errmsg.firstname.innerHTML =
section.edit.errmsg.lastname.innerHTML =
section.edit.errmsg.mail.innerHTML = '';
// Si l'id est defini
if( section.edit.input.search.id.value != '' ){
var request = {
path: 'userDefault/edit', // On veut creer un utilisateur
id_user: parseInt(section.edit.input.search.id.value),
code: section.edit.input.code.value,
username: section.edit.input.username.value,
firstname: section.edit.input.firstname.value,
lastname: section.edit.input.lastname.value,
mail: section.edit.input.mail.value
};
api.send(request, function(answer){
console.log( answer );
if( answer.error == 0 && answer.status == true ){ // Tout s'est bien deroule
console.log('Utilisateur modifie!');
section.edit.input.submit.anim('active', 1500);
/* (1) On met à jour les données utilisateur
---------------------------------------------------------*/
// On récupère la user d'uid donné dans l'URL
api.send({path:'userDefault/getById', id_user:request.id_user}, function(response){
// Si aucune erreur et un résultat
if( response.error == 0 ){
found_edit[searchindex_edit] = response.user; // On récupère le résultat
autofill_edit(); // On auto-remplit
}
});
}else{ // Erreur
console.error('ModuleError::'+answer.error);
// if missing or incorrect param
if( answer.error == 16 || answer.error == 17 ){
if( section.edit.errmsg[answer.ErrorArguments[0]] != null )
section.edit.errmsg[answer.ErrorArguments[0]].innerHTML = 'Le champ est manquant ou incorrect !';
}
}
});
}
}, false);
}
/* [5] chargement de l'utilisateur courant (depuis url)
=========================================================*/
// gestion automatique au chargement de la page si id_user dans url "/users/.+/:id_user:"
if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ){
// on récupère la user d'uid donné dans l'url
api.send({path:'userDefault/getById', id_user:pageManager.vars[1]}, function(response){
// si aucune erreur et un résultat
if( response.error == 0 ){
/* (1) On pré-remplis le formulaire de modification */
found_edit = [ response.user ]; // on récupère le résultat
searchindex_edit = 0; // on remet le compteur à 0
autofill_edit(); // on auto-remplit
/* (2) On pré-remplis le formulaire de suppression */
found_remove = [ response.user ]; // on récupère le résultat
searchindex_remove = 0; // on remet le compteur à 0
autofill_remove(); // on auto-remplit
}
});
}
/* [6] groups -> Groupes d'utilisateurs
=========================================================*/
if( section.groups.element != null ){
/* (1) On recupere tous les liens vers des sous-sections */
section.groups.link = {
remove: document.querySelectorAll(section.groups.text + '.link_remove[data-cluster]'),
edit: document.querySelectorAll(section.groups.text + '.link_edit[data-cluster]')
};
/* (2) On récupère toutes les actions de groupes */
section.groups.group = {
rem: document.querySelectorAll(section.groups.text + '.rem-member[data-cluster][data-member]'),
add: document.querySelectorAll(section.groups.text + '.add-member[data-cluster]')
};
/* (3) Gestion de la recherche instantannee */
section.groups.search.func = function(){
var search = {
path: 'clusterDefault/search',
keywords: section.groups.search.bar.value,
class: 0
};
// On envoie la requete
api.send(search, function(result){
if( result.error == 0 ){ // si aucune erreur
// On enregistre tous les UID dans un tableau
var uid_list = [];
for( var i = 0 ; i < result.clusters.length ; i++ )
uid_list.push( result.clusters[i].id_user_cluster);
// On recupere la liste des elements correspondants aux utilisateurs
var cluster_list = document.querySelectorAll(section.groups.text + '> article.inline-box[id]');
// Pour chaque cluster
for( var i = 0 ; i < cluster_list.length ; i++ ){
// Si doit etre visible
if( uid_list.indexOf( parseInt(cluster_list[i].id) ) > -1 )
cluster_list[i].remClass('hidden');
// Si ne doit pas etre visible
else
cluster_list[i].addClass('hidden');
}
}
});
};
section.groups.search.bar.addEventListener('keyup', function(e){
if( section.groups.search.time != null )
clearTimeout(section.groups.search.time);
section.groups.search.time = setTimeout(section.groups.search.func, 500);
}, false);
var i;
/* (4) On gere la "redirection" vers la modification */
for( i = 0 ; i < section.groups.link.edit.length ; i++ ){
section.groups.link.edit[i].addEventListener('click', function(e){
pageManager.vars = [ 'edit', 'u'+e.target.getData('cluster') ];
navMenu('groups', true);
}, false);
}
/* (5) On gere la "redirection" vers la suppression */
for( i = 0 ; i < section.groups.link.remove.length ; i++ ){
section.groups.link.remove[i].addEventListener('click', function(e){
pageManager.vars = [ 'remove', 'u'+e.target.getData('cluster') ];
navMenu('groups', true);
}, false);
}
/* (6) On gere la "suppression" d'un groupe */
section.groups.group.remfunc = function(e){
// {1} On rédige la requête //
var request = {
path: 'userDefault/unlink',
id_cluster: e.target.getData('cluster'),
id_user: e.target.getData('member')
};
// {2} On envoie la requête //
api.send(request, function(response){
// {3} Si erreur, on ne fait rien //
if( response.error != 0 )
return false;
// {4} Si tout s'est bien passé //
e.target.parentNode.parentNode.removeChild( e.target.parentNode );
});
};
for( i = 0 ; i < section.groups.group.rem.length ; i++ )
section.groups.group.rem[i].addEventListener('click', section.groups.group.remfunc, false);
/* (7) On gere la "modification" des membres d'un groupe */
for( i = 0 ; i < section.groups.group.add.length ; i++ ){
section.groups.group.add[i].addEventListener('mouseup', function(e){
/* (1) Check if already opened */
var opened = e.target.innerHTML != '+';
/* (2) Hide all dropdowns */
for( j = 0 ; j < section.groups.group.add.length ; j++ )
section.groups.group.add[j].innerHTML = '+';
/* (3) Try to show dropdown if not already */
if( !opened ){
/* (1) Get the group id/type */
if( !/^\d+$/.test(e.target.getData('cluster')) )
return;
var cluster_id = parseInt(e.target.getData('cluster'));
/* (2) Get already members ids */
var brothers = e.target.parentNode.children[1].children;
var already = []; // will contain already ids
for( j = 0 ; j < brothers.length ; j++ )
already += parseInt( brothers[j].children[0].getData('member') );
/* (3) Call list of members */
api.send({path: 'userDefault/getAll'}, function(response){
// Manage error
if( response.error != 0 ){
console.warn('error loading list of members');
return;
}
// remove already members
var members = response.users;
/* (4) Build dropdown content without already members */
dropdown = "<div class='dropdown'>";
for( j = 0 ; j < members.length ; j++ ){
if( already.indexOf(members[j].id_user) > -1 )
continue;
dropdown += "<span data-memberid='"+members[j].id_user+"'>"+members[j].username+"</span>";
}
dropdown += "</div>";
e.target.innerHTML = '+'+dropdown;
/* (5) Attach events to dropdown elements */
var dropdownElements = e.target.children[0].children;
for( j = 0 ; j < dropdownElements.length ; j++ )
dropdownElements[j].addEventListener('mousedown', function(e2){
// {1} Get member id/username //
var memberid = e2.target.getData('memberid');
var username = e2.target.innerHTML;
// manage error
if( memberid === false )
return;
// {2} Request : add member to group //
var request = {
path: 'userDefault/link',
id_cluster: cluster_id,
id_user: memberid
};
// {3} Call link //
api.send(request, function(response2){
// manage error
if( response2.error != 0 ){
console.warn('cannot link user to group');
return;
}
// Remove dropdown (in order to be reloaded with new members);
e.target.innerHTML = '+';
// {4} Update display //
var newMemberRemoveIcon = document.createElement('span');
newMemberRemoveIcon.setAttribute('class', 'rem-member');
newMemberRemoveIcon.setAttribute('data-member', memberid);
newMemberRemoveIcon.setAttribute('data-cluster', e.target.getData('cluster'));
newMemberRemoveIcon.addEventListener('click', section.groups.group.remfunc, false);
var newMember = document.createElement('span');
newMember.appendChild( document.createTextNode(username) );
newMember.appendChild(newMemberRemoveIcon);
e.target.parentNode.children[1].appendChild(newMember);
});
}, false);
});
}
}, false);
}
}