lists.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. 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. cards(swimlaneId) {
  223. const selector = {
  224. listId: this._id,
  225. archived: false,
  226. };
  227. if (swimlaneId) selector.swimlaneId = swimlaneId;
  228. return Cards.find(Filter.mongoSelector(selector), { sort: ['sort'] });
  229. },
  230. cardsUnfiltered(swimlaneId) {
  231. const selector = {
  232. listId: this._id,
  233. archived: false,
  234. };
  235. if (swimlaneId) selector.swimlaneId = swimlaneId;
  236. return Cards.find(selector, { sort: ['sort'] });
  237. },
  238. allCards() {
  239. return Cards.find({ listId: this._id });
  240. },
  241. board() {
  242. return Boards.findOne(this.boardId);
  243. },
  244. getWipLimit(option) {
  245. const list = Lists.findOne({ _id: this._id });
  246. if (!list.wipLimit) {
  247. // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  248. return 0;
  249. } else if (!option) {
  250. return list.wipLimit;
  251. } else {
  252. 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
  253. }
  254. },
  255. colorClass() {
  256. if (this.color) return `list-header-${this.color}`;
  257. return '';
  258. },
  259. isTemplateList() {
  260. return this.type === 'template-list';
  261. },
  262. isStarred() {
  263. return this.starred === true;
  264. },
  265. absoluteUrl() {
  266. const card = Cards.findOne({ listId: this._id });
  267. return card && card.absoluteUrl();
  268. },
  269. originRelativeUrl() {
  270. const card = Cards.findOne({ listId: this._id });
  271. return card && card.originRelativeUrl();
  272. },
  273. remove() {
  274. Lists.remove({ _id: this._id });
  275. },
  276. });
  277. Lists.mutations({
  278. rename(title) {
  279. return { $set: { title } };
  280. },
  281. star(enable = true) {
  282. return { $set: { starred: !!enable } };
  283. },
  284. move(boardId, swimlaneId, sort=null) {
  285. const mutatedFields = {
  286. boardId,
  287. swimlaneId,
  288. sort,
  289. };
  290. if (this.boardId !== boardId) {
  291. mutatedFields.boardId = boardId;
  292. }
  293. if (this.swimlaneId !== swimlaneId) {
  294. mutatedFields.swimlaneId = swimlaneId;
  295. }
  296. if (sort !== null && sort !== this.sort) {
  297. mutatedFields.sort = sort;
  298. }
  299. if (Object.keys(mutatedFields).length) {
  300. this.cards().forEach(card => {
  301. card.move(boardId, swimlaneId, this._id);
  302. });
  303. Lists.update(this._id, {
  304. $set: mutatedFields,
  305. });
  306. }
  307. },
  308. archive() {
  309. if (this.isTemplateList()) {
  310. this.cards().forEach(card => {
  311. return card.archive();
  312. });
  313. }
  314. return { $set: { archived: true, archivedAt: new Date() } };
  315. },
  316. restore() {
  317. if (this.isTemplateList()) {
  318. this.allCards().forEach(card => {
  319. return card.restore();
  320. });
  321. }
  322. return { $set: { archived: false } };
  323. },
  324. toggleSoftLimit(toggle) {
  325. return { $set: { 'wipLimit.soft': toggle } };
  326. },
  327. toggleWipLimit(toggle) {
  328. return { $set: { 'wipLimit.enabled': toggle } };
  329. },
  330. setWipLimit(limit) {
  331. return { $set: { 'wipLimit.value': limit } };
  332. },
  333. setColor(newColor) {
  334. if (newColor === 'silver') {
  335. newColor = null;
  336. }
  337. return {
  338. $set: {
  339. color: newColor,
  340. },
  341. };
  342. },
  343. });
  344. Lists.archivedLists = () => {
  345. return Lists.find({ archived: true });
  346. };
  347. Lists.archivedListIds = () => {
  348. return Lists.archivedLists().map(list => {
  349. return list._id;
  350. });
  351. };
  352. Meteor.methods({
  353. applyWipLimit(listId, limit) {
  354. check(listId, String);
  355. check(limit, Number);
  356. if (limit === 0) {
  357. limit = 1;
  358. }
  359. Lists.findOne({ _id: listId }).setWipLimit(limit);
  360. },
  361. enableWipLimit(listId) {
  362. check(listId, String);
  363. const list = Lists.findOne({ _id: listId });
  364. if (list.getWipLimit('value') === 0) {
  365. list.setWipLimit(1);
  366. }
  367. list.toggleWipLimit(!list.getWipLimit('enabled'));
  368. },
  369. enableSoftLimit(listId) {
  370. check(listId, String);
  371. const list = Lists.findOne({ _id: listId });
  372. list.toggleSoftLimit(!list.getWipLimit('soft'));
  373. },
  374. myLists() {
  375. // my lists
  376. return _.uniq(
  377. Lists.find(
  378. { boardId: { $in: Boards.userBoardIds(this.userId) } },
  379. { fields: { title: 1 } },
  380. )
  381. .fetch()
  382. .map(list => {
  383. return list.title;
  384. }),
  385. ).sort();
  386. },
  387. });
  388. Lists.hookOptions.after.update = { fetchPrevious: false };
  389. if (Meteor.isServer) {
  390. Meteor.startup(() => {
  391. Lists._collection._ensureIndex({ modifiedAt: -1 });
  392. Lists._collection._ensureIndex({ boardId: 1 });
  393. Lists._collection._ensureIndex({ archivedAt: -1 });
  394. });
  395. Lists.after.insert((userId, doc) => {
  396. Activities.insert({
  397. userId,
  398. type: 'list',
  399. activityType: 'createList',
  400. boardId: doc.boardId,
  401. listId: doc._id,
  402. // this preserves the name so that the activity can be useful after the
  403. // list is deleted
  404. title: doc.title,
  405. });
  406. });
  407. Lists.before.remove((userId, doc) => {
  408. const cards = Cards.find({ listId: doc._id });
  409. if (cards) {
  410. cards.forEach(card => {
  411. Cards.remove(card._id);
  412. });
  413. }
  414. Activities.insert({
  415. userId,
  416. type: 'list',
  417. activityType: 'removeList',
  418. boardId: doc.boardId,
  419. listId: doc._id,
  420. title: doc.title,
  421. });
  422. });
  423. Lists.after.update((userId, doc) => {
  424. if (doc.archived) {
  425. Activities.insert({
  426. userId,
  427. type: 'list',
  428. activityType: 'archivedList',
  429. listId: doc._id,
  430. boardId: doc.boardId,
  431. // this preserves the name so that the activity can be useful after the
  432. // list is deleted
  433. title: doc.title,
  434. });
  435. }
  436. });
  437. }
  438. //LISTS REST API
  439. if (Meteor.isServer) {
  440. /**
  441. * @operation get_all_lists
  442. * @summary Get the list of Lists attached to a board
  443. *
  444. * @param {string} boardId the board ID
  445. * @return_type [{_id: string,
  446. * title: string}]
  447. */
  448. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  449. try {
  450. const paramBoardId = req.params.boardId;
  451. Authentication.checkBoardAccess(req.userId, paramBoardId);
  452. JsonRoutes.sendResult(res, {
  453. code: 200,
  454. data: Lists.find({ boardId: paramBoardId, archived: false }).map(
  455. function(doc) {
  456. return {
  457. _id: doc._id,
  458. title: doc.title,
  459. };
  460. },
  461. ),
  462. });
  463. } catch (error) {
  464. JsonRoutes.sendResult(res, {
  465. code: 200,
  466. data: error,
  467. });
  468. }
  469. });
  470. /**
  471. * @operation get_list
  472. * @summary Get a List attached to a board
  473. *
  474. * @param {string} boardId the board ID
  475. * @param {string} listId the List ID
  476. * @return_type Lists
  477. */
  478. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  479. req,
  480. res,
  481. ) {
  482. try {
  483. const paramBoardId = req.params.boardId;
  484. const paramListId = req.params.listId;
  485. Authentication.checkBoardAccess(req.userId, paramBoardId);
  486. JsonRoutes.sendResult(res, {
  487. code: 200,
  488. data: Lists.findOne({
  489. _id: paramListId,
  490. boardId: paramBoardId,
  491. archived: false,
  492. }),
  493. });
  494. } catch (error) {
  495. JsonRoutes.sendResult(res, {
  496. code: 200,
  497. data: error,
  498. });
  499. }
  500. });
  501. /**
  502. * @operation new_list
  503. * @summary Add a List to a board
  504. *
  505. * @param {string} boardId the board ID
  506. * @param {string} title the title of the List
  507. * @return_type {_id: string}
  508. */
  509. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  510. try {
  511. Authentication.checkUserId(req.userId);
  512. const paramBoardId = req.params.boardId;
  513. const board = Boards.findOne(paramBoardId);
  514. const id = Lists.insert({
  515. title: req.body.title,
  516. boardId: paramBoardId,
  517. sort: board.lists().count(),
  518. });
  519. JsonRoutes.sendResult(res, {
  520. code: 200,
  521. data: {
  522. _id: id,
  523. },
  524. });
  525. } catch (error) {
  526. JsonRoutes.sendResult(res, {
  527. code: 200,
  528. data: error,
  529. });
  530. }
  531. });
  532. /**
  533. * @operation delete_list
  534. * @summary Delete a List
  535. *
  536. * @description This **deletes** a list from a board.
  537. * The list is not put in the recycle bin.
  538. *
  539. * @param {string} boardId the board ID
  540. * @param {string} listId the ID of the list to remove
  541. * @return_type {_id: string}
  542. */
  543. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  544. req,
  545. res,
  546. ) {
  547. try {
  548. Authentication.checkUserId(req.userId);
  549. const paramBoardId = req.params.boardId;
  550. const paramListId = req.params.listId;
  551. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  552. JsonRoutes.sendResult(res, {
  553. code: 200,
  554. data: {
  555. _id: paramListId,
  556. },
  557. });
  558. } catch (error) {
  559. JsonRoutes.sendResult(res, {
  560. code: 200,
  561. data: error,
  562. });
  563. }
  564. });
  565. }
  566. export default Lists;