utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Detect touch device
  93. isTouchDevice() {
  94. const isTouchable = (() => {
  95. const prefixes = ' -webkit- -moz- -o- -ms- '.split(' ');
  96. const mq = function(query) {
  97. return window.matchMedia(query).matches;
  98. };
  99. if (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch) {
  100. return true;
  101. }
  102. // include the 'heartz' as a way to have a non matching MQ to help terminate the join
  103. // https://git.io/vznFH
  104. const query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('');
  105. return mq(query);
  106. })();
  107. return isTouchable;
  108. },
  109. calculateTouchDistance(touchA, touchB) {
  110. return Math.sqrt(
  111. Math.pow(touchA.screenX - touchB.screenX, 2) +
  112. Math.pow(touchA.screenY - touchB.screenY, 2)
  113. );
  114. },
  115. enableClickOnTouch(selector) {
  116. let touchStart = null;
  117. let lastTouch = null;
  118. $(document).on('touchstart', selector, function(e) {
  119. touchStart = e.originalEvent.touches[0];
  120. });
  121. $(document).on('touchmove', selector, function(e) {
  122. const touches = e.originalEvent.touches;
  123. lastTouch = touches[touches.length - 1];
  124. });
  125. $(document).on('touchend', selector, function(e) {
  126. if (touchStart && lastTouch && Utils.calculateTouchDistance(touchStart, lastTouch) <= 20) {
  127. const clickEvent = document.createEvent('MouseEvents');
  128. clickEvent.initEvent('click', true, true);
  129. e.target.dispatchEvent(clickEvent);
  130. }
  131. });
  132. },
  133. };
  134. // A simple tracker dependency that we invalidate every time the window is
  135. // resized. This is used to reactively re-calculate the popup position in case
  136. // of a window resize. This is the equivalent of a "Signal" in some other
  137. // programming environments (eg, elm).
  138. $(window).on('resize', () => Utils.windowResizeDep.changed());