utils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. // Show mobile view when:
  403. // 1. Screen width is 800px or less (matches CSS media queries)
  404. // 2. Mobile phones in portrait mode
  405. // 3. iPad in very small screens (≤ 600px)
  406. const isSmallScreen = window.innerWidth <= 800;
  407. const isVerySmallScreen = window.innerWidth <= 600;
  408. const isPortrait = window.innerWidth < window.innerHeight || window.matchMedia("(orientation: portrait)").matches;
  409. const isMobilePhone = /Mobile|Android|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) && !/iPad/i.test(navigator.userAgent);
  410. const isIPhone = /iPhone|iPod/i.test(navigator.userAgent);
  411. const isIPad = /iPad/i.test(navigator.userAgent);
  412. const isUbuntuTouch = /Ubuntu/i.test(navigator.userAgent);
  413. // For iPhone: always show mobile view regardless of orientation
  414. // For other mobile phones: show mobile view in portrait, desktop view in landscape
  415. // For iPad: show mobile view only in very small screens (≤ 600px)
  416. // For Ubuntu Touch: smartphones behave like mobile phones, tablets like iPad
  417. // For desktop: show mobile view when screen width <= 800px
  418. if (isIPhone) {
  419. return true; // iPhone: always mobile view
  420. } else if (isMobilePhone) {
  421. return isPortrait; // Other mobile phones: portrait = mobile, landscape = desktop
  422. } else if (isIPad) {
  423. return isVerySmallScreen; // iPad: only very small screens get mobile view
  424. } else if (isUbuntuTouch) {
  425. // Ubuntu Touch: smartphones (≤ 600px) behave like mobile phones, tablets (> 600px) like iPad
  426. if (isVerySmallScreen) {
  427. return isPortrait; // Ubuntu Touch smartphone: portrait = mobile, landscape = desktop
  428. } else {
  429. return isVerySmallScreen; // Ubuntu Touch tablet: only very small screens get mobile view
  430. }
  431. } else {
  432. return isSmallScreen; // Desktop: based on 800px screen width
  433. }
  434. },
  435. isTouchScreen() {
  436. // NEW TOUCH DEVICE DETECTION:
  437. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
  438. var hasTouchScreen = false;
  439. if ("maxTouchPoints" in navigator) {
  440. hasTouchScreen = navigator.maxTouchPoints > 0;
  441. } else if ("msMaxTouchPoints" in navigator) {
  442. hasTouchScreen = navigator.msMaxTouchPoints > 0;
  443. } else {
  444. var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
  445. if (mQ && mQ.media === "(pointer:coarse)") {
  446. hasTouchScreen = !!mQ.matches;
  447. } else if ('orientation' in window) {
  448. hasTouchScreen = true; // deprecated, but good fallback
  449. } else {
  450. // Only as a last resort, fall back to user agent sniffing
  451. var UA = navigator.userAgent;
  452. hasTouchScreen = (
  453. /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
  454. /\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
  455. );
  456. }
  457. }
  458. return hasTouchScreen;
  459. },
  460. // returns if desktop drag handles are enabled
  461. isShowDesktopDragHandles() {
  462. if (this.isTouchScreen()) {
  463. return true;
  464. /*
  465. const currentUser = ReactiveCache.getCurrentUser();
  466. if (currentUser) {
  467. return (currentUser.profile || {}).showDesktopDragHandles;
  468. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  469. //
  470. if (window.localStorage.getItem('showDesktopDragHandles')) {
  471. return true;
  472. } else {
  473. return false;
  474. */
  475. } else {
  476. return false;
  477. }
  478. },
  479. // returns if mini screen or desktop drag handles
  480. isTouchScreenOrShowDesktopDragHandles() {
  481. // Always enable drag handles for mobile screens (touch devices)
  482. return this.isTouchScreen() || this.isMiniScreen();
  483. //return this.isTouchScreen() || this.isShowDesktopDragHandles();
  484. //return this.isShowDesktopDragHandles();
  485. },
  486. calculateIndexData(prevData, nextData, nItems = 1) {
  487. let base, increment;
  488. // If we drop the card to an empty column
  489. if (!prevData && !nextData) {
  490. base = 0;
  491. increment = 1;
  492. // If we drop the card in the first position
  493. } else if (!prevData) {
  494. const nextSortIndex = nextData.sort;
  495. const ceil = Math.ceil(nextSortIndex - 1);
  496. if (ceil < nextSortIndex) {
  497. increment = nextSortIndex - ceil;
  498. base = nextSortIndex - increment;
  499. } else {
  500. base = nextData.sort - 1;
  501. increment = -1;
  502. }
  503. // If we drop the card in the last position
  504. } else if (!nextData) {
  505. const prevSortIndex = prevData.sort;
  506. const floor = Math.floor(prevSortIndex + 1);
  507. if (floor > prevSortIndex) {
  508. increment = prevSortIndex - floor;
  509. base = prevSortIndex - increment;
  510. } else {
  511. base = prevData.sort + 1;
  512. increment = 1;
  513. }
  514. }
  515. // In the general case take the average of the previous and next element
  516. // sort indexes.
  517. else {
  518. const prevSortIndex = prevData.sort;
  519. const nextSortIndex = nextData.sort;
  520. if (nItems == 1 ) {
  521. if (prevSortIndex < 0 ) {
  522. const ceil = Math.ceil(nextSortIndex - 1);
  523. if (ceil < nextSortIndex && ceil > prevSortIndex) {
  524. increment = ceil - prevSortIndex;
  525. }
  526. } else {
  527. const floor = Math.floor(nextSortIndex - 1);
  528. if (floor < nextSortIndex && floor > prevSortIndex) {
  529. increment = floor - prevSortIndex;
  530. }
  531. }
  532. }
  533. if (!increment) {
  534. increment = (nextSortIndex - prevSortIndex) / (nItems + 1);
  535. }
  536. if (!base) {
  537. base = prevSortIndex + increment;
  538. }
  539. }
  540. // XXX Return a generator that yield values instead of a base with a
  541. // increment number.
  542. return {
  543. base,
  544. increment,
  545. };
  546. },
  547. // Determine the new sort index
  548. calculateIndex(prevCardDomElement, nextCardDomElement, nCards = 1) {
  549. let prevData = null;
  550. let nextData = null;
  551. if (prevCardDomElement) {
  552. prevData = Blaze.getData(prevCardDomElement)
  553. }
  554. if (nextCardDomElement) {
  555. nextData = Blaze.getData(nextCardDomElement);
  556. }
  557. const ret = Utils.calculateIndexData(prevData, nextData, nCards);
  558. return ret;
  559. },
  560. manageCustomUI() {
  561. Meteor.call('getCustomUI', (err, data) => {
  562. if (err && err.error[0] === 'var-not-exist') {
  563. Session.set('customUI', false); // siteId || address server not defined
  564. }
  565. if (!err) {
  566. Utils.setCustomUI(data);
  567. }
  568. });
  569. },
  570. setCustomUI(data) {
  571. const currentBoard = Utils.getCurrentBoard();
  572. if (currentBoard) {
  573. DocHead.setTitle(`${currentBoard.title} - ${data.productName}`);
  574. } else {
  575. DocHead.setTitle(`${data.productName}`);
  576. }
  577. },
  578. setMatomo(data) {
  579. window._paq = window._paq || [];
  580. window._paq.push(['setDoNotTrack', data.doNotTrack]);
  581. if (data.withUserName) {
  582. window._paq.push(['setUserId', ReactiveCache.getCurrentUser().username]);
  583. }
  584. window._paq.push(['trackPageView']);
  585. window._paq.push(['enableLinkTracking']);
  586. (function () {
  587. window._paq.push(['setTrackerUrl', `${data.address}piwik.php`]);
  588. window._paq.push(['setSiteId', data.siteId]);
  589. const script = document.createElement('script');
  590. Object.assign(script, {
  591. id: 'scriptMatomo',
  592. type: 'text/javascript',
  593. async: 'true',
  594. defer: 'true',
  595. src: `${data.address}piwik.js`,
  596. });
  597. const s = document.getElementsByTagName('script')[0];
  598. s.parentNode.insertBefore(script, s);
  599. })();
  600. Session.set('matomo', true);
  601. },
  602. manageMatomo() {
  603. const matomo = Session.get('matomo');
  604. if (matomo === undefined) {
  605. Meteor.call('getMatomoConf', (err, data) => {
  606. if (err && err.error[0] === 'var-not-exist') {
  607. Session.set('matomo', false); // siteId || address server not defined
  608. }
  609. if (!err) {
  610. Utils.setMatomo(data);
  611. }
  612. });
  613. } else if (matomo) {
  614. window._paq.push(['trackPageView']);
  615. }
  616. },
  617. getTriggerActionDesc(event, tempInstance) {
  618. const jqueryEl = tempInstance.$(event.currentTarget.parentNode);
  619. const triggerEls = jqueryEl.find('.trigger-content').children();
  620. let finalString = '';
  621. for (let i = 0; i < triggerEls.length; i++) {
  622. const element = tempInstance.$(triggerEls[i]);
  623. if (element.hasClass('trigger-text')) {
  624. finalString += element.text().toLowerCase();
  625. } else if (element.hasClass('user-details')) {
  626. let username = element.find('input').val();
  627. if (username === undefined || username === '') {
  628. username = '*';
  629. }
  630. finalString += `${element
  631. .find('.trigger-text')
  632. .text()
  633. .toLowerCase()} ${username}`;
  634. } else if (element.find('select').length > 0) {
  635. finalString += element
  636. .find('select option:selected')
  637. .text()
  638. .toLowerCase();
  639. } else if (element.find('input').length > 0) {
  640. let inputvalue = element.find('input').val();
  641. if (inputvalue === undefined || inputvalue === '') {
  642. inputvalue = '*';
  643. }
  644. finalString += inputvalue;
  645. }
  646. // Add space
  647. if (i !== length - 1) {
  648. finalString += ' ';
  649. }
  650. }
  651. return finalString;
  652. },
  653. fallbackCopyTextToClipboard(text) {
  654. var textArea = document.createElement("textarea");
  655. textArea.value = text;
  656. // Avoid scrolling to bottom
  657. textArea.style.top = "0";
  658. textArea.style.left = "0";
  659. textArea.style.position = "fixed";
  660. document.body.appendChild(textArea);
  661. textArea.focus();
  662. textArea.select();
  663. try {
  664. document.execCommand('copy');
  665. return Promise.resolve(true);
  666. } catch (e) {
  667. return Promise.reject(false);
  668. } finally {
  669. document.body.removeChild(textArea);
  670. }
  671. },
  672. /** copy the text to the clipboard
  673. * @see https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript/30810322#30810322
  674. * @param string copy this text to the clipboard
  675. * @return Promise
  676. */
  677. copyTextToClipboard(text) {
  678. let ret;
  679. if (navigator.clipboard) {
  680. ret = navigator.clipboard.writeText(text).then(function () {
  681. }, function (err) {
  682. console.error('Async: Could not copy text: ', err);
  683. });
  684. } else {
  685. ret = Utils.fallbackCopyTextToClipboard(text);
  686. }
  687. return ret;
  688. },
  689. /** show the "copied!" message
  690. * @param promise the promise of Utils.copyTextToClipboard
  691. * @param $tooltip jQuery tooltip element
  692. */
  693. showCopied(promise, $tooltip) {
  694. if (promise) {
  695. promise.then(() => {
  696. $tooltip.show(100);
  697. setTimeout(() => $tooltip.hide(100), 1000);
  698. }, (err) => {
  699. console.error("error: ", err);
  700. });
  701. }
  702. },
  703. };
  704. // A simple tracker dependency that we invalidate every time the window is
  705. // resized. This is used to reactively re-calculate the popup position in case
  706. // of a window resize. This is the equivalent of a "Signal" in some other
  707. // programming environments (eg, elm).
  708. $(window).on('resize', () => Utils.windowResizeDep.changed());