Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

Contact.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var argscheck = require('cordova/argscheck'),
  22. exec = require('cordova/exec'),
  23. ContactError = require('./ContactError'),
  24. utils = require('cordova/utils'),
  25. convertUtils = require('./convertUtils');
  26. /**
  27. * Contains information about a single contact.
  28. * @constructor
  29. * @param {DOMString} id unique identifier
  30. * @param {DOMString} displayName
  31. * @param {ContactName} name
  32. * @param {DOMString} nickname
  33. * @param {Array.<ContactField>} phoneNumbers array of phone numbers
  34. * @param {Array.<ContactField>} emails array of email addresses
  35. * @param {Array.<ContactAddress>} addresses array of addresses
  36. * @param {Array.<ContactField>} ims instant messaging user ids
  37. * @param {Array.<ContactOrganization>} organizations
  38. * @param {DOMString} birthday contact's birthday
  39. * @param {DOMString} note user notes about contact
  40. * @param {Array.<ContactField>} photos
  41. * @param {Array.<ContactField>} categories
  42. * @param {Array.<ContactField>} urls contact's web sites
  43. */
  44. var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, addresses,
  45. ims, organizations, birthday, note, photos, categories, urls) {
  46. this.id = id || null;
  47. this.rawId = null;
  48. this.displayName = displayName || null;
  49. this.name = name || null; // ContactName
  50. this.nickname = nickname || null;
  51. this.phoneNumbers = phoneNumbers || null; // ContactField[]
  52. this.emails = emails || null; // ContactField[]
  53. this.addresses = addresses || null; // ContactAddress[]
  54. this.ims = ims || null; // ContactField[]
  55. this.organizations = organizations || null; // ContactOrganization[]
  56. this.birthday = birthday || null;
  57. this.note = note || null;
  58. this.photos = photos || null; // ContactField[]
  59. this.categories = categories || null; // ContactField[]
  60. this.urls = urls || null; // ContactField[]
  61. };
  62. /**
  63. * Removes contact from device storage.
  64. * @param successCB success callback
  65. * @param errorCB error callback
  66. */
  67. Contact.prototype.remove = function(successCB, errorCB) {
  68. argscheck.checkArgs('FF', 'Contact.remove', arguments);
  69. var fail = errorCB && function(code) {
  70. errorCB(new ContactError(code));
  71. };
  72. if (this.id === null) {
  73. fail(ContactError.UNKNOWN_ERROR);
  74. }
  75. else {
  76. exec(successCB, fail, "Contacts", "remove", [this.id]);
  77. }
  78. };
  79. /**
  80. * Creates a deep copy of this Contact.
  81. * With the contact ID set to null.
  82. * @return copy of this Contact
  83. */
  84. Contact.prototype.clone = function() {
  85. var clonedContact = utils.clone(this);
  86. clonedContact.id = null;
  87. clonedContact.rawId = null;
  88. function nullIds(arr) {
  89. if (arr) {
  90. for (var i = 0; i < arr.length; ++i) {
  91. arr[i].id = null;
  92. }
  93. }
  94. }
  95. // Loop through and clear out any id's in phones, emails, etc.
  96. nullIds(clonedContact.phoneNumbers);
  97. nullIds(clonedContact.emails);
  98. nullIds(clonedContact.addresses);
  99. nullIds(clonedContact.ims);
  100. nullIds(clonedContact.organizations);
  101. nullIds(clonedContact.categories);
  102. nullIds(clonedContact.photos);
  103. nullIds(clonedContact.urls);
  104. return clonedContact;
  105. };
  106. /**
  107. * Persists contact to device storage.
  108. * @param successCB success callback
  109. * @param errorCB error callback
  110. */
  111. Contact.prototype.save = function(successCB, errorCB) {
  112. argscheck.checkArgs('FFO', 'Contact.save', arguments);
  113. var fail = errorCB && function(code) {
  114. errorCB(new ContactError(code));
  115. };
  116. var success = function(result) {
  117. if (result) {
  118. if (successCB) {
  119. var fullContact = require('./contacts').create(result);
  120. successCB(convertUtils.toCordovaFormat(fullContact));
  121. }
  122. }
  123. else {
  124. // no Entry object returned
  125. fail(ContactError.UNKNOWN_ERROR);
  126. }
  127. };
  128. var dupContact = convertUtils.toNativeFormat(utils.clone(this));
  129. exec(success, fail, "Contacts", "save", [dupContact]);
  130. };
  131. module.exports = Contact;