Gestion côté client du classement par `appels` et par `sms`, enregistrement dans les contacts, reste à modifier `mini` et `fiches`

This commit is contained in:
xdrm-brackets 2016-05-03 16:29:12 +02:00
parent 818f06d452
commit b029ac9062
10 changed files with 124 additions and 37 deletions

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,8 @@ contactBuilder.setLayout(
"<h4 data-icon='o' class='new-contact color2'> \n"+ "<h4 data-icon='o' class='new-contact color2'> \n"+
"\t<input type='hidden' data-name='uid' value='@uid'>\n"+ "\t<input type='hidden' data-name='uid' value='@uid'>\n"+
"\t<input type='hidden' data-name='call' value='@call'>\n"+
"\t<input type='hidden' data-name='sms' value='@sms'>\n"+
"\t<input type='text' data-name='number' placeholder='Numéro de téléphone' value='@number' > \n"+ "\t<input type='text' data-name='number' placeholder='Numéro de téléphone' value='@number' > \n"+
"\t<input type='text' data-name='username' placeholder='Pseudo' value='@username' > \n"+ "\t<input type='text' data-name='username' placeholder='Pseudo' value='@username' > \n"+

View File

@ -1,8 +1,9 @@
function inputPhoneContact(a,b){this.container=a;this.nav_container=b}inputPhoneContact.prototype={container:this.container,nav_container:this.nav_container,selected:1,handler:null}; function inputPhoneContact(a,c){this.container=a;this.nav_container=c}inputPhoneContact.prototype={container:this.container,nav_container:this.nav_container,selected:1,handler:null};
inputPhoneContact.prototype.fieldsToStorage=function(){console.log("CONTACTS: FIELDS TO STORAGE");for(var a=$$("article.contact-panel .new-contact"),b=0;b<a.length;b++){var c=(new FormDeflater(a[b],["input"],["data-name"])).deflate(),d=0<c.number.value.length;if(d=d&&0<c.username.value.length+c.firstname.value.length+c.lastname.value.length)d={uid:parseInt(c.uid.value),number:c.number.value,username:c.username.value,firstname:c.firstname.value,lastname:c.lastname.value},d.hash=crc32(JSON.stringify(d)), inputPhoneContact.prototype.fieldsToStorage=function(){console.log("CONTACTS: FIELDS TO STORAGE");for(var a=$$("article.contact-panel .new-contact"),c=0;c<a.length;c++){var b=(new FormDeflater(a[c],["input"],["data-name"])).deflate(),d=0<b.number.value.length;if(d=d&&0<b.username.value.length+b.firstname.value.length+b.lastname.value.length)d={uid:parseInt(b.uid.value),number:b.number.value,username:b.username.value,firstname:b.firstname.value,lastname:b.lastname.value,call:parseInt(b.call.value),
lsi.set("contacts",parseInt(c.uid.value),d)}}; sms:parseInt(b.sms.value)},d.hash=crc32(JSON.stringify(d)),lsi.set("contacts",parseInt(b.uid.value),d)}};
inputPhoneContact.prototype.add=function(a){console.log("CONTACTS: ADD");if(null==a.uid||"number"!=typeof a.uid)return!1;a.number=null!=a.number?a.number:"";a.username=null!=a.username?a.username:"";a.firstname=null!=a.firstname?a.firstname:"";a.lastname=null!=a.lastname?a.lastname:"";this.container.innerHTML+=contactBuilder.build(a);a=$$("article.contact-panel .new-contact");for(var b=this,c=0;c<a.length;c++)a[c].removeEventListener("click",function(a){b.nav(a.target);b.handler(a.target)},!1),a[c].addEventListener("click", inputPhoneContact.prototype.add=function(a){console.log("CONTACTS: ADD");if(null==a.uid||"number"!=typeof a.uid)return!1;a.number=null!=a.number?a.number:"";a.username=null!=a.username?a.username:"";a.firstname=null!=a.firstname?a.firstname:"";a.lastname=null!=a.lastname?a.lastname:"";a.call=null!=a.call?a.call:-1;a.sms=null!=a.sms?a.sms:-1;this.container.innerHTML+=contactBuilder.build(a);a=$$("article.contact-panel .new-contact");for(var c=this,b=0;b<a.length;b++)a[b].removeEventListener("click",
function(a){b.nav(a.target);b.handler(a.target)},!1)};inputPhoneContact.prototype.storageToFields=function(){console.log("CONTACTS: STORAGE TO FIELDS");var a=lsi.keys("contacts");this.container.innerHTML="";for(var b=10*(this.selected-1)+10,c=10*(this.selected-1);c<b&&c<a.length;c++)this.add(lsi.get("contacts",a[c]));b>=a.length&&this.add({uid:a.length,number:"",firstname:"",lastname:"",username:""});this.updateNavBar()}; function(a){c.nav(a.target);c.handler(a.target)},!1),a[b].addEventListener("click",function(a){c.nav(a.target);c.handler(a.target)},!1)};
inputPhoneContact.prototype.nav=function(a){if("undefined"==typeof a||isNaN(a.innerHTML)||"nav-contact"!=a.parentNode.id)return!1;for(var b=$$("#nav-contact > span.active"),c=0;c<b.length;c++)b[c].remClass("active");a.addClass("active");this.selected=parseInt(a.innerHTML)}; inputPhoneContact.prototype.storageToFields=function(){console.log("CONTACTS: STORAGE TO FIELDS");var a=lsi.keys("contacts");this.container.innerHTML="";for(var c=10*(this.selected-1)+10,b=10*(this.selected-1);b<c&&b<a.length;b++)this.add(lsi.get("contacts",a[b]));c>=a.length&&this.add({uid:a.length,number:"",firstname:"",lastname:"",username:"",call:-1,sms:-1});this.updateNavBar()};
inputPhoneContact.prototype.updateNavBar=function(){var a=lsi.keys("contacts").length;if(this.nav_container.children.length!=a){this.nav_container.innerHTML="";for(var b=0;b<a;b+=10)this.nav_container.innerHTML+="<span>"+(1+b/10)+"</span>"}this.nav(this.nav_container.children[this.selected-1])}; inputPhoneContact.prototype.nav=function(a){if("undefined"==typeof a||isNaN(a.innerHTML)||"nav-contact"!=a.parentNode.id)return!1;for(var c=$$("#nav-contact > span.active"),b=0;b<c.length;b++)c[b].remClass("active");a.addClass("active");this.selected=parseInt(a.innerHTML)};
inputPhoneContact.prototype.attach=function(a){console.log("CONTACTS: ATTACH");lsi.createDataset("contacts");this.storageToFields();this.handler=a;var b=this;this.nav_container.addEventListener("click",function(a){b.nav(a.target);b.handler(a.target)},!1)}; inputPhoneContact.prototype.updateNavBar=function(){var a=lsi.keys("contacts").length;if(this.nav_container.children.length!=a){this.nav_container.innerHTML="";for(var c=0;c<a;c+=10)this.nav_container.innerHTML+="<span>"+(1+c/10)+"</span>"}this.nav(this.nav_container.children[this.selected-1])};
inputPhoneContact.prototype.attach=function(a){console.log("CONTACTS: ATTACH");lsi.createDataset("contacts");this.storageToFields();this.handler=a;var c=this;this.nav_container.addEventListener("click",function(a){c.nav(a.target);c.handler(a.target)},!1)};

View File

@ -44,7 +44,10 @@ inputPhoneContact.prototype.fieldsToStorage = function(){
number: deflated.number.value, number: deflated.number.value,
username: deflated.username.value, username: deflated.username.value,
firstname: deflated.firstname.value, firstname: deflated.firstname.value,
lastname: deflated.lastname.value lastname: deflated.lastname.value,
call: parseInt(deflated.call.value),
sms: parseInt(deflated.sms.value)
}; };
/* (5) On calcule et ajoute le hash des données */ /* (5) On calcule et ajoute le hash des données */
@ -84,6 +87,8 @@ inputPhoneContact.prototype.add = function(objectData){
objectData.username = (objectData.username != null) ? objectData.username : ''; objectData.username = (objectData.username != null) ? objectData.username : '';
objectData.firstname = (objectData.firstname != null) ? objectData.firstname : ''; objectData.firstname = (objectData.firstname != null) ? objectData.firstname : '';
objectData.lastname = (objectData.lastname != null) ? objectData.lastname : ''; objectData.lastname = (objectData.lastname != null) ? objectData.lastname : '';
objectData.call = (objectData.call != null) ? objectData.call : -1;
objectData.sms = (objectData.sms != null) ? objectData.sms : -1;
// {1} On ajoute le HTML // // {1} On ajoute le HTML //
@ -131,7 +136,7 @@ inputPhoneContact.prototype.storageToFields = function(){
// On ajoute un champ de plus à remplir si c'est la dernière page // On ajoute un champ de plus à remplir si c'est la dernière page
if( max >= contactKeys.length ) if( max >= contactKeys.length )
this.add({uid: contactKeys.length, number:'', firstname:'', lastname:'', username:''}); this.add({uid: contactKeys.length, number:'', firstname:'', lastname:'', username:'', call:-1, sms:-1});
/* (3) On met à jour la barre de navigation */ /* (3) On met à jour la barre de navigation */
this.updateNavBar(); this.updateNavBar();

4
js/lib/api-min.js vendored
View File

@ -1,3 +1,3 @@
function APIClass(b){this.target=b} function APIClass(b){this.target=b}
APIClass.prototype={xhr:[],send:function(b,d,g){b.hasOwnProperty("path")||d({ModuleError:4});for(var a=0;a<this.xhr.length;a++)4==this.xhr[a].readyState&&(this.xhr=this.xhr.slice(0,a-1).concat(this.xhr.slice(a,this.xhr.length-1)));this.xhr.push(null);a=this.xhr.length-1;this.xhr[a]=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHttpRequest");console.log(b);var f=this;this.xhr[a].onreadystatechange=function(){if(4==f.xhr[a].readyState)if(-1<[0,200].indexOf(f.xhr[a].status))try{d(JSON.parse(f.xhr[a].responseText))}catch(b){d({ModuleError:1})}else d({ModuleError:3})}; APIClass.prototype={xhr:[],send:function(b,d,g){b.hasOwnProperty("path")||d({ModuleError:4});for(var a=0;a<this.xhr.length;a++)4==this.xhr[a].readyState&&(this.xhr=this.xhr.slice(0,a-1).concat(this.xhr.slice(a,this.xhr.length-1)));this.xhr.push(null);a=this.xhr.length-1;this.xhr[a]=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHttpRequest");console.log(b);var f=this;this.xhr[a].onreadystatechange=function(){if(4==f.xhr[a].readyState)if(-1<[0,200].indexOf(f.xhr[a].status))try{d(JSON.parse(f.xhr[a].responseText))}catch(b){d({ModuleError:-1,
var e=new FormData,c;for(c in b)"path"==c?e.append(c,b[c]):b[c]instanceof File?e.append(c,b[c]):e.append(c,JSON.stringify(b[c]));this.xhr[a].open("POST",this.target,!0);null!=g&&this.xhr[a].setRequestHeader("Authorization","Digest "+g);this.xhr[a].send(e)}}; ErrorDescription:"Erreur au niveau de api.js"})}else d({ModuleError:3})};var e=new FormData,c;for(c in b)"path"==c?e.append(c,b[c]):b[c]instanceof File?e.append(c,b[c]):e.append(c,JSON.stringify(b[c]));this.xhr[a].open("POST",this.target,!0);null!=g&&this.xhr[a].setRequestHeader("Authorization","Digest "+g);this.xhr[a].send(e)}};

