Skip to content

Commit 937e8b5

Browse files
Fix elipsis button not working if the last commit loading is deferred (#29544)
Before this change, if we had more than 200 entries being deferred in loading, the entire table would get replaced thus losing any event listeners attached to the elements within the table, such as the elipsis button and commit list with tippy. With this change we remove the previous javascript code that replaced the table and use htmx to replace the table. htmx attributes added: - `hx-indicator="tr.notready td.message span"`: attach the loading spinner to the files whose last commit is still being loaded - `hx-trigger="load"` trigger the request-replace behavior as soon as possible - `hx-swap="morph"`: use the idiomorph morphing algorithm, this is the thing that makes it so the elipsis button event listener is kept during the replacement, fixing the bug because we don't actually replace the table, only modifying it - `hx-post="{{.LastCommitLoaderURL}}"`: make a post request to this url to get the table with all of the commit information As part of this change I removed the handling of partial replacement in the case we have less than 200 "not ready" files. The first reason is that I couldn't make htmx replace only a subset of returned elements, the second reason is that we have a cache implemented in the backend already so the only cost added is that we query the cache a few times (which is sure to be populated due to the initial request), and the last reason is that since the last refactor of this functionality that removed jQuery we don't properly send the "not ready" entries as the backend expects `FormData` with `f[]` and we send a JSON with `f` so we always query for all rows anyway. # Before ![before](https://github.com/go-gitea/gitea/assets/20454870/482ebfec-66c5-40cc-9c1e-e3b3bfe1bbc1) # After ![after](https://github.com/go-gitea/gitea/assets/20454870/454c517e-3a4e-4006-a49f-99cc56e0fd60) --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent bf6502a commit 937e8b5

File tree

4 files changed

+12
-68
lines changed

4 files changed

+12
-68
lines changed

routers/web/repo/view.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"code.gitea.io/gitea/modules/actions"
3636
"code.gitea.io/gitea/modules/base"
3737
"code.gitea.io/gitea/modules/charset"
38-
"code.gitea.io/gitea/modules/container"
3938
"code.gitea.io/gitea/modules/git"
4039
"code.gitea.io/gitea/modules/highlight"
4140
"code.gitea.io/gitea/modules/lfs"
@@ -859,25 +858,18 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
859858
defer cancel()
860859
}
861860

862-
selected := make(container.Set[string])
863-
selected.AddMultiple(ctx.FormStrings("f[]")...)
864-
865-
entries := allEntries
866-
if len(selected) > 0 {
867-
entries = make(git.Entries, 0, len(selected))
868-
for _, entry := range allEntries {
869-
if selected.Contains(entry.Name()) {
870-
entries = append(entries, entry)
871-
}
872-
}
873-
}
874-
875-
var latestCommit *git.Commit
876-
ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath)
861+
files, latestCommit, err := allEntries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath)
877862
if err != nil {
878863
ctx.ServerError("GetCommitsInfo", err)
879864
return nil
880865
}
866+
ctx.Data["Files"] = files
867+
for _, f := range files {
868+
if f.Commit == nil {
869+
ctx.Data["HasFilesWithoutLatestCommit"] = true
870+
break
871+
}
872+
}
881873

