2
0

utils.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Utils = {
  2. // XXX We should remove these two methods
  3. goBoardId(_id) {
  4. const board = Boards.findOne(_id);
  5. return board && FlowRouter.go('board', {
  6. id: board._id,
  7. slug: board.slug,
  8. });
  9. },
  10. goCardId(_id) {
  11. const card = Cards.findOne(_id);
  12. const board = Boards.findOne(card.boardId);
  13. return board && FlowRouter.go('card', {
  14. cardId: card._id,
  15. boardId: board._id,
  16. slug: board.slug,
  17. });
  18. },
  19. capitalize(string) {
  20. return string.charAt(0).toUpperCase() + string.slice(1);
  21. },
  22. getLabelIndex(boardId, labelId) {
  23. const board = Boards.findOne(boardId);
  24. const labels = {};
  25. _.each(board.labels, (a, b) => {
  26. labels[a._id] = b;
  27. });
  28. return {
  29. index: labels[labelId],
  30. key(key) {
  31. return `labels.${labels[labelId]}.${key}`;
  32. },
  33. };
  34. },
  35. // Determine the new sort index
  36. calculateIndex(prevCardDomElement, nextCardDomElement, nCards = 1) {
  37. let base, increment;
  38. // If we drop the card to an empty column
  39. if (!prevCardDomElement && !nextCardDomElement) {
  40. base = 0;
  41. increment = 1;
  42. // If we drop the card in the first position
  43. } else if (!prevCardDomElement) {
  44. base = Blaze.getData(nextCardDomElement).sort - 1;
  45. increment = -1;
  46. // If we drop the card in the last position
  47. } else if (!nextCardDomElement) {
  48. base = Blaze.getData(prevCardDomElement).sort + 1;
  49. increment = 1;
  50. }
  51. // In the general case take the average of the previous and next element
  52. // sort indexes.
  53. else {
  54. const prevSortIndex = Blaze.getData(prevCardDomElement).sort;
  55. const nextSortIndex = Blaze.getData(nextCardDomElement).sort;
  56. increment = (nextSortIndex - prevSortIndex) / (nCards + 1);
  57. base = prevSortIndex + increment;
  58. }
  59. // XXX Return a generator that yield values instead of a base with a
  60. // increment number.
  61. return {
  62. base,
  63. increment,
  64. };
  65. },
  66. };