Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

contacts.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. Contact = require('./Contact'),
  25. fieldType = require('./ContactFieldType'),
  26. convertUtils = require('./convertUtils');
  27. /**
  28. * Represents a group of Contacts.
  29. * @constructor
  30. */
  31. var contacts = {
  32. fieldType: fieldType,
  33. /**
  34. * Returns an array of Contacts matching the search criteria.
  35. * @param fields that should be searched
  36. * @param successCB success callback
  37. * @param errorCB error callback
  38. * @param {ContactFindOptions} options that can be applied to contact searching
  39. * @return array of Contacts matching search criteria
  40. */
  41. find: function(fields, successCB, errorCB, options) {
  42. argscheck.checkArgs('afFO', 'contacts.find', arguments);
  43. if (!fields.length) {
  44. if (errorCB) {
  45. errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
  46. }
  47. } else {
  48. // missing 'options' param means return all contacts
  49. options = options || { filter: '', multiple: true };
  50. var win = function(result) {
  51. var cs = [];
  52. for (var i = 0, l = result.length; i < l; i++) {
  53. cs.push(convertUtils.toCordovaFormat(contacts.create(result[i])));
  54. }
  55. successCB(cs);
  56. };
  57. exec(win, errorCB, "Contacts", "search", [fields, options]);
  58. }
  59. },
  60. /**
  61. * This function picks contact from phone using contact picker UI
  62. * @returns new Contact object
  63. */
  64. pickContact: function (successCB, errorCB) {
  65. argscheck.checkArgs('fF', 'contacts.pick', arguments);
  66. var win = function (result) {
  67. // if Contacts.pickContact return instance of Contact object
  68. // don't create new Contact object, use current
  69. var contact = result instanceof Contact ? result : contacts.create(result);
  70. successCB(convertUtils.toCordovaFormat(contact));
  71. };
  72. exec(win, errorCB, "Contacts", "pickContact", []);
  73. },
  74. /**
  75. * This function creates a new contact, but it does not persist the contact
  76. * to device storage. To persist the contact to device storage, invoke
  77. * contact.save().
  78. * @param properties an object whose properties will be examined to create a new Contact
  79. * @returns new Contact object
  80. */
  81. create: function(properties) {
  82. argscheck.checkArgs('O', 'contacts.create', arguments);
  83. var contact = new Contact();
  84. for (var i in properties) {
  85. if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) {
  86. contact[i] = properties[i];
  87. }
  88. }
  89. return contact;
  90. }
  91. };
  92. module.exports = contacts;