utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. windowResizeDep: new Tracker.Dependency(),
  23. // in fact, what we really care is screen size
  24. // large mobile device like iPad or android Pad has a big screen, it should also behave like a desktop
  25. // in a small window (even on desktop), Wekan run in compact mode.
  26. // we can easily debug with a small window of desktop browser. :-)
  27. isMiniScreen() {
  28. this.windowResizeDep.depend();
  29. return $(window).width() <= 800;
  30. },
  31. calculateIndexData(prevData, nextData, nItems = 1) {
  32. let base, increment;
  33. // If we drop the card to an empty column
  34. if (!prevData && !nextData) {
  35. base = 0;
  36. increment = 1;
  37. // If we drop the card in the first position
  38. } else if (!prevData) {
  39. base = nextData.sort - 1;
  40. increment = -1;
  41. // If we drop the card in the last position
  42. } else if (!nextData) {
  43. base = prevData.sort + 1;
  44. increment = 1;
  45. }
  46. // In the general case take the average of the previous and next element
  47. // sort indexes.
  48. else {
  49. const prevSortIndex = prevData.sort;
  50. const nextSortIndex = nextData.sort;
  51. increment = (nextSortIndex - prevSortIndex) / (nItems + 1);
  52. base = prevSortIndex + increment;
  53. }
  54. // XXX Return a generator that yield values instead of a base with a
  55. // increment number.
  56. return {
  57. base,
  58. increment,
  59. };
  60. },
  61. // Determine the new sort index
  62. calculateIndex(prevCardDomElement, nextCardDomElement, nCards = 1) {
  63. let base, increment;
  64. // If we drop the card to an empty column
  65. if (!prevCardDomElement && !nextCardDomElement) {
  66. base = 0;
  67. increment = 1;
  68. // If we drop the card in the first position
  69. } else if (!prevCardDomElement) {
  70. base = Blaze.getData(nextCardDomElement).sort - 1;
  71. increment = -1;
  72. // If we drop the card in the last position
  73. } else if (!nextCardDomElement) {
  74. base = Blaze.getData(prevCardDomElement).sort + 1;
  75. increment = 1;
  76. }
  77. // In the general case take the average of the previous and next element
  78. // sort indexes.
  79. else {
  80. const prevSortIndex = Blaze.getData(prevCardDomElement).sort;
  81. const nextSortIndex = Blaze.getData(nextCardDomElement).sort;
  82. increment = (nextSortIndex - prevSortIndex) / (nCards + 1);
  83. base = prevSortIndex + increment;
  84. }
  85. // XXX Return a generator that yield values instead of a base with a
  86. // increment number.
  87. return {
  88. base,
  89. increment,
  90. };
  91. },
  92. };
  93. // A simple tracker dependency that we invalidate every time the window is
  94. // resized. This is used to reactively re-calculate the popup position in case
  95. // of a window resize. This is the equivalent of a "Signal" in some other
  96. // programming environments (eg, elm).
  97. $(window).on('resize', () => Utils.windowResizeDep.changed());