301 lines
7.5 KiB
JavaScript
301 lines
7.5 KiB
JavaScript
// On referencie toutes les sections
|
|
var section = {
|
|
view: {
|
|
text: '#CONTAINER > section[data-sublink="view"] ',
|
|
element: document.querySelector('#CONTAINER > section[data-sublink="view"]')
|
|
},
|
|
|
|
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 MACHINES
|
|
*
|
|
*/
|
|
if( section.view.element != null ){
|
|
|
|
// On recupere tous les liens
|
|
section.view.link = {
|
|
edit: document.querySelectorAll(section.view.text + '.link_edit[data-machine]'),
|
|
remove: document.querySelectorAll(section.view.text + '.link_remove[data-machine]')
|
|
};
|
|
|
|
// 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('machine');
|
|
autofill_edit(pageManager.vars[1]);
|
|
navSubMenu('edit');
|
|
}, false);
|
|
|
|
}
|
|
|
|
// 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('machine');
|
|
autofill_remove(pageManager.vars[1]);
|
|
navSubMenu('remove');
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* GESTION DE LA CREATION D'UNE MACHINE
|
|
*
|
|
*/
|
|
if( section.create.element != null ){
|
|
|
|
// On recupere tous les champs
|
|
section.create.input = {
|
|
code: document.querySelector(section.create.text + '#create_code'),
|
|
name: document.querySelector(section.create.text + '#create_name'),
|
|
|
|
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: 'machineDefault/create', // On veut creer un utilisateur
|
|
data: [
|
|
section.create.input.code.value,
|
|
section.create.input.name.value
|
|
]
|
|
};
|
|
|
|
api.send(request, function(answer){
|
|
if( answer.ModuleError == 0 ) // Tout s'est bien deroule
|
|
console.log('Machine cree!');
|
|
else // Erreur
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
|
|
});
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] remove -> Suppression de machine
|
|
=========================================================*/
|
|
if( section.remove.element != null ){
|
|
|
|
// Definition des champs
|
|
section.remove.input = {
|
|
code: document.querySelector(section.remove.text + '#remove_code'),
|
|
name: document.querySelector(section.remove.text + '#remove_name'),
|
|
|
|
submit: document.querySelector(section.remove.text + '#remove_submit')
|
|
};
|
|
|
|
|
|
// Gestion de l'auto-remplissage par lien
|
|
function autofill_remove(id_machine){
|
|
// On recupere le code + name de la machine selectionnee
|
|
var machine_data = null;
|
|
|
|
api.send({path:'machineDefault/getById', data: [id_machine]}, function(answer){
|
|
if( answer.ModuleError == 0 ){ // Aucune erreur
|
|
machine_data = answer.machine;
|
|
|
|
// Auto-remplissage du formulaire
|
|
section.remove.input.code.value = machine_data.code;
|
|
section.remove.input.name.value = machine_data.name;
|
|
}
|
|
});
|
|
}if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_remove(pageManager.vars[1]);
|
|
|
|
|
|
|
|
|
|
/* (n) Gestion de l'envoi du formulaire */
|
|
section.remove.input.submit.addEventListener('click', function(e){
|
|
// On annule l'envoi de base (PHP)
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
/* (1) On recupere l'UID de la machine */
|
|
var getuid = {};
|
|
|
|
|
|
// recuperation par CODE
|
|
if( section.remove.input.code.value != '' ){
|
|
getuid.path = 'machineDefault/getByCode';
|
|
getuid.data = [section.remove.input.code.value];
|
|
|
|
// recuperation par NAME
|
|
}else if( section.remove.input.name.value != '' ){
|
|
getuid.path = 'machineDefault/getByName';
|
|
getuid.data = [section.remove.input.name.value];
|
|
}
|
|
|
|
// On envoie la requete de demande d'uid
|
|
api.send(getuid, function(uid_answer){
|
|
if( uid_answer.ModuleError == 0 ){ // Si aucune erreur
|
|
|
|
/* (2) On supprime la machine */
|
|
var request = {
|
|
path: 'machineDefault/delete', // On veut supprimer cet utilisateur
|
|
data: [uid_answer.machine.id_machine]
|
|
};
|
|
|
|
api.send(request, function(answer){
|
|
if( answer.ModuleError == 0 && answer.status == true ) // Tout s'est bien deroule
|
|
console.log('Machine supprimee!');
|
|
else // Erreur
|
|
console.error('ModuleError::'+answer.ModuleError);
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] edit -> Modification de machine
|
|
=========================================================*/
|
|
if( section.edit.element != null ){
|
|
|
|
// Definition des champs
|
|
section.edit.input = {
|
|
search: {
|
|
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'),
|
|
name: document.querySelector(section.edit.text + '#edit_name'),
|
|
|
|
submit: document.querySelector(section.edit.text + '#edit_submit')
|
|
};
|
|
|
|
|
|
// Gestion de l'auto-remplissage par lien
|
|
function autofill_edit(id_machine){
|
|
// On recupere le code + name de la machine selectionnee
|
|
var machine_data = null;
|
|
|
|
api.send({path:'machineDefault/getById', data: [id_machine]}, function(answer){
|
|
if( answer.ModuleError == 0 ){ // Aucune erreur
|
|
machine_data = answer.machine;
|
|
|
|
// Auto-remplissage du formulaire
|
|
section.edit.input.code.value = machine_data.code;
|
|
section.edit.input.name.value = machine_data.name;
|
|
}
|
|
});
|
|
}if( pageManager.vars.length >= 2 && !isNaN(pageManager.vars[1]) ) autofill_edit(pageManager.vars[1]);
|
|
|
|
|
|
|
|
/* (2) Gestion de la recherche de l'utilisateur */
|
|
var lastsearch = null;
|
|
var searchindex = 0;
|
|
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
|
|
// Si on trouve le meme utilisateur que lors de la derniere recherche
|
|
if( lastsearch == uidanswer.users[searchindex].id_user )
|
|
searchindex = (searchindex+1) % uidanswer.users.length; // on affiche le prochain (decalage cyclique)
|
|
|
|
lastsearch = uidanswer.users[searchindex].id_user;
|
|
section.edit.input.search.id.value = lastsearch;
|
|
// on auto-remplit le formulaire
|
|
autofill_edit(lastsearch);
|
|
|
|
}else{
|
|
autofill_edit(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |