swimlanes.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. Swimlanes = new Mongo.Collection('swimlanes');
  2. /**
  3. * A swimlane is an line in the kaban board.
  4. */
  5. Swimlanes.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the swimlane
  9. */
  10. type: String,
  11. },
  12. archived: {
  13. /**
  14. * is the swimlane archived?
  15. */
  16. type: Boolean,
  17. autoValue() { // eslint-disable-line consistent-return
  18. if (this.isInsert && !this.isSet) {
  19. return false;
  20. }
  21. },
  22. },
  23. boardId: {
  24. /**
  25. * the ID of the board the swimlane is attached to
  26. */
  27. type: String,
  28. },
  29. createdAt: {
  30. /**
  31. * creation date of the swimlane
  32. */
  33. type: Date,
  34. autoValue() { // eslint-disable-line consistent-return
  35. if (this.isInsert) {
  36. return new Date();
  37. } else {
  38. this.unset();
  39. }
  40. },
  41. },
  42. sort: {
  43. /**
  44. * the sort value of the swimlane
  45. */
  46. type: Number,
  47. decimal: true,
  48. // XXX We should probably provide a default
  49. optional: true,
  50. },
  51. color: {
  52. /**
  53. * the color of the swimlane
  54. */
  55. type: String,
  56. optional: true,
  57. // silver is the default, so it is left out
  58. allowedValues: [
  59. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  60. 'blue', 'sky', 'lime', 'pink', 'black',
  61. 'peachpuff', 'crimson', 'plum', 'darkgreen',
  62. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  63. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  64. ],
  65. },
  66. updatedAt: {
  67. /**
  68. * when was the swimlane last edited
  69. */
  70. type: Date,
  71. optional: true,
  72. autoValue() { // eslint-disable-line consistent-return
  73. if (this.isUpdate) {
  74. return new Date();
  75. } else {
  76. this.unset();
  77. }
  78. },
  79. },
  80. type: {
  81. /**
  82. * The type of swimlane
  83. */
  84. type: String,
  85. defaultValue: 'swimlane',
  86. },
  87. }));
  88. Swimlanes.allow({
  89. insert(userId, doc) {
  90. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  91. },
  92. update(userId, doc) {
  93. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  94. },
  95. remove(userId, doc) {
  96. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  97. },
  98. fetch: ['boardId'],
  99. });
  100. Swimlanes.helpers({
  101. copy(oldBoardId) {
  102. const oldId = this._id;
  103. delete this._id;
  104. const _id = Swimlanes.insert(this);
  105. const query = {
  106. swimlaneId: {$in: [oldId, '']},
  107. archived: false,
  108. };
  109. if (oldBoardId) {
  110. query.boardId = oldBoardId;
  111. }
  112. // Copy all lists in swimlane
  113. Lists.find(query).forEach((list) => {
  114. list.type = 'list';
  115. list.swimlaneId = oldId;
  116. list.boardId = this.boardId;
  117. list.copy(_id);
  118. });
  119. },
  120. cards() {
  121. return Cards.find(Filter.mongoSelector({
  122. swimlaneId: this._id,
  123. archived: false,
  124. }), { sort: ['sort'] });
  125. },
  126. lists() {
  127. return Lists.find(Filter.mongoSelector({
  128. boardId: this.boardId,
  129. swimlaneId: {$in: [this._id, '']},
  130. archived: false,
  131. }), { sort: ['sort'] });
  132. },
  133. allLists() {
  134. return Lists.find({ swimlaneId: this._id });
  135. },
  136. allCards() {
  137. return Cards.find({ swimlaneId: this._id });
  138. },
  139. board() {
  140. return Boards.findOne(this.boardId);
  141. },
  142. colorClass() {
  143. if (this.color)
  144. return this.color;
  145. return '';
  146. },
  147. isTemplateSwimlane() {
  148. return this.type === 'template-swimlane';
  149. },
  150. isTemplateContainer() {
  151. return this.type === 'template-container';
  152. },
  153. isListTemplatesSwimlane() {
  154. const user = Users.findOne(Meteor.userId());
  155. return user.profile.listTemplatesSwimlaneId === this._id;
  156. },
  157. isCardTemplatesSwimlane() {
  158. const user = Users.findOne(Meteor.userId());
  159. return user.profile.cardTemplatesSwimlaneId === this._id;
  160. },
  161. isBoardTemplatesSwimlane() {
  162. const user = Users.findOne(Meteor.userId());
  163. return user.profile.boardTemplatesSwimlaneId === this._id;
  164. },
  165. });
  166. Swimlanes.mutations({
  167. rename(title) {
  168. return { $set: { title } };
  169. },
  170. archive() {
  171. if (this.isTemplateSwimlane()) {
  172. this.lists().forEach((list) => {
  173. return list.archive();
  174. });
  175. }
  176. return { $set: { archived: true } };
  177. },
  178. restore() {
  179. if (this.isTemplateSwimlane()) {
  180. this.allLists().forEach((list) => {
  181. return list.restore();
  182. });
  183. }
  184. return { $set: { archived: false } };
  185. },
  186. setColor(newColor) {
  187. if (newColor === 'silver') {
  188. newColor = null;
  189. }
  190. return {
  191. $set: {
  192. color: newColor,
  193. },
  194. };
  195. },
  196. });
  197. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  198. if (Meteor.isServer) {
  199. Meteor.startup(() => {
  200. Swimlanes._collection._ensureIndex({ boardId: 1 });
  201. });
  202. Swimlanes.after.insert((userId, doc) => {
  203. Activities.insert({
  204. userId,
  205. type: 'swimlane',
  206. activityType: 'createSwimlane',
  207. boardId: doc.boardId,
  208. swimlaneId: doc._id,
  209. });
  210. });
  211. Swimlanes.before.remove((userId, doc) => {
  212. Activities.insert({
  213. userId,
  214. type: 'swimlane',
  215. activityType: 'removeSwimlane',
  216. boardId: doc.boardId,
  217. swimlaneId: doc._id,
  218. title: doc.title,
  219. });
  220. });
  221. Swimlanes.after.update((userId, doc) => {
  222. if (doc.archived) {
  223. Activities.insert({
  224. userId,
  225. type: 'swimlane',
  226. activityType: 'archivedSwimlane',
  227. swimlaneId: doc._id,
  228. boardId: doc.boardId,
  229. });
  230. }
  231. });
  232. }
  233. //SWIMLANE REST API
  234. if (Meteor.isServer) {
  235. /**
  236. * @operation get_all_swimlanes
  237. *
  238. * @summary Get the list of swimlanes attached to a board
  239. *
  240. * @param {string} boardId the ID of the board
  241. * @return_type [{_id: string,
  242. * title: string}]
  243. */
  244. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function (req, res) {
  245. try {
  246. const paramBoardId = req.params.boardId;
  247. Authentication.checkBoardAccess( req.userId, paramBoardId);
  248. JsonRoutes.sendResult(res, {
  249. code: 200,
  250. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  251. return {
  252. _id: doc._id,
  253. title: doc.title,
  254. };
  255. }),
  256. });
  257. }
  258. catch (error) {
  259. JsonRoutes.sendResult(res, {
  260. code: 200,
  261. data: error,
  262. });
  263. }
  264. });
  265. /**
  266. * @operation get_swimlane
  267. *
  268. * @summary Get a swimlane
  269. *
  270. * @param {string} boardId the ID of the board
  271. * @param {string} swimlaneId the ID of the swimlane
  272. * @return_type Swimlanes
  273. */
  274. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  275. try {
  276. const paramBoardId = req.params.boardId;
  277. const paramSwimlaneId = req.params.swimlaneId;
  278. Authentication.checkBoardAccess( req.userId, paramBoardId);
  279. JsonRoutes.sendResult(res, {
  280. code: 200,
  281. data: Swimlanes.findOne({ _id: paramSwimlaneId, boardId: paramBoardId, archived: false }),
  282. });
  283. }
  284. catch (error) {
  285. JsonRoutes.sendResult(res, {
  286. code: 200,
  287. data: error,
  288. });
  289. }
  290. });
  291. /**
  292. * @operation new_swimlane
  293. *
  294. * @summary Add a swimlane to a board
  295. *
  296. * @param {string} boardId the ID of the board
  297. * @param {string} title the new title of the swimlane
  298. * @return_type {_id: string}
  299. */
  300. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function (req, res) {
  301. try {
  302. Authentication.checkUserId( req.userId);
  303. const paramBoardId = req.params.boardId;
  304. const board = Boards.findOne(paramBoardId);
  305. const id = Swimlanes.insert({
  306. title: req.body.title,
  307. boardId: paramBoardId,
  308. sort: board.swimlanes().count(),
  309. });
  310. JsonRoutes.sendResult(res, {
  311. code: 200,
  312. data: {
  313. _id: id,
  314. },
  315. });
  316. }
  317. catch (error) {
  318. JsonRoutes.sendResult(res, {
  319. code: 200,
  320. data: error,
  321. });
  322. }
  323. });
  324. /**
  325. * @operation delete_swimlane
  326. *
  327. * @summary Delete a swimlane
  328. *
  329. * @description The swimlane will be deleted, not moved to the recycle bin
  330. *
  331. * @param {string} boardId the ID of the board
  332. * @param {string} swimlaneId the ID of the swimlane
  333. * @return_type {_id: string}
  334. */
  335. JsonRoutes.add('DELETE', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  336. try {
  337. Authentication.checkUserId( req.userId);
  338. const paramBoardId = req.params.boardId;
  339. const paramSwimlaneId = req.params.swimlaneId;
  340. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  341. JsonRoutes.sendResult(res, {
  342. code: 200,
  343. data: {
  344. _id: paramSwimlaneId,
  345. },
  346. });
  347. }
  348. catch (error) {
  349. JsonRoutes.sendResult(res, {
  350. code: 200,
  351. data: error,
  352. });
  353. }
  354. });
  355. }