2
0

lists.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. });
  209. Lists.mutations({
  210. rename(title) {
  211. return { $set: { title } };
  212. },
  213. archive() {
  214. if (this.isTemplateList()) {
  215. this.cards().forEach((card) => {
  216. return card.archive();
  217. });
  218. }
  219. return { $set: { archived: true } };
  220. },
  221. restore() {
  222. if (this.isTemplateList()) {
  223. this.allCards().forEach((card) => {
  224. return card.restore();
  225. });
  226. }
  227. return { $set: { archived: false } };
  228. },
  229. toggleSoftLimit(toggle) {
  230. return { $set: { 'wipLimit.soft': toggle } };
  231. },
  232. toggleWipLimit(toggle) {
  233. return { $set: { 'wipLimit.enabled': toggle } };
  234. },
  235. setWipLimit(limit) {
  236. return { $set: { 'wipLimit.value': limit } };
  237. },
  238. setColor(newColor) {
  239. if (newColor === 'silver') {
  240. newColor = null;
  241. }
  242. return {
  243. $set: {
  244. color: newColor,
  245. },
  246. };
  247. },
  248. });
  249. Meteor.methods({
  250. applyWipLimit(listId, limit){
  251. check(listId, String);
  252. check(limit, Number);
  253. if(limit === 0){
  254. limit = 1;
  255. }
  256. Lists.findOne({ _id: listId }).setWipLimit(limit);
  257. },
  258. enableWipLimit(listId) {
  259. check(listId, String);
  260. const list = Lists.findOne({ _id: listId });
  261. if(list.getWipLimit('value') === 0){
  262. list.setWipLimit(1);
  263. }
  264. list.toggleWipLimit(!list.getWipLimit('enabled'));
  265. },
  266. enableSoftLimit(listId) {
  267. check(listId, String);
  268. const list = Lists.findOne({ _id: listId });
  269. list.toggleSoftLimit(!list.getWipLimit('soft'));
  270. },
  271. });
  272. Lists.hookOptions.after.update = { fetchPrevious: false };
  273. if (Meteor.isServer) {
  274. Meteor.startup(() => {
  275. Lists._collection._ensureIndex({ boardId: 1 });
  276. });
  277. Lists.after.insert((userId, doc) => {
  278. Activities.insert({
  279. userId,
  280. type: 'list',
  281. activityType: 'createList',
  282. boardId: doc.boardId,
  283. listId: doc._id,
  284. });
  285. });
  286. Lists.before.remove((userId, doc) => {
  287. Activities.insert({
  288. userId,
  289. type: 'list',
  290. activityType: 'removeList',
  291. boardId: doc.boardId,
  292. listId: doc._id,
  293. title: doc.title,
  294. });
  295. });
  296. Lists.after.update((userId, doc) => {
  297. if (doc.archived) {
  298. Activities.insert({
  299. userId,
  300. type: 'list',
  301. activityType: 'archivedList',
  302. listId: doc._id,
  303. boardId: doc.boardId,
  304. });
  305. }
  306. });
  307. }
  308. //LISTS REST API
  309. if (Meteor.isServer) {
  310. /**
  311. * @operation get_all_lists
  312. * @summary Get the list of Lists attached to a board
  313. *
  314. * @param {string} boardId the board ID
  315. * @return_type [{_id: string,
  316. * title: string}]
  317. */
  318. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  319. try {
  320. const paramBoardId = req.params.boardId;
  321. Authentication.checkBoardAccess( req.userId, paramBoardId);
  322. JsonRoutes.sendResult(res, {
  323. code: 200,
  324. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  325. return {
  326. _id: doc._id,
  327. title: doc.title,
  328. };
  329. }),
  330. });
  331. }
  332. catch (error) {
  333. JsonRoutes.sendResult(res, {
  334. code: 200,
  335. data: error,
  336. });
  337. }
  338. });
  339. /**
  340. * @operation get_list
  341. * @summary Get a List attached to a board
  342. *
  343. * @param {string} boardId the board ID
  344. * @param {string} listId the List ID
  345. * @return_type Lists
  346. */
  347. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  348. try {
  349. const paramBoardId = req.params.boardId;
  350. const paramListId = req.params.listId;
  351. Authentication.checkBoardAccess( req.userId, paramBoardId);
  352. JsonRoutes.sendResult(res, {
  353. code: 200,
  354. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  355. });
  356. }
  357. catch (error) {
  358. JsonRoutes.sendResult(res, {
  359. code: 200,
  360. data: error,
  361. });
  362. }
  363. });
  364. /**
  365. * @operation new_list
  366. * @summary Add a List to a board
  367. *
  368. * @param {string} boardId the board ID
  369. * @param {string} title the title of the List
  370. * @return_type {_id: string}
  371. */
  372. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  373. try {
  374. Authentication.checkUserId( req.userId);
  375. const paramBoardId = req.params.boardId;
  376. const board = Boards.findOne(paramBoardId);
  377. const id = Lists.insert({
  378. title: req.body.title,
  379. boardId: paramBoardId,
  380. sort: board.lists().count(),
  381. });
  382. JsonRoutes.sendResult(res, {
  383. code: 200,
  384. data: {
  385. _id: id,
  386. },
  387. });
  388. }
  389. catch (error) {
  390. JsonRoutes.sendResult(res, {
  391. code: 200,
  392. data: error,
  393. });
  394. }
  395. });
  396. /**
  397. * @operation delete_list
  398. * @summary Delete a List
  399. *
  400. * @description This **deletes** a list from a board.
  401. * The list is not put in the recycle bin.
  402. *
  403. * @param {string} boardId the board ID
  404. * @param {string} listId the ID of the list to remove
  405. * @return_type {_id: string}
  406. */
  407. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  408. try {
  409. Authentication.checkUserId( req.userId);
  410. const paramBoardId = req.params.boardId;
  411. const paramListId = req.params.listId;
  412. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  413. JsonRoutes.sendResult(res, {
  414. code: 200,
  415. data: {
  416. _id: paramListId,
  417. },
  418. });
  419. }
  420. catch (error) {
  421. JsonRoutes.sendResult(res, {
  422. code: 200,
  423. data: error,
  424. });
  425. }
  426. });
  427. }