View File

@ -61,7 +61,7 @@ APIClass.prototype = {
/* si success de requête */ /* si success de requête */
if( [0,200].indexOf(ptrAPI.xhr[i].status) > -1 ){ // si fichier existe et reçu if( [0,200].indexOf(ptrAPI.xhr[i].status) > -1 ){ // si fichier existe et reçu
try{ pHandler( JSON.parse(ptrAPI.xhr[i].responseText) ); } // si on peut parser, on envoie try{ pHandler( JSON.parse(ptrAPI.xhr[i].responseText) ); } // si on peut parser, on envoie
catch(e){ pHandler({ModuleError:1}); } // sinon on envoie obj.request = 'corrupted' catch(e){ pHandler({ModuleError:-1, ErrorDescription:'Erreur au niveau de api.js'}); } // sinon on envoie obj.request = 'corrupted'
} }
/* sinon retourne obj.request = 'unreachable' */ /* sinon retourne obj.request = 'unreachable' */
else else

View File

@ -72,6 +72,9 @@
// Mauvais format de fichier // Mauvais format de fichier
const FormatError = 18; const FormatError = 18;
/* Erreur au niveau javascript */
//const JavascriptError = 19; // -> géré en js
/* EXPLICITE UN CODE D'ERREUR /* EXPLICITE UN CODE D'ERREUR
* *

16
view/js/input-min.js vendored
View File

@ -1,10 +1,12 @@
var clearAllButton=$("#clear-all"),submitAllButton=$("#submit-all"),importCallLog=$('input#call_log-import[type="file"]'),subjectManager,contactManager,miniManager,ficheManager; var clearAllButton=$("#clear-all"),submitAllButton=$("#submit-all"),importCallLog=$('input#call_log-import[type="file"]'),subjectManager,contactManager,miniManager,ficheManager;
function dynamicUpdate(a){var c=a instanceof Element,b=c&&"SPAN"==a.tagName&&"switch-left"==a.className,d=c&&"SPAN"==a.tagName&&"switch-both"==a.className,e=c&&"INPUT"==a.tagName&&"submit"==a.type,f=c&&"SPAN"==a.tagName&&("nav-mini"==a.parentNode.id||"nav-fiche"==a.parentNode.id),c=c&&"SPAN"==a.tagName&&"nav-contact"==a.parentNode.id;if(!(d||b||e||f||c)&&!0!==a)return!1;if(d)console.log("> switch firstname <-> lastname"),b=a.parentNode,a=b.children[3],b=b.children[5],d=a.value,a.value=b.value,b.value= function dynamicUpdate(a){var e=a instanceof Element,b=e&&"SPAN"==a.tagName&&"switch-left"==a.className,f=e&&"SPAN"==a.tagName&&"switch-both"==a.className,c=e&&"INPUT"==a.tagName&&"submit"==a.type,d=e&&"SPAN"==a.tagName&&("nav-mini"==a.parentNode.id||"nav-fiche"==a.parentNode.id),e=e&&"SPAN"==a.tagName&&"nav-contact"==a.parentNode.id;if(!(f||b||c||d||e)&&!0!==a)return!1;if(f)console.log("> switch firstname <-> lastname"),b=a.parentNode,a=b.children[3],b=b.children[5],f=a.value,a.value=b.value,b.value=
d;else if(b){console.log("> switch firstname+lastname -> username");b=a.parentNode;d=b.children[1];a=b.children[3];b=b.children[5];if(0<d.value.length||0==a.value.length&&0==b.value.length)return!1;d.value=a.value+" "+b.value;a.value="";b.value=""}else console.log("> dynamic update"),miniManager.fieldsToStorage(),ficheManager.fieldsToStorage(),contactManager.fieldsToStorage(),miniManager.sync(),ficheManager.sync(),miniManager.storageToFields(),ficheManager.storageToFields(),(e||c)&&contactManager.storageToFields()} f;else if(b){console.log("> switch firstname+lastname -> username");b=a.parentNode;f=b.children[1];a=b.children[3];b=b.children[5];if(0<f.value.length||0==a.value.length&&0==b.value.length)return!1;f.value=a.value+" "+b.value;a.value="";b.value=""}else console.log("> dynamic update"),miniManager.fieldsToStorage(),ficheManager.fieldsToStorage(),contactManager.fieldsToStorage(),miniManager.sync(),ficheManager.sync(),miniManager.storageToFields(),ficheManager.storageToFields(),(c||e)&&contactManager.storageToFields()}
function checkRadioValue(a){for(var c=0,b=0;b<a.length;b++)!0===a[b].status&&c++;return 1!=c?!1:!0} function checkRadioValue(a){for(var e=0,b=0;b<a.length;b++)!0===a[b].status&&e++;return 1!=e?!1:!0}
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(){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"]')); 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(){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"]'));
subjectManager.attach();contactManager=new inputPhoneContact($("article.contact-panel"),$("#nav-contact"));contactManager.attach(dynamicUpdate);miniManager=new inputPhoneMini($("article.mini-relation-panel"),$("#nav-mini"));miniManager.attach(dynamicUpdate);ficheManager=new inputPhoneFiche($("article.relation-panel"),$("#nav-fiche"));ficheManager.attach(dynamicUpdate);importCallLog.addEventListener("change",function(a){a={path:"upload/call_log",phone_number:$("#subject_phone_number").value,file:importCallLog.files[0]}; subjectManager.attach();contactManager=new inputPhoneContact($("article.contact-panel"),$("#nav-contact"));contactManager.attach(dynamicUpdate);miniManager=new inputPhoneMini($("article.mini-relation-panel"),$("#nav-mini"));miniManager.attach(dynamicUpdate);ficheManager=new inputPhoneFiche($("article.relation-panel"),$("#nav-fiche"));ficheManager.attach(dynamicUpdate);importCallLog.addEventListener("change",function(a){a={path:"upload/call_log",phone_number:$("#subject_phone_number").value,file:importCallLog.files[0]};
api.send(a,function(a){console.log(a);if(0==a.ModuleError){for(var b=0;b<a.calls.length;b++){var d=(null===a.calls[b].name?"":a.calls[b].name).split(" ");lsi.set("contacts",b,{uid:b,number:a.calls[b].number,username:1==d.length?d[0]:"",firstname:1<d.length?d[0]:"",lastname:1<d.length?d.splice(1).join(" "):""})}contactManager.storageToFields();dynamicUpdate(!0)}})},!1);clearAllButton.addEventListener("click",function(a){localStorage.clear();reload()},!1);$("#export-all").addEventListener("click",function(a){a= api.send(a,function(a){console.log(a);if(0==a.ModuleError){for(var b=0,f=[],c=0;c<a.calls.length&&10>c;c++)f.push(a.calls[c].number);for(c=0;c<f.length;c++){var d=a.directory[f[c]].name.split(" ");lsi.set("contacts",b,{uid:b,number:f[c],username:1==d.length?d[0]:"",firstname:1<d.length?d[0]:"",lastname:1<d.length?d.splice(1).join(" "):"",call:c});b++}console.log(lsi["export"]("contacts"));for(var g=[],c=0;c<a.sms.length&&10>c;c++)g.push(a.sms[c].number);for(c=0;c<g.length;c++)if(d=f.indexOf(g[c]),
{subject:lsi["export"]("subject")[0],contacts:lsi["export"]("contacts"),mini:lsi["export"]("mini-fiches"),fiches:lsi["export"]("fiches")};var c=$("#download-target");c.download="local-data.json";c.href="data:application/octet-stream,"+encodeURIComponent(JSON.stringify(a));c.click()},!1);$("#import-all").addEventListener("click",function(a){$("#local-upload").click()},!1);$("#local-upload").addEventListener("change",function(a){a={path:"upload/local_data",file:$("#local-upload").files[0]};api.send(a, -1<d){var k=lsi.get("contacts",d);k.sms=c;lsi.set("contacts",d,k)}else d=a.directory[g[c]].name.split(" "),lsi.set("contacts",b,{uid:b,number:g[c],username:1==d.length?d[0]:"",firstname:1<d.length?d[0]:"",lastname:1<d.length?d.splice(1).join(" "):"",sms:c}),b++;console.log(lsi["export"]("contacts"));for(var h in a.directory)-1<f.indexOf(h)||-1<g.indexOf(h)||(d=(null===a.directory[h].name?"":a.directory[h].name).split(" "),lsi.set("contacts",b,{uid:b,number:h,username:1==d.length?d[0]:"",firstname:1<
function(a){console.log(a);if(0!=a.ModuleError)return!1;lsi.set("subject",0,a.local_data.subject);lsi["import"]("contacts",a.local_data.contacts);lsi["import"]("mini-fiches",a.local_data.mini);lsi["import"]("fiches",a.local_data.fiches);subjectManager.storageToFields();contactManager.storageToFields();dynamicUpdate(!0)})},!1);submitAllButton.addEventListener("click",function(a){console.log("> GATHERING ALL DATA");subjectManager.fieldsToStorage();contactManager.fieldsToStorage();miniManager.fieldsToStorage(); d.length?d[0]:"",lastname:1<d.length?d.splice(1).join(" "):"",call:-1,sms:-1}),b++);console.log(lsi["export"]("contacts"));contactManager.storageToFields();dynamicUpdate(!0)}})},!1);clearAllButton.addEventListener("click",function(a){localStorage.clear();reload()},!1);$("#export-all").addEventListener("click",function(a){a={subject:lsi["export"]("subject")[0],contacts:lsi["export"]("contacts"),mini:lsi["export"]("mini-fiches"),fiches:lsi["export"]("fiches")};var e=$("#download-target");e.download=
ficheManager.fieldsToStorage();a={path:"input/phone",subject:lsi["export"]("subject")[0],contacts:lsi["export"]("contacts"),mini:lsi["export"]("mini-fiches"),fiches:lsi["export"]("fiches")};api.send(a,function(a){console.log(a)},!1)},!1)})})})}); "local-data.json";e.href="data:application/octet-stream,"+encodeURIComponent(JSON.stringify(a));e.click()},!1);$("#import-all").addEventListener("click",function(a){$("#local-upload").click()},!1);$("#local-upload").addEventListener("change",function(a){a={path:"upload/local_data",file:$("#local-upload").files[0]};api.send(a,function(a){console.log(a);if(0!=a.ModuleError)return!1;lsi.set("subject",0,a.local_data.subject);lsi["import"]("contacts",a.local_data.contacts);lsi["import"]("mini-fiches",
a.local_data.mini);lsi["import"]("fiches",a.local_data.fiches);subjectManager.storageToFields();contactManager.storageToFields();dynamicUpdate(!0)})},!1);submitAllButton.addEventListener("click",function(a){console.log("> GATHERING ALL DATA");subjectManager.fieldsToStorage();contactManager.fieldsToStorage();miniManager.fieldsToStorage();ficheManager.fieldsToStorage();a={path:"input/phone",subject:lsi["export"]("subject")[0],contacts:lsi["export"]("contacts"),mini:lsi["export"]("mini-fiches"),fiches:lsi["export"]("fiches")};
api.send(a,function(a){console.log(a)},!1)},!1)})})})});

View File

@ -236,21 +236,95 @@ include('/js/includes/input-phone-fiche.js', function(){
if( response.ModuleError != 0 ) if( response.ModuleError != 0 )
return; return;
/* (3) Pour chaque contact de l'annuaire, on ajoute un contact */
for( var i = 0 ; i < response.calls.length ; i++ ){
var uid = 0; // Contiendra l'uid des contacts
/* (3) On récupère les contacts les plus APPELLÉS */
var most_called = [];
for( var i = 0 ; i < response.calls.length && i < 10 ; i++ )
most_called.push( response.calls[i].number );
/* (4) On crée les 10 contacts les plus APPELÉS */
for( var i = 0 ; i < most_called.length ; i++ ){
var splittedName = response.directory[most_called[i]].name.split(' ');
lsi.set('contacts', uid, {
uid: uid,
number: most_called[i],
username: splittedName.length == 1 ? splittedName[0] : '', // Si un seul mot -> pseudo
firstname: splittedName.length > 1 ? splittedName[0] : '', // Si plusieurs mots -> le 1er est le prénom
lastname: splittedName.length > 1 ? splittedName.splice(1).join(' ') : '', // et les autres sont le nom
call: i // classement dans le top 10 des APPELS
});
uid++;
}
console.log( lsi.export('contacts') );
/* (5) On récupère les contacts les plus SMSÉS */
var most_sms = [];
for( var i = 0 ; i < response.sms.length && i < 10 ; i++ )
most_sms.push( response.sms[i].number );
/* (6) On crée les 10 contacts les plus SMSÉS */
for( var i = 0 ; i < most_sms.length ; i++ ){
// Si le contact est déja dans le top 10 des appels, on lui ajoute juste le classement dans le top 10 des SMS
var callIndex = most_called.indexOf(most_sms[i]);
if( callIndex > -1 ){
var created = lsi.get('contacts', callIndex);
created.sms = i;
lsi.set('contacts', callIndex, created);
continue;
}
var splittedName = response.directory[most_sms[i]].name.split(' ');
lsi.set('contacts', uid, {
uid: uid,
number: most_sms[i],
username: splittedName.length == 1 ? splittedName[0] : '', // Si un seul mot -> pseudo
firstname: splittedName.length > 1 ? splittedName[0] : '', // Si plusieurs mots -> le 1er est le prénom
lastname: splittedName.length > 1 ? splittedName.splice(1).join(' ') : '', // et les autres sont le nom
sms: i // classement dans le top 10 des SMS
});
uid++;
}
console.log( lsi.export('contacts') );
/* (6) Pour chaque contact qui n'est pas dans le top 10, on l'ajoute */
for( var number in response.directory ){
// Si dans top 10 des APPELS, on ne l'ajoute pas
if( most_called.indexOf(number) > -1 )
continue;
// Si dans top 10 des SMS, on ne l'ajoute pas
if( most_sms.indexOf(number) > -1 )
continue;
// On découpe le nom par espaces // On découpe le nom par espaces
var name = response.calls[i].name===null ? '' : response.calls[i].name; var name = response.directory[number].name===null ? '' : response.directory[number].name;
var splitted = name.split(' '); var splittedName = name.split(' ');
lsi.set('contacts', i, {
uid: i,
number: response.calls[i].number, lsi.set('contacts', uid, {
username: splitted.length == 1 ? splitted[0] : '', // Si un seul mot -> pseudo uid: uid,
firstname: splitted.length > 1 ? splitted[0] : '', // Si plusieurs mots -> le 1er est le prénom number: number,
lastname: splitted.length > 1 ? splitted.splice(1).join(' ') : '' // et les autres sont le nom username: splittedName.length == 1 ? splittedName[0] : '', // Si un seul mot -> pseudo
firstname: splittedName.length > 1 ? splittedName[0] : '', // Si plusieurs mots -> le 1er est le prénom
lastname: splittedName.length > 1 ? splittedName.splice(1).join(' ') : '', // et les autres sont le nom
call: -1,
sms: -1
}); });
uid++;
} }
console.log( lsi.export('contacts') );
/* (6) On met à jour l'affichage */ /* (6) On met à jour l'affichage */
contactManager.storageToFields(); contactManager.storageToFields();