contact-item.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const contact_address_1 = require("./contact-address");
  4. class ContactItem {
  5. constructor(props) {
  6. this.data = props;
  7. this.hash = props.hash;
  8. this.id = props.id;
  9. this.canSendFromAddrs = (props.canSendFromAddrs || []).map(addrString => contact_address_1.ContactAddress.fromPrefixedString(addrString));
  10. this.addrs = (props.addrs || []).map(addrString => contact_address_1.ContactAddress.fromPrefixedString(addrString));
  11. this.groups = props.groups || [];
  12. this.names = props.names || [];
  13. if (this.names.length === 0) {
  14. this.names.push(this.generateName());
  15. }
  16. this.notes = props.notes || '';
  17. }
  18. matchesGroup(group) {
  19. if (this.groups.filter(x => x === group.trim())[0].length > 0) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. getFirstEmail() {
  25. const first = this.addrs.filter(addr => addr.type === 'email')[0];
  26. if (first == null) {
  27. return undefined;
  28. }
  29. return first.address;
  30. }
  31. getFirstPhone() {
  32. const first = this.addrs.filter(addr => addr.type === 'phone')[0];
  33. if (first == null) {
  34. return undefined;
  35. }
  36. return first.address;
  37. }
  38. search(search) {
  39. if (this.names.find(i => i.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0)) {
  40. return true;
  41. }
  42. if (this.addrs.find(addr => addr.matches(search))) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. matchesAddressExactly(addressType, addressValue) {
  48. if (this.addrs.find(addr => addr.matchesExactly(addressType, addressValue))) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. generateName() {
  54. const firstEmail = this.getFirstEmail();
  55. if (firstEmail != null) {
  56. return firstEmail;
  57. }
  58. const firstPhone = this.getFirstPhone();
  59. if (firstPhone != null) {
  60. return firstPhone;
  61. }
  62. return 'New contact ' + this.id.substring(0, 8);
  63. }
  64. getName() {
  65. if (this.names.length > 0) {
  66. return this.names[0];
  67. }
  68. else {
  69. return '';
  70. }
  71. }
  72. getData() {
  73. return {
  74. addrs: this.addrs.map(addr => addr.toPrefixedString()),
  75. canSendFromAddrs: this.canSendFromAddrs.map(addr => addr.toPrefixedString()),
  76. groups: this.groups,
  77. hash: this.hash,
  78. id: this.id,
  79. names: this.names,
  80. notes: this.notes
  81. };
  82. }
  83. }
  84. exports.ContactItem = ContactItem;
  85. //# sourceMappingURL=contact-item.js.map