You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mm...@apache.org on 2014/05/05 18:10:27 UTC

[40/50] [abbrv] git commit: CB-1291 Windows8 supports multiple, added some error checking, converts Windows8 Contact to Cordova Contact

CB-1291 Windows8 supports multiple, added some error checking, converts Windows8 Contact to Cordova Contact


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

Branch: refs/heads/cdvtest
Commit: 2f76f2bfbed5a6ba72e96275765a73023e3e6c22
Parents: 92ebe1a
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Mon Mar 17 18:35:42 2014 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Mon Mar 17 18:35:42 2014 -0700

----------------------------------------------------------------------
 src/windows8/ContactProxy.js | 173 +++++++++++++++++++++++++-------------
 1 file changed, 115 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/2f76f2bf/src/windows8/ContactProxy.js
----------------------------------------------------------------------
diff --git a/src/windows8/ContactProxy.js b/src/windows8/ContactProxy.js
index db54a09..10f9402 100644
--- a/src/windows8/ContactProxy.js
+++ b/src/windows8/ContactProxy.js
@@ -1,4 +1,4 @@
-// cordova.define("org.apache.cordova.contacts.ContactProxy", function (require, exports, module) {
+//cordova.define("org.apache.cordova.contacts.ContactProxy", function (require, exports, module) {
 /*
  *
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -20,75 +20,132 @@
  *
 */
 
