lists.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. Lists = new Mongo.Collection('lists');
  2. /**
  3. * A list (column) in the Wekan board.
  4. */
  5. Lists.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the list
  9. */
  10. type: String,
  11. },
  12. archived: {
  13. /**
  14. * is the list 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 board associated to this list
  26. */
  27. type: String,
  28. },
  29. swimlaneId: {
  30. /**
  31. * the swimlane associated to this list. Used for templates
  32. */
  33. type: String,
  34. defaultValue: '',
  35. },
  36. createdAt: {
  37. /**
  38. * creation date
  39. */
  40. type: Date,
  41. autoValue() { // eslint-disable-line consistent-return
  42. if (this.isInsert) {
  43. return new Date();
  44. } else {
  45. this.unset();
  46. }
  47. },
  48. },
  49. sort: {
  50. /**
  51. * is the list sorted
  52. */
  53. type: Number,
  54. decimal: true,
  55. // XXX We should probably provide a default
  56. optional: true,
  57. },
  58. updatedAt: {
  59. /**
  60. * last update of the list
  61. */
  62. type: Date,
  63. optional: true,
  64. autoValue() { // eslint-disable-line consistent-return
  65. if (this.isUpdate) {
  66. return new Date();
  67. } else {
  68. this.unset();
  69. }
  70. },
  71. },
  72. wipLimit: {
  73. /**
  74. * WIP object, see below
  75. */
  76. type: Object,
  77. optional: true,
  78. },
  79. 'wipLimit.value': {
  80. /**
  81. * value of the WIP
  82. */
  83. type: Number,
  84. decimal: false,
  85. defaultValue: 1,
  86. },
  87. 'wipLimit.enabled': {
  88. /**
  89. * is the WIP enabled
  90. */
  91. type: Boolean,
  92. defaultValue: false,
  93. },
  94. 'wipLimit.soft': {
  95. /**
  96. * is the WIP a soft or hard requirement
  97. */
  98. type: Boolean,
  99. defaultValue: false,
  100. },
  101. color: {
  102. /**
  103. * the color of the list
  104. */
  105. type: String,
  106. optional: true,
  107. // silver is the default, so it is left out
  108. allowedValues: [
  109. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  110. 'blue', 'sky', 'lime', 'pink', 'black',
  111. 'peachpuff', 'crimson', 'plum', 'darkgreen',
  112. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  113. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  114. ],
  115. },
  116. type: {
  117. /**
  118. * The type of list
  119. */
  120. type: String,
  121. defaultValue: 'list',
  122. },
  123. }));
  124. Lists.allow({
  125. insert(userId, doc) {
  126. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  127. },
  128. update(userId, doc) {
  129. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  130. },
  131. remove(userId, doc) {
  132. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  133. },
  134. fetch: ['boardId'],
  135. });
  136. Lists.helpers({
  137. copy(boardId, swimlaneId) {
  138. const oldId = this._id;
  139. const oldSwimlaneId = this.swimlaneId || null;
  140. this.boardId = boardId;
  141. this.swimlaneId = swimlaneId;
  142. let _id = null;
  143. existingListWithSameName = Lists.findOne({
  144. boardId,
  145. title: this.title,
  146. archived: false,
  147. });
  148. if (existingListWithSameName) {
  149. _id = existingListWithSameName._id;
  150. } else {
  151. delete this._id;
  152. delete this.swimlaneId;
  153. _id = Lists.insert(this);
  154. }
  155. // Copy all cards in list
  156. Cards.find({
  157. swimlaneId: oldSwimlaneId,
  158. listId: oldId,
  159. archived: false,
  160. }).forEach((card) => {
  161. card.copy(boardId, swimlaneId, _id);
  162. });
  163. },
  164. cards(swimlaneId) {
  165. const selector = {
  166. listId: this._id,
  167. archived: false,
  168. };
  169. if (swimlaneId)
  170. selector.swimlaneId = swimlaneId;
  171. return Cards.find(Filter.mongoSelector(selector),
  172. { sort: ['sort'] });
  173. },
  174. cardsUnfiltered(swimlaneId) {
  175. const selector = {
  176. listId: this._id,
  177. archived: false,
  178. };
  179. if (swimlaneId)
  180. selector.swimlaneId = swimlaneId;
  181. return Cards.find(selector,
  182. { sort: ['sort'] });
  183. },
  184. allCards() {
  185. return Cards.find({ listId: this._id });
  186. },
  187. board() {
  188. return Boards.findOne(this.boardId);
  189. },
  190. getWipLimit(option){
  191. const list = Lists.findOne({ _id: this._id });
  192. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  193. return 0;
  194. } else if(!option) {
  195. return list.wipLimit;
  196. } else {
  197. return list.wipLimit[option] ? list.wipLimit[option] : 0; // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  198. }
  199. },
  200. colorClass() {
  201. if (this.color)
  202. return this.color;
  203. return '';
  204. },
  205. isTemplateList() {
  206. return this.type === 'template-list';
  207. },
  208. remove() {
  209. Lists.remove({ _id: this._id});
  210. },
  211. });
  212. Lists.mutations({
  213. rename(title) {
  214. return { $set: { title } };
  215. },
  216. archive() {
  217. if (this.isTemplateList()) {
  218. this.cards().forEach((card) => {
  219. return card.archive();
  220. });
  221. }
  222. return { $set: { archived: true } };
  223. },
  224. restore() {
  225. if (this.isTemplateList()) {
  226. this.allCards().forEach((card) => {
  227. return card.restore();
  228. });
  229. }
  230. return { $set: { archived: false } };
  231. },
  232. toggleSoftLimit(toggle) {
  233. return { $set: { 'wipLimit.soft': toggle } };
  234. },
  235. toggleWipLimit(toggle) {
  236. return { $set: { 'wipLimit.enabled': toggle } };
  237. },
  238. setWipLimit(limit) {
  239. return { $set: { 'wipLimit.value': limit } };
  240. },
  241. setColor(newColor) {
  242. if (newColor === 'silver') {
  243. newColor = null;
  244. }
  245. return {
  246. $set: {
  247. color: newColor,
  248. },
  249. };
  250. },
  251. });
  252. Meteor.methods({
  253. applyWipLimit(listId, limit){
  254. check(listId, String);
  255. check(limit, Number);
  256. if(limit === 0){
  257. limit = 1;
  258. }
  259. Lists.findOne({ _id: listId }).setWipLimit(limit);
  260. },
  261. enableWipLimit(listId) {
  262. check(listId, String);
  263. const list = Lists.findOne({ _id: listId });
  264. if(list.getWipLimit('value') === 0){
  265. list.setWipLimit(1);
  266. }
  267. list.toggleWipLimit(!list.getWipLimit('enabled'));
  268. },
  269. enableSoftLimit(listId) {
  270. check(listId, String);
  271. const list = Lists.findOne({ _id: listId });
  272. list.toggleSoftLimit(!list.getWipLimit('soft'));
  273. },
  274. });
  275. Lists.hookOptions.after.update = { fetchPrevious: false };
  276. if (Meteor.isServer) {
  277. Meteor.startup(() => {
  278. Lists._collection._ensureIndex({ boardId: 1 });
  279. });
  280. Lists.after.insert((userId, doc) => {
  281. Activities.insert({
  282. userId,
  283. type: 'list',
  284. activityType: 'createList',
  285. boardId: doc.boardId,
  286. listId: doc._id,
  287. });
  288. });
  289. Lists.before.remove((userId, doc) => {
  290. const cards = Cards.find({ listId: doc._id });
  291. if (cards) {
  292. cards.forEach((card) => {
  293. Cards.remove(card._id);
  294. });
  295. }
  296. Activities.insert({
  297. userId,
  298. type: 'list',
  299. activityType: 'removeList',
  300. boardId: doc.boardId,
  301. listId: doc._id,
  302. title: doc.title,
  303. });
  304. });
  305. Lists.after.update((userId, doc) => {
  306. if (doc.archived) {
  307. Activities.insert({
  308. userId,
  309. type: 'list',
  310. activityType: 'archivedList',
  311. listId: doc._id,
  312. boardId: doc.boardId,
  313. });
  314. }
  315. });
  316. }
  317. //LISTS REST API
  318. if (Meteor.isServer) {
  319. /**
  320. * @operation get_all_lists
  321. * @summary Get the list of Lists attached to a board
  322. *
  323. * @param {string} boardId the board ID
  324. * @return_type [{_id: string,
  325. * title: string}]
  326. */
  327. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  328. try {
  329. const paramBoardId = req.params.boardId;
  330. Authentication.checkBoardAccess( req.userId, paramBoardId);
  331. JsonRoutes.sendResult(res, {
  332. code: 200,
  333. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  334. return {
  335. _id: doc._id,
  336. title: doc.title,
  337. };
  338. }),
  339. });
  340. }
  341. catch (error) {
  342. JsonRoutes.sendResult(res, {
  343. code: 200,
  344. data: error,
  345. });
  346. }
  347. });
  348. /**
  349. * @operation get_list
  350. * @summary Get a List attached to a board
  351. *
  352. * @param {string} boardId the board ID
  353. * @param {string} listId the List ID
  354. * @return_type Lists
  355. */
  356. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  357. try {
  358. const paramBoardId = req.params.boardId;
  359. const paramListId = req.params.listId;
  360. Authentication.checkBoardAccess( req.userId, paramBoardId);
  361. JsonRoutes.sendResult(res, {
  362. code: 200,
  363. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  364. });
  365. }
  366. catch (error) {
  367. JsonRoutes.sendResult(res, {
  368. code: 200,
  369. data: error,
  370. });
  371. }
  372. });
  373. /**
  374. * @operation new_list
  375. * @summary Add a List to a board
  376. *
  377. * @param {string} boardId the board ID
  378. * @param {string} title the title of the List
  379. * @return_type {_id: string}
  380. */
  381. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  382. try {
  383. Authentication.checkUserId( req.userId);
  384. const paramBoardId = req.params.boardId;
  385. const board = Boards.findOne(paramBoardId);
  386. const id = Lists.insert({
  387. title: req.body.title,
  388. boardId: paramBoardId,
  389. sort: board.lists().count(),
  390. });
  391. JsonRoutes.sendResult(res, {
  392. code: 200,
  393. data: {
  394. _id: id,
  395. },
  396. });
  397. }
  398. catch (error) {
  399. JsonRoutes.sendResult(res, {
  400. code: 200,
  401. data: error,
  402. });
  403. }
  404. });
  405. /**
  406. * @operation delete_list
  407. * @summary Delete a List
  408. *
  409. * @description This **deletes** a list from a board.
  410. * The list is not put in the recycle bin.
  411. *
  412. * @param {string} boardId the board ID
  413. * @param {string} listId the ID of the list to remove
  414. * @return_type {_id: string}
  415. */
  416. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  417. try {
  418. Authentication.checkUserId( req.userId);
  419. const paramBoardId = req.params.boardId;
  420. const paramListId = req.params.listId;
  421. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  422. JsonRoutes.sendResult(res, {
  423. code: 200,
  424. data: {
  425. _id: paramListId,
  426. },
  427. });
  428. }
  429. catch (error) {
  430. JsonRoutes.sendResult(res, {
  431. code: 200,
  432. data: error,
  433. });
  434. }
  435. });
  436. }