swimlanes.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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(boardId) {
  102. const oldId = this._id;
  103. const oldBoardId = this.boardId;
  104. this.boardId = boardId;
  105. delete this._id;
  106. const _id = Swimlanes.insert(this);
  107. const query = {
  108. swimlaneId: {$in: [oldId, '']},
  109. archived: false,
  110. };
  111. if (oldBoardId) {
  112. query.boardId = oldBoardId;
  113. }
  114. // Copy all lists in swimlane
  115. Lists.find(query).forEach((list) => {
  116. list.type = 'list';
  117. list.swimlaneId = oldId;
  118. list.boardId = boardId;
  119. list.copy(boardId, _id);
  120. });
  121. },
  122. cards() {
  123. return Cards.find(Filter.mongoSelector({
  124. swimlaneId: this._id,
  125. archived: false,
  126. }), { sort: ['sort'] });
  127. },
  128. lists() {
  129. return Lists.find({
  130. boardId: this.boardId,
  131. swimlaneId: {$in: [this._id, '']},
  132. archived: false,
  133. }, { sort: ['sort'] });
  134. },
  135. myLists() {
  136. return Lists.find({ swimlaneId: this._id });
  137. },
  138. allCards() {
  139. return Cards.find({ swimlaneId: this._id });
  140. },
  141. board() {
  142. return Boards.findOne(this.boardId);
  143. },
  144. colorClass() {
  145. if (this.color)
  146. return this.color;
  147. return '';
  148. },
  149. isTemplateSwimlane() {
  150. return this.type === 'template-swimlane';
  151. },
  152. isTemplateContainer() {
  153. return this.type === 'template-container';
  154. },
  155. isListTemplatesSwimlane() {
  156. const user = Users.findOne(Meteor.userId());
  157. return user.profile.listTemplatesSwimlaneId === this._id;
  158. },
  159. isCardTemplatesSwimlane() {
  160. const user = Users.findOne(Meteor.userId());
  161. return user.profile.cardTemplatesSwimlaneId === this._id;
  162. },
  163. isBoardTemplatesSwimlane() {
  164. const user = Users.findOne(Meteor.userId());
  165. return user.profile.boardTemplatesSwimlaneId === this._id;
  166. },
  167. });
  168. Swimlanes.mutations({
  169. rename(title) {
  170. return { $set: { title } };
  171. },
  172. archive() {
  173. if (this.isTemplateSwimlane()) {
  174. this.myLists().forEach((list) => {
  175. return list.archive();
  176. });
  177. }
  178. return { $set: { archived: true } };
  179. },
  180. restore() {
  181. if (this.isTemplateSwimlane()) {
  182. this.myLists().forEach((list) => {
  183. return list.restore();
  184. });
  185. }
  186. return { $set: { archived: false } };
  187. },
  188. setColor(newColor) {
  189. if (newColor === 'silver') {
  190. newColor = null;
  191. }
  192. return {
  193. $set: {
  194. color: newColor,
  195. },
  196. };
  197. },
  198. });
  199. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  200. if (Meteor.isServer) {
  201. Meteor.startup(() => {
  202. Swimlanes._collection._ensureIndex({ boardId: 1 });
  203. });
  204. Swimlanes.after.insert((userId, doc) => {
  205. Activities.insert({
  206. userId,
  207. type: 'swimlane',
  208. activityType: 'createSwimlane',
  209. boardId: doc.boardId,
  210. swimlaneId: doc._id,
  211. });
  212. });
  213. Swimlanes.before.remove((userId, doc) => {
  214. Activities.insert({
  215. userId,
  216. type: 'swimlane',
  217. activityType: 'removeSwimlane',
  218. boardId: doc.boardId,
  219. swimlaneId: doc._id,
  220. title: doc.title,
  221. });
  222. });
  223. Swimlanes.after.update((userId, doc) => {
  224. if (doc.archived) {
  225. Activities.insert({
  226. userId,
  227. type: 'swimlane',
  228. activityType: 'archivedSwimlane',
  229. swimlaneId: doc._id,
  230. boardId: doc.boardId,
  231. });
  232. }
  233. });
  234. }
  235. //SWIMLANE REST API
  236. if (Meteor.isServer) {
  237. /**
  238. * @operation get_all_swimlanes
  239. *
  240. * @summary Get the list of swimlanes attached to a board
  241. *
  242. * @param {string} boardId the ID of the board
  243. * @return_type [{_id: string,
  244. * title: string}]
  245. */
  246. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function (req, res) {
  247. try {
  248. const paramBoardId = req.params.boardId;
  249. Authentication.checkBoardAccess( req.userId, paramBoardId);
  250. JsonRoutes.sendResult(res, {
  251. code: 200,
  252. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  253. return {
  254. _id: doc._id,
  255. title: doc.title,
  256. };
  257. }),
  258. });
  259. }
  260. catch (error) {
  261. JsonRoutes.sendResult(res, {
  262. code: 200,
  263. data: error,
  264. });
  265. }
  266. });
  267. /**
  268. * @operation get_swimlane
  269. *
  270. * @summary Get a swimlane
  271. *
  272. * @param {string} boardId the ID of the board
  273. * @param {string} swimlaneId the ID of the swimlane
  274. * @return_type Swimlanes
  275. */
  276. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  277. try {
  278. const paramBoardId = req.params.boardId;
  279. const paramSwimlaneId = req.params.swimlaneId;
  280. Authentication.checkBoardAccess( req.userId, paramBoardId);
  281. JsonRoutes.sendResult(res, {
  282. code: 200,
  283. data: Swimlanes.findOne({ _id: paramSwimlaneId, boardId: paramBoardId, archived: false }),
  284. });
  285. }
  286. catch (error) {
  287. JsonRoutes.sendResult(res, {
  288. code: 200,
  289. data: error,
  290. });
  291. }
  292. });
  293. /**
  294. * @operation new_swimlane
  295. *
  296. * @summary Add a swimlane to a board
  297. *
  298. * @param {string} boardId the ID of the board
  299. * @param {string} title the new title of the swimlane
  300. * @return_type {_id: string}
  301. */
  302. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function (req, res) {
  303. try {
  304. Authentication.checkUserId( req.userId);
  305. const paramBoardId = req.params.boardId;
  306. const board = Boards.findOne(paramBoardId);
  307. const id = Swimlanes.insert({
  308. title: req.body.title,
  309. boardId: paramBoardId,
  310. sort: board.swimlanes().count(),
  311. });
  312. JsonRoutes.sendResult(res, {
  313. code: 200,
  314. data: {
  315. _id: id,
  316. },
  317. });
  318. }
  319. catch (error) {
  320. JsonRoutes.sendResult(res, {
  321. code: 200,
  322. data: error,
  323. });
  324. }
  325. });
  326. /**
  327. * @operation delete_swimlane
  328. *
  329. * @summary Delete a swimlane
  330. *
  331. * @description The swimlane will be deleted, not moved to the recycle bin
  332. *
  333. * @param {string} boardId the ID of the board
  334. * @param {string} swimlaneId the ID of the swimlane
  335. * @return_type {_id: string}
  336. */
  337. JsonRoutes.add('DELETE', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  338. try {
  339. Authentication.checkUserId( req.userId);
  340. const paramBoardId = req.params.boardId;
  341. const paramSwimlaneId = req.params.swimlaneId;
  342. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  343. JsonRoutes.sendResult(res, {
  344. code: 200,
  345. data: {
  346. _id: paramSwimlaneId,
  347. },
  348. });
  349. }
  350. catch (error) {
  351. JsonRoutes.sendResult(res, {
  352. code: 200,
  353. data: error,
  354. });
  355. }
  356. });
  357. }