// On referencie toutes les sections var section = { view: { text: '#CONTAINER > section[data-sublink="view"] ', element: document.querySelector('#CONTAINER > section[data-sublink="view"]'), searchbar: document.querySelector('#CONTAINER > section[data-sublink="view"] > .searchbar') }, 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"]') } }; /* GESTION DE L'AFFICHAGE DES GROUPES * */ 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]; document.getElementById(pageManager.vars[1]).addClass('selected'); } /* (1) On recupere tous les liens */ section.view.link = { edit: document.querySelectorAll(section.view.text + '.link_edit[data-cluster]'), remove: document.querySelectorAll(section.view.text + '.link_remove[data-cluster]') }; /* (2) On récupère toutes les actions de groupes */ section.view.group = { rem: document.querySelectorAll(section.view.text + '.rem-group[data-cluster][data-group]'), add: document.querySelectorAll(section.view.text + '.add-group[data-cluster]') }; /* (3) Gestion de la recherche instantannee */ section.view.searchbar.addEventListener('keyup', function(e){ var search = { path: 'clusterDefault/search', keywords: section.view.searchbar.value }; // On envoie la requete api.send(search, function(result){ if( result.ModuleError == 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_cluster); // On recupere la liste des elements correspondants aux utilisateurs var cluster_list = document.querySelectorAll(section.view.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(cluster_list[i].id) > -1 ) cluster_list[i].remClass('hidden'); // Si ne doit pas etre visible else cluster_list[i].addClass('hidden'); } } }); }, 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[1] = e.target.getData('cluster'); autofill_edit(pageManager.vars[1]); navSubMenu('edit'); }, 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[1] = e.target.getData('cluster'); autofill_remove(pageManager.vars[1]); navSubMenu('remove'); }, false); } /* (6) On gere la "suppression" d'un groupe */ for( var i = 0 ; i < section.view.group.rem.length ; i++ ){ section.view.group.rem[i].addEventListener('click', function(e){ // {1} On rédige la requête // var request = { path: 'clusterDefault/unlink', id_cluster: e.target.getData('cluster'), 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.ModuleError != 0 ) return false; // {4} Si tout s'est bien passé // e.target.parentNode.parentNode.removeChild( e.target.parentNode ); }); }, false); } } /* GESTION DE LA CREATION D'UNE MACHINE * */ if( section.create.element != null ){ // On recupere tous les champs section.create.input = { name: document.querySelector(section.create.text + '#create_name'), class: document.querySelector(section.create.text + '#create_class'), submit: document.querySelector(section.create.text + '#create_submit') } /* (n) Gestion de l'envoi du formulaire */ section.create.input.submit.addEventListener('click', function(e){ // On annule l'envoi de base (PHP) e.preventDefault(); var request = { path: 'clusterDefault/create', // On veut creer un utilisateur name: section.create.input.name.value, class: section.create.input.class.value }; api.send(request, function(answer){ if( answer.ModuleError == 0 ){ // Tout s'est bien deroule console.log('Groupe cree!'); // On redirige sur la cluster pageManager.vars = ['view', answer.id_cluster]; pageManager.refresh(); document.location = ''; section.create.input.submit.anim('active', 1500); } }); }, false); } /* [3] remove -> Suppression de cluster =========================================================*/ 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') }, name: document.querySelector(section.remove.text + '#remove_name'), submit: document.querySelector(section.remove.text + '#remove_submit') }; var lastsearch_remove = null; var searchindex_remove = 0; /* (2) Gestion de l'auto-remplissage par lien */ function autofill_remove(id_cluster){ // On recupere le name de la cluster selectionnee var cluster_data = { id_cluster: '', name: '', disable_input: true }; api.send({path:'clusterDefault/getById', id_cluster: id_cluster}, function(answer){ if( answer.ModuleError == 0 ){ // Aucune erreur cluster_data = answer.cluster; cluster_data.diable_input = false; } // On enregistre l'UID de la cluster section.remove.input.search.id.value = cluster_data.id_cluster; // Auto-remplissage du formulaire section.remove.input.name.value = cluster_data.name; // On gere le caractere "inactif" du submit section.remove.input.submit.disabled = cluster_data.disable_input; // Mise a jour des indices des resultats section.remove.input.search.num.innerHTML = searchindex_remove+1; }); }if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_remove(pageManager.vars[1]); /* (3) Gestion de la recherche */ section.remove.input.search.submit.addEventListener('click', function(e){ // On annule l'envoi de base (PHP) e.preventDefault(); // Si le champ n'est pas vide, on lance la recherche if( section.remove.input.search.keyword.value != '' ){ // Redaction de la requete var getuid = { path: 'clusterDefault/search', keywords: section.remove.input.search.keyword.value }; // On envoie la requete api.send(getuid, function(uidanswer){ if( uidanswer.ModuleError == 0 && uidanswer.clusters.length > 0){ // si on a trouve qqch section.remove.input.search.submit.anim('active', 1000); // Si on trouve la meme cluster que lors de la derniere recherche if( lastsearch_remove == uidanswer.clusters[searchindex_remove].id_cluster ) searchindex_remove = (searchindex_remove+1) % uidanswer.clusters.length; // on affiche le prochain (decalage cyclique) lastsearch_remove = uidanswer.clusters[searchindex_remove].id_cluster; section.remove.input.search.id.value = lastsearch_remove; // Mise a jour des indices des resultats section.remove.input.search.sum.innerHTML = uidanswer.clusters.length; // on auto-remplit le formulaire autofill_remove(lastsearch_remove); }else{ section.remove.input.search.sum.innerHTML = 0; autofill_remove(null); } }); } }, 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 est defini if( section.remove.input.search.id.value != '' ){ /* (2) On supprime la cluster */ var request = { path: 'clusterDefault/delete', // On veut supprimer cet utilisateur id_cluster: section.remove.input.search.id.value }; api.send(request, function(answer){ if( answer.ModuleError == 0 && answer.status == true ){ // Tout s'est bien deroule console.log('Groupe supprimee!'); section.remove.input.submit.anim('active', 1500); // On vide le formulaire autofill_remove(null); }else // Erreur console.error('ModuleError::'+answer.ModuleError); }); } }, false); } /* [4] edit -> Modification de cluster =========================================================*/ 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') }, name: document.querySelector(section.edit.text + '#edit_name'), submit: document.querySelector(section.edit.text + '#edit_submit') }; var lastsearch_edit = null; var searchindex_edit = 0; // Gestion de l'auto-remplissage par lien function autofill_edit(id_cluster){ // On recupere le name de la cluster selectionnee var cluster_data = { id_cluster: '', name: '', disable_input: true }; // Si @id_cluster est un entier if( !isNaN(id_cluster) ){ api.send({path:'clusterDefault/getById', id_cluster: id_cluster}, function(answer){ if( answer.ModuleError == 0 ){ // Aucune erreur cluster_data = answer.cluster; cluster_data.disable_input = false; } // On enregistre l'UID de la cluster section.edit.input.search.id.value = cluster_data.id_cluster; // Auto-remplissage du formulaire section.edit.input.name.value = cluster_data.name; // On active ou pas le bouton "Modifier" section.edit.input.submit.disabled = cluster_data.disable_input; // Mise a jour des indices des resultats section.edit.input.search.num.innerHTML = searchindex_edit+1; }); }else section.edit.input.search.num.innerHTML = 0; }if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_edit(pageManager.vars[1]); /* (2) Gestion de la recherche de la cluster */ section.edit.input.search.submit.addEventListener('click', function(e){ // On annule l'envoi de base (PHP) e.preventDefault(); // Si le champ n'est pas vide, on lance la recherche if( section.edit.input.search.keyword.value != '' ){ // Redaction de la requete var getuid = { path: 'clusterDefault/search', keywords: section.edit.input.search.keyword.value }; // On envoie la requete api.send(getuid, function(uidanswer){ if( uidanswer.ModuleError == 0 && uidanswer.clusters.length > 0 ){ // si on a trouve qqch section.edit.input.search.submit.anim('active', 1000); // Si on trouve la meme cluster que lors de la derniere recherche if( lastsearch_edit == uidanswer.clusters[searchindex_edit].id_cluster ) searchindex_edit = (searchindex_edit+1) % uidanswer.clusters.length; // on affiche le prochain (decalage cyclique) else // si nouveau resultat, on remet l'indice a 0 searchindex_edit = 0; lastsearch_edit = uidanswer.clusters[searchindex_edit].id_cluster; section.edit.input.search.id.value = lastsearch_edit; // Mise a jour des indices des resultats section.edit.input.search.sum.innerHTML = uidanswer.clusters.length; // on auto-remplit le formulaire autofill_edit(lastsearch_edit); }else{ section.edit.input.search.sum.innerHTML = 0; autofill_edit(null); } }); } }, 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(); // Si l'id est defini if( section.edit.input.search.id.value != '' ){ var request = { path: 'clusterDefault/edit', // On veut creer une cluster id_cluster: section.edit.input.search.id.value, name: section.edit.input.name.value }; api.send(request, function(answer){ if( answer.ModuleError == 0 && answer.status == true ){ // Tout s'est bien deroule console.log('Groupe modifiee!'); section.edit.input.submit.anim('active', 1500); // On met a jour la cluster autofill_edit(section.edit.input.search.id.value); }else // Erreur console.error('ModuleError::'+answer.ModuleError); }); } }, false); }