882874
if !loadLatestCommitData(ctx, latestCommit) {
883875
return nil

templates/repo/view_list.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<table id="repo-files-table" class="ui single line table gt-mt-0" data-last-commit-loader-url="{{.LastCommitLoaderURL}}">
1+
<table id="repo-files-table" class="ui single line table gt-mt-0" {{if .HasFilesWithoutLatestCommit}}hx-indicator="tr.notready td.message span" hx-trigger="load" hx-swap="morph" hx-post="{{.LastCommitLoaderURL}}"{{end}}>
22
<thead>
33
<tr class="commit-list">
4-
<th colspan="2" {{if not .LatestCommit}}class="notready"{{end}}>
4+
<th colspan="2">
55
{{template "repo/latest_commit" .}}
66
</th>
77
<th class="text grey right age">{{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When ctx.Locale}}{{end}}{{end}}</th>
@@ -55,7 +55,7 @@
5555
{{$commitLink := printf "%s/commit/%s" $.RepoLink (PathEscape $commit.ID.String)}}
5656
{{RenderCommitMessageLinkSubject $.Context $commit.Message $commitLink ($.Repository.ComposeMetas ctx)}}
5757
{{else}}
58-
<div class="ui active tiny slow centered inline"></div>
58+
<div class="ui active tiny slow centered inline"></div>
5959
{{end}}
6060
</span>
6161
</td>

web_src/js/features/repo-commit.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {createTippy} from '../modules/tippy.js';
22
import {toggleElem} from '../utils/dom.js';
3-
import {parseDom} from '../utils.js';
4-
import {POST} from '../modules/fetch.js';
53

64
export function initRepoEllipsisButton() {
75
for (const button of document.querySelectorAll('.js-toggle-commit-body')) {
@@ -14,47 +12,6 @@ export function initRepoEllipsisButton() {
1412
}
1513
}
1614

17-
export async function initRepoCommitLastCommitLoader() {
18-
const entryMap = {};
19-
20-
const entries = Array.from(document.querySelectorAll('table#repo-files-table tr.notready'), (el) => {
21-
const entryName = el.getAttribute('data-entryname');
22-
entryMap[entryName] = el;
23-
return entryName;
24-
});
25-
26-
if (entries.length === 0) {
27-
return;
28-
}
29-
30-
const lastCommitLoaderURL = document.querySelector('table#repo-files-table').getAttribute('data-last-commit-loader-url');
31-
32-
if (entries.length > 200) {
33-
// For more than 200 entries, replace the entire table
34-
const response = await POST(lastCommitLoaderURL);
35-
const data = await response.text();
36-
document.querySelector('table#repo-files-table').outerHTML = data;
37-
return;
38-
}
39-
40-
// For fewer entries, update individual rows
41-
const response = await POST(lastCommitLoaderURL, {data: {'f': entries}});
42-
const data = await response.text();
43-
const doc = parseDom(data, 'text/html');
44-
for (const row of doc.querySelectorAll('tr')) {
45-
if (row.className === 'commit-list') {
46-
document.querySelector('table#repo-files-table .commit-list')?.replaceWith(row);
47-
continue;
48-
}
49-
// there are other <tr> rows in response (eg: <tr class="has-parent">)
50-
// at the moment only the "data-entryname" rows should be processed
51-
const entryName = row.getAttribute('data-entryname');
52-
if (entryName) {
53-
entryMap[entryName]?.replaceWith(row);
54-
}
55-
}
56-
}
57-
5815
export function initCommitStatuses() {
5916
for (const element of document.querySelectorAll('[data-tippy="commit-statuses"]')) {
6017
const top = document.querySelector('.repository.file.list') || document.querySelector('.repository.diff');

web_src/js/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ import {
3333
initRepoPullRequestAllowMaintainerEdit,
3434
initRepoPullRequestReview, initRepoIssueSidebarList, initArchivedLabelHandler,
3535
} from './features/repo-issue.js';
36-
import {
37-
initRepoEllipsisButton,
38-
initRepoCommitLastCommitLoader,
39-
initCommitStatuses,
40-
} from './features/repo-commit.js';
36+
import {initRepoEllipsisButton, initCommitStatuses} from './features/repo-commit.js';
4137
import {
4238
initFootLanguageMenu,
4339
initGlobalButtonClickOnEnter,
@@ -148,7 +144,6 @@ onDomReady(() => {
148144
initRepoCommentForm();
149145
initRepoEllipsisButton();
150146
initRepoDiffCommitBranchesAndTags();
151-
initRepoCommitLastCommitLoader();
152147
initRepoEditor();
153148
initRepoGraphGit();
154149
initRepoIssueContentHistory();

0 commit comments

Comments
 (0)