1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 var _next_promise_id = 0;
9 // Preserve first element for "oncontactschange" way event listener.
11 // Compared with addEventListener, the main difference is there is only one
12 // oncontactschange callback will be invoked. While addEventListener can
13 // add multiple callbacks when event "contactschanged" arrives.
15 // This listener can be set as "oncontactschange = function(){...};"
16 // and can be unset as "oncontactschange = null;"
18 var _next_listener_id = 1;
20 var Promise = requireNative('sysapps_promise').Promise;
22 var _postMessage = function(msg) {
23 var p = new Promise();
25 _promises[_next_promise_id] = p;
26 msg._promise_id = _next_promise_id.toString();
27 _next_promise_id += 1;
29 extension.postMessage(JSON.stringify(msg));
33 function _addConstProperty(obj, propertyKey, propertyValue) {
34 Object.defineProperty(obj, propertyKey, {
41 function _createConstClone(obj) {
43 for (var key in obj) {
44 _addConstProperty(const_obj, key, obj[key]);
49 extension.setMessageListener(function(json) {
50 var msg = JSON.parse(json);
52 if (msg.reply == 'contactschange') {
53 for (var id in _listeners) {
54 if (typeof _listeners[id] === 'function') {
55 _listeners[id](_createConstClone(msg.data));
61 if (msg.data && msg.data.error) {
62 _promises[msg._promise_id].reject(msg.data.error);
64 _promises[msg._promise_id].fulfill(msg.data);
67 delete _promises[msg._promise_id];
70 exports.save = function(contact) {
73 msg['contact'] = contact;
74 return _postMessage(msg);
77 exports.find = function(options) {
80 msg['options'] = options;
81 return _postMessage(msg);
84 exports.remove = function(contactId) {
86 msg['cmd'] = 'remove';
87 msg['contactId'] = contactId;
88 return _postMessage(msg);
91 exports.clear = function() {
94 return _postMessage(msg);
97 function _addListener(isOnChange, callback) {
98 // Check validation of callback for addEventListener way.
99 if (!isOnChange && (typeof callback !== 'function')) {
100 console.log('Invalid parameters of callback!');
104 // Check validation of callback for oncontactschanged way, it can be null or a function.
105 if (isOnChange && (callback !== null) && (typeof callback !== 'function')) {
106 console.log('Invalid parameters of callback!');
111 if (isOnChange) { // Set callback to oncontactschange()
112 _listeners[0] = callback;
114 } else { // Set callback by addEventListner()
115 listener_id = _next_listener_id;
116 _next_listener_id += 1;
117 _listeners[listener_id] = callback;
120 // Notify native code there is valid listener.
121 if (_listeners[0] != null || _listeners.length > 1) {
122 var msg = { 'cmd': 'addEventListener' };
123 extension.postMessage(JSON.stringify(msg));
129 exports.addEventListener = function(eventName, callback) {
130 if (eventName !== 'contactschange') {
131 console.log("Invalid parameters of eventName: "+eventName);
134 return _addListener(false, callback);
137 Object.defineProperty(exports, 'oncontactschange', {
138 set: function(callback) {
139 _addListener(true, callback);
143 window.ContactField = function(init) {
144 this.types = init.types;
145 this.preferred = init.preferred;
146 this.value = init.value;
149 window.ContactTelField = function(init) {
150 this.carrier = init.carrier;
151 this.types = init.types;
152 this.preferred = init.preferred;
153 this.value = init.value;
156 window.ContactAddress = function(init) {
157 this.types = init.types;
158 this.preferred = init.preferred;
159 this.streetAddress = init.streetAddress;
160 this.locality = init.locality;
161 this.region = init.region;
162 this.postalCode = init.postalCode;
163 this.countryName = init.countryName;
166 window.ContactName = function(init) {
167 this.displayName = init.displayName;
168 this.honorificPrefixes = init.honorificPrefixes;
169 this.givenNames = init.givenNames;
170 this.additionalNames = init.additionalNames;
171 this.familyNames = init.familyNames;
172 this.honorificSuffixes = init.honorificSuffixes;
173 this.nicknames = init.nicknames;
176 window.Contact = function(init) {
178 this.lastUpdated = new Date();
179 this.name = init.name;
180 this.emails = init.emails;
181 this.photos = init.photos;
182 this.urls = init.urls;
183 this.categories = init.categories;
184 this.addresses = init.addresses;
185 this.phoneNumbers = init.phoneNumbers;
186 this.organizations = init.organizations;
187 this.jobTitles = init.jobTitles;
188 this.birthday = init.birthday;
189 this.notes = init.notes;
190 this.impp = init.impp;
191 this.anniversary = init.anniversary;
192 this.gender = init.gender;
195 window.ContactsChangeEvent = function(init) {
196 this.added = init.added;
197 this.modified = init.modified;
198 this.removed = init.removed;