You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stopwatch.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import $ from 'jquery';
  2. import prettyMilliseconds from 'pretty-ms';
  3. const {appSubUrl, csrfToken, notificationSettings, enableTimeTracking} = window.config;
  4. let updateTimeInterval = null; // holds setInterval id when active
  5. export function initStopwatch() {
  6. if (!enableTimeTracking) {
  7. return;
  8. }
  9. const stopwatchEl = $('.active-stopwatch-trigger');
  10. if (!stopwatchEl.length) {
  11. return;
  12. }
  13. stopwatchEl.removeAttr('href'); // intended for noscript mode only
  14. stopwatchEl.popup({
  15. position: 'bottom right',
  16. hoverable: true,
  17. });
  18. // form handlers
  19. $('form > button', stopwatchEl).on('click', function () {
  20. $(this).parent().trigger('submit');
  21. });
  22. if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
  23. // Try to connect to the event source via the shared worker first
  24. const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
  25. worker.addEventListener('error', (event) => {
  26. console.error(event);
  27. });
  28. worker.port.addEventListener('messageerror', () => {
  29. console.error('Unable to deserialize message');
  30. });
  31. worker.port.postMessage({
  32. type: 'start',
  33. url: `${window.location.origin}${appSubUrl}/user/events`,
  34. });
  35. worker.port.addEventListener('message', (event) => {
  36. if (!event.data || !event.data.type) {
  37. console.error(event);
  38. return;
  39. }
  40. if (event.data.type === 'stopwatches') {
  41. updateStopwatchData(JSON.parse(event.data.data));
  42. } else if (event.data.type === 'error') {
  43. console.error(event.data);
  44. } else if (event.data.type === 'logout') {
  45. if (event.data.data !== 'here') {
  46. return;
  47. }
  48. worker.port.postMessage({
  49. type: 'close',
  50. });
  51. worker.port.close();
  52. window.location.href = appSubUrl;
  53. } else if (event.data.type === 'close') {
  54. worker.port.postMessage({
  55. type: 'close',
  56. });
  57. worker.port.close();
  58. }
  59. });
  60. worker.port.addEventListener('error', (e) => {
  61. console.error(e);
  62. });
  63. worker.port.start();
  64. window.addEventListener('beforeunload', () => {
  65. worker.port.postMessage({
  66. type: 'close',
  67. });
  68. worker.port.close();
  69. });
  70. return;
  71. }
  72. if (notificationSettings.MinTimeout <= 0) {
  73. return;
  74. }
  75. const fn = (timeout) => {
  76. setTimeout(() => {
  77. const _promise = updateStopwatchWithCallback(fn, timeout);
  78. }, timeout);
  79. };
  80. fn(notificationSettings.MinTimeout);
  81. const currSeconds = $('.stopwatch-time').data('seconds');
  82. if (currSeconds) {
  83. updateTimeInterval = updateStopwatchTime(currSeconds);
  84. }
  85. }
  86. async function updateStopwatchWithCallback(callback, timeout) {
  87. const isSet = await updateStopwatch();
  88. if (!isSet) {
  89. timeout = notificationSettings.MinTimeout;
  90. } else if (timeout < notificationSettings.MaxTimeout) {
  91. timeout += notificationSettings.TimeoutStep;
  92. }
  93. callback(timeout);
  94. }
  95. async function updateStopwatch() {
  96. const data = await $.ajax({
  97. type: 'GET',
  98. url: `${appSubUrl}/user/stopwatches`,
  99. headers: {'X-Csrf-Token': csrfToken},
  100. });
  101. if (updateTimeInterval) {
  102. clearInterval(updateTimeInterval);
  103. updateTimeInterval = null;
  104. }
  105. return updateStopwatchData(data);
  106. }
  107. function updateStopwatchData(data) {
  108. const watch = data[0];
  109. const btnEl = $('.active-stopwatch-trigger');
  110. if (!watch) {
  111. if (updateTimeInterval) {
  112. clearInterval(updateTimeInterval);
  113. updateTimeInterval = null;
  114. }
  115. btnEl.addClass('hidden');
  116. } else {
  117. const {repo_owner_name, repo_name, issue_index, seconds} = watch;
  118. const issueUrl = `${appSubUrl}/${repo_owner_name}/${repo_name}/issues/${issue_index}`;
  119. $('.stopwatch-link').attr('href', issueUrl);
  120. $('.stopwatch-commit').attr('action', `${issueUrl}/times/stopwatch/toggle`);
  121. $('.stopwatch-cancel').attr('action', `${issueUrl}/times/stopwatch/cancel`);
  122. $('.stopwatch-issue').text(`${repo_owner_name}/${repo_name}#${issue_index}`);
  123. $('.stopwatch-time').text(prettyMilliseconds(seconds * 1000));
  124. updateStopwatchTime(seconds);
  125. btnEl.removeClass('hidden');
  126. }
  127. return !!data.length;
  128. }
  129. function updateStopwatchTime(seconds) {
  130. const secs = parseInt(seconds);
  131. if (!Number.isFinite(secs)) return;
  132. const start = Date.now();
  133. updateTimeInterval = setInterval(() => {
  134. const delta = Date.now() - start;
  135. const dur = prettyMilliseconds(secs * 1000 + delta, {compact: true});
  136. $('.stopwatch-time').text(dur);
  137. }, 1000);
  138. }