|
1 |
| -require('@nativescript/core/globals') |
2 |
| -var Contact = require("./models/contact").Contact; |
| 1 | +import '@nativescript/core/globals'; |
| 2 | + |
| 3 | +import { Contact } from './models/contact'; |
3 | 4 |
|
4 | 5 | /* pass debug messages to main thread since web workers do not have console access */
|
5 | 6 | // function console_log(msg) { postMessage({ type: 'debug', message: msg }); }
|
6 | 7 | // function console_dump(msg) { postMessage({ type: 'dump', message: msg }); }
|
7 | 8 |
|
8 | 9 | self.onmessage = function (event) {
|
9 |
| - contactFields = event.data.contactFields || [ |
10 |
| - 'name', |
11 |
| - 'organization', |
12 |
| - 'nickname', |
13 |
| - 'notes', |
14 |
| - 'photo', |
15 |
| - 'urls', |
16 |
| - 'phoneNumbers', |
17 |
| - 'emailAddresses', |
18 |
| - 'postalAddresses', |
19 |
| - ]; |
20 |
| - |
21 |
| - var keysToFetch = []; // All Properties that we are using in the Model |
22 |
| - if (contactFields.indexOf('name') > -1) { |
23 |
| - keysToFetch.push( |
24 |
| - "givenName", "familyName", "middleName", "namePrefix", "nameSuffix", |
25 |
| - "phoneticGivenName", "phoneticMiddleName", "phoneticFamilyName" |
26 |
| - ); |
27 |
| - } |
28 |
| - |
29 |
| - if (contactFields.indexOf('organization') > -1) { keysToFetch.push("jobTitle", "departmentName", "organizationName"); } |
30 |
| - if (contactFields.indexOf('nickname') > -1) { keysToFetch.push("nickname"); } |
31 |
| - if (contactFields.indexOf('notes') > -1) { keysToFetch.push("note"); } |
32 |
| - if (contactFields.indexOf('photo') > -1) { keysToFetch.push("imageData", "imageDataAvailable"); } |
33 |
| - if (contactFields.indexOf('phoneNumbers') > -1) { keysToFetch.push("phoneNumbers"); } |
34 |
| - if (contactFields.indexOf('emailAddresses') > -1) { keysToFetch.push("emailAddresses"); } |
35 |
| - if (contactFields.indexOf('postalAddresses') > -1) { keysToFetch.push("postalAddresses"); } |
36 |
| - if (contactFields.indexOf('urlAddresses') > -1) { keysToFetch.push("urlAddresses"); } |
37 |
| - |
38 |
| - var store = new CNContactStore(), |
39 |
| - error, |
40 |
| - fetch = CNContactFetchRequest.alloc().initWithKeysToFetch(keysToFetch), |
41 |
| - cts = [], |
42 |
| - nativeMutableArray = NSMutableArray.alloc().init(); |
43 |
| - |
44 |
| - fetch.unifyResults = true; |
45 |
| - fetch.predicate = null; |
46 |
| - |
47 |
| - store.enumerateContactsWithFetchRequestErrorUsingBlock(fetch, error, function(c,s){ |
48 |
| - nativeMutableArray.addObject(c); |
49 |
| - var contactModel = new Contact(); |
50 |
| - contactModel.initializeFromNative(c,contactFields); |
51 |
| - cts.push(contactModel); |
52 |
| - }); |
53 |
| - |
54 |
| - if(error) { postMessage({ type: 'error', message: error }); } |
55 |
| - if(cts.length > 0) { postMessage({ type: 'result', message: { data: cts, response: "fetch" }}); } |
56 |
| - else { postMessage({ type: 'result', message: { data: null, response: "fetch" }}); } |
57 |
| -} |
| 10 | + // prettier-ignore |
| 11 | + const contactFields = event.data.contactFields || [ |
| 12 | + 'name', |
| 13 | + 'organization', |
| 14 | + 'nickname', |
| 15 | + 'notes', |
| 16 | + 'photo', |
| 17 | + 'urls', |
| 18 | + 'phoneNumbers', |
| 19 | + 'emailAddresses', |
| 20 | + 'postalAddresses' |
| 21 | + ]; |
| 22 | + |
| 23 | + const keysToFetch = []; // All Properties that we are using in the Model |
| 24 | + if (contactFields.indexOf('name') > -1) { |
| 25 | + // prettier-ignore |
| 26 | + keysToFetch.push( |
| 27 | + 'givenName', |
| 28 | + 'familyName', |
| 29 | + 'middleName', |
| 30 | + 'namePrefix', |
| 31 | + 'nameSuffix', |
| 32 | + 'phoneticGivenName', |
| 33 | + 'phoneticMiddleName', |
| 34 | + 'phoneticFamilyName' |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + if (contactFields.indexOf('organization') > -1) { |
| 39 | + keysToFetch.push('jobTitle', 'departmentName', 'organizationName'); |
| 40 | + } |
| 41 | + if (contactFields.indexOf('nickname') > -1) { |
| 42 | + keysToFetch.push('nickname'); |
| 43 | + } |
| 44 | + if (contactFields.indexOf('notes') > -1) { |
| 45 | + keysToFetch.push('note'); |
| 46 | + } |
| 47 | + if (contactFields.indexOf('photo') > -1) { |
| 48 | + keysToFetch.push('imageData', 'imageDataAvailable'); |
| 49 | + } |
| 50 | + if (contactFields.indexOf('phoneNumbers') > -1) { |
| 51 | + keysToFetch.push('phoneNumbers'); |
| 52 | + } |
| 53 | + if (contactFields.indexOf('emailAddresses') > -1) { |
| 54 | + keysToFetch.push('emailAddresses'); |
| 55 | + } |
| 56 | + if (contactFields.indexOf('postalAddresses') > -1) { |
| 57 | + keysToFetch.push('postalAddresses'); |
| 58 | + } |
| 59 | + if (contactFields.indexOf('urlAddresses') > -1) { |
| 60 | + keysToFetch.push('urlAddresses'); |
| 61 | + } |
| 62 | + |
| 63 | + const store = new CNContactStore(); |
| 64 | + let error: any; |
| 65 | + let fetch = CNContactFetchRequest.alloc().initWithKeysToFetch(keysToFetch); |
| 66 | + let cts = []; |
| 67 | + let nativeMutableArray = NSMutableArray.alloc().init(); |
| 68 | + |
| 69 | + fetch.unifyResults = true; |
| 70 | + fetch.predicate = null; |
| 71 | + |
| 72 | + store.enumerateContactsWithFetchRequestErrorUsingBlock(fetch, error, function (c, s) { |
| 73 | + nativeMutableArray.addObject(c); |
| 74 | + const contactModel = new Contact(); |
| 75 | + contactModel.initializeFromNative(c, contactFields); |
| 76 | + cts.push(contactModel); |
| 77 | + }); |
| 78 | + |
| 79 | + if (error) { |
| 80 | + postMessage({ type: 'error', message: error }); |
| 81 | + } |
| 82 | + if (cts.length > 0) { |
| 83 | + postMessage({ type: 'result', message: { data: cts, response: 'fetch' } }); |
| 84 | + } else { |
| 85 | + postMessage({ type: 'result', message: { data: null, response: 'fetch' } }); |
| 86 | + } |
| 87 | +}; |
0 commit comments