utils.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 broswer. :-)
  27. isMiniScreen() {
  28. this.windowResizeDep.depend();
  29. return $(window).width() <= 800;
  30. },
  31. // Determine the new sort index
  32. calculateIndex(prevCardDomElement, nextCardDomElement, nCards = 1) {
  33. let base, increment;
  34. // If we drop the card to an empty column
  35. if (!prevCardDomElement && !nextCardDomElement) {
  36. base = 0;
  37. increment = 1;
  38. // If we drop the card in the first position
  39. } else if (!prevCardDomElement) {
  40. base = Blaze.getData(nextCardDomElement).sort - 1;
  41. increment = -1;
  42. // If we drop the card in the last position
  43. } else if (!nextCardDomElement) {
  44. base = Blaze.getData(prevCardDomElement).sort + 1;
  45. increment = 1;
  46. }
  47. // In the general case take the average of the previous and next element
  48. // sort indexes.
  49. else {
  50. const prevSortIndex = Blaze.getData(prevCardDomElement).sort;
  51. const nextSortIndex = Blaze.getData(nextCardDomElement).sort;
  52. increment = (nextSortIndex - prevSortIndex) / (nCards + 1);
  53. base = prevSortIndex + increment;
  54. }
  55. // XXX Return a generator that yield values instead of a base with a
  56. // increment number.
  57. return {
  58. base,
  59. increment,
  60. };
  61. },
  62. };
  63. // A simple tracker dependency that we invalidate every time the window is
  64. // resized. This is used to reactively re-calculate the popup position in case
  65. // of a window resize. This is the equivalent of a "Signal" in some other
  66. // programming environments (eg, elm).
  67. $(window).on('resize', () => Utils.windowResizeDep.changed());