-
Notifications
You must be signed in to change notification settings - Fork 32
/
get-contacts-by-name-worker.ios.js
50 lines (42 loc) · 2.2 KB
/
get-contacts-by-name-worker.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require('globals'); // necessary to bootstrap tns modules on the new thread
var Contact = require("./contact-model");
var helper = require("./contact-helper");
/* pass debug messages to main thread since web workers do not have console access */
function console_log(msg) { postMessage({ type: 'debug', message: msg }); }
function console_dump(msg) { postMessage({ type: 'dump', message: msg }); }
self.onmessage = function (event) {
contactFields = event.data.contactFields;
var keysToFetch = []; // All Properties that we are using in the Model
if (contactFields.indexOf('name') > -1) {
keysToFetch.push(
"givenName", "familyName", "middleName", "namePrefix", "nameSuffix",
"phoneticGivenName", "phoneticMiddleName", "phoneticFamilyName"
);
}
if (contactFields.indexOf('organization') > -1) { keysToFetch.push("jobTitle", "departmentName", "organizationName"); }
if (contactFields.indexOf('nickname') > -1) { keysToFetch.push("nickname"); }
if (contactFields.indexOf('notes') > -1) { keysToFetch.push("note"); }
if (contactFields.indexOf('photo') > -1) { keysToFetch.push("imageData", "imageDataAvailable"); }
if (contactFields.indexOf('phoneNumbers') > -1) { keysToFetch.push("phoneNumbers"); }
if (contactFields.indexOf('emailAddresses') > -1) { keysToFetch.push("emailAddresses"); }
if (contactFields.indexOf('postalAddresses') > -1) { keysToFetch.push("postalAddresses"); }
if (contactFields.indexOf('urlAddresses') > -1) { keysToFetch.push("urlAddresses"); }
var store = new CNContactStore(),
error,
foundContacts = store.unifiedContactsMatchingPredicateKeysToFetchError(
CNContact.predicateForContactsMatchingName(event.data.searchPredicate),
keysToFetch,
error
);
if(error) { postMessage({ type: 'error', message: error }); }
if (foundContacts.count > 0) {
var cts = [];
for(var i=0; i<foundContacts.count; i++){
var contactModel = new Contact();
contactModel.initializeFromNative(foundContacts[i],contactFields);
cts.push(contactModel);
}
postMessage({ type: 'result', message: { data: cts, response: "fetch" }});
}
else { postMessage({ type: 'result', message: { data: null, response: "fetch" }}); }
}