Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / extension / api / contacts / contacts_api.js
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.
4
5 var _promises = {};
6 var _next_promise_id = 0;
7 var _listeners = [];
8
9 // Preserve first element for "oncontactschange" way event listener.
10 //
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.
14 //
15 // This listener can be set as "oncontactschange = function(){...};"
16 // and can be unset as "oncontactschange = null;"
17 _listeners[0] = null;
18 var _next_listener_id = 1;
19
20 var Promise = requireNative('sysapps_promise').Promise;
21
22 var _postMessage = function(msg) {
23   var p = new Promise();
24
25   _promises[_next_promise_id] = p;
26   msg._promise_id = _next_promise_id.toString();
27   _next_promise_id += 1;
28
29   extension.postMessage(JSON.stringify(msg));
30   return p;
31 };
32
33 function _addConstProperty(obj, propertyKey, propertyValue) {
34   Object.defineProperty(obj, propertyKey, {
35     configurable: false,
36     writable: false,
37     value: propertyValue
38   });
39 }
40
41 function _createConstClone(obj) {
42   var const_obj = {};
43   for (var key in obj) {
44     _addConstProperty(const_obj, key, obj[key]);
45   }
46   return const_obj;
47 }
48
49 extension.setMessageListener(function(json) {
50   var msg = JSON.parse(json);
51
52   if (msg.reply == 'contactschange') {
53     for (var id in _listeners) {
54       if (typeof _listeners[id] === 'function') {
55         _listeners[id](_createConstClone(msg.data));
56       }
57     }
58     return;
59   }
60
61   if (msg.data && msg.data.error) {
62     _promises[msg._promise_id].reject(msg.data.error);
63   } else {
64     _promises[msg._promise_id].fulfill(msg.data);
65   }
66
67   delete _promises[msg._promise_id];
68 });
69
70 exports.save = function(contact) {
71   var msg = {};
72   msg['cmd'] = 'save';
73   msg['contact'] = contact;
74   return _postMessage(msg);
75 }
76
77 exports.find = function(options) {
78   var msg = {};
79   msg['cmd'] = 'find';
80   msg['options'] = options;
81   return _postMessage(msg);
82 };
83
84 exports.remove = function(contactId) {
85   var msg = {};
86   msg['cmd'] = 'remove';
87   msg['contactId'] = contactId;
88   return _postMessage(msg);
89 };
90
91 exports.clear = function() {
92   var msg = {};
93   msg['cmd'] = 'clear';
94   return _postMessage(msg);
95 }
96
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!');
101     return -1;
102   }
103   
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!');
107     return -1;
108   }
109
110   var listener_id;
111   if (isOnChange) { // Set callback to oncontactschange()
112       _listeners[0] = callback;
113       listener_id = 0;
114   } else { // Set callback by addEventListner()
115       listener_id = _next_listener_id;
116       _next_listener_id += 1;
117       _listeners[listener_id] = callback;
118   }
119
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));
124   }
125
126   return listener_id;
127 }
128
129 exports.addEventListener = function(eventName, callback) {
130   if (eventName !== 'contactschange') {
131     console.log("Invalid parameters of eventName: "+eventName);
132     return -1;
133   }
134   return _addListener(false, callback);
135 }
136
137 Object.defineProperty(exports, 'oncontactschange', {
138   set: function(callback) {
139     _addListener(true, callback);
140   }
141 });
142
143 window.ContactField = function(init) {
144   this.types = init.types;
145   this.preferred = init.preferred;
146   this.value = init.value;
147 };
148
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;
154 };
155
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;
164 };
165
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;
174 };
175
176 window.Contact = function(init) {
177   this.id = null;
178   this.lastUpdated = null;
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;
193 };
194
195 window.ContactsChangeEvent = function(init) {
196   this.added = init.added;
197   this.modified = init.modified;
198   this.removed = init.removed;
199 };