lists.js 12 KB

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