utils.js 1.9 KB

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