-var cordova = require('cordova');
 
 module.exports = {
     search:function(win,fail,args){
-        var fields = args[0];
+        var fields = args[0]; // ignored, always returns entire object
         var options = args[1];
 
-        var filter = options.filter;
-        var multiple = options.multiple;
+        var filter = options.filter;   // ignored
+        var multiple = true;//options.multiple;
 
         var picker = new Windows.ApplicationModel.Contacts.ContactPicker();
-        picker.selectionMode = Windows.ApplicationModel.Contacts.ContactSelectionMode.contacts;
-        picker.pickSingleContactAsync().done(function (res) {
-
-            var contact = {
-                id:"",
-                name: { formatted: res.name },  // ContactName
-                displayName: res.name,          // DOMString
-                nickname: res.name,             // DOMString
-                phoneNumbers: res.phoneNumbers, // ContactField[]
-                addresses: res.locations,       // ContactAddress[]
-                emails: [],                     // ContactField
-                ims: res.instantMessages,       // ContactField[]
-                organizations: [],              // ContactOrganization[]
-                birthday: null,                 // Date
-                note: "",                       // DOMString
-                photos: [],                     // ContactField[]
-                categories: [],                 // ContactField[]
-                urls: []                        // ContactField[]
-            };
-
-            if (contact.phoneNumbers && contact.phoneNumbers.length) {
-                contact.phoneNumbers[0].pref = true; // cordova contact field needs a 'prefered' property on  a contact
-            }
-
-            if (contact.addresses && contact.addresses.length) {
-                contact.addresses[0].pref = true;
-                // convert addresses/locations to Cordova.ContactAddresses
-                // pref: Set to true if this ContactAddress contains the user's preferred value. (boolean)
-                // type: A string indicating what type of field this is, home for example. (DOMString)
-                // formatted: The full address formatted for display. (DOMString)
-                // streetAddress: The full street address. (DOMString)
-                // locality: The city or locality. (DOMString)
-                // region: The state or region. (DOMString)
-                // postalCode: The zip code or postal code. (DOMString)
-                // country: The country name. (DOMString)
-            }
-            // Maybe, seems the types are compatible -jm
-            // convert ims to ContactField
-            //if (contact.ims && contact.ims.length) {
-            //    // MS ContactInstantMessageField has : displayText, launchUri, service, userName, category, type
-            //    for (var n = 0; n < contact.ims.length; n++) {
-            //        contact.ims[n] = new ContactField(contact.ims[n].type,contact.ims[n].value, false);
-            //        }
-            //    }
-            //}
-            win([contact]);
-        });
+        picker.selectionMode = Windows.ApplicationModel.Contacts.ContactSelectionMode.contacts;   // select entire contact
+        if (picker.pickContactAsync) {
+            // TODO: 8.1 has better contact support via the 'Contact' object
+        }
+        else {
+            // 8.0 use the ContactInformation class
+            // decide which function we will call
+            var pickerFunkName = multiple ? 'pickMultipleContactsAsync' : 'pickSingleContactAsync';
+            picker[pickerFunkName]().done(function (res) {
+                if (!res) {
+                    fail && setTimeout(function () {
+                        fail(new Error("User did not pick a contact."));
+                    }, 0);
+                    return;
+                }
+
+                var contactResults = [];
+
+                for (var i = 0; i < res.length; i++) {
+
+
+                    var index,
+                        contactResult = res[i],
+                        contact = {
+                            id: "",
+                            name: { formatted: contactResult.name },  // ContactName
+                            displayName: contactResult.name,          // DOMString
+                            nickname: contactResult.name,             // DOMString
+                            phoneNumbers: contactResult.phoneNumbers, // ContactField[]
+                            addresses: contactResult.locations,       // ContactAddress[]
+                            emails: [],                               // ContactField
+                            ims: contactResult.instantMessages,       // ContactField[]
+                            organizations: [],              // ContactOrganization[]
+                            birthday: null,                 // Date
+                            note: "",                       // DOMString
+                            photos: [],                     // ContactField[]
+                            categories: [],                 // ContactField[]
+                            urls: []                        // ContactField[]
+                        };
+
+                    // Win8-ContactField is {category, name, type, value};
+                    // Cordova ContactField is {type,value, pref:bool };
+                    // Win8 type means 'email' cordova type means 'work|home|...' so we convert them
+                    if (contact.emails && contact.emails.length) {
+                        contact.emails[0].pref = true; // add a preferred prop 
+                        for (index = 0; index < contacts.emails.length; index++) {
+                            contact.emails[index].type = contact.emails[index].category;
+                        }
+                    }
+
+                    if (contact.phoneNumbers && contact.phoneNumbers.length) {
+                        contact.phoneNumbers[0].pref = true; // cordova contact field needs a 'prefered' property on  a contact
+                        // change the meaning of type from 'telephonenumber' to 'work|home|...'
+                        for (index = 0; index < contact.phoneNumbers.length; index++) {
+                            contact.phoneNumbers[index].type = contact.phoneNumbers[index].category;
+                        }
+                    }
+
+                    if (contact.addresses && contact.addresses.length) {
+
+                        // convert addresses/locations to Cordova.ContactAddresses                    
+                        // constr: ContactAddress(pref, type, formatted, streetAddress, locality, region, postalCode, country)
+                        var address, formatted;
+                        for (index = 0; index < contact.addresses.length; index++) {
+                            address = contact.addresses[index];   // make an alias
+                            var formattedArray = [];
+                            // get rid of the empty fields.
+                            var fields = [address.street, address.city, address.region, address.country, address.postalCode];
+                            for (var n = 0; n < fields.length; n++) {
+                                if (fields[n].length > 0) {
+                                    formattedArray.push(fields[n]);
+                                }
+                            }
+                            formattedAddress = formattedArray.join(", ");
+                            console.log(contact.name.formatted + " formatted looks like " + formattedAddress);
+                            contact.addresses[index] = new ContactAddress(false,
+                                                                          address.name,
+                                                                          formattedAddress,
+                                                                          address.street,
+                                                                          address.city,
+                                                                          address.region,
+                                                                          address.postalCode,
+                                                                          address.country);
+                        }
+
+                    }
+
+                    // convert ims to ContactField
+                    if (contact.ims && contact.ims.length) {
+                        // MS ContactInstantMessageField has : displayText, launchUri, service, userName, category, type
+                        contact.ims[0].pref = true;
+                        for (index = 0; index < contact.ims.length; index++) {
+                            contact.ims[index] = new ContactField(contact.ims[index].type, contact.ims[index].value, false);
+                        }
+                    }
+
+                    contactResults.push(contact);
+
+                }
+                // send em back
+                win(contactResults);
+
+            });
+        }
     },
 
     save:function(win,fail,args){
-        console.error && console.error("Windows 8 does not support creating/saving contacts");
-        fail && setTimeout(function(){
-            fail(new Error("Contact create/save not supported on Windows8"));
-        },0);
+        console && console.error && console.error("Error : Windows 8 does not support creating/saving contacts");
+        fail && setTimeout(function () {
+            fail(new Error("Contact create/save not supported on Windows 8"));
+        }, 0);
+
     }
 
 
 }
 
-require("cordova/windows8/commandProxy").add("Contacts",module.exports);
-
+require("cordova/exec/proxy").add("Contacts", module.exports);
+// });