swimlanes.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. remove() {
  168. Swimlanes.remove({ _id: this._id});
  169. },
  170. });
  171. Swimlanes.mutations({
  172. rename(title) {
  173. return { $set: { title } };
  174. },
  175. archive() {
  176. if (this.isTemplateSwimlane()) {
  177. this.myLists().forEach((list) => {
  178. return list.archive();
  179. });
  180. }
  181. return { $set: { archived: true } };
  182. },
  183. restore() {
  184. if (this.isTemplateSwimlane()) {
  185. this.myLists().forEach((list) => {
  186. return list.restore();
  187. });
  188. }
  189. return { $set: { archived: false } };
  190. },
  191. setColor(newColor) {
  192. if (newColor === 'silver') {
  193. newColor = null;
  194. }
  195. return {
  196. $set: {
  197. color: newColor,
  198. },
  199. };
  200. },
  201. });
  202. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  203. if (Meteor.isServer) {
  204. Meteor.startup(() => {
  205. Swimlanes._collection._ensureIndex({ boardId: 1 });
  206. });
  207. Swimlanes.after.insert((userId, doc) => {
  208. Activities.insert({
  209. userId,
  210. type: 'swimlane',
  211. activityType: 'createSwimlane',
  212. boardId: doc.boardId,
  213. swimlaneId: doc._id,
  214. });
  215. });
  216. Swimlanes.before.remove(function(userId, doc) {
  217. const lists = Lists.find({
  218. boardId: doc.boardId,
  219. swimlaneId: {$in: [doc._id, '']},
  220. archived: false,
  221. }, { sort: ['sort'] });
  222. if (lists.count() < 2) {
  223. lists.forEach((list) => {
  224. list.remove();
  225. });
  226. } else {
  227. Cards.remove({swimlaneId: doc._id});
  228. }
  229. Activities.insert({
  230. userId,
  231. type: 'swimlane',
  232. activityType: 'removeSwimlane',
  233. boardId: doc.boardId,
  234. swimlaneId: doc._id,
  235. title: doc.title,
  236. });
  237. });
  238. Swimlanes.after.update((userId, doc) => {
  239. if (doc.archived) {
  240. Activities.insert({
  241. userId,
  242. type: 'swimlane',
  243. activityType: 'archivedSwimlane',
  244. swimlaneId: doc._id,
  245. boardId: doc.boardId,
  246. });
  247. }
  248. });
  249. }
  250. //SWIMLANE REST API
  251. if (Meteor.isServer) {
  252. /**
  253. * @operation get_all_swimlanes
  254. *
  255. * @summary Get the list of swimlanes attached to a board
  256. *
  257. * @param {string} boardId the ID of the board
  258. * @return_type [{_id: string,
  259. * title: string}]
  260. */
  261. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function (req, res) {
  262. try {
  263. const paramBoardId = req.params.boardId;
  264. Authentication.checkBoardAccess( req.userId, paramBoardId);
  265. JsonRoutes.sendResult(res, {
  266. code: 200,
  267. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  268. return {
  269. _id: doc._id,
  270. title: doc.title,
  271. };
  272. }),
  273. });
  274. }
  275. catch (error) {
  276. JsonRoutes.sendResult(res, {
  277. code: 200,
  278. data: error,
  279. });
  280. }
  281. });
  282. /**
  283. * @operation get_swimlane
  284. *
  285. * @summary Get a swimlane
  286. *
  287. * @param {string} boardId the ID of the board
  288. * @param {string} swimlaneId the ID of the swimlane
  289. * @return_type Swimlanes
  290. */
  291. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  292. try {
  293. const paramBoardId = req.params.boardId;
  294. const paramSwimlaneId = req.params.swimlaneId;
  295. Authentication.checkBoardAccess( req.userId, paramBoardId);
  296. JsonRoutes.sendResult(res, {
  297. code: 200,
  298. data: Swimlanes.findOne({ _id: paramSwimlaneId, boardId: paramBoardId, archived: false }),
  299. });
  300. }
  301. catch (error) {
  302. JsonRoutes.sendResult(res, {
  303. code: 200,
  304. data: error,
  305. });
  306. }
  307. });
  308. /**
  309. * @operation new_swimlane
  310. *
  311. * @summary Add a swimlane to a board
  312. *
  313. * @param {string} boardId the ID of the board
  314. * @param {string} title the new title of the swimlane
  315. * @return_type {_id: string}
  316. */
  317. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function (req, res) {
  318. try {
  319. Authentication.checkUserId( req.userId);
  320. const paramBoardId = req.params.boardId;
  321. const board = Boards.findOne(paramBoardId);
  322. const id = Swimlanes.insert({
  323. title: req.body.title,
  324. boardId: paramBoardId,
  325. sort: board.swimlanes().count(),
  326. });
  327. JsonRoutes.sendResult(res, {
  328. code: 200,
  329. data: {
  330. _id: id,
  331. },
  332. });
  333. }
  334. catch (error) {
  335. JsonRoutes.sendResult(res, {
  336. code: 200,
  337. data: error,
  338. });
  339. }
  340. });
  341. /**
  342. * @operation delete_swimlane
  343. *
  344. * @summary Delete a swimlane
  345. *
  346. * @description The swimlane will be deleted, not moved to the recycle bin
  347. *
  348. * @param {string} boardId the ID of the board
  349. * @param {string} swimlaneId the ID of the swimlane
  350. * @return_type {_id: string}
  351. */
  352. JsonRoutes.add('DELETE', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  353. try {
  354. Authentication.checkUserId( req.userId);
  355. const paramBoardId = req.params.boardId;
  356. const paramSwimlaneId = req.params.swimlaneId;
  357. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  358. JsonRoutes.sendResult(res, {
  359. code: 200,
  360. data: {
  361. _id: paramSwimlaneId,
  362. },
  363. });
  364. }
  365. catch (error) {
  366. JsonRoutes.sendResult(res, {
  367. code: 200,
  368. data: error,
  369. });
  370. }
  371. });
  372. }