mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
167 lines
5.9 KiB
167 lines
5.9 KiB
5 months ago
|
import {createTippy} from '../modules/tippy.ts';
|
||
|
import {GET} from '../modules/fetch.ts';
|
||
|
import {hideElem, showElem} from '../utils/dom.ts';
|
||
|
import {logoutFromWorker} from '../modules/worker.ts';
|
||
4 years ago
|
|
||
9 months ago
|
const {appSubUrl, notificationSettings, enableTimeTracking, assetVersionEncoded} = window.config;
|
||
4 years ago
|
|
||
3 years ago
|
export function initStopwatch() {
|
||
3 years ago
|
if (!enableTimeTracking) {
|
||
4 years ago
|
return;
|
||
|
}
|
||
4 years ago
|
|
||
7 months ago
|
const stopwatchEls = document.querySelectorAll('.active-stopwatch');
|
||
2 years ago
|
const stopwatchPopup = document.querySelector('.active-stopwatch-popup');
|
||
4 years ago
|
|
||
7 months ago
|
if (!stopwatchEls.length || !stopwatchPopup) {
|
||
4 years ago
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
// global stop watch (in the head_navbar), it should always work in any case either the EventSource or the PeriodicPoller is used.
|
||
7 months ago
|
const seconds = stopwatchEls[0]?.getAttribute('data-seconds');
|
||
|
if (seconds) {
|
||
|
updateStopwatchTime(parseInt(seconds));
|
||
|
}
|
||
|
|
||
|
for (const stopwatchEl of stopwatchEls) {
|
||
|
stopwatchEl.removeAttribute('href'); // intended for noscript mode only
|
||
|
|
||
|
createTippy(stopwatchEl, {
|
||
|
content: stopwatchPopup.cloneNode(true),
|
||
|
placement: 'bottom-end',
|
||
|
trigger: 'click',
|
||
|
maxWidth: 'none',
|
||
|
interactive: true,
|
||
|
hideOnClick: true,
|
||
|
theme: 'default',
|
||
|
});
|
||
2 years ago
|
}
|
||
|
|
||
|
let usingPeriodicPoller = false;
|
||
|
const startPeriodicPoller = (timeout) => {
|
||
|
if (timeout <= 0 || !Number.isFinite(timeout)) return;
|
||
|
usingPeriodicPoller = true;
|
||
|
setTimeout(() => updateStopwatchWithCallback(startPeriodicPoller, timeout), timeout);
|
||
|
};
|
||
|
|
||
|
// if the browser supports EventSource and SharedWorker, use it instead of the periodic poller
|
||
|
if (notificationSettings.EventSourceUpdateTime > 0 && window.EventSource && window.SharedWorker) {
|
||
4 years ago
|
// Try to connect to the event source via the shared worker first
|
||
2 years ago
|
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js?v=${assetVersionEncoded}`, 'notification-worker');
|
||
4 years ago
|
worker.addEventListener('error', (event) => {
|
||
2 years ago
|
console.error('worker error', event);
|
||
4 years ago
|
});
|
||
3 years ago
|
worker.port.addEventListener('messageerror', () => {
|
||
2 years ago
|
console.error('unable to deserialize message');
|
||
3 years ago
|
});
|
||
4 years ago
|
worker.port.postMessage({
|
||
|
type: 'start',
|
||
3 years ago
|
url: `${window.location.origin}${appSubUrl}/user/events`,
|
||
4 years ago
|
});
|
||
|
worker.port.addEventListener('message', (event) => {
|
||
|
if (!event.data || !event.data.type) {
|
||
2 years ago
|
console.error('unknown worker message event', event);
|
||
4 years ago
|
return;
|
||
|
}
|
||
|
if (event.data.type === 'stopwatches') {
|
||
|
updateStopwatchData(JSON.parse(event.data.data));
|
||
2 years ago
|
} else if (event.data.type === 'no-event-source') {
|
||
|
// browser doesn't support EventSource, falling back to periodic poller
|
||
|
if (!usingPeriodicPoller) startPeriodicPoller(notificationSettings.MinTimeout);
|
||
4 years ago
|
} else if (event.data.type === 'error') {
|
||
2 years ago
|
console.error('worker port event error', event.data);
|
||
4 years ago
|
} else if (event.data.type === 'logout') {
|
||
4 years ago
|
if (event.data.data !== 'here') {
|
||
4 years ago
|
return;
|
||
|
}
|
||
|
worker.port.postMessage({
|
||
|
type: 'close',
|
||
|
});
|
||
|
worker.port.close();
|
||
7 months ago
|
logoutFromWorker();
|
||
4 years ago
|
} else if (event.data.type === 'close') {
|
||
|
worker.port.postMessage({
|
||
|
type: 'close',
|
||
|
});
|
||
|
worker.port.close();
|
||
4 years ago
|
}
|
||
|
});
|
||
|
worker.port.addEventListener('error', (e) => {
|
||
2 years ago
|
console.error('worker port error', e);
|
||
4 years ago
|
});
|
||
|
worker.port.start();
|
||
|
window.addEventListener('beforeunload', () => {
|
||
|
worker.port.postMessage({
|
||
|
type: 'close',
|
||
|
});
|
||
|
worker.port.close();
|
||
|
});
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
startPeriodicPoller(notificationSettings.MinTimeout);
|
||
4 years ago
|
}
|
||
|
|
||
|
async function updateStopwatchWithCallback(callback, timeout) {
|
||
|
const isSet = await updateStopwatch();
|
||
|
|
||
|
if (!isSet) {
|
||
3 years ago
|
timeout = notificationSettings.MinTimeout;
|
||
|
} else if (timeout < notificationSettings.MaxTimeout) {
|
||
|
timeout += notificationSettings.TimeoutStep;
|
||
4 years ago
|
}
|
||
|
|
||
|
callback(timeout);
|
||
|
}
|
||
|
|
||
|
async function updateStopwatch() {
|
||
9 months ago
|
const response = await GET(`${appSubUrl}/user/stopwatches`);
|
||
|
if (!response.ok) {
|
||
|
console.error('Failed to fetch stopwatch data');
|
||
|
return false;
|
||
|
}
|
||
|
const data = await response.json();
|
||
4 years ago
|
return updateStopwatchData(data);
|
||
|
}
|
||
|
|
||
3 years ago
|
function updateStopwatchData(data) {
|
||
4 years ago
|
const watch = data[0];
|
||
7 months ago
|
const btnEls = document.querySelectorAll('.active-stopwatch');
|
||
4 years ago
|
if (!watch) {
|
||
7 months ago
|
hideElem(btnEls);
|
||
4 years ago
|
} else {
|
||
|
const {repo_owner_name, repo_name, issue_index, seconds} = watch;
|
||
3 years ago
|
const issueUrl = `${appSubUrl}/${repo_owner_name}/${repo_name}/issues/${issue_index}`;
|
||
9 months ago
|
document.querySelector('.stopwatch-link')?.setAttribute('href', issueUrl);
|
||
|
document.querySelector('.stopwatch-commit')?.setAttribute('action', `${issueUrl}/times/stopwatch/toggle`);
|
||
|
document.querySelector('.stopwatch-cancel')?.setAttribute('action', `${issueUrl}/times/stopwatch/cancel`);
|
||
|
const stopwatchIssue = document.querySelector('.stopwatch-issue');
|
||
|
if (stopwatchIssue) stopwatchIssue.textContent = `${repo_owner_name}/${repo_name}#${issue_index}`;
|
||
2 years ago
|
updateStopwatchTime(seconds);
|
||
7 months ago
|
showElem(btnEls);
|
||
4 years ago
|
}
|
||
2 years ago
|
return Boolean(data.length);
|
||
4 years ago
|
}
|
||
|
|
||
7 months ago
|
// TODO: This flickers on page load, we could avoid this by making a custom
|
||
|
// element to render time periods. Feeding a datetime in backend does not work
|
||
|
// when time zone between server and client differs.
|
||
3 years ago
|
function updateStopwatchTime(seconds) {
|
||
7 months ago
|
if (!Number.isFinite(seconds)) return;
|
||
|
const datetime = (new Date(Date.now() - seconds * 1000)).toISOString();
|
||
|
for (const parent of document.querySelectorAll('.header-stopwatch-dot')) {
|
||
|
const existing = parent.querySelector(':scope > relative-time');
|
||
|
if (existing) {
|
||
|
existing.setAttribute('datetime', datetime);
|
||
|
} else {
|
||
|
const el = document.createElement('relative-time');
|
||
|
el.setAttribute('format', 'micro');
|
||
|
el.setAttribute('datetime', datetime);
|
||
|
el.setAttribute('lang', 'en-US');
|
||
|
el.setAttribute('title', ''); // make <relative-time> show no title and therefor no tooltip
|
||
|
parent.append(el);
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}
|