lists.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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(swimlaneId) {
  138. const oldId = this._id;
  139. const oldSwimlaneId = this.swimlaneId || null;
  140. let _id = null;
  141. existingListWithSameName = Lists.findOne({
  142. boardId: this.boardId,
  143. title: this.title,
  144. archived: false,
  145. });
  146. if (existingListWithSameName) {
  147. _id = existingListWithSameName._id;
  148. } else {
  149. delete this._id;
  150. delete this.swimlaneId;
  151. _id = Lists.insert(this);
  152. }
  153. // Copy all cards in list
  154. Cards.find({
  155. swimlaneId: oldSwimlaneId,
  156. listId: oldId,
  157. archived: false,
  158. }).forEach((card) => {
  159. card.type = 'cardType-card';
  160. card.listId = _id;
  161. card.boardId = this.boardId;
  162. card.swimlaneId = swimlaneId;
  163. card.copy();
  164. });
  165. },
  166. cards(swimlaneId) {
  167. const selector = {
  168. listId: this._id,
  169. archived: false,
  170. };
  171. if (swimlaneId)
  172. selector.swimlaneId = swimlaneId;
  173. return Cards.find(Filter.mongoSelector(selector),
  174. { sort: ['sort'] });
  175. },
  176. cardsUnfiltered(swimlaneId) {
  177. const selector = {
  178. listId: this._id,
  179. archived: false,
  180. };
  181. if (swimlaneId)
  182. selector.swimlaneId = swimlaneId;
  183. return Cards.find(selector,
  184. { sort: ['sort'] });
  185. },
  186. allCards() {
  187. return Cards.find({ listId: this._id });
  188. },
  189. board() {
  190. return Boards.findOne(this.boardId);
  191. },
  192. getWipLimit(option){
  193. const list = Lists.findOne({ _id: this._id });
  194. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  195. return 0;
  196. } else if(!option) {
  197. return list.wipLimit;
  198. } else {
  199. 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
  200. }
  201. },
  202. colorClass() {
  203. if (this.color)
  204. return this.color;
  205. return '';
  206. },
  207. isTemplateList() {
  208. return this.type === 'template-list';
  209. },
  210. });
  211. Lists.mutations({
  212. rename(title) {
  213. return { $set: { title } };
  214. },
  215. archive() {
  216. if (this.isTemplateList()) {
  217. this.cards().forEach((card) => {
  218. return card.archive();
  219. });
  220. }
  221. return { $set: { archived: true } };
  222. },
  223. restore() {
  224. if (this.isTemplateList()) {
  225. this.allCards().forEach((card) => {
  226. return card.restore();
  227. });
  228. }
  229. return { $set: { archived: false } };
  230. },
  231. toggleSoftLimit(toggle) {
  232. return { $set: { 'wipLimit.soft': toggle } };
  233. },
  234. toggleWipLimit(toggle) {
  235. return { $set: { 'wipLimit.enabled': toggle } };
  236. },
  237. setWipLimit(limit) {
  238. return { $set: { 'wipLimit.value': limit } };
  239. },
  240. setColor(newColor) {
  241. if (newColor === 'silver') {
  242. newColor = null;
  243. }
  244. return {
  245. $set: {
  246. color: newColor,
  247. },
  248. };
  249. },
  250. });
  251. Meteor.methods({
  252. applyWipLimit(listId, limit){
  253. check(listId, String);
  254. check(limit, Number);
  255. if(limit === 0){
  256. limit = 1;
  257. }
  258. Lists.findOne({ _id: listId }).setWipLimit(limit);
  259. },
  260. enableWipLimit(listId) {
  261. check(listId, String);
  262. const list = Lists.findOne({ _id: listId });
  263. if(list.getWipLimit('value') === 0){
  264. list.setWipLimit(1);
  265. }
  266. list.toggleWipLimit(!list.getWipLimit('enabled'));
  267. },
  268. enableSoftLimit(listId) {
  269. check(listId, String);
  270. const list = Lists.findOne({ _id: listId });
  271. list.toggleSoftLimit(!list.getWipLimit('soft'));
  272. },
  273. });
  274. Lists.hookOptions.after.update = { fetchPrevious: false };
  275. if (Meteor.isServer) {
  276. Meteor.startup(() => {
  277. Lists._collection._ensureIndex({ boardId: 1 });
  278. });
  279. Lists.after.insert((userId, doc) => {
  280. Activities.insert({
  281. userId,
  282. type: 'list',
  283. activityType: 'createList',
  284. boardId: doc.boardId,
  285. listId: doc._id,
  286. });
  287. });
  288. Lists.before.remove((userId, doc) => {
  289. Activities.insert({
  290. userId,
  291. type: 'list',
  292. activityType: 'removeList',
  293. boardId: doc.boardId,
  294. listId: doc._id,
  295. title: doc.title,
  296. });
  297. });
  298. Lists.after.update((userId, doc) => {
  299. if (doc.archived) {
  300. Activities.insert({
  301. userId,
  302. type: 'list',
  303. activityType: 'archivedList',
  304. listId: doc._id,
  305. boardId: doc.boardId,
  306. });
  307. }
  308. });
  309. }
  310. //LISTS REST API
  311. if (Meteor.isServer) {
  312. /**
  313. * @operation get_all_lists
  314. * @summary Get the list of Lists attached to a board
  315. *
  316. * @param {string} boardId the board ID
  317. * @return_type [{_id: string,
  318. * title: string}]
  319. */
  320. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  321. try {
  322. const paramBoardId = req.params.boardId;
  323. Authentication.checkBoardAccess( req.userId, paramBoardId);
  324. JsonRoutes.sendResult(res, {
  325. code: 200,
  326. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  327. return {
  328. _id: doc._id,
  329. title: doc.title,
  330. };
  331. }),
  332. });
  333. }
  334. catch (error) {
  335. JsonRoutes.sendResult(res, {
  336. code: 200,
  337. data: error,
  338. });
  339. }
  340. });
  341. /**
  342. * @operation get_list
  343. * @summary Get a List attached to a board
  344. *
  345. * @param {string} boardId the board ID
  346. * @param {string} listId the List ID
  347. * @return_type Lists
  348. */
  349. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  350. try {
  351. const paramBoardId = req.params.boardId;
  352. const paramListId = req.params.listId;
  353. Authentication.checkBoardAccess( req.userId, paramBoardId);
  354. JsonRoutes.sendResult(res, {
  355. code: 200,
  356. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  357. });
  358. }
  359. catch (error) {
  360. JsonRoutes.sendResult(res, {
  361. code: 200,
  362. data: error,
  363. });
  364. }
  365. });
  366. /**
  367. * @operation new_list
  368. * @summary Add a List to a board
  369. *
  370. * @param {string} boardId the board ID
  371. * @param {string} title the title of the List
  372. * @return_type {_id: string}
  373. */
  374. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  375. try {
  376. Authentication.checkUserId( req.userId);
  377. const paramBoardId = req.params.boardId;
  378. const board = Boards.findOne(paramBoardId);
  379. const id = Lists.insert({
  380. title: req.body.title,
  381. boardId: paramBoardId,
  382. sort: board.lists().count(),
  383. });
  384. JsonRoutes.sendResult(res, {
  385. code: 200,
  386. data: {
  387. _id: id,
  388. },
  389. });
  390. }
  391. catch (error) {
  392. JsonRoutes.sendResult(res, {
  393. code: 200,
  394. data: error,
  395. });
  396. }
  397. });
  398. /**
  399. * @operation delete_list
  400. * @summary Delete a List
  401. *
  402. * @description This **deletes** a list from a board.
  403. * The list is not put in the recycle bin.
  404. *
  405. * @param {string} boardId the board ID
  406. * @param {string} listId the ID of the list to remove
  407. * @return_type {_id: string}
  408. */
  409. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  410. try {
  411. Authentication.checkUserId( req.userId);
  412. const paramBoardId = req.params.boardId;
  413. const paramListId = req.params.listId;
  414. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  415. JsonRoutes.sendResult(res, {
  416. code: 200,
  417. data: {
  418. _id: paramListId,
  419. },
  420. });
  421. }
  422. catch (error) {
  423. JsonRoutes.sendResult(res, {
  424. code: 200,
  425. data: error,
  426. });
  427. }
  428. });
  429. }