Source: trellinator-libs/Comment.js

  1. /**
  2. * @class Comment
  3. * @memberof module:TrelloEntities
  4. * @param data (Object} key/value pairs of
  5. * information, must at least contain "id",
  6. * can basically just pass in response from Trello API
  7. * @constructor
  8. * @classdesc The Comment class represents
  9. * a Comment in Trello. You will mostly interact
  10. * with this class when returned in an IterableCollection
  11. * from a Card method or as a result of a Notification
  12. *
  13. * @example
  14. * var notif = new Notification(posted);
  15. *
  16. * if(new RegExp("Remove.*").test(notif.addedComment().text()))
  17. * notif.card().removeMember(notif.member());
  18. */
  19. var Comment = function(data)
  20. {
  21. this.data = data;
  22. this.containing_card = null;
  23. /**
  24. * Return the id of this comment
  25. * @memberof module:TrelloEntities.Comment
  26. * @example
  27. * card.comments().first().id();
  28. */
  29. this.id = function()
  30. {
  31. if(!this.data.data)
  32. throw new Error("Malformed comment object");
  33. return this.data.data.id ? this.data.data.id:this.data.id;
  34. }
  35. /**
  36. * Return the Card on which this comment
  37. * was added
  38. * @memberof module:TrelloEntities.Comment
  39. * @example
  40. * var card = new Notification(posted).addedComment().card();
  41. */
  42. this.card = function()
  43. {
  44. return this.containing_card;
  45. }
  46. /**
  47. * Return the text of the comment
  48. * @memberof module:TrelloEntities.Comment
  49. * @example
  50. * card.comments().first().text();
  51. */
  52. this.text = function()
  53. {
  54. if(!this.data.data)
  55. throw new Error("Malformed comment object");
  56. return this.data.data.text;
  57. }
  58. /**
  59. * Return the member who made a comment
  60. * @memberof module:TrelloEntities.Comment
  61. * @example
  62. * card.comments().first().member();
  63. */
  64. this.member = function()
  65. {
  66. return new Member(this.data.memberCreator);
  67. }
  68. /**
  69. * Return an IterableCollection of mentioned members
  70. * @memberof module:TrelloEntities.Comment
  71. * @example
  72. * card.comments().first().mentionedMembers();
  73. */
  74. this.mentionedMembers = function()
  75. {
  76. var ret = new Array();
  77. this.card().board().members().each(function(member)
  78. {
  79. if(new RegExp(".*@"+member.name()+".*").test(this.text()))
  80. ret.push(member);
  81. });
  82. if(!ret.length)
  83. throw new InvalidActionException("No members were mentioned in this comment");
  84. return new IterableCollection(ret);
  85. }
  86. /**
  87. * Return a Date when this comment was made
  88. * @memberof module:TrelloEntities.Comment
  89. * @example
  90. * card.comments().first().when().toString();
  91. */
  92. this.when = function()
  93. {
  94. return new Date(this.data.date);
  95. }
  96. /**
  97. * A name() function used in name comparisons
  98. * in the IterableCollection findByName method
  99. * @memberof module:TrelloEntities.Comment
  100. * @example
  101. * card.comments().findByName(new RegExp(".*search.*")).first().text();
  102. */
  103. this.name = function()
  104. {
  105. return this.text();
  106. }
  107. /**
  108. * Delete this comment, works only if you have
  109. * more permissions than the person who made the
  110. * comment, you made the comment or the person
  111. * who made the comment has deleted their account
  112. * Returns the containing card
  113. * @memberof module:TrelloEntities.Comment
  114. * @example
  115. * card.comments().first().del();
  116. */
  117. this.del = function()
  118. {
  119. TrelloApi.del("actions/"+this.id());
  120. return this.card();
  121. }
  122. /**
  123. * Edit this comment, works only if you have
  124. * more permissions than the person who made the
  125. * comment, you made the comment or the person
  126. * who made the comment has deleted their account
  127. * Returns the containing card
  128. * @memberof module:TrelloEntities.Comment
  129. * @example
  130. * card.comments().first().del();
  131. */
  132. this.setText = function(text)
  133. {
  134. TrelloApi.put("actions/"+this.id()+"/text?value="+encodeURIComponent(text));
  135. return this.card();
  136. }
  137. //INTERNAL USE ONLY
  138. this.setContainingCard = function(card)
  139. {
  140. this.containing_card = card;
  141. return this;
  142. }
  143. }