Skip to content

Commit

Permalink
Remove jQuery from repo commit functions
Browse files Browse the repository at this point in the history
- 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

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
  • Loading branch information
yardenshoham committed Feb 17, 2024
1 parent 5e1bf3e commit 169a9b5
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions web_src/js/features/repo-commit.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,76 @@
import $ from 'jquery';
import {createTippy} from '../modules/tippy.js';
import {toggleElem} from '../utils/dom.js';

const {csrfToken} = window.config;
import {POST} from '../modules/fetch.js';

export function initRepoEllipsisButton() {
$('.js-toggle-commit-body').on('click', function (e) {
e.preventDefault();
const expanded = $(this).attr('aria-expanded') === 'true';
toggleElem($(this).parent().find('.commit-body'));
$(this).attr('aria-expanded', String(!expanded));
});
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 function initRepoCommitLastCommitLoader() {
const parser = new DOMParser();

export async function initRepoCommitLastCommitLoader() {
const entryMap = {};

const entries = $('table#repo-files-table tr.notready')
.map((_, v) => {
entryMap[$(v).attr('data-entryname')] = $(v);
return $(v).attr('data-entryname');
})
.get();
const entries = Array.from(document.querySelectorAll('table#repo-files-table tr.notready'), (v) => {
entryMap[v.getAttribute('data-entryname')] = v;
return v.getAttribute('data-entryname');
});

if (entries.length === 0) {
return;
}

const lastCommitLoaderURL = $('table#repo-files-table').data('lastCommitLoaderUrl');
const lastCommitLoaderURL = document.querySelector('table#repo-files-table').getAttribute('data-last-commit-loader-url');

if (entries.length > 200) {
$.post(lastCommitLoaderURL, {
_csrf: csrfToken,
}, (data) => {
$('table#repo-files-table').replaceWith(data);
});
// For more than 200 entries, replace the entire table
const response = await POST(lastCommitLoaderURL);
const data = await response.text();
const table = document.querySelector('table#repo-files-table');
const parent = table.parentNode;
const wrapper = document.createElement('div');
wrapper.innerHTML = data;
const newTable = wrapper.querySelector('table');
if (newTable) {
parent.replaceChild(newTable, table);
}
return;
}

$.post(lastCommitLoaderURL, {
_csrf: csrfToken,
'f': entries,
}, (data) => {
$(data).find('tr').each((_, row) => {
if (row.className === 'commit-list') {
$('table#repo-files-table .commit-list').replaceWith(row);
return;
}
// 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).attr('data-entryname');
if (entryName) {
entryMap[entryName].replaceWith(row);
}
});
});
// For fewer entries, update individual rows
const response = POST(lastCommitLoaderURL, {data: {'f': entries}});
const data = await response.text();
const doc = parser.parseFromString(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);
return;
}

const entryName = row.getAttribute('data-entryname');
if (entryName) {
entryMap[entryName].replaceWith(row);
}
}
}

export function initCommitStatuses() {
$('[data-tippy="commit-statuses"]').each(function () {
const top = $('.repository.file.list').length > 0 || $('.repository.diff').length > 0;
for (const element of document.querySelectorAll('[data-tippy="commit-statuses"]')) {
const top = document.querySelector('.repository.file.list') || document.querySelector('.repository.diff');

createTippy(this, {
content: this.nextElementSibling,
createTippy(element, {
content: element.nextElementSibling,
placement: top ? 'top-start' : 'bottom-start',
interactive: true,
role: 'dialog',
theme: 'box-with-header',
});
});
}
}

0 comments on commit 169a9b5

Please sign in to comment.