2
0

lists.js 13 KB

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