utils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. Utils = {
  3. setBackgroundImage(url) {
  4. const currentBoard = Utils.getCurrentBoard();
  5. if (currentBoard.backgroundImageURL !== undefined) {
  6. $(".board-wrapper").css({"background":"url(" + currentBoard.backgroundImageURL + ")","background-size":"cover"});
  7. $(".swimlane,.swimlane .list,.swimlane .list .list-body,.swimlane .list:first-child .list-body").css({"background-color":"transparent"});
  8. $(".minicard").css({"opacity": "0.9"});
  9. } else if (currentBoard["background-color"]) {
  10. currentBoard.setColor(currentBoard["background-color"]);
  11. }
  12. },
  13. /** returns the current board id
  14. * <li> returns the current board id or the board id of the popup card if set
  15. */
  16. getCurrentBoardId() {
  17. let popupCardBoardId = Session.get('popupCardBoardId');
  18. let currentBoard = Session.get('currentBoard');
  19. let ret = currentBoard;
  20. if (popupCardBoardId) {
  21. ret = popupCardBoardId;
  22. }
  23. return ret;
  24. },
  25. getCurrentCardId(ignorePopupCard) {
  26. let ret = Session.get('currentCard');
  27. if (!ret && !ignorePopupCard) {
  28. ret = Utils.getPopupCardId();
  29. }
  30. return ret;
  31. },
  32. getPopupCardId() {
  33. const ret = Session.get('popupCardId');
  34. return ret;
  35. },
  36. getCurrentListId() {
  37. const ret = Session.get('currentList');
  38. return ret;
  39. },
  40. /** returns the current board
  41. * <li> returns the current board or the board of the popup card if set
  42. */
  43. getCurrentBoard() {
  44. const boardId = Utils.getCurrentBoardId();
  45. const ret = ReactiveCache.getBoard(boardId);
  46. return ret;
  47. },
  48. getCurrentCard(ignorePopupCard) {
  49. const cardId = Utils.getCurrentCardId(ignorePopupCard);
  50. const ret = ReactiveCache.getCard(cardId);
  51. return ret;
  52. },
  53. // Zoom and mobile mode utilities
  54. getZoomLevel() {
  55. const user = ReactiveCache.getCurrentUser();
  56. if (user && user.profile && user.profile.zoomLevel !== undefined) {
  57. return user.profile.zoomLevel;
  58. }
  59. // For non-logged-in users, check localStorage
  60. const stored = localStorage.getItem('wekan-zoom-level');
  61. return stored ? parseFloat(stored) : 1.0;
  62. },
  63. setZoomLevel(level) {
  64. const user = ReactiveCache.getCurrentUser();
  65. if (user) {
  66. // Update user profile
  67. user.setZoomLevel(level);
  68. } else {
  69. // Store in localStorage for non-logged-in users
  70. localStorage.setItem('wekan-zoom-level', level.toString());
  71. }
  72. Utils.applyZoomLevel(level);
  73. // Trigger reactive updates for UI components
  74. Session.set('wekan-zoom-level', level);
  75. },
  76. getMobileMode() {
  77. const user = ReactiveCache.getCurrentUser();
  78. if (user && user.profile && user.profile.mobileMode !== undefined) {
  79. return user.profile.mobileMode;
  80. }
  81. // For non-logged-in users, check localStorage
  82. const stored = localStorage.getItem('wekan-mobile-mode');
  83. return stored ? stored === 'true' : false;
  84. },
  85. setMobileMode(enabled) {
  86. const user = ReactiveCache.getCurrentUser();
  87. if (user) {
  88. // Update user profile
  89. user.setMobileMode(enabled);
  90. } else {
  91. // Store in localStorage for non-logged-in users
  92. localStorage.setItem('wekan-mobile-mode', enabled.toString());
  93. }
  94. Utils.applyMobileMode(enabled);
  95. // Trigger reactive updates for UI components
  96. Session.set('wekan-mobile-mode', enabled);
  97. },
  98. applyZoomLevel(level) {
  99. const boardWrapper = document.querySelector('.board-wrapper');
  100. const body = document.body;
  101. const isMobileMode = body.classList.contains('mobile-mode');
  102. if (boardWrapper) {
  103. if (isMobileMode) {
  104. // On mobile mode, only apply zoom to text and icons, not the entire layout
  105. // Remove any existing transform from board-wrapper
  106. boardWrapper.style.transform = '';
  107. boardWrapper.style.transformOrigin = '';
  108. // Apply zoom to text and icon elements instead
  109. const textElements = boardWrapper.querySelectorAll('h1, h2, h3, h4, h5, h6, p, span, div, .minicard, .list-header-name, .board-header-btn, .fa, .icon');
  110. textElements.forEach(element => {
  111. element.style.transform = `scale(${level})`;
  112. element.style.transformOrigin = 'center';
  113. });
  114. // Reset board-canvas height
  115. const boardCanvas = document.querySelector('.board-canvas');
  116. if (boardCanvas) {
  117. boardCanvas.style.height = '';
  118. }
  119. } else {
  120. // Desktop mode: apply zoom to entire board-wrapper as before
  121. boardWrapper.style.transform = `scale(${level})`;
  122. boardWrapper.style.transformOrigin = 'top left';
  123. // If zoom is 50% or lower, make board wrapper full width like content
  124. if (level <= 0.5) {
  125. boardWrapper.style.width = '100%';
  126. boardWrapper.style.maxWidth = '100%';
  127. boardWrapper.style.margin = '0';
  128. } else {
  129. // Reset to normal width for higher zoom levels
  130. boardWrapper.style.width = '';
  131. boardWrapper.style.maxWidth = '';
  132. boardWrapper.style.margin = '';
  133. }
  134. // Adjust container height to prevent scroll issues
  135. const boardCanvas = document.querySelector('.board-canvas');
  136. if (boardCanvas) {
  137. boardCanvas.style.height = `${100 / level}%`;
  138. // For high zoom levels (200%+), enable both horizontal and vertical scrolling
  139. if (level >= 2.0) {
  140. boardCanvas.style.overflowX = 'auto';
  141. boardCanvas.style.overflowY = 'auto';
  142. // Ensure the content area can scroll both horizontally and vertically
  143. const content = document.querySelector('#content');
  144. if (content) {
  145. content.style.overflowX = 'auto';
  146. content.style.overflowY = 'auto';
  147. }
  148. } else {
  149. // Reset overflow for normal zoom levels
  150. boardCanvas.style.overflowX = '';
  151. boardCanvas.style.overflowY = '';
  152. const content = document.querySelector('#content');
  153. if (content) {
  154. content.style.overflowX = '';
  155. content.style.overflowY = '';
  156. }
  157. }
  158. }
  159. }
  160. }
  161. },
  162. applyMobileMode(enabled) {
  163. const body = document.body;
  164. if (enabled) {
  165. body.classList.add('mobile-mode');
  166. body.classList.remove('desktop-mode');
  167. } else {
  168. body.classList.add('desktop-mode');
  169. body.classList.remove('mobile-mode');
  170. }
  171. },
  172. initializeUserSettings() {
  173. // Apply saved settings on page load
  174. const zoomLevel = Utils.getZoomLevel();
  175. const mobileMode = Utils.getMobileMode();
  176. Utils.applyZoomLevel(zoomLevel);
  177. Utils.applyMobileMode(mobileMode);
  178. },
  179. getCurrentList() {
  180. const listId = this.getCurrentListId();
  181. let ret = null;
  182. if (listId) {
  183. ret = ReactiveCache.getList(listId);
  184. }
  185. return ret;
  186. },
  187. getPopupCard() {
  188. const cardId = Utils.getPopupCardId();
  189. const ret = ReactiveCache.getCard(cardId);
  190. return ret;
  191. },
  192. canModifyCard() {
  193. const currentUser = ReactiveCache.getCurrentUser();
  194. const ret = (
  195. currentUser &&
  196. currentUser.isBoardMember() &&
  197. !currentUser.isCommentOnly() &&
  198. !currentUser.isWorker()
  199. );
  200. return ret;
  201. },
  202. canModifyBoard() {
  203. const currentUser = ReactiveCache.getCurrentUser();
  204. const ret = (
  205. currentUser &&
  206. currentUser.isBoardMember() &&
  207. !currentUser.isCommentOnly()
  208. );
  209. return ret;
  210. },
  211. reload() {
  212. // we move all window.location.reload calls into this function
  213. // so we can disable it when running tests.
  214. // This is because we are not allowed to override location.reload but
  215. // we can override Utils.reload to prevent reload during tests.
  216. window.location.reload();
  217. },
  218. setBoardView(view) {
  219. currentUser = ReactiveCache.getCurrentUser();
  220. if (currentUser) {
  221. ReactiveCache.getCurrentUser().setBoardView(view);
  222. } else if (view === 'board-view-swimlanes') {
  223. window.localStorage.setItem('boardView', 'board-view-swimlanes'); //true
  224. Utils.reload();
  225. } else if (view === 'board-view-lists') {
  226. window.localStorage.setItem('boardView', 'board-view-lists'); //true
  227. Utils.reload();
  228. } else if (view === 'board-view-cal') {
  229. window.localStorage.setItem('boardView', 'board-view-cal'); //true
  230. Utils.reload();
  231. } else {
  232. window.localStorage.setItem('boardView', 'board-view-swimlanes'); //true
  233. Utils.reload();
  234. }
  235. },
  236. unsetBoardView() {
  237. window.localStorage.removeItem('boardView');
  238. window.localStorage.removeItem('collapseSwimlane');
  239. },
  240. boardView() {
  241. currentUser = ReactiveCache.getCurrentUser();
  242. if (currentUser) {
  243. return (currentUser.profile || {}).boardView;
  244. } else if (
  245. window.localStorage.getItem('boardView') === 'board-view-swimlanes'
  246. ) {
  247. return 'board-view-swimlanes';
  248. } else if (
  249. window.localStorage.getItem('boardView') === 'board-view-lists'
  250. ) {
  251. return 'board-view-lists';
  252. } else if (window.localStorage.getItem('boardView') === 'board-view-cal') {
  253. return 'board-view-cal';
  254. } else {
  255. window.localStorage.setItem('boardView', 'board-view-swimlanes'); //true
  256. Utils.reload();
  257. return 'board-view-swimlanes';
  258. }
  259. },
  260. myCardsSort() {
  261. let sort = window.localStorage.getItem('myCardsSort');
  262. if (!sort || !['board', 'dueAt'].includes(sort)) {
  263. sort = 'board';
  264. }
  265. return sort;
  266. },
  267. myCardsSortToggle() {
  268. if (this.myCardsSort() === 'board') {
  269. this.setMyCardsSort('dueAt');
  270. } else {
  271. this.setMyCardsSort('board');
  272. }
  273. },
  274. setMyCardsSort(sort) {
  275. window.localStorage.setItem('myCardsSort', sort);
  276. Utils.reload();
  277. },
  278. archivedBoardIds() {
  279. const ret = ReactiveCache.getBoards({ archived: false }).map(board => board._id);
  280. return ret;
  281. },
  282. dueCardsView() {
  283. let view = window.localStorage.getItem('dueCardsView');
  284. if (!view || !['me', 'all'].includes(view)) {
  285. view = 'me';
  286. }
  287. return view;
  288. },
  289. setDueCardsView(view) {
  290. window.localStorage.setItem('dueCardsView', view);
  291. Utils.reload();
  292. },
  293. myCardsView() {
  294. let view = window.localStorage.getItem('myCardsView');
  295. if (!view || !['boards', 'table'].includes(view)) {
  296. view = 'boards';
  297. }
  298. return view;
  299. },
  300. setMyCardsView(view) {
  301. window.localStorage.setItem('myCardsView', view);
  302. Utils.reload();
  303. },
  304. // XXX We should remove these two methods
  305. goBoardId(_id) {
  306. const board = ReactiveCache.getBoard(_id);
  307. return (
  308. board &&
  309. FlowRouter.go('board', {
  310. id: board._id,
  311. slug: board.slug,
  312. })
  313. );
  314. },
  315. goCardId(_id) {
  316. const card = ReactiveCache.getCard(_id);
  317. const board = ReactiveCache.getBoard(card.boardId);
  318. return (
  319. board &&
  320. FlowRouter.go('card', {
  321. cardId: card._id,
  322. boardId: board._id,
  323. slug: board.slug,
  324. })
  325. );
  326. },
  327. getCommonAttachmentMetaFrom(card) {
  328. const meta = {};
  329. if (card.isLinkedCard()) {
  330. meta.boardId = ReactiveCache.getCard(card.linkedId).boardId;
  331. meta.cardId = card.linkedId;
  332. } else {
  333. meta.boardId = card.boardId;
  334. meta.swimlaneId = card.swimlaneId;
  335. meta.listId = card.listId;
  336. meta.cardId = card._id;
  337. }
  338. return meta;
  339. },
  340. MAX_IMAGE_PIXEL: Meteor.settings.public.MAX_IMAGE_PIXEL,
  341. COMPRESS_RATIO: Meteor.settings.public.IMAGE_COMPRESS_RATIO,
  342. shrinkImage(options) {
  343. // shrink image to certain size
  344. const dataurl = options.dataurl,
  345. callback = options.callback,
  346. toBlob = options.toBlob;
  347. let canvas = document.createElement('canvas'),
  348. image = document.createElement('img');
  349. const maxSize = options.maxSize || 1024;
  350. const ratio = options.ratio || 1.0;
  351. const next = function (result) {
  352. image = null;
  353. canvas = null;
  354. if (typeof callback === 'function') {
  355. callback(result);
  356. }
  357. };
  358. image.onload = function () {
  359. let width = this.width,
  360. height = this.height;
  361. let changed = false;
  362. if (width > height) {
  363. if (width > maxSize) {
  364. height *= maxSize / width;
  365. width = maxSize;
  366. changed = true;
  367. }
  368. } else if (height > maxSize) {
  369. width *= maxSize / height;
  370. height = maxSize;
  371. changed = true;
  372. }
  373. canvas.width = width;
  374. canvas.height = height;
  375. canvas.getContext('2d').drawImage(this, 0, 0, width, height);
  376. if (changed === true) {
  377. const type = 'image/jpeg';
  378. if (toBlob) {
  379. canvas.toBlob(next, type, ratio);
  380. } else {
  381. next(canvas.toDataURL(type, ratio));
  382. }
  383. } else {
  384. next(changed);
  385. }
  386. };
  387. image.onerror = function () {
  388. next(false);
  389. };
  390. image.src = dataurl;
  391. },
  392. capitalize(string) {
  393. return string.charAt(0).toUpperCase() + string.slice(1);
  394. },
  395. windowResizeDep: new Tracker.Dependency(),
  396. // in fact, what we really care is screen size
  397. // large mobile device like iPad or android Pad has a big screen, it should also behave like a desktop
  398. // in a small window (even on desktop), Wekan run in compact mode.
  399. // we can easily debug with a small window of desktop browser. :-)
  400. isMiniScreen() {
  401. this.windowResizeDep.depend();
  402. // Also depend on mobile mode changes to make this reactive
  403. Session.get('wekan-mobile-mode');
  404. // Show mobile view when:
  405. // 1. Screen width is 800px or less (matches CSS media queries)
  406. // 2. Mobile phones in portrait mode
  407. // 3. iPad in very small screens (≤ 600px)
  408. // 4. All iPhone models by default (including largest models), but respect user preference
  409. const isSmallScreen = window.innerWidth <= 800;
  410. const isVerySmallScreen = window.innerWidth <= 600;
  411. const isPortrait = window.innerWidth < window.innerHeight || window.matchMedia("(orientation: portrait)").matches;
  412. const isMobilePhone = /Mobile|Android|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) && !/iPad/i.test(navigator.userAgent);
  413. const isIPhone = /iPhone|iPod/i.test(navigator.userAgent);
  414. const isIPad = /iPad/i.test(navigator.userAgent);
  415. const isUbuntuTouch = /Ubuntu/i.test(navigator.userAgent);
  416. // Check if user has explicitly set mobile mode preference
  417. const userMobileMode = this.getMobileMode();
  418. // For iPhone: default to mobile view, but respect user's mobile mode toggle preference
  419. // This ensures all iPhone models (including iPhone 15 Pro Max, 14 Pro Max, etc.) start with mobile view
  420. // but users can still switch to desktop mode if they prefer
  421. if (isIPhone) {
  422. // If user has explicitly set a preference, respect it
  423. if (userMobileMode !== null && userMobileMode !== undefined) {
  424. return userMobileMode;
  425. }
  426. // Otherwise, default to mobile view for iPhones
  427. return true;
  428. } else if (isMobilePhone) {
  429. return isPortrait; // Other mobile phones: portrait = mobile, landscape = desktop
  430. } else if (isIPad) {
  431. return isVerySmallScreen; // iPad: only very small screens get mobile view
  432. } else if (isUbuntuTouch) {
  433. // Ubuntu Touch: smartphones (≤ 600px) behave like mobile phones, tablets (> 600px) like iPad
  434. if (isVerySmallScreen) {
  435. return isPortrait; // Ubuntu Touch smartphone: portrait = mobile, landscape = desktop
  436. } else {
  437. return isVerySmallScreen; // Ubuntu Touch tablet: only very small screens get mobile view
  438. }
  439. } else {
  440. return isSmallScreen; // Desktop: based on 800px screen width
  441. }
  442. },
  443. isTouchScreen() {
  444. // NEW TOUCH DEVICE DETECTION:
  445. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
  446. var hasTouchScreen = false;
  447. if ("maxTouchPoints" in navigator) {
  448. hasTouchScreen = navigator.maxTouchPoints > 0;
  449. } else if ("msMaxTouchPoints" in navigator) {
  450. hasTouchScreen = navigator.msMaxTouchPoints > 0;
  451. } else {
  452. var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
  453. if (mQ && mQ.media === "(pointer:coarse)") {
  454. hasTouchScreen = !!mQ.matches;
  455. } else if ('orientation' in window) {
  456. hasTouchScreen = true; // deprecated, but good fallback
  457. } else {
  458. // Only as a last resort, fall back to user agent sniffing
  459. var UA = navigator.userAgent;
  460. hasTouchScreen = (
  461. /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
  462. /\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
  463. );
  464. }
  465. }
  466. return hasTouchScreen;
  467. },
  468. // returns if desktop drag handles are enabled
  469. isShowDesktopDragHandles() {
  470. // Always show drag handles on all displays
  471. return true;
  472. },
  473. // returns if mini screen or desktop drag handles
  474. isTouchScreenOrShowDesktopDragHandles() {
  475. // Always enable drag handles for all displays
  476. return true;
  477. },
  478. calculateIndexData(prevData, nextData, nItems = 1) {
  479. let base, increment;
  480. // If we drop the card to an empty column
  481. if (!prevData && !nextData) {
  482. base = 0;
  483. increment = 1;
  484. // If we drop the card in the first position
  485. } else if (!prevData) {
  486. const nextSortIndex = nextData.sort;
  487. const ceil = Math.ceil(nextSortIndex - 1);
  488. if (ceil < nextSortIndex) {
  489. increment = nextSortIndex - ceil;
  490. base = nextSortIndex - increment;
  491. } else {
  492. base = nextData.sort - 1;
  493. increment = -1;
  494. }
  495. // If we drop the card in the last position
  496. } else if (!nextData) {
  497. const prevSortIndex = prevData.sort;
  498. const floor = Math.floor(prevSortIndex + 1);
  499. if (floor > prevSortIndex) {
  500. increment = prevSortIndex - floor;
  501. base = prevSortIndex - increment;
  502. } else {
  503. base = prevData.sort + 1;
  504. increment = 1;
  505. }
  506. }
  507. // In the general case take the average of the previous and next element
  508. // sort indexes.
  509. else {
  510. const prevSortIndex = prevData.sort;
  511. const nextSortIndex = nextData.sort;
  512. if (nItems == 1 ) {
  513. if (prevSortIndex < 0 ) {
  514. const ceil = Math.ceil(nextSortIndex - 1);
  515. if (ceil < nextSortIndex && ceil > prevSortIndex) {
  516. increment = ceil - prevSortIndex;
  517. }
  518. } else {
  519. const floor = Math.floor(nextSortIndex - 1);
  520. if (floor < nextSortIndex && floor > prevSortIndex) {
  521. increment = floor - prevSortIndex;
  522. }
  523. }
  524. }
  525. if (!increment) {
  526. increment = (nextSortIndex - prevSortIndex) / (nItems + 1);
  527. }
  528. if (!base) {
  529. base = prevSortIndex + increment;
  530. }
  531. }
  532. // XXX Return a generator that yield values instead of a base with a
  533. // increment number.
  534. return {
  535. base,
  536. increment,
  537. };
  538. },
  539. // Determine the new sort index
  540. calculateIndex(prevCardDomElement, nextCardDomElement, nCards = 1) {
  541. let prevData = null;
  542. let nextData = null;
  543. if (prevCardDomElement) {
  544. prevData = Blaze.getData(prevCardDomElement)
  545. }
  546. if (nextCardDomElement) {
  547. nextData = Blaze.getData(nextCardDomElement);
  548. }
  549. const ret = Utils.calculateIndexData(prevData, nextData, nCards);
  550. return ret;
  551. },
  552. manageCustomUI() {
  553. Meteor.call('getCustomUI', (err, data) => {
  554. if (err && err.error[0] === 'var-not-exist') {
  555. Session.set('customUI', false); // siteId || address server not defined
  556. }
  557. if (!err) {
  558. Utils.setCustomUI(data);
  559. }
  560. });
  561. },
  562. setCustomUI(data) {
  563. const currentBoard = Utils.getCurrentBoard();
  564. if (currentBoard) {
  565. DocHead.setTitle(`${currentBoard.title} - ${data.productName}`);
  566. } else {
  567. DocHead.setTitle(`${data.productName}`);
  568. }
  569. },
  570. setMatomo(data) {
  571. window._paq = window._paq || [];
  572. window._paq.push(['setDoNotTrack', data.doNotTrack]);
  573. if (data.withUserName) {
  574. window._paq.push(['setUserId', ReactiveCache.getCurrentUser().username]);
  575. }
  576. window._paq.push(['trackPageView']);
  577. window._paq.push(['enableLinkTracking']);
  578. (function () {
  579. window._paq.push(['setTrackerUrl', `${data.address}piwik.php`]);
  580. window._paq.push(['setSiteId', data.siteId]);
  581. const script = document.createElement('script');
  582. Object.assign(script, {
  583. id: 'scriptMatomo',
  584. type: 'text/javascript',
  585. async: 'true',
  586. defer: 'true',
  587. src: `${data.address}piwik.js`,
  588. });
  589. const s = document.getElementsByTagName('script')[0];
  590. s.parentNode.insertBefore(script, s);
  591. })();
  592. Session.set('matomo', true);
  593. },
  594. manageMatomo() {
  595. const matomo = Session.get('matomo');
  596. if (matomo === undefined) {
  597. Meteor.call('getMatomoConf', (err, data) => {
  598. if (err && err.error[0] === 'var-not-exist') {
  599. Session.set('matomo', false); // siteId || address server not defined
  600. }
  601. if (!err) {
  602. Utils.setMatomo(data);
  603. }
  604. });
  605. } else if (matomo) {
  606. window._paq.push(['trackPageView']);
  607. }
  608. },
  609. getTriggerActionDesc(event, tempInstance) {
  610. const jqueryEl = tempInstance.$(event.currentTarget.parentNode);
  611. const triggerEls = jqueryEl.find('.trigger-content').children();
  612. let finalString = '';
  613. for (let i = 0; i < triggerEls.length; i++) {
  614. const element = tempInstance.$(triggerEls[i]);
  615. if (element.hasClass('trigger-text')) {
  616. finalString += element.text().toLowerCase();
  617. } else if (element.hasClass('user-details')) {
  618. let username = element.find('input').val();
  619. if (username === undefined || username === '') {
  620. username = '*';
  621. }
  622. finalString += `${element
  623. .find('.trigger-text')
  624. .text()
  625. .toLowerCase()} ${username}`;
  626. } else if (element.find('select').length > 0) {
  627. finalString += element
  628. .find('select option:selected')
  629. .text()
  630. .toLowerCase();
  631. } else if (element.find('input').length > 0) {
  632. let inputvalue = element.find('input').val();
  633. if (inputvalue === undefined || inputvalue === '') {
  634. inputvalue = '*';
  635. }
  636. finalString += inputvalue;
  637. }
  638. // Add space
  639. if (i !== length - 1) {
  640. finalString += ' ';
  641. }
  642. }
  643. return finalString;
  644. },
  645. fallbackCopyTextToClipboard(text) {
  646. var textArea = document.createElement("textarea");
  647. textArea.value = text;
  648. // Avoid scrolling to bottom
  649. textArea.style.top = "0";
  650. textArea.style.left = "0";
  651. textArea.style.position = "fixed";
  652. document.body.appendChild(textArea);
  653. textArea.focus();
  654. textArea.select();
  655. try {
  656. document.execCommand('copy');
  657. return Promise.resolve(true);
  658. } catch (e) {
  659. return Promise.reject(false);
  660. } finally {
  661. document.body.removeChild(textArea);
  662. }
  663. },
  664. /** copy the text to the clipboard
  665. * @see https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript/30810322#30810322
  666. * @param string copy this text to the clipboard
  667. * @return Promise
  668. */
  669. copyTextToClipboard(text) {
  670. let ret;
  671. if (navigator.clipboard) {
  672. ret = navigator.clipboard.writeText(text).then(function () {
  673. }, function (err) {
  674. console.error('Async: Could not copy text: ', err);
  675. });
  676. } else {
  677. ret = Utils.fallbackCopyTextToClipboard(text);
  678. }
  679. return ret;
  680. },
  681. /** show the "copied!" message
  682. * @param promise the promise of Utils.copyTextToClipboard
  683. * @param $tooltip jQuery tooltip element
  684. */
  685. showCopied(promise, $tooltip) {
  686. if (promise) {
  687. promise.then(() => {
  688. $tooltip.show(100);
  689. setTimeout(() => $tooltip.hide(100), 1000);
  690. }, (err) => {
  691. console.error("error: ", err);
  692. });
  693. }
  694. },
  695. };
  696. // A simple tracker dependency that we invalidate every time the window is
  697. // resized. This is used to reactively re-calculate the popup position in case
  698. // of a window resize. This is the equivalent of a "Signal" in some other
  699. // programming environments (eg, elm).
  700. $(window).on('resize', () => Utils.windowResizeDep.changed());