-
Notifications
You must be signed in to change notification settings - Fork 32
/
get-all-contacts-worker.ios.js
58 lines (50 loc) · 2.36 KB
/
get-all-contacts-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
51
52
53
54
55
56
57
58
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 || [
'name',
'organization',
'nickname',
'notes',
'photo',
'urls',
'phoneNumbers',
'emailAddresses',
'postalAddresses',
];
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,
fetch = CNContactFetchRequest.alloc().initWithKeysToFetch(keysToFetch),
cts = [],
nativeMutableArray = new NSMutableArray();
fetch.unifyResults = true;
fetch.predicate = null;
store.enumerateContactsWithFetchRequestErrorUsingBlock(fetch, error, function(c,s){
nativeMutableArray.addObject(c);
var contactModel = new Contact();
contactModel.initializeFromNative(c,contactFields);
cts.push(contactModel);
});
if(error) { postMessage({ type: 'error', message: error }); }
if(cts.length > 0) { postMessage({ type: 'result', message: { data: cts, response: "fetch" }}); }
else { postMessage({ type: 'result', message: { data: null, response: "fetch" }}); }
}