You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2013/12/05 01:56:54 UTC

[18/28] git commit: createMozillaFromCordova and vice versa are used to translate contact objects from one API to another.

createMozillaFromCordova and vice versa are used to translate contact objects
from one API to another.

phoneNumbers are now translated with more info


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/f10f6661
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/f10f6661
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/f10f6661

Branch: refs/heads/master
Commit: f10f66616708ea299bc74eb79d3fdc7e8e4965e9
Parents: 144d69a
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Mon Nov 18 14:14:40 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:28:35 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 186 +++++++++++++++++++++++-------------
 1 file changed, 118 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/f10f6661/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index bc5d871..0f0f0c9 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -26,102 +26,152 @@
 // FxOS contact definition:
 // https://developer.mozilla.org/en-US/docs/Web/API/mozContact
 
-function saveContacts(successCB, errorCB, contacts) {
-    // success and/or fail will be called every time a contact is saved
 
-    // a closure which is holding the object to be returned to sucessCB
-    function makeSuccess(contact, moz) {
-        return function(result) {
-            // TODO modify contact so it will contain the link to moz
-            contact.id = moz.id;
-            // call callback
-            successCB(contact);
-        }
-    }
-        
+var Contact = require('./Contact');
+var ContactField = require('./ContactField');
+
+function createMozillaFromCordova(contact) {
     function exportContactFieldArray(contactFieldArray, key) {
         if (!key) {
             key = 'value';
         }                 
-        
         var arr = [];
-        
         for (var i in contactFieldArray) {
             arr.push(contactFieldArray[i][key]);
         };                                       
-        
         return arr;
     }              
-    
-    function exportAddress (addresses) {
+
+    function exportAddress(addresses) {
         // TODO: check moz address format
         var arr = [];
         
         for (var i in addresses) {
             var addr = {};
-        
             for (var key in addresses[i]) {
                 addr[key] = addresses[i][key];    
             } 
-            
             arr.push(addr);
-            
         }                                 
-        
         return arr;
     } 
-    var i=0;
-    var contact;
-    while(contact = contacts[i++]){
-        // prepare mozContact object
-        var moz = new mozContact();
-        if (contact.id) {
-            moz.id = contact.id;
-        }
-        // building name
-        var nameArray = [];
-        var fields = ['honorificPrefix', 'familyName', 'givenName', 'middleName', 'nickname'];
-        var j = 0, field; while(field = fields[j++]) {
-            if (contact.name[field]) {
-                nameArray.push(contact.name[field]);
-            }
-        }
-        moz.name = nameArray.join(' ');
-        // adding simple fields [contactField, eventualMozContactField]
-        var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
-            ['honorificSuffix'], ['nickname'], ['birthday', 'bday'], ['note']];
-        j = 0; while(field = simpleFields[j++]) {
-          if (contact.name[field[0]]) {
-            moz[field[1] || field[0]] = contact.name[field[0]];
-          }
-        }
-        if (contact.emails) {
-            moz.email = exportContactFieldArray(contact.emails);
-        }
-        if (contact.categories) {
-            moz.category = exportContactFieldArray(contact.categories);
+
+    function exportPhoneNumbers(phoneNumbers) {
+        var mozNumbers = [];
+        for (var i in phoneNumbers) {
+            var number = phoneNumbers[i];
+            mozNumbers.push({
+                type: number.type,
+                value: number.value,
+                pref: number.pref
+            });
         }
-        if (contact.addresses) {
-            moz.adr = exportAddress(contact.addresses);
+        return mozNumbers;
+    }
+
+    // prepare mozContact object
+    var moz = new mozContact();
+    if (contact.id) {
+        moz.id = contact.id;
+    }
+    // building name
+    var nameArray = [];
+    var fields = ['honorificPrefix', 'familyName', 'givenName', 'middleName', 'nickname'];
+    var j = 0, field; while(field = fields[j++]) {
+        if (contact.name[field]) {
+            nameArray.push(contact.name[field]);
         }
-        if (contact.phoneNumbers) {
-            moz.tel = exportContactFieldArray(contact.phoneNumbers);
+    }
+    moz.name = nameArray.join(' ');
+    // adding simple fields [contactField, eventualMozContactField]
+    var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
+        ['honorificSuffix'], ['nickname'], ['birthday', 'bday'], ['note']];
+    j = 0; while(field = simpleFields[j++]) {
+      if (contact.name[field[0]]) {
+        moz[field[1] || field[0]] = contact.name[field[0]];
+      }
+    }
+    if (contact.emails) {
+        moz.email = exportContactFieldArray(contact.emails);
+    }
+    if (contact.categories) {
+        moz.category = exportContactFieldArray(contact.categories);
+    }
+    if (contact.addresses) {
+        moz.adr = exportAddress(contact.addresses);
+    }
+    if (contact.phoneNumbers) {
+        moz.tel = exportPhoneNumbers(contact.phoneNumbers);
+    }
+    if (contact.organizations) {
+        moz.org = exportContactFieldArray(contact.organizations, 'name');
+        moz.jobTitle = exportContactFieldArray(contact.organizations, 'title');
+    }
+    /*  Find out how to translate these parameters
+        // photo: Blob
+        // url: Array with metadata (?)
+        // impp: exportIM(contact.ims), TODO: find the moz impp definition
+        // anniversary
+        // sex
+        // genderIdentity
+        // key
+    */
+    return moz;
+}
+
+function createCordovaFromMozilla(moz) {
+    function exportPhoneNumbers(mozNumbers) {
+        var phoneNumbers = [];
+        for (var i in mozNumbers) {
+            var number = mozNumbers[i];
+            phoneNumbers.push(
+                new ContactField( number.type, number.value, number.pref));
         }
-        if (contact.organizations) {
-            moz.org = exportContactFieldArray(contact.organizations, 'name');
-            moz.jobTitle = exportContactFieldArray(contact.organizations, 'title');
+        return phoneNumbers;
+    }
+
+    var contact = new Contact();
+
+    if (moz.id) {
+        contact.id = moz.id;
+    }
+    // adding simple fields [contactField, eventualCordovaContactField]
+    var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
+        ['honorificSuffix'], ['nickname'], ['bday', 'birthday'], ['note']];
+    j = 0; while(field = simpleFields[j++]) {
+      if (moz.name[field[0]]) {
+        contact[field[1] || field[0]] = moz.name[field[0]];
+      }
+    }
+    // emails
+    // categories
+    // addresses
+    if (moz.tel) {
+        contact.phoneNumbers = exportPhoneNumbers(moz.tel);
+    }
+    // organizations
+    return contact;
+}
+
+
+function saveContacts(successCB, errorCB, contacts) {
+    // a closure which is holding the right moz contact
+    function makeSaveSuccessCB(moz) {
+        return function(result) {
+            // create contact from FXOS contact (might be different than
+            // the original one due to differences in API)
+            var contact = createCordovaFromMozilla(moz);
+            // call callback
+            successCB(contact);
         }
-        /*  Find out how to translate these parameters
-            // photo: Blob
-            // url: Array with metadata (?)
-            // impp: exportIM(contact.ims), TODO: find the moz impp definition
-            // anniversary
-            // sex
-            // genderIdentity
-            // key
-        */
+    }
+    var i=0;
+    var contact;
+    while(contact = contacts[i++]){
+        var moz = createMozillaFromCordova(contact);
         var request = navigator.mozContacts.save(moz);
-        request.onsuccess = makeSuccess(contact, moz);
+        // success and/or fail will be called every time a contact is saved
+        request.onsuccess = makeSaveSuccessCB(moz);
         request.onerror = errorCB;                
     }
 }