1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-22 08:42:32 -05:00
forgejo/web_src/js/features/repo-commit.js
Yarden Shoham 6db296f1cb
Remove jQuery from the repo commit functions (#29230)
- Switched to plain JavaScript
- Tested the commit ellipsis button functionality and it works as before
- Tested the commits statuses tippy functionality and it works as before
- Tested the last commit loader functionality and it works as before

# Demo using JavaScript without jQuery

![action](https://github.com/go-gitea/gitea/assets/20454870/465516f8-0ff3-438c-a17e-26cbab82750b)

![action](https://github.com/go-gitea/gitea/assets/20454870/968da210-9382-4b50-a4c2-09419dc86e07)

---------

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit eaede2de98fbe0ac2156c9f4cd8b5899d2c7cbbf)
2024-02-26 22:30:26 +01:00

70 lines
2.4 KiB
JavaScript

import {createTippy} from '../modules/tippy.js';
import {toggleElem} from '../utils/dom.js';
import {parseDom} from '../utils.js';
import {POST} from '../modules/fetch.js';
export function initRepoEllipsisButton() {
for (const button of document.querySelectorAll('.js-toggle-commit-body')) {
button.addEventListener('click', function (e) {
e.preventDefault();
const expanded = this.getAttribute('aria-expanded') === 'true';
toggleElem(this.parentElement.querySelector('.commit-body'));
this.setAttribute('aria-expanded', String(!expanded));
});
}
}
export async function initRepoCommitLastCommitLoader() {
const entryMap = {};
const entries = Array.from(document.querySelectorAll('table#repo-files-table tr.notready'), (el) => {
const entryName = el.getAttribute('data-entryname');
entryMap[entryName] = el;
return entryName;
});
if (entries.length === 0) {
return;
}
const lastCommitLoaderURL = document.querySelector('table#repo-files-table').getAttribute('data-last-commit-loader-url');
if (entries.length > 200) {
// For more than 200 entries, replace the entire table
const response = await POST(lastCommitLoaderURL);
const data = await response.text();
document.querySelector('table#repo-files-table').outerHTML = data;
return;
}
// For fewer entries, update individual rows
const response = await POST(lastCommitLoaderURL, {data: {'f': entries}});
const data = await response.text();
const doc = parseDom(data, 'text/html');
for (const row of doc.querySelectorAll('tr')) {
if (row.className === 'commit-list') {
document.querySelector('table#repo-files-table .commit-list')?.replaceWith(row);
continue;
}
// there are other <tr> rows in response (eg: <tr class="has-parent">)
// at the moment only the "data-entryname" rows should be processed
const entryName = row.getAttribute('data-entryname');
if (entryName) {
entryMap[entryName]?.replaceWith(row);
}
}
}
export function initCommitStatuses() {
for (const element of document.querySelectorAll('[data-tippy="commit-statuses"]')) {
const top = document.querySelector('.repository.file.list') || document.querySelector('.repository.diff');
createTippy(element, {
content: element.nextElementSibling,
placement: top ? 'top-start' : 'bottom-start',
interactive: true,
role: 'dialog',
theme: 'box-with-header',
});
}
}