lists.js 13 KB

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