You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by he...@apache.org on 2013/12/03 22:32:44 UTC

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

Updated Branches:
  refs/heads/dev c070a4fe6 -> 72e97f307


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/dev
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;                
     }
 }   


[11/11] git commit: FxOS name fields are arrays hackedSearch refactored search based on find commented out

Posted by he...@apache.org.
FxOS name fields are arrays
hackedSearch refactored
search based on find commented out


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/334080af
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/334080af
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/334080af

Branch: refs/heads/dev
Commit: 334080aff9f0b04a615d4c4de47a615acbd8f7b0
Parents: c2fb0d0
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Thu Nov 28 14:28:53 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:32:27 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 137 ++++++++++++++++++++----------------
 1 file changed, 77 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/334080af/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index 10f0288..afd43a4 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -77,9 +77,13 @@ function createMozillaFromCordova(contact) {
         moz.id = contact.id;
     }
     // adding simple fields [contactField, eventualMozContactField]
-    var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
-        ['honorificSuffix'], ['nickname'], ['birthday', 'bday'], ['note'],
-        ['displayName', 'name']];
+    var arrayFields = [['givenName'], ['familyName'], ['displayName', 'name']];
+    var simpleFields = [['honorificPrefix'], ['honorificSuffix'], ['nickname'], ['birthday', 'bday'], ['note']];
+    j = 0; while(field = arrayFields[j++]) {
+      if (contact.name[field[0]]) {
+        moz[field[1] || field[0]] = contact.name[field[0]].split(' ');
+      }
+    }
     j = 0; while(field = simpleFields[j++]) {
       if (contact.name[field[0]]) {
         moz[field[1] || field[0]] = contact.name[field[0]];
@@ -129,11 +133,14 @@ function createCordovaFromMozilla(moz) {
     if (moz.id) {
         contact.id = moz.id;
     }
-    // adding simple fields [contactField, eventualCordovaContactField]
-    var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
-        ['honorificSuffix'], ['nickname'], ['bday', 'birthday'], ['note'],
-        ['name', 'displayName']];
+    var arrayFields = [['givenName'], ['familyName'], ['name', 'displayName']];
+    var simpleFields = [['honorificPrefix'], ['honorificSuffix'], ['nickname'], ['bday', 'birthday'], ['note']];
     var name = new ContactName();
+    var j = 0; while(field = arrayFields[j++]) {
+      if (moz[field[0]]) {
+        name[field[1] || field[0]] = moz[field[0]].join(' ');
+      }
+    }
     j = 0; while(field = simpleFields[j++]) {
       if (moz[field[0]]) {
         name[field[1] || field[0]] = moz[field[0]];
@@ -190,52 +197,56 @@ function remove(successCB, errorCB, ids) {
 var mozContactSearchFields = ['name', 'givenName', 'additionalName', 
     'familyName', 'nickname', 'email', 'tel', 'jobTitle', 'note'];
 
-function search(successCB, errorCB, params) {
-    var options = params[1] || {}; 
-    var filter = [];
-    // filter out inallowed fields
-    for (var i=0; i < params[0].length; i++) {
-        if (mozContactSearchFields.indexOf([params[0][i]])) {
-            filter.push(params[0][i]);
-        } else if (params[0][i] == 'displayName') {
-            filter.push('name');
-        } else {
-            console.log('FXOS ContactProxy: inallowed field passed to search filtered out: ' + params[0][i]);
-        }
-    }
-
-    var request;
-    // TODO find out how to handle searching by numbers
-    // filterOp: The filter comparison operator to use. Possible values are 
-    //           equals, startsWith, and match, the latter being specific 
-    //           to telephone numbers.
-    var mozOptions = {filterBy: filter, filterOp: 'startsWith'};
-    if (!options.multiple) {
-        mozOptions.filterLimit = 1;
-    }
-    if (!options.filter) {
-        // this is returning 0 contacts
-        request = navigator.mozContacts.getAll({});
-    } else {
-        // XXX This is searching for regardless of the filterValue !!!
-        mozOptions.filterValue = options.filter;
-        console.log('mozoptions: filterBy: ' + mozOptions.filterBy.join(' ') + '; fiterValue: ' + mozOptions.filterValue);
-        request = navigator.mozContacts.find(mozOptions);
-    }
-    request.onsuccess = function() {
-        var contacts = [];
-        var mozContacts = request.result;
-        for (var i=0; i < mozContacts.length; i++) {
-            contacts.push(createCordovaFromMozilla(mozContacts[i]));
-        }
-        successCB(contacts);
-    };
-    request.onerror = errorCB;
-}
+// function search(successCB, errorCB, params) {
+//     var options = params[1] || {}; 
+//     var filter = [];
+//     // filter out inallowed fields
+//     for (var i=0; i < params[0].length; i++) {
+//         if (mozContactSearchFields.indexOf([params[0][i]])) {
+//             filter.push(params[0][i]);
+//         } else if (params[0][i] == 'displayName') {
+//             filter.push('name');
+//         } else {
+//             console.log('FXOS ContactProxy: inallowed field passed to search filtered out: ' + params[0][i]);
+//         }
+//     }
+// 
+//     var request;
+//     // TODO find out how to handle searching by numbers
+//     // filterOp: The filter comparison operator to use. Possible values are 
+//     //           equals, startsWith, and match, the latter being specific 
+//     //           to telephone numbers.
+//     var mozOptions = {filterBy: filter, filterOp: 'startsWith'};
+//     if (!options.multiple) {
+//         mozOptions.filterLimit = 1;
+//     }
+//     if (!options.filter) {
+//         // this is returning 0 contacts
+//         request = navigator.mozContacts.getAll({});
+//     } else {
+//         // XXX This is searching for regardless of the filterValue !!!
+//         mozOptions.filterValue = options.filter;
+//         console.log('mozoptions: filterBy: ' + mozOptions.filterBy.join(' ') + '; fiterValue: ' + mozOptions.filterValue);
+//         request = navigator.mozContacts.find(mozOptions);
+//     }
+//     request.onsuccess = function() {
+//         var contacts = [];
+//         var mozContacts = request.result;
+//         for (var i=0; i < mozContacts.length; i++) {
+//             contacts.push(createCordovaFromMozilla(mozContacts[i]));
+//         }
+//         successCB(contacts);
+//     };
+//     request.onerror = errorCB;
+// }
 
 
-/* navigator.mozContacts.find has issues - using getAll */
+/* navigator.mozContacts.find has issues - using getAll 
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=941008
+ */
 function hackedSearch(successCB, errorCB, params) {
+    // [contactField, eventualMozContactField]
+    var arrayFields = ['givenName', 'familyName', 'name'];
     var options = params[1] || {}; 
     var filter = [];
     // filter out inallowed fields
@@ -250,18 +261,24 @@ function hackedSearch(successCB, errorCB, params) {
     }
     var getall = navigator.mozContacts.getAll({});
     var contacts = [];
-    
+
+    function isValid(mozContact) {
+        if (!options.filter) {
+            // get all
+            return true;
+        }
+        for (var j=0; j < filter.length; j++) {
+            var field = filter[0];
+            var value = (arrayFields.indexOf(field) >= 0) ? mozContact[field].join(' ') : mozContact[field];
+            if (value.indexOf(options.filter) >= 0) {
+                return true;
+            }
+        }
+    }
     getall.onsuccess = function() {
         if (getall.result) {
-            var mozContact = getall.result;
-            var valid = false;
-            for (var j=0; j < filter.length; j++) {
-                if (mozContact[filter[0]].indexOf(options.filter)) {
-                    valid = true;
-                }
-            }
-            if (valid) {
-                contacts.push(createCordovaFromMozilla(mozContact));
+            if (isValid(getall.result)) {
+                contacts.push(createCordovaFromMozilla(getall.result));
             }
             
             getall.continue();


[05/11] git commit: save is working

Posted by he...@apache.org.
save is working


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/0f7ff419
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/0f7ff419
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/0f7ff419

Branch: refs/heads/dev
Commit: 0f7ff4195f4a1a29a3e4cc9bb3d8d5945d3cff0c
Parents: 35def21
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Wed Nov 13 19:28:47 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:28:35 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 61 +++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/0f7ff419/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index 7029541..c2ac0fd 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -27,10 +27,13 @@
 // https://developer.mozilla.org/en-US/docs/Web/API/mozContact
 function saveContacts(successCB, errorCB, contacts) {
     // success and fail will be called every time a contact is saved
-    function success(result) {
-        // TODO: this will need to amend the result
-        console.log('SUCCESS from moz');
-        successCB(result);
+    // 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
+            // call callback
+            successCB(contact);
+        }
     }
     function error(e) {
         console.log('BOO from moz');
@@ -75,36 +78,42 @@ function saveContacts(successCB, errorCB, contacts) {
         var request;
         // prepare mozContact object
         var translatedContact = {};
+        // building name
         var nameArray = [];
-        var j = 0;
-        var field;
         var fields = ['honorificPrefix', 'familyName', 'givenName', 'middleName', 'nickname'];
-        while(field = fields[j++]) {
+        var j = 0, field; while(field = fields[j++]) {
             if (contact.name[field]) {
                 nameArray.push(contact.name[field]);
             }
         }
         translatedContact.name = nameArray.join(' ');
-        if (contact.name.honorificPrefix) {
-            translatedContact.honorificPrefix = contact.name.honorificPrefix;
+        // 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]]) {
+            translatedContact[field[1] || field[0]] = contact.name[field[0]];
+          }
+        }
+        if (contact.emails) {
+            translatedContact.email = exportContactFieldArray(contact.emails);
+        }
+        if (contact.categories) {
+            translatedContact.category = exportContactFieldArray(contact.categories);
+        }
+        if (contact.addresses) {
+            translatedContact.adr = exportAddress(contact.addresses);
         }
-        translatedContact.familyName = contact.name.familyName;
-        /*
-            honorificPrefix: [contact.name.honorificPrefix],
-            givenName: [contact.name.givenName],
-            familyName: [contact.name.familyName],
-            honorificSuffix: [contact.name.honorificSuffix], 
-            nickname: [contact.nickname],
-            email: exportContactFieldArray(contact.emails),
+        if (contact.phoneNumbers) {
+            translatedContact.tel = exportContactFieldArray(contact.phoneNumbers);
+        }
+        if (contact.organizations) {
+            translatedContact.org = exportContactFieldArray(contact.organizations, 'name');
+            translatedContact.jobTitle = exportContactFieldArray(contact.organizations, 'title');
+        }
+        /* 
             // photo: Blob
             // url: Array with metadata (?)
-            category: exportContactFieldArray(contact.categories),
-            adr: exportAddress(contact.addresses),
-            tel: exportContactFieldArray(contact.phoneNumbers),
-            org: exportContactFieldArray(contact.organizations, 'name'),
-            jobTitle: exportContactFieldArray(contact.organizations, 'title'),
-            bday: contact.birthday,
-            note: contact.note,
             // impp: exportIM(contact.ims), TODO: find the moz impp definition
             // anniversary
             // sex
@@ -114,10 +123,8 @@ function saveContacts(successCB, errorCB, contacts) {
         */
         // TODO: find a way to link existing mozContact and Contact by ID
         var moz = new mozContact(translatedContact);
-        // XXX: moz.name is undefined
-        
         request = navigator.mozContacts.save(moz);
-        request.onsuccess = success;
+        request.onsuccess = makeSuccess(contact, moz);
         request.onerror = error;                
     }
 }   


[03/11] git commit: attempt to save is failing trying to limit the translated contact fields to name and familyName, but still failing

Posted by he...@apache.org.
attempt to save is failing
trying to limit the translated contact fields to name and familyName,
but still failing


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/35def21d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/35def21d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/35def21d

Branch: refs/heads/dev
Commit: 35def21d258710b8275df88077e4c0b1b290a9d3
Parents: 5833bc0
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Thu Nov 7 16:27:04 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:28:35 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 108 +++++++++++++++++++++---------------
 1 file changed, 64 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/35def21d/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index 550ddf7..7029541 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -27,53 +27,69 @@
 // https://developer.mozilla.org/en-US/docs/Web/API/mozContact
 function saveContacts(successCB, errorCB, contacts) {
     // success and fail will be called every time a contact is saved
-    for (var contact in contacts) {
-        var request;
-
-        function success(result) {
-            // TODO: this will need to amend the result
-            successCB(result);
-        }
-            
-        function exportContactFieldArray(contactFieldArray, key) {
-            if (!key) {
-                key = 'value';
-            }                 
-            
-            var arr = [];
-            
-            for (var i in contactFieldArray) {
-                arr.push(contactFieldArray[i][key]);
-            };                                       
-            
-            return arr;
-        }              
+    function success(result) {
+        // TODO: this will need to amend the result
+        console.log('SUCCESS from moz');
+        successCB(result);
+    }
+    function error(e) {
+        console.log('BOO from moz');
+        errorCB(e);
+    }
         
-        function exportAddress (addresses) {
-            // TODO: check moz address format
-            var arr = [];
-            
-            for (var i in addresses) {
-                var addr = {};
+    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) {
+        // 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];    
+            } 
             
-                for (var key in addresses[i]) {
-                    addr[key] = addresses[i][key];    
-                } 
-                
-                arr.push(addr);
-                
-            }                                 
+            arr.push(addr);
             
-            return arr;
-        } 
+        }                                 
+        
+        return arr;
+    } 
+    var i=0;
+    var contact;
 
+    while(contact = contacts[i++]){
+        var request;
         // prepare mozContact object
-        // TODO: find a way to link existing mozContact and Contact by ID
-        var moz = new mozContact({
-            name: [contact.name.familyName, 
-                   contact.name.givenName, 
-                   contact.name.middleName, 
-                   contact.name.nickname],
+        var translatedContact = {};
+        var nameArray = [];
+        var j = 0;
+        var field;
+        var fields = ['honorificPrefix', 'familyName', 'givenName', 'middleName', 'nickname'];
+        while(field = fields[j++]) {
+            if (contact.name[field]) {
+                nameArray.push(contact.name[field]);
+            }
+        }
+        translatedContact.name = nameArray.join(' ');
+        if (contact.name.honorificPrefix) {
+            translatedContact.honorificPrefix = contact.name.honorificPrefix;
+        }
+        translatedContact.familyName = contact.name.familyName;
+        /*
             honorificPrefix: [contact.name.honorificPrefix],
             givenName: [contact.name.givenName],
             familyName: [contact.name.familyName],
@@ -94,11 +110,15 @@ function saveContacts(successCB, errorCB, contacts) {
             // sex
             // genderIdentity
             // key
-        });
+        }
+        */
+        // TODO: find a way to link existing mozContact and Contact by ID
+        var moz = new mozContact(translatedContact);
+        // XXX: moz.name is undefined
         
         request = navigator.mozContacts.save(moz);
         request.onsuccess = success;
-        request.onerror = errorCB;                
+        request.onerror = error;                
     }
 }   
 


[09/11] git commit: CB-3035 Fix issue with windows new line char \n\r

Posted by he...@apache.org.
CB-3035 Fix issue with windows new line char \n\r


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/72e97f30
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/72e97f30
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/72e97f30

Branch: refs/heads/dev
Commit: 72e97f3078c1dc27aadb0d40efff192dc399bd66
Parents: 148774d
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Mon Dec 2 14:02:15 2013 -0800
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:32:27 2013 -0800

----------------------------------------------------------------------
 src/wp/Contacts.cs | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/72e97f30/src/wp/Contacts.cs
----------------------------------------------------------------------
diff --git a/src/wp/Contacts.cs b/src/wp/Contacts.cs
index 8d35011..50e4e08 100644
--- a/src/wp/Contacts.cs
+++ b/src/wp/Contacts.cs
@@ -677,17 +677,9 @@ namespace WPCordovaClassLib.Cordova.Commands
 
             //Debug.WriteLine("jsonContact = " + jsonContact);
             // JSON requires new line characters be escaped
-            return "{" + jsonContact + "}";
-        }
 
-        private static string EscapeJson(string str)
-        {
-            if (String.IsNullOrEmpty(str))
-            {
-                return str;
-            }
+            return "{" + jsonContact.Replace("\n", "\\n").Replace("\r","") + "}";
 
-            return str.Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t").Replace("\"", "\\\"").Replace("&", "\\&");
         }
     }
 }


[10/11] git commit: wrong example given

Posted by he...@apache.org.
wrong example given


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/148774d6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/148774d6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/148774d6

Branch: refs/heads/dev
Commit: 148774d66a8f22db817e3f70effaf92de651eb9b
Parents: 3a65011
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Thu Nov 28 14:50:32 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:32:27 2013 -0800

----------------------------------------------------------------------
 README.md | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/148774d6/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 2c42361..f536c6e 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,4 @@ If you are not using the Cordova Command-line Interface, follow [Using Plugman t
 
 Edit manifest.webapp and add permissions field as described in [Manifest Docs](https://developer.mozilla.org/en-US/Apps/Developing/Manifest#permissions).
 There is also a need to change the webapp type to "privileged"  - [Manifest Docs](https://developer.mozilla.org/en-US/Apps/Developing/Manifest#type).
-All privileged apps enforce [Content Security Policy](https://developer.mozilla.org/en-US/Apps/CSP) which forbids inline script. Initialize your application in another way (i.e. start it on document load in index.js)
-
-    document.onload = app.initialize;
-
+All privileged apps enforce [Content Security Policy](https://developer.mozilla.org/en-US/Apps/CSP) which forbids inline script. Initialize your application in another way.


[07/11] git commit: search hacked via getAll

Posted by he...@apache.org.
search hacked via getAll


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/c2fb0d0d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/c2fb0d0d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/c2fb0d0d

Branch: refs/heads/dev
Commit: c2fb0d0d1a9950e5512d6542f3382a2a06c3955d
Parents: 08e6dab
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Wed Nov 27 15:46:26 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:31:28 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/c2fb0d0d/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index cc8f9c1..10f0288 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -249,11 +249,11 @@ function hackedSearch(successCB, errorCB, params) {
         }
     }
     var getall = navigator.mozContacts.getAll({});
+    var contacts = [];
+    
     getall.onsuccess = function() {
-        var contacts = [];
-        var allContacts = getall.result;
-        for (var i=0; i < allContacts.length; i++) {
-            var mozContact = allContacts[i];
+        if (getall.result) {
+            var mozContact = getall.result;
             var valid = false;
             for (var j=0; j < filter.length; j++) {
                 if (mozContact[filter[0]].indexOf(options.filter)) {
@@ -263,8 +263,13 @@ function hackedSearch(successCB, errorCB, params) {
             if (valid) {
                 contacts.push(createCordovaFromMozilla(mozContact));
             }
+            
+            getall.continue();
+            
+        } else {
+            successCB(contacts);
         }
-        successCB(contacts);
+
     };
     getall.onerror = errorCB;
 }


[06/11] git commit: search added - no idea if this is working

Posted by he...@apache.org.
search added - no idea if this is working


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/08e6dabf
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/08e6dabf
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/08e6dabf

Branch: refs/heads/dev
Commit: 08e6dabf53a29247e61447091d5f721ec47c2f04
Parents: f10f666
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Tue Nov 26 16:36:36 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:28:36 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 121 ++++++++++++++++++++++++++++++------
 1 file changed, 101 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/08e6dabf/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index 0f0f0c9..cc8f9c1 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -29,6 +29,8 @@
 
 var Contact = require('./Contact');
 var ContactField = require('./ContactField');
+var ContactName = require('./ContactName');
+
 
 function createMozillaFromCordova(contact) {
     function exportContactFieldArray(contactFieldArray, key) {
@@ -36,7 +38,7 @@ function createMozillaFromCordova(contact) {
             key = 'value';
         }                 
         var arr = [];
-        for (var i in contactFieldArray) {
+        for (var i=0; i < contactFieldArray.length; i++) {
             arr.push(contactFieldArray[i][key]);
         };                                       
         return arr;
@@ -46,7 +48,7 @@ function createMozillaFromCordova(contact) {
         // TODO: check moz address format
         var arr = [];
         
-        for (var i in addresses) {
+        for (var i=0; i < addresses.length; i++) {
             var addr = {};
             for (var key in addresses[i]) {
                 addr[key] = addresses[i][key];    
@@ -58,7 +60,7 @@ function createMozillaFromCordova(contact) {
 
     function exportPhoneNumbers(phoneNumbers) {
         var mozNumbers = [];
-        for (var i in phoneNumbers) {
+        for (var i=0; i < phoneNumbers.length; i++) {
             var number = phoneNumbers[i];
             mozNumbers.push({
                 type: number.type,
@@ -74,18 +76,10 @@ function createMozillaFromCordova(contact) {
     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']];
+        ['honorificSuffix'], ['nickname'], ['birthday', 'bday'], ['note'],
+        ['displayName', 'name']];
     j = 0; while(field = simpleFields[j++]) {
       if (contact.name[field[0]]) {
         moz[field[1] || field[0]] = contact.name[field[0]];
@@ -122,7 +116,7 @@ function createMozillaFromCordova(contact) {
 function createCordovaFromMozilla(moz) {
     function exportPhoneNumbers(mozNumbers) {
         var phoneNumbers = [];
-        for (var i in mozNumbers) {
+        for (var i=0; i < mozNumbers.length; i++) {
             var number = mozNumbers[i];
             phoneNumbers.push(
                 new ContactField( number.type, number.value, number.pref));
@@ -137,12 +131,15 @@ function createCordovaFromMozilla(moz) {
     }
     // adding simple fields [contactField, eventualCordovaContactField]
     var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
-        ['honorificSuffix'], ['nickname'], ['bday', 'birthday'], ['note']];
+        ['honorificSuffix'], ['nickname'], ['bday', 'birthday'], ['note'],
+        ['name', 'displayName']];
+    var name = new ContactName();
     j = 0; while(field = simpleFields[j++]) {
-      if (moz.name[field[0]]) {
-        contact[field[1] || field[0]] = moz.name[field[0]];
+      if (moz[field[0]]) {
+        name[field[1] || field[0]] = moz[field[0]];
       }
     }
+    contact.name = name;
     // emails
     // categories
     // addresses
@@ -176,22 +173,106 @@ function saveContacts(successCB, errorCB, contacts) {
     }
 }   
 
+
 function remove(successCB, errorCB, ids) {
     var i=0;
     var id;
-    while(id = ids[i++]){
+    for (var i=0; i < ids.length; i++){
         var moz = new mozContact();
-        moz.id = id;
+        moz.id = ids[i];
         var request = navigator.mozContacts.remove(moz);
         request.onsuccess = successCB;
         request.onerror = errorCB;
     }
 }
 
+
+var mozContactSearchFields = ['name', 'givenName', 'additionalName', 
+    'familyName', 'nickname', 'email', 'tel', 'jobTitle', 'note'];
+
+function search(successCB, errorCB, params) {
+    var options = params[1] || {}; 
+    var filter = [];
+    // filter out inallowed fields
+    for (var i=0; i < params[0].length; i++) {
+        if (mozContactSearchFields.indexOf([params[0][i]])) {
+            filter.push(params[0][i]);
+        } else if (params[0][i] == 'displayName') {
+            filter.push('name');
+        } else {
+            console.log('FXOS ContactProxy: inallowed field passed to search filtered out: ' + params[0][i]);
+        }
+    }
+
+    var request;
+    // TODO find out how to handle searching by numbers
+    // filterOp: The filter comparison operator to use. Possible values are 
+    //           equals, startsWith, and match, the latter being specific 
+    //           to telephone numbers.
+    var mozOptions = {filterBy: filter, filterOp: 'startsWith'};
+    if (!options.multiple) {
+        mozOptions.filterLimit = 1;
+    }
+    if (!options.filter) {
+        // this is returning 0 contacts
+        request = navigator.mozContacts.getAll({});
+    } else {
+        // XXX This is searching for regardless of the filterValue !!!
+        mozOptions.filterValue = options.filter;
+        console.log('mozoptions: filterBy: ' + mozOptions.filterBy.join(' ') + '; fiterValue: ' + mozOptions.filterValue);
+        request = navigator.mozContacts.find(mozOptions);
+    }
+    request.onsuccess = function() {
+        var contacts = [];
+        var mozContacts = request.result;
+        for (var i=0; i < mozContacts.length; i++) {
+            contacts.push(createCordovaFromMozilla(mozContacts[i]));
+        }
+        successCB(contacts);
+    };
+    request.onerror = errorCB;
+}
+
+
+/* navigator.mozContacts.find has issues - using getAll */
+function hackedSearch(successCB, errorCB, params) {
+    var options = params[1] || {}; 
+    var filter = [];
+    // filter out inallowed fields
+    for (var i=0; i < params[0].length; i++) {
+        if (mozContactSearchFields.indexOf([params[0][i]])) {
+            filter.push(params[0][i]);
+        } else if (params[0][i] == 'displayName') {
+            filter.push('name');
+        } else {
+            console.log('FXOS ContactProxy: inallowed field passed to search filtered out: ' + params[0][i]);
+        }
+    }
+    var getall = navigator.mozContacts.getAll({});
+    getall.onsuccess = function() {
+        var contacts = [];
+        var allContacts = getall.result;
+        for (var i=0; i < allContacts.length; i++) {
+            var mozContact = allContacts[i];
+            var valid = false;
+            for (var j=0; j < filter.length; j++) {
+                if (mozContact[filter[0]].indexOf(options.filter)) {
+                    valid = true;
+                }
+            }
+            if (valid) {
+                contacts.push(createCordovaFromMozilla(mozContact));
+            }
+        }
+        successCB(contacts);
+    };
+    getall.onerror = errorCB;
+}
+
 module.exports = {
     save: saveContacts,
     remove: remove,
-    search: function(){},
+    search: hackedSearch
 };    
     
 require("cordova/firefoxos/commandProxy").add("Contacts", module.exports); 


[08/11] git commit: docs added

Posted by he...@apache.org.
docs added


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/3a65011d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/3a65011d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/3a65011d

Branch: refs/heads/dev
Commit: 3a65011d6411b08ae9313ec8a0eb5cf545fd7b4f
Parents: 334080a
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Thu Nov 28 14:42:50 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:32:27 2013 -0800

----------------------------------------------------------------------
 README.md | 9 +++++++++
 1 file changed, 9 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/3a65011d/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 22112eb..2c42361 100644
--- a/README.md
+++ b/README.md
@@ -3,3 +3,12 @@ cordova-plugin-contacts
 To install this plugin, follow the [Command-line Interface Guide](http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface).
 
 If you are not using the Cordova Command-line Interface, follow [Using Plugman to Manage Plugins](http://cordova.apache.org/docs/en/edge/plugin_ref_plugman.md.html).
+
+## Using Contacts in Firefox OS
+
+Edit manifest.webapp and add permissions field as described in [Manifest Docs](https://developer.mozilla.org/en-US/Apps/Developing/Manifest#permissions).
+There is also a need to change the webapp type to "privileged"  - [Manifest Docs](https://developer.mozilla.org/en-US/Apps/Developing/Manifest#type).
+All privileged apps enforce [Content Security Policy](https://developer.mozilla.org/en-US/Apps/CSP) which forbids inline script. Initialize your application in another way (i.e. start it on document load in index.js)
+
+    document.onload = app.initialize;
+


[04/11] git commit: save is linked with the proxy contact.name doesn't exist www/Contact.js#Contact.prototype.save check on which side is the error

Posted by he...@apache.org.
save is linked with the proxy
contact.name doesn't exist www/Contact.js#Contact.prototype.save
check on which side is the error


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/5833bc0a
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/5833bc0a
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/5833bc0a

Branch: refs/heads/dev
Commit: 5833bc0ae8ebc88f544e7e2783e49a6ee40ecdfc
Parents: c070a4f
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Thu Nov 7 15:04:11 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:28:35 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 136 +++++++++++++++++++-----------------
 1 file changed, 70 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/5833bc0a/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index 6c074ce..550ddf7 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -25,83 +25,87 @@
 // http://cordova.apache.org/docs/en/2.5.0/cordova_contacts_contacts.md.html#Contact
 // FxOS contact definition:
 // https://developer.mozilla.org/en-US/docs/Web/API/mozContact
-function saveContact(contacts, success, fail) {
+function saveContacts(successCB, errorCB, contacts) {
     // success and fail will be called every time a contact is saved
     for (var contact in contacts) {
-        var moz = new mozContact(),
-            request;
+        var request;
+
+        function success(result) {
+            // TODO: this will need to amend the result
+            successCB(result);
+        }
             
-            function exportContactFieldArray(contactFieldArray, key) {
-                if (!key) {
-                    key = 'value';
-                }                 
-                
-                var arr = [];
-                
-                for (var i in contactFieldArray) {
-                    arr.push(contactFieldArray[i][key]);
-                };                                       
-                
-                return arr;
-            }              
+        function exportContactFieldArray(contactFieldArray, key) {
+            if (!key) {
+                key = 'value';
+            }                 
             
-            function exportAddress (addresses) {
-                // TODO: check moz address format
-                var arr = [];
-                
-                for (var i in addresses) {
-                    var addr = {};
+            var arr = [];
+            
+            for (var i in contactFieldArray) {
+                arr.push(contactFieldArray[i][key]);
+            };                                       
+            
+            return arr;
+        }              
+        
+        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];    
+                } 
                 
-                    for (var key in addresses[i]) {
-                        addr[key] = addresses[i][key];    
-                    } 
-                    
-                    arr.push(addr);
-                    
-                }                                 
+                arr.push(addr);
                 
-                return arr;
-            } 
+            }                                 
             
-            // prepare mozContact object
-            // TODO: find a way to link existing mozContact and Contact 
-            // (by ID?)
-            moz.init({
-                name: [contact.name.familyName, 
-                       contact.name.givenName, 
-                       contact.name.middleName, 
-                       contact.name.nickname],
-                honorificPrefix: [contact.name.honorificPrefix],
-                givenName: [contact.name.givenName],
-                familyName: [contact.name.familyName],
-                honorificSuffix: [contact.name.honorificSuffix], 
-                nickname: [contact.nickname],
-                email: exportContactFieldArray(contact.emails),
-                // photo: Blob
-                // url: Array with metadata (?)
-                category: exportContactFieldArray(contact.categories),
-                adr: exportAddress(contact.addresses),
-                tel: exportContactFieldArray(contact.phoneNumbers),
-                org: exportContactFieldArray(contact.organizations, 'name'),
-                jobTitle: exportContactFieldArray(contact.organizations, 'title'),
-                bday: contact.birthday,
-                note: contact.note,
-                // impp: exportIM(contact.ims), TODO: find the moz impp definition
-                // anniversary
-                // sex
-                // genderIdentity
-                // key
-            });
-            
-            request = navigator.mozContacts.save(moz);
-            request.onsuccess = success;
-            request.onerror = fail;                
+            return arr;
+        } 
+
+        // prepare mozContact object
+        // TODO: find a way to link existing mozContact and Contact by ID
+        var moz = new mozContact({
+            name: [contact.name.familyName, 
+                   contact.name.givenName, 
+                   contact.name.middleName, 
+                   contact.name.nickname],
+            honorificPrefix: [contact.name.honorificPrefix],
+            givenName: [contact.name.givenName],
+            familyName: [contact.name.familyName],
+            honorificSuffix: [contact.name.honorificSuffix], 
+            nickname: [contact.nickname],
+            email: exportContactFieldArray(contact.emails),
+            // photo: Blob
+            // url: Array with metadata (?)
+            category: exportContactFieldArray(contact.categories),
+            adr: exportAddress(contact.addresses),
+            tel: exportContactFieldArray(contact.phoneNumbers),
+            org: exportContactFieldArray(contact.organizations, 'name'),
+            jobTitle: exportContactFieldArray(contact.organizations, 'title'),
+            bday: contact.birthday,
+            note: contact.note,
+            // impp: exportIM(contact.ims), TODO: find the moz impp definition
+            // anniversary
+            // sex
+            // genderIdentity
+            // key
+        });
+        
+        request = navigator.mozContacts.save(moz);
+        request.onsuccess = success;
+        request.onerror = errorCB;                
     }
 }   
 
 module.exports = {
-    saveContact: saveContact,
-    cleanup: function(){}
+    save: saveContacts,
+    remove: function(){},
+    search: function(){},
 };    
     
 require("cordova/firefoxos/commandProxy").add("Contacts", module.exports); 


[02/11] git commit: add/remove working

Posted by he...@apache.org.
add/remove working


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/144d69af
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/144d69af
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/144d69af

Branch: refs/heads/dev
Commit: 144d69af5a806abd3932b8f56094f490e5d9765b
Parents: 0f7ff41
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Fri Nov 15 13:44:21 2013 +0100
Committer: hermwong <he...@gmail.com>
Committed: Tue Dec 3 13:28:35 2013 -0800

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 55 +++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/144d69af/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index c2ac0fd..bc5d871 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -25,20 +25,19 @@
 // http://cordova.apache.org/docs/en/2.5.0/cordova_contacts_contacts.md.html#Contact
 // FxOS contact definition:
 // https://developer.mozilla.org/en-US/docs/Web/API/mozContact
+
 function saveContacts(successCB, errorCB, contacts) {
-    // success and fail will be called every time a contact is saved
+    // 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);
         }
     }
-    function error(e) {
-        console.log('BOO from moz');
-        errorCB(e);
-    }
         
     function exportContactFieldArray(contactFieldArray, key) {
         if (!key) {
@@ -73,11 +72,12 @@ function saveContacts(successCB, errorCB, contacts) {
     } 
     var i=0;
     var contact;
-
     while(contact = contacts[i++]){
-        var request;
         // prepare mozContact object
-        var translatedContact = {};
+        var moz = new mozContact();
+        if (contact.id) {
+            moz.id = contact.id;
+        }
         // building name
         var nameArray = [];
         var fields = ['honorificPrefix', 'familyName', 'givenName', 'middleName', 'nickname'];
@@ -86,32 +86,32 @@ function saveContacts(successCB, errorCB, contacts) {
                 nameArray.push(contact.name[field]);
             }
         }
-        translatedContact.name = nameArray.join(' ');
+        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]]) {
-            translatedContact[field[1] || field[0]] = contact.name[field[0]];
+            moz[field[1] || field[0]] = contact.name[field[0]];
           }
         }
         if (contact.emails) {
-            translatedContact.email = exportContactFieldArray(contact.emails);
+            moz.email = exportContactFieldArray(contact.emails);
         }
         if (contact.categories) {
-            translatedContact.category = exportContactFieldArray(contact.categories);
+            moz.category = exportContactFieldArray(contact.categories);
         }
         if (contact.addresses) {
-            translatedContact.adr = exportAddress(contact.addresses);
+            moz.adr = exportAddress(contact.addresses);
         }
         if (contact.phoneNumbers) {
-            translatedContact.tel = exportContactFieldArray(contact.phoneNumbers);
+            moz.tel = exportContactFieldArray(contact.phoneNumbers);
         }
         if (contact.organizations) {
-            translatedContact.org = exportContactFieldArray(contact.organizations, 'name');
-            translatedContact.jobTitle = exportContactFieldArray(contact.organizations, 'title');
+            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
@@ -119,19 +119,28 @@ function saveContacts(successCB, errorCB, contacts) {
             // sex
             // genderIdentity
             // key
-        }
         */
-        // TODO: find a way to link existing mozContact and Contact by ID
-        var moz = new mozContact(translatedContact);
-        request = navigator.mozContacts.save(moz);
+        var request = navigator.mozContacts.save(moz);
         request.onsuccess = makeSuccess(contact, moz);
-        request.onerror = error;                
+        request.onerror = errorCB;                
     }
 }   
 
+function remove(successCB, errorCB, ids) {
+    var i=0;
+    var id;
+    while(id = ids[i++]){
+        var moz = new mozContact();
+        moz.id = id;
+        var request = navigator.mozContacts.remove(moz);
+        request.onsuccess = successCB;
+        request.onerror = errorCB;
+    }
+}
+
 module.exports = {
     save: saveContacts,
-    remove: function(){},
+    remove: remove,
     search: function(){},
 };