utils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. Utils.isTouchDevice = () => isTouchable;
  108. return isTouchable;
  109. },
  110. calculateTouchDistance(touchA, touchB) {
  111. return Math.sqrt(
  112. Math.pow(touchA.screenX - touchB.screenX, 2) +
  113. Math.pow(touchA.screenY - touchB.screenY, 2)
  114. );
  115. },
  116. enableClickOnTouch(selector) {
  117. let touchStart = null;
  118. let lastTouch = null;
  119. $(document).on('touchstart', selector, function(e) {
  120. touchStart = e.originalEvent.touches[0];
  121. });
  122. $(document).on('touchmove', selector, function(e) {
  123. const touches = e.originalEvent.touches;
  124. lastTouch = touches[touches.length - 1];
  125. });
  126. $(document).on('touchend', selector, function(e) {
  127. if (touchStart && lastTouch && Utils.calculateTouchDistance(touchStart, lastTouch) <= 20) {
  128. e.preventDefault();
  129. const clickEvent = document.createEvent('MouseEvents');
  130. clickEvent.initEvent('click', true, true);
  131. e.target.dispatchEvent(clickEvent);
  132. }
  133. });
  134. },
  135. setMatomo(data){
  136. window._paq = window._paq || [];
  137. window._paq.push(['setDoNotTrack', data.doNotTrack]);
  138. if (data.withUserName){
  139. window._paq.push(['setUserId', Meteor.user().username]);
  140. }
  141. window._paq.push(['trackPageView']);
  142. window._paq.push(['enableLinkTracking']);
  143. (function() {
  144. window._paq.push(['setTrackerUrl', `${data.address}piwik.php`]);
  145. window._paq.push(['setSiteId', data.siteId]);
  146. const script = document.createElement('script');
  147. Object.assign(script, {
  148. id: 'scriptMatomo',
  149. type: 'text/javascript',
  150. async: 'true',
  151. defer: 'true',
  152. src: `${data.address}piwik.js`,
  153. });
  154. const s = document.getElementsByTagName('script')[0];
  155. s.parentNode.insertBefore(script, s);
  156. })();
  157. Session.set('matomo', true);
  158. },
  159. manageMatomo() {
  160. const matomo = Session.get('matomo');
  161. if (matomo === undefined){
  162. Meteor.call('getMatomoConf', (err, data) => {
  163. if (err && err.error[0] === 'var-not-exist'){
  164. Session.set('matomo', false); // siteId || address server not defined
  165. }
  166. if (!err){
  167. Utils.setMatomo(data);
  168. }
  169. });
  170. } else if (matomo) {
  171. window._paq.push(['trackPageView']);
  172. }
  173. },
  174. getTriggerActionDesc(event, tempInstance) {
  175. const jqueryEl = tempInstance.$(event.currentTarget.parentNode);
  176. const triggerEls = jqueryEl.find('.trigger-content').children();
  177. let finalString = '';
  178. for (let i = 0; i < triggerEls.length; i++) {
  179. const element = tempInstance.$(triggerEls[i]);
  180. if (element.hasClass('trigger-text')) {
  181. finalString += element.text().toLowerCase();
  182. } else if (element.find('select').length > 0) {
  183. finalString += element.find('select option:selected').text().toLowerCase();
  184. } else if (element.find('input').length > 0) {
  185. finalString += element.find('input').val();
  186. }
  187. // Add space
  188. if (i !== length - 1) {
  189. finalString += ' ';
  190. }
  191. }
  192. return finalString;
  193. },
  194. };
  195. // A simple tracker dependency that we invalidate every time the window is
  196. // resized. This is used to reactively re-calculate the popup position in case
  197. // of a window resize. This is the equivalent of a "Signal" in some other
  198. // programming environments (eg, elm).
  199. $(window).on('resize', () => Utils.windowResizeDep.changed());