contacts.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. function ContactAddress() {
  2. }
  3. ContactAddress.create = function(obj) {
  4. var result = new ContactAddress()
  5. result.pref = obj.pref
  6. result.type = obj.type
  7. result.formatted = obj.formatted
  8. result.streetAddress = obj.streetAddress
  9. result.locality = obj.streetLocality
  10. result.region = obj.region
  11. result.postalCode = obj.postalCode
  12. result.country = obj.country
  13. return result
  14. }
  15. ContactAddress.prototype.pref = false
  16. ContactAddress.prototype.type = ""
  17. ContactAddress.prototype.formatted = ""
  18. ContactAddress.prototype.streetAddress = ""
  19. ContactAddress.prototype.locality = ""
  20. ContactAddress.prototype.region = ""
  21. ContactAddress.prototype.postalCode = ""
  22. ContactAddress.prototype.country = ""
  23. function ContactField() {
  24. }
  25. function ContactField(p_type, p_value, p_pref) {
  26. this.type = p_type;
  27. this.value = p_value;
  28. this.pref = p_pref;
  29. return this;
  30. }
  31. ContactField.create = function(obj) {
  32. var result = new ContactField()
  33. result.type = obj.type
  34. result.value = obj.value
  35. result.pref = obj.pref
  36. return result
  37. }
  38. ContactField.prototype.type = ""
  39. ContactField.prototype.value = ""
  40. ContactField.prototype.pref = false
  41. function ContactFindOptions() {
  42. }
  43. ContactFindOptions.create = function(obj) {
  44. var result = new ContactFindOptions()
  45. result.filter = obj.filter
  46. result.multiple = obj.multiple
  47. return result;
  48. }
  49. ContactFindOptions.prototype.filter = ""
  50. ContactFindOptions.prototype.multiple = false
  51. function ContactName() {
  52. }
  53. ContactName.create = function(obj) {
  54. var result = new ContactName()
  55. result.familyName = obj.familyName
  56. result.givenName = obj.givenName
  57. result.formatted = obj.formatted
  58. result.middleName = obj.middleName
  59. result.honorificPrefix = obj.honorificPrefix
  60. result.honorificSuffix = obj.honorificSuffix
  61. var formattedArr = []
  62. if (typeof result.honorificPrefix === 'undefined')
  63. result.honorificPrefix = ""
  64. else if (result.honorificPrefix !== "")
  65. formattedArr.push(result.honorificPrefix)
  66. if (typeof result.givenName === 'undefined')
  67. result.givenName = ""
  68. else if (result.givenName !== "")
  69. formattedArr.push(result.givenName)
  70. if (typeof result.middleName === 'undefined')
  71. result.middleName = ""
  72. else if (result.middleName !== "")
  73. formattedArr.push(result.middleName)
  74. if (typeof result.familyName == 'undefined')
  75. result.familyName = ""
  76. else if (result.familyName !== "")
  77. formattedArr.push(result.familyName)
  78. if (typeof result.honorificSuffix == 'undefined')
  79. result.honorificSuffix = ""
  80. else if (result.honorificSuffix !== "")
  81. formattedArr.push(result.honorificSuffix)
  82. // result.formatted = formattedArr.join(" ")
  83. return result
  84. }
  85. ContactName.prototype.formatted = ""
  86. ContactName.prototype.familyName = ""
  87. ContactName.prototype.givenName = ""
  88. ContactName.prototype.middleName = ""
  89. ContactName.prototype.honorificPrefix = ""
  90. ContactName.prototype.honorificSuffix = ""
  91. function ContactOrganization() {
  92. }
  93. ContactOrganization.create = function(obj) {
  94. var result = new ContactOrganization()
  95. result.pref = obj.pref
  96. result.type = obj.type
  97. result.name = obj.name
  98. result.department = obj.department
  99. result.title = obj.title
  100. return result
  101. }
  102. ContactOrganization.prototype.pref = false
  103. ContactOrganization.prototype.type = ""
  104. ContactOrganization.prototype.name = ""
  105. ContactOrganization.prototype.department = ""
  106. ContactOrganization.prototype.title = ""
  107. function ContactError() {
  108. }
  109. ContactError.UNKNOWN_ERROR = 0
  110. ContactError.INVALID_ARGUMENT_ERROR = 1
  111. ContactError.TIMEOUT_ERROR = 2
  112. ContactError.PENDING_OPERATION_ERROR = 3
  113. ContactError.IO_ERROR = 4
  114. ContactError.NOT_SUPPORTED_ERROR = 5
  115. ContactError.PERMISSION_DENIED_ERROR = 20
  116. ContactError.prototype.code = ContactError.UNKNOWN_ERROR
  117. function Contact(p_id, p_displayName, p_name, p_nickname,
  118. p_phoneNumbers,
  119. p_emails,
  120. p_addresses,
  121. p_ims,
  122. p_organizations,
  123. p_birthday,
  124. p_note,
  125. p_photos,
  126. p_categories,
  127. p_urls) {
  128. this.id = p_id || "";
  129. this.displayName = p_displayName || "";
  130. this.name = new ContactName(p_name);
  131. this.nickname = p_nickname || "";
  132. this.phoneNumbers = p_phoneNumbers || []
  133. this.emails = p_emails || []
  134. this.addresses = p_addresses || []
  135. this.ims = p_ims || []
  136. this.organizations = p_organizations || []
  137. this.birthday = p_birthday || ""
  138. this.note = p_note || ""
  139. this.photos = p_photos || []
  140. this.categories = p_categories || []
  141. this.urls = p_urls || []
  142. }
  143. Contact.create = function(obj) {
  144. var result = new Contact()
  145. result.id = obj.id
  146. result.displayName = obj.displayName
  147. result.name = ContactName.create(obj.name)
  148. result.nickname = obj.nickname || null
  149. var subObj
  150. for (subObj in obj.phoneNumbers)
  151. result.phoneNumbers.push(ContactField.create(obj.phoneNumbers[subObj]))
  152. for (subObj in obj.emails)
  153. result.emails.push(ContactField.create(obj.emails[subObj]))
  154. for (subObj in obj.addresses)
  155. result.addresses.push(ContactAddress.create(obj.addresses[subObj]))
  156. for (subObj in obj.ims)
  157. result.ims.push(ContactField.create(obj.ims[subObj]))
  158. for (subObj in obj.organizations)
  159. result.organizations.push(ContactOrganization.create(obj.organizations[subObj]))
  160. result.birthday = obj.birthday
  161. result.note = obj.note
  162. for (subObj in obj.photos)
  163. result.photos.push(ContactField.create(obj.photos[subObj]))
  164. for (subObj in obj.categories)
  165. result.categories.push(ContactField.create(obj.categories[subObj]))
  166. for (subObj in obj.urls)
  167. result.urls.push(ContactField.create(obj.urls[subObj]))
  168. return result
  169. }
  170. Contact.prototype.id = null
  171. Contact.prototype.displayName = ""
  172. Contact.prototype.name = new ContactName()
  173. Contact.prototype.nickname = ""
  174. Contact.prototype.phoneNumbers = []
  175. Contact.prototype.emails = []
  176. Contact.prototype.addresses = []
  177. Contact.prototype.ims = []
  178. Contact.prototype.organizations = []
  179. Contact.prototype.birthday = new Date()
  180. Contact.prototype.note = ""
  181. Contact.prototype.photos = []
  182. Contact.prototype.categories = []
  183. Contact.prototype.urls = []
  184. Contact.prototype.clone = function() {
  185. var newContact = Contact.create(this)
  186. newContact.id = null
  187. return newContact
  188. }
  189. Contact.prototype.remove = function(contactSuccess,contactError) {
  190. console.log("Contact.remove 1")
  191. if( typeof contactSuccess !== "function" ) contactSuccess = function() {}
  192. if( typeof contactError !== "function" ) contactError = function() {}
  193. //TODO: call onSaveError here
  194. if (this.id === null || this.id === "")
  195. return
  196. console.log("Contact.remove 2")
  197. Cordova.exec( contactSuccess, contactError, "com.cordova.Contacts", "removeContact", [ this.id ] )
  198. console.log("Contact.remove 3")
  199. }
  200. Contact.prototype.save = function(contactSuccess,contactError) {
  201. console.log("Contact.save 1")
  202. if( typeof contactSuccess !== "function" ) contactSuccess = function() {}
  203. if( typeof contactError !== "function" ) contactError = function() {}
  204. console.log("Contact.save 2")
  205. Cordova.exec( contactSuccess, contactError, "com.cordova.Contacts", "saveContact", [ this ] )
  206. console.log("Contact.save 3")
  207. }
  208. function ContactsManager() {
  209. }
  210. ContactsManager.prototype.create = function(properties) {
  211. return Contact.create(properties)
  212. }
  213. ContactsManager.prototype.find = function(contactFields, contactSuccess, contactError, contactFindOptions) {
  214. // Check the callbacks
  215. if( typeof contactSuccess !== "function" ) {throw "no callback";}
  216. if( typeof contactError !== "function" ) {
  217. contactError = function() {}
  218. }
  219. Cordova.exec( contactSuccess, contactError, "com.cordova.Contacts", "findContacts", [ contactFields, contactFindOptions.filter, contactFindOptions.multiple ] )
  220. }
  221. /**
  222. * Add the ContactsManager object to the navigator
  223. */
  224. Cordova.addConstructor( "com.cordova.Contacts", function () {
  225. navigator.contacts = new ContactsManager()
  226. } );