lists.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. {
  382. boardId: { $in: Boards.userBoardIds(this.userId) },
  383. archived: false,
  384. },
  385. {
  386. fields: { title: 1 },
  387. },
  388. )
  389. .fetch()
  390. .map(list => {
  391. return list.title;
  392. }),
  393. ).sort();
  394. },
  395. });
  396. Lists.hookOptions.after.update = { fetchPrevious: false };
  397. if (Meteor.isServer) {
  398. Meteor.startup(() => {
  399. Lists._collection._ensureIndex({ modifiedAt: -1 });
  400. Lists._collection._ensureIndex({ boardId: 1 });
  401. Lists._collection._ensureIndex({ archivedAt: -1 });
  402. });
  403. Lists.after.insert((userId, doc) => {
  404. Activities.insert({
  405. userId,
  406. type: 'list',
  407. activityType: 'createList',
  408. boardId: doc.boardId,
  409. listId: doc._id,
  410. // this preserves the name so that the activity can be useful after the
  411. // list is deleted
  412. title: doc.title,
  413. });
  414. });
  415. Lists.before.remove((userId, doc) => {
  416. const cards = Cards.find({ listId: doc._id });
  417. if (cards) {
  418. cards.forEach(card => {
  419. Cards.remove(card._id);
  420. });
  421. }
  422. Activities.insert({
  423. userId,
  424. type: 'list',
  425. activityType: 'removeList',
  426. boardId: doc.boardId,
  427. listId: doc._id,
  428. title: doc.title,
  429. });
  430. });
  431. Lists.after.update((userId, doc) => {
  432. if (doc.archived) {
  433. Activities.insert({
  434. userId,
  435. type: 'list',
  436. activityType: 'archivedList',
  437. listId: doc._id,
  438. boardId: doc.boardId,
  439. // this preserves the name so that the activity can be useful after the
  440. // list is deleted
  441. title: doc.title,
  442. });
  443. }
  444. });
  445. }
  446. //LISTS REST API
  447. if (Meteor.isServer) {
  448. /**
  449. * @operation get_all_lists
  450. * @summary Get the list of Lists attached to a board
  451. *
  452. * @param {string} boardId the board ID
  453. * @return_type [{_id: string,
  454. * title: string}]
  455. */
  456. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  457. try {
  458. const paramBoardId = req.params.boardId;
  459. Authentication.checkBoardAccess(req.userId, paramBoardId);
  460. JsonRoutes.sendResult(res, {
  461. code: 200,
  462. data: Lists.find({ boardId: paramBoardId, archived: false }).map(
  463. function(doc) {
  464. return {
  465. _id: doc._id,
  466. title: doc.title,
  467. };
  468. },
  469. ),
  470. });
  471. } catch (error) {
  472. JsonRoutes.sendResult(res, {
  473. code: 200,
  474. data: error,
  475. });
  476. }
  477. });
  478. /**
  479. * @operation get_list
  480. * @summary Get a List attached to a board
  481. *
  482. * @param {string} boardId the board ID
  483. * @param {string} listId the List ID
  484. * @return_type Lists
  485. */
  486. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  487. req,
  488. res,
  489. ) {
  490. try {
  491. const paramBoardId = req.params.boardId;
  492. const paramListId = req.params.listId;
  493. Authentication.checkBoardAccess(req.userId, paramBoardId);
  494. JsonRoutes.sendResult(res, {
  495. code: 200,
  496. data: Lists.findOne({
  497. _id: paramListId,
  498. boardId: paramBoardId,
  499. archived: false,
  500. }),
  501. });
  502. } catch (error) {
  503. JsonRoutes.sendResult(res, {
  504. code: 200,
  505. data: error,
  506. });
  507. }
  508. });
  509. /**
  510. * @operation new_list
  511. * @summary Add a List to a board
  512. *
  513. * @param {string} boardId the board ID
  514. * @param {string} title the title of the List
  515. * @return_type {_id: string}
  516. */
  517. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  518. try {
  519. Authentication.checkUserId(req.userId);
  520. const paramBoardId = req.params.boardId;
  521. const board = Boards.findOne(paramBoardId);
  522. const id = Lists.insert({
  523. title: req.body.title,
  524. boardId: paramBoardId,
  525. sort: board.lists().count(),
  526. });
  527. JsonRoutes.sendResult(res, {
  528. code: 200,
  529. data: {
  530. _id: id,
  531. },
  532. });
  533. } catch (error) {
  534. JsonRoutes.sendResult(res, {
  535. code: 200,
  536. data: error,
  537. });
  538. }
  539. });
  540. /**
  541. * @operation delete_list
  542. * @summary Delete a List
  543. *
  544. * @description This **deletes** a list from a board.
  545. * The list is not put in the recycle bin.
  546. *
  547. * @param {string} boardId the board ID
  548. * @param {string} listId the ID of the list to remove
  549. * @return_type {_id: string}
  550. */
  551. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  552. req,
  553. res,
  554. ) {
  555. try {
  556. Authentication.checkUserId(req.userId);
  557. const paramBoardId = req.params.boardId;
  558. const paramListId = req.params.listId;
  559. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  560. JsonRoutes.sendResult(res, {
  561. code: 200,
  562. data: {
  563. _id: paramListId,
  564. },
  565. });
  566. } catch (error) {
  567. JsonRoutes.sendResult(res, {
  568. code: 200,
  569. data: error,
  570. });
  571. }
  572. });
  573. }
  574. export default Lists;