lists.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. Lists = new Mongo.Collection('lists');
  2. /**
  3. * A list (column) in the Wekan board.
  4. */
  5. Lists.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the list
  9. */
  10. type: String,
  11. },
  12. archived: {
  13. /**
  14. * is the list archived
  15. */
  16. type: Boolean,
  17. autoValue() { // eslint-disable-line consistent-return
  18. if (this.isInsert && !this.isSet) {
  19. return false;
  20. }
  21. },
  22. },
  23. boardId: {
  24. /**
  25. * the board associated to this list
  26. */
  27. type: String,
  28. },
  29. createdAt: {
  30. /**
  31. * creation date
  32. */
  33. type: Date,
  34. autoValue() { // eslint-disable-line consistent-return
  35. if (this.isInsert) {
  36. return new Date();
  37. } else {
  38. this.unset();
  39. }
  40. },
  41. },
  42. sort: {
  43. /**
  44. * is the list sorted
  45. */
  46. type: Number,
  47. decimal: true,
  48. // XXX We should probably provide a default
  49. optional: true,
  50. },
  51. updatedAt: {
  52. /**
  53. * last update of the list
  54. */
  55. type: Date,
  56. optional: true,
  57. autoValue() { // eslint-disable-line consistent-return
  58. if (this.isUpdate) {
  59. return new Date();
  60. } else {
  61. this.unset();
  62. }
  63. },
  64. },
  65. wipLimit: {
  66. /**
  67. * WIP object, see below
  68. */
  69. type: Object,
  70. optional: true,
  71. },
  72. 'wipLimit.value': {
  73. /**
  74. * value of the WIP
  75. */
  76. type: Number,
  77. decimal: false,
  78. defaultValue: 1,
  79. },
  80. 'wipLimit.enabled': {
  81. /**
  82. * is the WIP enabled
  83. */
  84. type: Boolean,
  85. defaultValue: false,
  86. },
  87. 'wipLimit.soft': {
  88. /**
  89. * is the WIP a soft or hard requirement
  90. */
  91. type: Boolean,
  92. defaultValue: false,
  93. },
  94. color: {
  95. /**
  96. * the color of the list
  97. */
  98. type: String,
  99. optional: true,
  100. // silver is the default, so it is left out
  101. allowedValues: [
  102. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  103. 'blue', 'sky', 'lime', 'pink', 'black',
  104. 'peachpuff', 'crimson', 'plum', 'darkgreen',
  105. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  106. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  107. ],
  108. },
  109. type: {
  110. /**
  111. * The type of list
  112. */
  113. type: String,
  114. defaultValue: 'list',
  115. },
  116. }));
  117. Lists.allow({
  118. insert(userId, doc) {
  119. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  120. },
  121. update(userId, doc) {
  122. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  123. },
  124. remove(userId, doc) {
  125. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  126. },
  127. fetch: ['boardId'],
  128. });
  129. Lists.helpers({
  130. cards(swimlaneId) {
  131. const selector = {
  132. listId: this._id,
  133. archived: false,
  134. };
  135. if (swimlaneId)
  136. selector.swimlaneId = swimlaneId;
  137. return Cards.find(Filter.mongoSelector(selector),
  138. { sort: ['sort'] });
  139. },
  140. cardsUnfiltered(swimlaneId) {
  141. const selector = {
  142. listId: this._id,
  143. archived: false,
  144. };
  145. if (swimlaneId)
  146. selector.swimlaneId = swimlaneId;
  147. return Cards.find(selector,
  148. { sort: ['sort'] });
  149. },
  150. allCards() {
  151. return Cards.find({ listId: this._id });
  152. },
  153. board() {
  154. return Boards.findOne(this.boardId);
  155. },
  156. getWipLimit(option){
  157. const list = Lists.findOne({ _id: this._id });
  158. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  159. return 0;
  160. } else if(!option) {
  161. return list.wipLimit;
  162. } else {
  163. 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
  164. }
  165. },
  166. colorClass() {
  167. if (this.color)
  168. return this.color;
  169. return '';
  170. },
  171. isTemplateList() {
  172. return this.type === 'template-list';
  173. },
  174. });
  175. Lists.mutations({
  176. rename(title) {
  177. return { $set: { title } };
  178. },
  179. archive() {
  180. return { $set: { archived: true } };
  181. },
  182. restore() {
  183. return { $set: { archived: false } };
  184. },
  185. toggleSoftLimit(toggle) {
  186. return { $set: { 'wipLimit.soft': toggle } };
  187. },
  188. toggleWipLimit(toggle) {
  189. return { $set: { 'wipLimit.enabled': toggle } };
  190. },
  191. setWipLimit(limit) {
  192. return { $set: { 'wipLimit.value': limit } };
  193. },
  194. setColor(newColor) {
  195. if (newColor === 'silver') {
  196. newColor = null;
  197. }
  198. return {
  199. $set: {
  200. color: newColor,
  201. },
  202. };
  203. },
  204. });
  205. Meteor.methods({
  206. applyWipLimit(listId, limit){
  207. check(listId, String);
  208. check(limit, Number);
  209. if(limit === 0){
  210. limit = 1;
  211. }
  212. Lists.findOne({ _id: listId }).setWipLimit(limit);
  213. },
  214. enableWipLimit(listId) {
  215. check(listId, String);
  216. const list = Lists.findOne({ _id: listId });
  217. if(list.getWipLimit('value') === 0){
  218. list.setWipLimit(1);
  219. }
  220. list.toggleWipLimit(!list.getWipLimit('enabled'));
  221. },
  222. enableSoftLimit(listId) {
  223. check(listId, String);
  224. const list = Lists.findOne({ _id: listId });
  225. list.toggleSoftLimit(!list.getWipLimit('soft'));
  226. },
  227. });
  228. Lists.hookOptions.after.update = { fetchPrevious: false };
  229. if (Meteor.isServer) {
  230. Meteor.startup(() => {
  231. Lists._collection._ensureIndex({ boardId: 1 });
  232. });
  233. Lists.after.insert((userId, doc) => {
  234. Activities.insert({
  235. userId,
  236. type: 'list',
  237. activityType: 'createList',
  238. boardId: doc.boardId,
  239. listId: doc._id,
  240. });
  241. });
  242. Lists.before.remove((userId, doc) => {
  243. Activities.insert({
  244. userId,
  245. type: 'list',
  246. activityType: 'removeList',
  247. boardId: doc.boardId,
  248. listId: doc._id,
  249. title: doc.title,
  250. });
  251. });
  252. Lists.after.update((userId, doc) => {
  253. if (doc.archived) {
  254. Activities.insert({
  255. userId,
  256. type: 'list',
  257. activityType: 'archivedList',
  258. listId: doc._id,
  259. boardId: doc.boardId,
  260. });
  261. }
  262. });
  263. }
  264. //LISTS REST API
  265. if (Meteor.isServer) {
  266. /**
  267. * @operation get_all_lists
  268. * @summary Get the list of Lists attached to a board
  269. *
  270. * @param {string} boardId the board ID
  271. * @return_type [{_id: string,
  272. * title: string}]
  273. */
  274. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  275. try {
  276. const paramBoardId = req.params.boardId;
  277. Authentication.checkBoardAccess( req.userId, paramBoardId);
  278. JsonRoutes.sendResult(res, {
  279. code: 200,
  280. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  281. return {
  282. _id: doc._id,
  283. title: doc.title,
  284. };
  285. }),
  286. });
  287. }
  288. catch (error) {
  289. JsonRoutes.sendResult(res, {
  290. code: 200,
  291. data: error,
  292. });
  293. }
  294. });
  295. /**
  296. * @operation get_list
  297. * @summary Get a List attached to a board
  298. *
  299. * @param {string} boardId the board ID
  300. * @param {string} listId the List ID
  301. * @return_type Lists
  302. */
  303. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  304. try {
  305. const paramBoardId = req.params.boardId;
  306. const paramListId = req.params.listId;
  307. Authentication.checkBoardAccess( req.userId, paramBoardId);
  308. JsonRoutes.sendResult(res, {
  309. code: 200,
  310. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  311. });
  312. }
  313. catch (error) {
  314. JsonRoutes.sendResult(res, {
  315. code: 200,
  316. data: error,
  317. });
  318. }
  319. });
  320. /**
  321. * @operation new_list
  322. * @summary Add a List to a board
  323. *
  324. * @param {string} boardId the board ID
  325. * @param {string} title the title of the List
  326. * @return_type {_id: string}
  327. */
  328. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  329. try {
  330. Authentication.checkUserId( req.userId);
  331. const paramBoardId = req.params.boardId;
  332. const board = Boards.findOne(paramBoardId);
  333. const id = Lists.insert({
  334. title: req.body.title,
  335. boardId: paramBoardId,
  336. sort: board.lists().count(),
  337. });
  338. JsonRoutes.sendResult(res, {
  339. code: 200,
  340. data: {
  341. _id: id,
  342. },
  343. });
  344. }
  345. catch (error) {
  346. JsonRoutes.sendResult(res, {
  347. code: 200,
  348. data: error,
  349. });
  350. }
  351. });
  352. /**
  353. * @operation delete_list
  354. * @summary Delete a List
  355. *
  356. * @description This **deletes** a list from a board.
  357. * The list is not put in the recycle bin.
  358. *
  359. * @param {string} boardId the board ID
  360. * @param {string} listId the ID of the list to remove
  361. * @return_type {_id: string}
  362. */
  363. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  364. try {
  365. Authentication.checkUserId( req.userId);
  366. const paramBoardId = req.params.boardId;
  367. const paramListId = req.params.listId;
  368. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  369. JsonRoutes.sendResult(res, {
  370. code: 200,
  371. data: {
  372. _id: paramListId,
  373. },
  374. });
  375. }
  376. catch (error) {
  377. JsonRoutes.sendResult(res, {
  378. code: 200,
  379. data: error,
  380. });
  381. }
  382. });
  383. }