395 lines
18 KiB
JavaScript
395 lines
18 KiB
JavaScript
// STRUCTURE DU localStorage
|
|
// subject -> contient les données du sujet
|
|
// contacts -> contient les données de tous les contacts
|
|
// mini-fiches -> contient les données de toutes les mini fiches
|
|
// fiches -> contient les données de toutes les fiches
|
|
|
|
|
|
/* [1] Initialisation des utilitaires et variables
|
|
=========================================================*/
|
|
/* (1) Boutons primaires */
|
|
// {1} pour effacer toutes les données //
|
|
var clearAllButton = $('#clear-all');
|
|
// {2} pour envoyer toutes les données //
|
|
var submitAllButton = $('#submit-all');
|
|
|
|
|
|
/* (2) Input de type fichier pour l'import */
|
|
var importCallLog = $('input#call_log-import[type="file"]');
|
|
|
|
/* (3) Initialisation des managers */
|
|
var subjectManager;
|
|
var contactManager;
|
|
var miniManager;
|
|
var ficheManager;
|
|
|
|
/* (4) Fonctions utiles */
|
|
function dynamicUpdate(target){
|
|
|
|
|
|
/* (0) Vérification de la cohérence de la @target
|
|
---------------------------------------------------------*/
|
|
var isElement = target instanceof Element;
|
|
var isSwitchLeft = isElement && target.tagName == 'SPAN' && target.className == 'switch-left';
|
|
var isSwitchBoth = isElement && target.tagName == 'SPAN' && target.className == 'switch-both';
|
|
var isSaveButton = isElement && target.tagName == 'INPUT' && target.type == 'submit';
|
|
var isNavButton = isElement && target.tagName == 'SPAN' && (target.parentNode.id == 'nav-mini' || target.parentNode.id == 'nav-fiche');
|
|
var isNavContact = isElement && target.tagName == 'SPAN' && target.parentNode.id == 'nav-contact';
|
|
|
|
// Si erreur, on retourne une erreur
|
|
if( !isSwitchBoth && !isSwitchLeft && !isSaveButton && !isNavButton && !isNavContact && target !== true )
|
|
return false;
|
|
|
|
/* (1) Gestion de l'échange de nom/prénom
|
|
---------------------------------------------------------*/
|
|
if( isSwitchBoth ){
|
|
console.log('> switch firstname <-> lastname');
|
|
|
|
/* (1) On récupère le formulaire parent */
|
|
var currentForm = target.parentNode;
|
|
|
|
/* (2) On récupère Nom et Prénom */
|
|
var firstname = currentForm.children[3];
|
|
var lastname = currentForm.children[5];
|
|
|
|
/* (3) On échange leurs valeurs */
|
|
var tmp = firstname.value;
|
|
firstname.value = lastname.value;
|
|
lastname.value = tmp;
|
|
|
|
}else if( isSwitchLeft ){
|
|
/* (2) Gestion de l'échange Prénom+Nom = Pseudo
|
|
---------------------------------------------------------*/
|
|
console.log('> switch firstname+lastname -> username');
|
|
|
|
/* (1) On récupère le formulaire parent */
|
|
var currentForm = target.parentNode;
|
|
|
|
/* (2) On récupère Pseudo, Nom et Prénom */
|
|
var username = currentForm.children[1];
|
|
var firstname = currentForm.children[3];
|
|
var lastname = currentForm.children[5];
|
|
|
|
/* (3) Vérification de la validité */
|
|
// Si pseudo pas vide, ou nom et prenom vide -> on ne fais rien
|
|
if( username.value.length > 0 || firstname.value.length == 0 && lastname.value.length == 0 )
|
|
return false;
|
|
|
|
/* (4) On attribue à Pseudo, la valeur de Prénom+Nom */
|
|
username.value = firstname.value +' '+ lastname.value;
|
|
firstname.value = '';
|
|
lastname.value = '';
|
|
|
|
|
|
/* (3) Gestion de la mise à jour dynamique
|
|
---------------------------------------------------------*/
|
|
}else{
|
|
console.log('> dynamic update');
|
|
|
|
/* (1) On enregistre les MINI fiches relation */
|
|
miniManager.fieldsToStorage();
|
|
|
|
/* (2) On enregister les FICHES relation */
|
|
ficheManager.fieldsToStorage();
|
|
|
|
/* (3) On enregistre les CONTACTS */
|
|
contactManager.fieldsToStorage();
|
|
|
|
/* (4) On synchronise les MINI avec les CONTACTS */
|
|
miniManager.sync();
|
|
|
|
/* (5) On synchronise les FICHE avec les CONTACTS */
|
|
// La synchronisation des champs communs est aussi faite entre `mini` et `fiches`
|
|
// 1. Si les valeurs de `fiche` sont pas définis, on prend les valeurs de `mini`
|
|
// 2. Sinon, on prend les valeurs de `fiche`
|
|
ficheManager.sync();
|
|
|
|
|
|
/* (6) On affiche le tout */
|
|
miniManager.storageToFields();
|
|
ficheManager.storageToFields();
|
|
|
|
/* (7) On met à jour les contacts si on les a modifié on si on navigue dans les contacts */
|
|
if( isSaveButton || isNavContact )
|
|
contactManager.storageToFields();
|
|
}
|
|
}
|
|
|
|
// Vérifie qu'une seule valeur vaut TRUE et les autres false
|
|
function checkRadioValue(selectData){
|
|
var nbTrue = 0;
|
|
for( var i = 0 ; i < selectData.length ; i++ )
|
|
if( selectData[i].status === true )
|
|
nbTrue++;
|
|
|
|
if( nbTrue != 1 ) return false;
|
|
else return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Inclusion des scripts externes et lancement
|
|
=========================================================*/
|
|
include('/js/includes/input-phone-subject.js', function(){
|
|
include('/js/includes/input-phone-contact.js', function(){
|
|
include('/js/includes/input-phone-mini.js', function(){
|
|
include('/js/includes/input-phone-fiche.js', function(){
|
|
|
|
|
|
/* (1) Gestion du formulaire du sujet
|
|
---------------------------------------------------------*/
|
|
/* (1) On crée une instance du manager du sujet */
|
|
subjectManager = new inputPhoneSubject(
|
|
$('article.subject-panel [data-name="number"]'),
|
|
$('article.subject-panel [data-name="username"]'),
|
|
$('article.subject-panel [data-name="firstname"]'),
|
|
$('article.subject-panel [data-name="lastname"]'),
|
|
$('article.subject-panel [data-name="submit"]')
|
|
);
|
|
|
|
/* (2) On le démarre */
|
|
subjectManager.attach();
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Gestion des formulaires de contact
|
|
---------------------------------------------------------*/
|
|
/* (1) On crée une instance du gestionnaire des CONTACTS */
|
|
contactManager = new inputPhoneContact(
|
|
$('article.contact-panel'),
|
|
$('#nav-contact')
|
|
);
|
|
|
|
/* (2) On le démarre */
|
|
contactManager.attach(dynamicUpdate);
|
|
|
|
|
|
|
|
/* (3) Gestion des mini fiches relation
|
|
---------------------------------------------------------*/
|
|
/* (1) On crée une instance du gestionnaire des mini fiches relation */
|
|
miniManager = new inputPhoneMini(
|
|
$('article.mini-relation-panel'),
|
|
$('#nav-mini')
|
|
);
|
|
|
|
/* (2) On le démarre */
|
|
miniManager.attach(dynamicUpdate);
|
|
|
|
|
|
/* (4) Gestion des fiches relation
|
|
---------------------------------------------------------*/
|
|
/* (1) On crée une instance du gestionnaire des fiches relation */
|
|
ficheManager = new inputPhoneFiche(
|
|
$('article.relation-panel'),
|
|
$('#nav-fiche')
|
|
);
|
|
|
|
/* (2) On le démarre */
|
|
ficheManager.attach(dynamicUpdate);
|
|
|
|
|
|
|
|
|
|
/* (5) Gestion de l'import du fichier
|
|
---------------------------------------------------------*/
|
|
importCallLog.addEventListener('change', function(e){
|
|
console.log( importCallLog.files[0] );
|
|
/* (1) On rédige la requête */
|
|
var uploadRequest = {
|
|
path: 'upload/call_log',
|
|
file: importCallLog.files[0]
|
|
};
|
|
|
|
/* (2) On effectue l'upload (import) */
|
|
api.send(uploadRequest, function(uploadResponse){
|
|
console.log(uploadResponse);
|
|
|
|
// Si tout est bon, on met l'input en bleu
|
|
if( uploadResponse.ModuleError == 0 ){
|
|
importCallLog.addClass('active');
|
|
|
|
/* (3) On rédige la requête pour récupérer les données du fichier */
|
|
var callLogRequest = {
|
|
path: 'call_log/unserialize',
|
|
phone_number: $('#subject_phone_number').value
|
|
};
|
|
|
|
/* (4) On effectue la récupération d'informations */
|
|
api.send(callLogRequest, function(dataResponse){
|
|
console.log(dataResponse);
|
|
|
|
// Si erreur, on quitte
|
|
if( dataResponse.ModuleError != 0 )
|
|
return;
|
|
|
|
|
|
|
|
/* (5) Pour chaque contact de l'annuaire, on ajoute un contact */
|
|
for( var i = 0 ; i < dataResponse.directory.length ; i++ ){
|
|
|
|
// On découpe le nom par espaces
|
|
var name = dataResponse.directory[i].name===null ? '' : dataResponse.directory[i].name;
|
|
var splitted = name.split(' ');
|
|
|
|
lsi.set('contacts', i, {
|
|
uid: i,
|
|
number: dataResponse.directory[i].number,
|
|
username: splitted.length == 1 ? splitted[0] : '', // Si un seul mot -> pseudo
|
|
firstname: splitted.length > 1 ? splitted[0] : '', // Si plusieurs mots -> le 1er est le prénom
|
|
lastname: splitted.length > 1 ? splitted.splice(1).join(' ') : '' // et les autres sont le nom
|
|
});
|
|
}
|
|
|
|
/* (6) On met à jour l'affichage */
|
|
contactManager.storageToFields();
|
|
dynamicUpdate(true);
|
|
});
|
|
}
|
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
|
|
/* (6) Gestion de l'effacement des données locales
|
|
---------------------------------------------------------*/
|
|
clearAllButton.addEventListener('click', function(e){
|
|
localStorage.clear();
|
|
reload();
|
|
}, false);
|
|
|
|
|
|
/* (7) Gestion de l'export des données locales
|
|
---------------------------------------------------------*/
|
|
$('#export-all').addEventListener('click', function(e){
|
|
/* (1) On construit les données */
|
|
var data = {
|
|
subject: lsi.export('subject')[0],
|
|
contacts: lsi.export('contacts'),
|
|
mini: lsi.export('mini-fiches'),
|
|
fiches: lsi.export('fiches')
|
|
};
|
|
|
|
/* (2) On lance le téléchargement */
|
|
var downloadTarget = $('#download-target'); // On récupère le lien (<a>) caché qui fera le téléchargement
|
|
downloadTarget.download = 'local-data.json'; // Nom du fichier qui sera téléchargé
|
|
downloadTarget.href = 'data:application/octet-stream,'+encodeURIComponent(JSON.stringify(data)); // Contenu
|
|
downloadTarget.click(); // On lance le téléchargement
|
|
|
|
}, false);
|
|
|
|
|
|
/* (8) Gestion de l'import des données locales
|
|
---------------------------------------------------------*/
|
|
$('#import-all').addEventListener('click', function(e){
|
|
/* (1) On récupère les données uploadées */
|
|
// TODO: Gestion de la récupération de données uploadées
|
|
var json = '{"subject":{"number":"06 69 05 19 10","username":"","firstname":"Adrien","lastname":""},"contacts":{"0":{"uid":0,"number":"1","username":"1","firstname":"","lastname":"","hash":241770401},"1":{"uid":1,"number":"2","username":"2","firstname":"","lastname":"","hash":363925312}},"mini":{"0":{"uid":0,"firstname":"","lastname":"","username":"1","sexe":[{"value":"on","status":true},{"value":"on","status":false}],"age":"25","job":"0","loc":[{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":true},{"value":"on","status":false}],"hash":1923019850,"valid":true,"timestamp":1462196014908},"1":{"uid":1,"firstname":"","lastname":"","username":"2","sexe":[{"value":"on","status":true},{"value":"on","status":false}],"age":"23","job":"32","loc":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"hash":3042965309,"valid":true,"timestamp":1462196032703}},"fiches":{"0":{"uid":0,"firstname":"","lastname":"","username":"1","sexe":[{"value":"on","status":true},{"value":"on","status":false}],"age":"25","job":"0","famsit":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false}],"studies":".","reltype":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"reltypeSpecial":"","city":"","loc":[{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":true},{"value":"on","status":false}],"loc2":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"duration":["",""],"context":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"contextSpecial":["","",""],"freq":[[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}]],"connect":[[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}]],"connectSpecial":["",""],"hash":1570439147,"valid":false,"timestamp":1462196015606},"1":{"uid":1,"firstname":"","lastname":"","username":"2","sexe":[{"value":"on","status":true},{"value":"on","status":false}],"age":"23","job":"32","famsit":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false}],"studies":".","reltype":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"reltypeSpecial":"","city":"","loc":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"loc2":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"duration":["",""],"context":[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],"contextSpecial":["","",""],"freq":[[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}],[{"value":"on","status":true},{"value":"on","status":false},{"value":"on","status":false},{"value":"on","status":false}]],"connect":[[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}],[{"value":"on","status":false},{"value":"on","status":true}]],"connectSpecial":["",""],"hash":3347750419,"valid":false,"timestamp":1462195434069}}}';
|
|
|
|
/* (2) On les met en forme */
|
|
var data = JSON.parse(json);
|
|
|
|
/* (3) On met les données dans le 'localStorage' et on recharge la page */
|
|
console.log(data);
|
|
lsi.import('subject', data.subject);
|
|
lsi.import('contacts', data.contacts);
|
|
lsi.import('mini-fiches', data.mini);
|
|
lsi.import('fiches', data.fiches);
|
|
}, false);
|
|
|
|
|
|
/* (9) Gestion de la validation et de l'envoi des données
|
|
---------------------------------------------------------*/
|
|
submitAllButton.addEventListener('click', function(e){
|
|
console.log('> GATHERING ALL DATA')
|
|
|
|
/* (1) On met dans la mémoire tout les champs non enregistrés */
|
|
subjectManager.fieldsToStorage();
|
|
contactManager.fieldsToStorage();
|
|
miniManager.fieldsToStorage();
|
|
ficheManager.fieldsToStorage();
|
|
|
|
/* (2) On prépare la requête avec toutes les données */
|
|
var request = {
|
|
path: 'input/phone',
|
|
|
|
subject: lsi.export('subject')[0],
|
|
contacts: lsi.export('contacts'),
|
|
mini: lsi.export('mini-fiches'),
|
|
fiches: lsi.export('fiches')
|
|
};
|
|
|
|
/* (3) On envoie la requête et traite la réponse */
|
|
api.send(request, function(response){
|
|
console.log(response);
|
|
}, false);
|
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
}); }); }); });
|
|
|
|
|
|
|
|
|
|
// ETAPE 1 : INITIALISATION (ON RESTAURE LES DONNEES)
|
|
// 1. contact.storageToFields();
|
|
// 2. contact.add();
|
|
// 3. mini.storageToFields();
|
|
// 4. fiche.storageToFields();
|
|
|
|
// ETAPE 2 : ENREGISTREMENT + MISE À JOUR
|
|
// 1. mini.fieldsToStorage();
|
|
// 2. fiche.fieldsToStorage();
|
|
// 3. contact.fieldsToStorage();
|
|
// 4. mini.sync();
|
|
// 5. fiche.sync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var number = $('input.number[type="text"]');
|
|
// ic.append(number, format_number, '06 06 06 06 06');
|
|
// // format_number -> format pour les numéros de téléphones
|
|
//
|
|
// number.addEventListener('keyup', function(e){
|
|
// ic.correct(number, false);
|
|
// }, false);
|