NxTIC/view/js/users.js

504 lines
14 KiB
JavaScript
Raw 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"]'),
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"]')
}
};
/* [1] view -> AFFICHAGE DES UTILISATEURS
=========================================================*/
if( section.view.element != null ){
/* (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) Gestion de la recherche instantannee */
section.view.searchbar.addEventListener('keyup', function(e){
var search = {
path: 'userDefault/search',
data: [section.view.searchbar.value]
};
// On envoie la requete
api.send(search, function(result){
console.log(result.users.length);
if( result.ModuleError == 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(user_list[i].id) > -1 )
user_list[i].remClass('hidden');
// Si ne doit pas etre visible
else
user_list[i].addClass('hidden');
}
}
});
}, false);
/* (3) 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('user');
autofill_edit(pageManager.vars[1]);
navSubMenu('edit');
}, false);
}
/* (4) 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('user');
autofill_remove(pageManager.vars[1]);
navSubMenu('remove');
}, false);
}
}
/* [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'),
password: document.querySelector(section.create.text + '#create_password'),
status: document.querySelector(section.create.text + '#create_status'),
submit: document.querySelector(section.create.text + '#create_submit')
}
// 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();
var request = {
path: 'userDefault/create', // On veut creer un utilisateur
data: [
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,
section.create.input.password.value,
section.create.input.status.value
]
};
api.send(request, function(answer){
if( answer.ModuleError == 0 ){ // Tout s'est bien deroule
console.log('Utilisateur cree!');
section.create.input.submit.anim('active', 1500);
}else // Erreur
console.error('ModuleError::'+answer.ModuleError);
});
}, false);
}
/* [3] remove -> Suppression d'utilisateur
=========================================================*/
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')
};
var lastsearch_remove = null;
var searchindex_remove = 0;
/* (2) Gestion de l'auto-remplissage par url */
function autofill_remove(id_user){
// On recupere le code + username de l'utilisateur selectionne
var user_data = {
id_user: '',
code: '',
username: '',
firstname: '',
lastname: '',
mail: '',
status: '',
disable_input: true
};
api.send({path:'userDefault/getById', data: [id_user]}, function(answer){
if( answer.ModuleError == 0 ){ // Si un resultat
user_data = answer.user;
user_data.disable_input = false;
}
// On enregistre l'UID de l'utilisateur
section.remove.input.search.id.value = user_data.id_user;
// Auto-remplissage du formulaire
section.remove.input.code.value = user_data.code;
section.remove.input.username.value = user_data.username;
// On affiche le bouton de suppression
section.remove.input.submit.disabled = user_data.disable_input;
// Muse 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: 'userDefault/search',
data: [section.remove.input.search.keyword.value]
};
// On envoie la requete
api.send(getuid, function(uidanswer){
if( uidanswer.ModuleError == 0 && uidanswer.users.length > 0){ // si on a trouve qqch
section.remove.input.search.submit.anim('active', 1000);
// Si on trouve la meme machine que lors de la derniere recherche
if( lastsearch_remove == uidanswer.users[searchindex_remove].id_user )
searchindex_remove = (searchindex_remove+1) % uidanswer.users.length; // on affiche le prochain (decalage cyclique)
else // si nouveau resultat, on remet l'indice a 0
searchindex_remove = 0;
lastsearch_remove = uidanswer.users[searchindex_remove].id_user;
section.remove.input.search.id.value = lastsearch_remove;
// Mise a jour des indices des resultats
section.remove.input.search.sum.innerHTML = uidanswer.users.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 l'utilisateur */
var request = {
path: 'userDefault/delete', // On veut supprimer cet utilisateur
data: [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('Utilisateur supprime!');
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 d'utilisateur
=========================================================*/
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'),
password: document.querySelector(section.edit.text + '#edit_password'),
status: document.querySelector(section.edit.text + '#edit_status'),
submit: document.querySelector(section.edit.text + '#edit_submit')
};
var lastsearch_edit = null;
var searchindex_edit = 0;
/* (1) Gestion de l'auto-remplissage par url */
function autofill_edit(id_user){
// On definit les valeurs par defaut
var user_data = {
id_user: '',
code: '',
username: '',
firstname: '',
lastname: '',
mail: '',
status: '',
disable_input: true
};
// Si @id_user est un entier
if( !isNaN(id_user) ){
// On recupere le code + username de l'utilisateur selectionne
api.send({path:'userDefault/getById', data: [id_user]}, function(answer){
if( answer.ModuleError == 0 ){ // Si aucune erreur
user_data = answer.user;
user_data.disable_input = false;
}
// On enregistre l'UID de l'utilisateur
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;
section.edit.input.password.value = '';
section.edit.input.status.value = user_data.status;
// On active ou pas le bouton "Modifier"
section.edit.input.submit.disabled = user_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 l'utilisateur */
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: 'userDefault/search',
data: [section.edit.input.search.keyword.value]
};
// On envoie la requete
api.send(getuid, function(uidanswer){
if( uidanswer.ModuleError == 0 && uidanswer.users.length > 0 ){ // si on a trouve qqch
section.edit.input.search.submit.anim('active', 1000);
// Si on trouve le meme utilisateur que lors de la derniere recherche
if( lastsearch_edit == uidanswer.users[searchindex_edit].id_user )
searchindex_edit = (searchindex_edit+1) % uidanswer.users.length; // on affiche le prochain (decalage cyclique)
else // si nouveau resultat, on remet l'indice a 0
searchindex_edit = 0;
lastsearch_edit = uidanswer.users[searchindex_edit].id_user;
section.edit.input.search.id.value = lastsearch_edit;
// Mise a jour des indices des resultats
section.edit.input.search.sum.innerHTML = uidanswer.users.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: 'userDefault/edit', // On veut creer un utilisateur
data: [
section.edit.input.search.id.value,
section.edit.input.code.value,
section.edit.input.username.value,
section.edit.input.firstname.value,
section.edit.input.lastname.value,
section.edit.input.mail.value,
section.edit.input.password.value,
section.edit.input.status.value
]
};
api.send(request, function(answer){
console.log( answer );
if( answer.ModuleError == 0 && answer.status == true ){ // Tout s'est bien deroule
console.log('Utilisateur modifie!');
section.edit.input.submit.anim('active', 1500);
// On met a jour l'utilisateur
autofill_edit(section.edit.input.search.id.value);
}else // Erreur
console.error('ModuleError::'+answer.ModuleError);
});
}
}, false);
}