lists.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. }));
  110. Lists.allow({
  111. insert(userId, doc) {
  112. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  113. },
  114. update(userId, doc) {
  115. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  116. },
  117. remove(userId, doc) {
  118. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  119. },
  120. fetch: ['boardId'],
  121. });
  122. Lists.helpers({
  123. cards(swimlaneId) {
  124. const selector = {
  125. listId: this._id,
  126. archived: false,
  127. };
  128. if (swimlaneId)
  129. selector.swimlaneId = swimlaneId;
  130. return Cards.find(Filter.mongoSelector(selector),
  131. { sort: ['sort'] });
  132. },
  133. cardsUnfiltered(swimlaneId) {
  134. const selector = {
  135. listId: this._id,
  136. archived: false,
  137. };
  138. if (swimlaneId)
  139. selector.swimlaneId = swimlaneId;
  140. return Cards.find(selector,
  141. { sort: ['sort'] });
  142. },
  143. allCards() {
  144. return Cards.find({ listId: this._id });
  145. },
  146. board() {
  147. return Boards.findOne(this.boardId);
  148. },
  149. getWipLimit(option){
  150. const list = Lists.findOne({ _id: this._id });
  151. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  152. return 0;
  153. } else if(!option) {
  154. return list.wipLimit;
  155. } else {
  156. 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
  157. }
  158. },
  159. colorClass() {
  160. if (this.color)
  161. return this.color;
  162. return '';
  163. },
  164. });
  165. Lists.mutations({
  166. rename(title) {
  167. return { $set: { title } };
  168. },
  169. archive() {
  170. return { $set: { archived: true } };
  171. },
  172. restore() {
  173. return { $set: { archived: false } };
  174. },
  175. toggleSoftLimit(toggle) {
  176. return { $set: { 'wipLimit.soft': toggle } };
  177. },
  178. toggleWipLimit(toggle) {
  179. return { $set: { 'wipLimit.enabled': toggle } };
  180. },
  181. setWipLimit(limit) {
  182. return { $set: { 'wipLimit.value': limit } };
  183. },
  184. setColor(newColor) {
  185. if (newColor === 'silver') {
  186. newColor = null;
  187. }
  188. return {
  189. $set: {
  190. color: newColor,
  191. },
  192. };
  193. },
  194. });
  195. Meteor.methods({
  196. applyWipLimit(listId, limit){
  197. check(listId, String);
  198. check(limit, Number);
  199. if(limit === 0){
  200. limit = 1;
  201. }
  202. Lists.findOne({ _id: listId }).setWipLimit(limit);
  203. },
  204. enableWipLimit(listId) {
  205. check(listId, String);
  206. const list = Lists.findOne({ _id: listId });
  207. if(list.getWipLimit('value') === 0){
  208. list.setWipLimit(1);
  209. }
  210. list.toggleWipLimit(!list.getWipLimit('enabled'));
  211. },
  212. enableSoftLimit(listId) {
  213. check(listId, String);
  214. const list = Lists.findOne({ _id: listId });
  215. list.toggleSoftLimit(!list.getWipLimit('soft'));
  216. },
  217. });
  218. Lists.hookOptions.after.update = { fetchPrevious: false };
  219. if (Meteor.isServer) {
  220. Meteor.startup(() => {
  221. Lists._collection._ensureIndex({ boardId: 1 });
  222. });
  223. Lists.after.insert((userId, doc) => {
  224. Activities.insert({
  225. userId,
  226. type: 'list',
  227. activityType: 'createList',
  228. boardId: doc.boardId,
  229. listId: doc._id,
  230. });
  231. });
  232. Lists.before.remove((userId, doc) => {
  233. Activities.insert({
  234. userId,
  235. type: 'list',
  236. activityType: 'removeList',
  237. boardId: doc.boardId,
  238. listId: doc._id,
  239. title: doc.title,
  240. });
  241. });
  242. Lists.after.update((userId, doc) => {
  243. if (doc.archived) {
  244. Activities.insert({
  245. userId,
  246. type: 'list',
  247. activityType: 'archivedList',
  248. listId: doc._id,
  249. boardId: doc.boardId,
  250. });
  251. }
  252. });
  253. }
  254. //LISTS REST API
  255. if (Meteor.isServer) {
  256. /**
  257. * @operation get_all_lists
  258. * @summary Get the list of Lists attached to a board
  259. *
  260. * @param {string} boardId the board ID
  261. * @return_type [{_id: string,
  262. * title: string}]
  263. */
  264. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  265. try {
  266. const paramBoardId = req.params.boardId;
  267. Authentication.checkBoardAccess( req.userId, paramBoardId);
  268. JsonRoutes.sendResult(res, {
  269. code: 200,
  270. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  271. return {
  272. _id: doc._id,
  273. title: doc.title,
  274. };
  275. }),
  276. });
  277. }
  278. catch (error) {
  279. JsonRoutes.sendResult(res, {
  280. code: 200,
  281. data: error,
  282. });
  283. }
  284. });
  285. /**
  286. * @operation get_list
  287. * @summary Get a List attached to a board
  288. *
  289. * @param {string} boardId the board ID
  290. * @param {string} listId the List ID
  291. * @return_type Lists
  292. */
  293. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  294. try {
  295. const paramBoardId = req.params.boardId;
  296. const paramListId = req.params.listId;
  297. Authentication.checkBoardAccess( req.userId, paramBoardId);
  298. JsonRoutes.sendResult(res, {
  299. code: 200,
  300. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  301. });
  302. }
  303. catch (error) {
  304. JsonRoutes.sendResult(res, {
  305. code: 200,
  306. data: error,
  307. });
  308. }
  309. });
  310. /**
  311. * @operation new_list
  312. * @summary Add a List to a board
  313. *
  314. * @param {string} boardId the board ID
  315. * @param {string} title the title of the List
  316. * @return_type {_id: string}
  317. */
  318. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  319. try {
  320. Authentication.checkUserId( req.userId);
  321. const paramBoardId = req.params.boardId;
  322. const board = Boards.findOne(paramBoardId);
  323. const id = Lists.insert({
  324. title: req.body.title,
  325. boardId: paramBoardId,
  326. sort: board.lists().count(),
  327. });
  328. JsonRoutes.sendResult(res, {
  329. code: 200,
  330. data: {
  331. _id: id,
  332. },
  333. });
  334. }
  335. catch (error) {
  336. JsonRoutes.sendResult(res, {
  337. code: 200,
  338. data: error,
  339. });
  340. }
  341. });
  342. /**
  343. * @operation delete_list
  344. * @summary Delete a List
  345. *
  346. * @description This **deletes** a list from a board.
  347. * The list is not put in the recycle bin.
  348. *
  349. * @param {string} boardId the board ID
  350. * @param {string} listId the ID of the list to remove
  351. * @return_type {_id: string}
  352. */
  353. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  354. try {
  355. Authentication.checkUserId( req.userId);
  356. const paramBoardId = req.params.boardId;
  357. const paramListId = req.params.listId;
  358. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  359. JsonRoutes.sendResult(res, {
  360. code: 200,
  361. data: {
  362. _id: paramListId,
  363. },
  364. });
  365. }
  366. catch (error) {
  367. JsonRoutes.sendResult(res, {
  368. code: 200,
  369. data: error,
  370. });
  371. }
  372. });
  373. }