checklists.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Checklists = new Mongo.Collection('checklists');
  2. Checklists.attachSchema(new SimpleSchema({
  3. cardId: {
  4. type: String,
  5. },
  6. title: {
  7. type: String,
  8. },
  9. items: {
  10. type: [Object],
  11. defaultValue: [],
  12. },
  13. 'items.$._id': {
  14. type: String,
  15. },
  16. 'items.$.title': {
  17. type: String,
  18. },
  19. 'items.$.isFinished': {
  20. type: Boolean,
  21. defaultValue: false,
  22. },
  23. finishedAt: {
  24. type: Date,
  25. optional: true,
  26. },
  27. createdAt: {
  28. type: Date,
  29. denyUpdate: false,
  30. },
  31. }));
  32. Checklists.helpers({
  33. itemCount () {
  34. return this.items.length;
  35. },
  36. finishedCount () {
  37. return this.items.filter((item) => {
  38. return item.isFinished;
  39. }).length;
  40. },
  41. isFinished () {
  42. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  43. },
  44. getItem (_id) {
  45. return _.findWhere(this.items, { _id });
  46. },
  47. itemIndex(itemId) {
  48. return _.pluck(this.items, '_id').indexOf(itemId);
  49. },
  50. });
  51. Checklists.allow({
  52. insert(userId, doc) {
  53. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  54. },
  55. update(userId, doc) {
  56. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  57. },
  58. remove(userId, doc) {
  59. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  60. },
  61. fetch: ['userId', 'cardId'],
  62. });
  63. Checklists.before.insert((userId, doc) => {
  64. doc.createdAt = new Date();
  65. if (!doc.userId) {
  66. doc.userId = userId;
  67. }
  68. });
  69. Checklists.mutations({
  70. //for checklist itself
  71. setTitle(title){
  72. return { $set: { title }};
  73. },
  74. //for items in checklist
  75. addItem(title) {
  76. const itemCount = this.itemCount();
  77. const _id = `${this._id}${itemCount}`;
  78. return { $addToSet: {items: {_id, title, isFinished: false}} };
  79. },
  80. removeItem(itemId) {
  81. return {$pull: {items: {_id : itemId}}};
  82. },
  83. editItem(itemId, title) {
  84. if (this.getItem(itemId)) {
  85. const itemIndex = this.itemIndex(itemId);
  86. return {
  87. $set: {
  88. [`items.${itemIndex}.title`]: title,
  89. },
  90. };
  91. }
  92. return {};
  93. },
  94. finishItem(itemId) {
  95. if (this.getItem(itemId)) {
  96. const itemIndex = this.itemIndex(itemId);
  97. return {
  98. $set: {
  99. [`items.${itemIndex}.isFinished`]: true,
  100. },
  101. };
  102. }
  103. return {};
  104. },
  105. resumeItem(itemId) {
  106. if (this.getItem(itemId)) {
  107. const itemIndex = this.itemIndex(itemId);
  108. return {
  109. $set: {
  110. [`items.${itemIndex}.isFinished`]: false,
  111. },
  112. };
  113. }
  114. return {};
  115. },
  116. toggleItem(itemId) {
  117. const item = this.getItem(itemId);
  118. if (item) {
  119. const itemIndex = this.itemIndex(itemId);
  120. return {
  121. $set: {
  122. [`items.${itemIndex}.isFinished`]: !item.isFinished,
  123. },
  124. };
  125. }
  126. return {};
  127. },
  128. });
  129. if (Meteor.isServer) {
  130. Meteor.startup(() => {
  131. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  132. });
  133. Checklists.after.insert((userId, doc) => {
  134. Activities.insert({
  135. userId,
  136. activityType: 'addChecklist',
  137. cardId: doc.cardId,
  138. boardId: Cards.findOne(doc.cardId).boardId,
  139. checklistId: doc._id,
  140. });
  141. });
  142. //TODO: so there will be no activity for adding item into checklist, maybe will be implemented in the future.
  143. // Checklists.after.update((userId, doc) => {
  144. // console.log('update:', doc)
  145. // Activities.insert({
  146. // userId,
  147. // activityType: 'addChecklist',
  148. // boardId: doc.boardId,
  149. // cardId: doc.cardId,
  150. // checklistId: doc._id,
  151. // });
  152. // });
  153. Checklists.before.remove((userId, doc) => {
  154. const activity = Activities.findOne({ checklistId: doc._id });
  155. if (activity) {
  156. Activities.remove(activity._id);
  157. }
  158. });
  159. }