lists.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 `list-header-${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. Lists.archivedLists = () => {
  310. return Lists.find({ archived: true });
  311. };
  312. Lists.archivedListIds = () => {
  313. return Lists.archivedLists().map(list => {
  314. return list._id;
  315. });
  316. };
  317. Meteor.methods({
  318. applyWipLimit(listId, limit) {
  319. check(listId, String);
  320. check(limit, Number);
  321. if (limit === 0) {
  322. limit = 1;
  323. }
  324. Lists.findOne({ _id: listId }).setWipLimit(limit);
  325. },
  326. enableWipLimit(listId) {
  327. check(listId, String);
  328. const list = Lists.findOne({ _id: listId });
  329. if (list.getWipLimit('value') === 0) {
  330. list.setWipLimit(1);
  331. }
  332. list.toggleWipLimit(!list.getWipLimit('enabled'));
  333. },
  334. enableSoftLimit(listId) {
  335. check(listId, String);
  336. const list = Lists.findOne({ _id: listId });
  337. list.toggleSoftLimit(!list.getWipLimit('soft'));
  338. },
  339. myLists() {
  340. // my lists
  341. return _.uniq(
  342. Lists.find(
  343. { boardId: { $in: Boards.userBoardIds(this.userId) } },
  344. { fields: { title: 1 } },
  345. )
  346. .fetch()
  347. .map(list => {
  348. return list.title;
  349. }),
  350. ).sort();
  351. },
  352. });
  353. Lists.hookOptions.after.update = { fetchPrevious: false };
  354. if (Meteor.isServer) {
  355. Meteor.startup(() => {
  356. Lists._collection._ensureIndex({ modifiedAt: -1 });
  357. Lists._collection._ensureIndex({ boardId: 1 });
  358. });
  359. Lists.after.insert((userId, doc) => {
  360. Activities.insert({
  361. userId,
  362. type: 'list',
  363. activityType: 'createList',
  364. boardId: doc.boardId,
  365. listId: doc._id,
  366. // this preserves the name so that the activity can be useful after the
  367. // list is deleted
  368. title: doc.title,
  369. });
  370. });
  371. Lists.before.remove((userId, doc) => {
  372. const cards = Cards.find({ listId: doc._id });
  373. if (cards) {
  374. cards.forEach(card => {
  375. Cards.remove(card._id);
  376. });
  377. }
  378. Activities.insert({
  379. userId,
  380. type: 'list',
  381. activityType: 'removeList',
  382. boardId: doc.boardId,
  383. listId: doc._id,
  384. title: doc.title,
  385. });
  386. });
  387. Lists.after.update((userId, doc) => {
  388. if (doc.archived) {
  389. Activities.insert({
  390. userId,
  391. type: 'list',
  392. activityType: 'archivedList',
  393. listId: doc._id,
  394. boardId: doc.boardId,
  395. // this preserves the name so that the activity can be useful after the
  396. // list is deleted
  397. title: doc.title,
  398. });
  399. }
  400. });
  401. }
  402. //LISTS REST API
  403. if (Meteor.isServer) {
  404. /**
  405. * @operation get_all_lists
  406. * @summary Get the list of Lists attached to a board
  407. *
  408. * @param {string} boardId the board ID
  409. * @return_type [{_id: string,
  410. * title: string}]
  411. */
  412. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  413. try {
  414. const paramBoardId = req.params.boardId;
  415. Authentication.checkBoardAccess(req.userId, paramBoardId);
  416. JsonRoutes.sendResult(res, {
  417. code: 200,
  418. data: Lists.find({ boardId: paramBoardId, archived: false }).map(
  419. function(doc) {
  420. return {
  421. _id: doc._id,
  422. title: doc.title,
  423. };
  424. },
  425. ),
  426. });
  427. } catch (error) {
  428. JsonRoutes.sendResult(res, {
  429. code: 200,
  430. data: error,
  431. });
  432. }
  433. });
  434. /**
  435. * @operation get_list
  436. * @summary Get a List attached to a board
  437. *
  438. * @param {string} boardId the board ID
  439. * @param {string} listId the List ID
  440. * @return_type Lists
  441. */
  442. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  443. req,
  444. res,
  445. ) {
  446. try {
  447. const paramBoardId = req.params.boardId;
  448. const paramListId = req.params.listId;
  449. Authentication.checkBoardAccess(req.userId, paramBoardId);
  450. JsonRoutes.sendResult(res, {
  451. code: 200,
  452. data: Lists.findOne({
  453. _id: paramListId,
  454. boardId: paramBoardId,
  455. archived: false,
  456. }),
  457. });
  458. } catch (error) {
  459. JsonRoutes.sendResult(res, {
  460. code: 200,
  461. data: error,
  462. });
  463. }
  464. });
  465. /**
  466. * @operation new_list
  467. * @summary Add a List to a board
  468. *
  469. * @param {string} boardId the board ID
  470. * @param {string} title the title of the List
  471. * @return_type {_id: string}
  472. */
  473. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  474. try {
  475. Authentication.checkUserId(req.userId);
  476. const paramBoardId = req.params.boardId;
  477. const board = Boards.findOne(paramBoardId);
  478. const id = Lists.insert({
  479. title: req.body.title,
  480. boardId: paramBoardId,
  481. sort: board.lists().count(),
  482. });
  483. JsonRoutes.sendResult(res, {
  484. code: 200,
  485. data: {
  486. _id: id,
  487. },
  488. });
  489. } catch (error) {
  490. JsonRoutes.sendResult(res, {
  491. code: 200,
  492. data: error,
  493. });
  494. }
  495. });
  496. /**
  497. * @operation delete_list
  498. * @summary Delete a List
  499. *
  500. * @description This **deletes** a list from a board.
  501. * The list is not put in the recycle bin.
  502. *
  503. * @param {string} boardId the board ID
  504. * @param {string} listId the ID of the list to remove
  505. * @return_type {_id: string}
  506. */
  507. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  508. req,
  509. res,
  510. ) {
  511. try {
  512. Authentication.checkUserId(req.userId);
  513. const paramBoardId = req.params.boardId;
  514. const paramListId = req.params.listId;
  515. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  516. JsonRoutes.sendResult(res, {
  517. code: 200,
  518. data: {
  519. _id: paramListId,
  520. },
  521. });
  522. } catch (error) {
  523. JsonRoutes.sendResult(res, {
  524. code: 200,
  525. data: error,
  526. });
  527. }
  528. });
  529. }
  530. export default Lists;