2016-04-26 14:16:43 +00:00
|
|
|
/* [0] Constructeur -> définit le conteneur et le bouton d'ajout
|
|
|
|
=========================================================*/
|
2016-12-13 18:37:44 +00:00
|
|
|
function inputPhoneSubject(iSubjectId, iCoords, store_button){
|
2016-05-26 14:47:15 +00:00
|
|
|
this.subject_id = iSubjectId;
|
2016-12-13 18:37:44 +00:00
|
|
|
this.coords = iCoords;
|
2016-05-18 13:28:02 +00:00
|
|
|
this.store_button = store_button;
|
2016-04-26 14:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [1] Attributs
|
|
|
|
=========================================================*/
|
|
|
|
inputPhoneSubject.prototype = {
|
2016-05-18 13:28:02 +00:00
|
|
|
store_button: this.store_button, // Bouton d'ajout d'un formulaire
|
2016-05-27 14:32:26 +00:00
|
|
|
subject_id: this.subject_id,
|
2016-12-13 18:37:44 +00:00
|
|
|
coords: this.coords,
|
2016-05-27 14:32:26 +00:00
|
|
|
handler: null
|
2016-04-26 14:16:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-05-10 14:51:09 +00:00
|
|
|
/* [2] Vérification d'un formulaire
|
2016-04-26 14:16:43 +00:00
|
|
|
=========================================================*/
|
2016-05-10 14:51:09 +00:00
|
|
|
inputPhoneSubject.prototype.check = function(){
|
2016-09-27 14:13:51 +00:00
|
|
|
/* (1) Le numéro n'est pas vide (et c'est un numéro) */
|
2016-05-27 14:32:26 +00:00
|
|
|
return this.subject_id.value.length > 0 && !isNaN(this.subject_id.value);
|
2016-05-10 14:51:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* [3] Gestion de l'enregistrement des formulaires de contact
|
|
|
|
=========================================================*/
|
2016-12-14 11:18:15 +00:00
|
|
|
inputPhoneSubject.prototype.fieldsToStorage = function(onlyCoords){
|
2016-10-10 08:58:44 +00:00
|
|
|
console.group('[phone.subject] fields to storage');
|
2016-05-10 14:51:09 +00:00
|
|
|
|
2016-12-14 11:18:15 +00:00
|
|
|
( typeof onlyCoords != 'boolean' ) ? false : onlyCoords;
|
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
// {1} Si le formulaire n'est pas valide, on ne l'enregistre pas //
|
2016-12-14 11:18:15 +00:00
|
|
|
if( !onlyCoords )
|
|
|
|
if( !this.check() )
|
|
|
|
return false;
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-10-12 15:37:21 +00:00
|
|
|
// {2} Si on a déja saisie 1+ contact, on ne l'enregistre pas //
|
2016-12-14 11:18:15 +00:00
|
|
|
// et si on modifie le sujet
|
|
|
|
if( lsi.keys('p_subject').length === 1 && this.subject_id.value != lsi.get('p_subject',0).subject_id && lsi.keys('p_contacts').length > 0 ){
|
2016-10-12 15:37:21 +00:00
|
|
|
Notification.warning('Attention', 'Vous devez effacer le formulaire pour changer de sujet');
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-30 07:59:42 +00:00
|
|
|
|
2016-10-12 15:37:21 +00:00
|
|
|
// {3} On récupère et met en forme les valeurs du deflater //
|
2016-12-13 18:37:44 +00:00
|
|
|
var obj = { subject_id: this.subject_id.value, coords: this.coords.value };
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-10-12 15:37:21 +00:00
|
|
|
// {4} On enregistre les données dans le 'localStorage' //
|
2016-05-18 13:28:02 +00:00
|
|
|
lsi.set('p_subject', 0, obj);
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-10-11 12:05:59 +00:00
|
|
|
console.groupEnd();
|
2016-04-26 14:16:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-10 14:51:09 +00:00
|
|
|
/* [4] Gestion de l'affichage depuis le 'localStorage'
|
2016-04-26 14:16:43 +00:00
|
|
|
=========================================================*/
|
|
|
|
inputPhoneSubject.prototype.storageToFields = function(){
|
2016-10-10 08:58:44 +00:00
|
|
|
console.group('[phone.subject] storage to fields');
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
// {1} On récupère les informations du sujet //
|
|
|
|
var subjectData = lsi.get('p_subject', 0);
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-12-13 18:37:44 +00:00
|
|
|
subjectData = subjectData || { subject_id: '', coords: '' };
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
// {2} On restore les valeurs //
|
2016-05-27 14:32:26 +00:00
|
|
|
this.subject_id.value = subjectData.subject_id;
|
2016-12-13 18:37:44 +00:00
|
|
|
this.coords.value = subjectData.coords;
|
2016-10-10 08:58:44 +00:00
|
|
|
|
|
|
|
|
2016-10-11 12:05:59 +00:00
|
|
|
console.groupEnd();
|
2016-04-26 14:16:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-10 14:51:09 +00:00
|
|
|
/* [5] Point d'amorçage de la gestion des contacts
|
2016-04-26 14:16:43 +00:00
|
|
|
=========================================================*/
|
2016-05-27 14:32:26 +00:00
|
|
|
inputPhoneSubject.prototype.attach = function(handler){
|
2016-10-10 08:58:44 +00:00
|
|
|
console.group('[phone.subject] attaching events');
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
/* (1) On initialise le jeu de données */
|
|
|
|
lsi.createDataset('p_subject');
|
2016-05-27 14:32:26 +00:00
|
|
|
lsi.createDataset('p_friends');
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
// Pointeur pour les scopes des addEventListener()
|
2016-05-27 14:32:26 +00:00
|
|
|
this.handler = handler;
|
2016-10-11 11:16:52 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
// Handler de mise à jour quand les champs sont bons
|
2016-12-14 11:18:15 +00:00
|
|
|
function fts(e){ this.fieldsToStorage(); this.handler(true); this.storageToFields(); };
|
|
|
|
function fts2(e){ this.fieldsToStorage(true); this.storageToFields(); };
|
2016-05-10 14:51:09 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
/* (2) On attache l'évènement sur le bouton d'enregistrement */
|
2016-12-14 11:18:15 +00:00
|
|
|
this.store_button.addEventListener('click', fts.bind(this), false);
|
2016-05-10 14:51:09 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
/* (3) On attache un évènement de 'blur' sur chaque '<input type="text">' */
|
2016-12-14 11:18:15 +00:00
|
|
|
this.subject_id.addEventListener('blur', fts.bind(this), false);
|
2016-04-26 14:16:43 +00:00
|
|
|
|
2016-12-13 18:37:44 +00:00
|
|
|
|
|
|
|
/* (4) On attache un évènement pour le champ coord */
|
2016-12-14 11:18:15 +00:00
|
|
|
this.coords.addEventListener('blur', fts2.bind(this), false);
|
2016-12-13 18:37:44 +00:00
|
|
|
|
2016-05-18 13:28:02 +00:00
|
|
|
/* (4) On charge le sujet depuis la mémoire ('localStorage') */
|
|
|
|
this.storageToFields();
|
2016-10-10 08:58:44 +00:00
|
|
|
|
|
|
|
|
2016-10-11 12:05:59 +00:00
|
|
|
console.groupEnd();
|
2016-04-26 14:16:43 +00:00
|
|
|
};
|