Skip to content

Commit 75022f8

Browse files
Refactor branch/tag selector dropdown (first step) (#23394)
Follow: * #23345 The branch/tag selector dropdown mixes jQuery/Fomantic UI/Vue together, it's very diffcult to maintain and causes unfixable a11y problems. It also causes problems like #19851 #21314 #21952 This PR is the first step for the refactoring, move `data-` attributes to JS object and use Vue data as much as possible. The old selector `'.choose.reference .dropdown'` was also wrong, it hits `<div class="choose reference"><svg class="dropdown icon">` and would cause undefined behaviors. I have done some quick tests and it works. After this PR gets merged, I will move the code into a Vue SFC in next PR. ![image](https://user-images.githubusercontent.com/2114189/224099638-378a8a86-0865-47d1-bcba-f972506374c7.png) ![image](https://user-images.githubusercontent.com/2114189/224099690-70276cf5-b1e4-404a-b0c6-582448abf40e.png) --------- Co-authored-by: techknowlogick <[email protected]>
1 parent f20bf2f commit 75022f8

File tree

4 files changed

+101
-104
lines changed

4 files changed

+101
-104
lines changed

templates/repo/branch_dropdown.tmpl

+69-59
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,111 @@
22
{{$defaultBranch := $.root.BranchName}}{{if and .root.IsViewTag (not .noTag)}}{{$defaultBranch = .root.TagName}}{{end}}{{if eq $defaultBranch ""}}{{$defaultBranch = $.root.Repository.DefaultBranch}}{{end}}
33
{{$type := ""}}{{if and .root.IsViewTag (not .noTag)}}{{$type = "tag"}}{{else if .root.IsViewBranch}}{{$type = "branch"}}{{else}}{{$type = "tree"}}{{end}}
44
{{$showBranchesInDropdown := not .root.HideBranchesInDropdown}}
5+
6+
<script type="module">
7+
const data = {
8+
'textReleaseCompare': {{.root.locale.Tr "repo.release.compare"}},
9+
'textCreateTag': {{.root.locale.Tr "repo.tag.create_tag"}},
10+
'textCreateBranch': {{.root.locale.Tr "repo.branch.create_branch"}},
11+
'textCreateBranchFrom': {{.root.locale.Tr "repo.branch.create_from"}},
12+
'textBranches': {{.root.locale.Tr "repo.branches"}},
13+
'textTags': {{.root.locale.Tr "repo.tags"}},
14+
15+
'mode': '{{if or .root.IsViewTag .isTag}}tags{{else}}branches{{end}}',
16+
'showBranchesInDropdown': {{$showBranchesInDropdown}},
17+
'searchFieldPlaceholder': '{{if $.noTag}}{{.root.locale.Tr "repo.pulls.filter_branch"}}{{else if $showBranchesInDropdown}}{{.root.locale.Tr "repo.filter_branch_and_tag"}}{{else}}{{.root.locale.Tr "repo.find_tag"}}{{end}}...',
18+
'branchForm': {{$.branchForm}},
19+
'disableCreateBranch': {{if .disableCreateBranch}}{{.disableCreateBranch}}{{else}}{{not .root.CanCreateBranch}}{{end}},
20+
'setAction': {{.setAction}},
21+
'submitForm': {{.submitForm}},
22+
'viewType': {{$type}},
23+
'refName': {{if and .root.IsViewTag (not .noTag)}}{{.root.TagName}}{{else if .root.IsViewBranch}}{{.root.BranchName}}{{else}}{{ShortSha .root.CommitID}}{{end}},
24+
'commitIdShort': {{ShortSha .root.CommitID}},
25+
'tagName': {{.root.TagName}},
26+
'branchName': {{.root.BranchName}},
27+
'noTag': {{.noTag}},
28+
'branches': {{.root.Branches}},
29+
'tags': {{.root.Tags}},
30+
'defaultBranch': {{$defaultBranch}},
31+
'branchURLPrefix': '{{if .branchURLPrefix}}{{.branchURLPrefix}}{{else}}{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/branch/{{end}}',
32+
'branchURLSuffix': '{{if .branchURLSuffix}}{{.branchURLSuffix}}{{else}}{{if $.root.TreePath}}/{{PathEscapeSegments $.root.TreePath}}{{end}}{{end}}',
33+
'tagURLPrefix': '{{if .tagURLPrefix}}{{.tagURLPrefix}}{{else if $release}}{{$.root.RepoLink}}/compare/{{else}}{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/tag/{{end}}',
34+
'tagURLSuffix': '{{if .tagURLSuffix}}{{.tagURLSuffix}}{{else if $release}}...{{if $release.IsDraft}}{{PathEscapeSegments $release.Target}}{{else}}{{if $release.TagName}}{{PathEscapeSegments $release.TagName}}{{else}}{{PathEscapeSegments $release.Sha1}}{{end}}{{end}}{{else}}{{if $.root.TreePath}}/{{PathEscapeSegments $.root.TreePath}}{{end}}{{end}}',
35+
'repoLink': {{.root.RepoLink}},
36+
'treePath': {{.root.TreePath}},
37+
'branchNameSubURL': {{.root.BranchNameSubURL}},
38+
'noResults': {{.root.locale.Tr "repo.pulls.no_results"}},
39+
};
40+
{{if .release}}
41+
data.release = {
42+
'tagName': {{.release.TagName}},
43+
};
44+
{{end}}
45+
window.config.pageData.branchDropdownDataList = window.config.pageData.branchDropdownDataList || [];
46+
window.config.pageData.branchDropdownDataList.push(data);
47+
</script>
48+
549
<div class="fitted item choose reference">
6-
<div class="ui floating filter dropdown custom"
7-
data-branch-form="{{if $.branchForm}}{{$.branchForm}}{{end}}"
8-
data-can-create-branch="{{if .canCreateBranch}}{{.canCreateBranch}}{{else}}{{.root.CanCreateBranch}}{{end}}"
9-
data-no-results="{{.root.locale.Tr "repo.pulls.no_results"}}"
10-
data-set-action="{{.setAction}}" data-submit-form="{{.submitForm}}"
11-
data-view-type="{{$type}}"
12-
data-ref-name="{{if and .root.IsViewTag (not .noTag)}}{{.root.TagName}}{{else if .root.IsViewBranch}}{{.root.BranchName}}{{else}}{{ShortSha .root.CommitID}}{{end}}"
13-
data-branch-url-prefix="{{if .branchURLPrefix}}{{.branchURLPrefix}}{{else}}{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/branch/{{end}}"
14-
data-branch-url-suffix="{{if .branchURLSuffix}}{{.branchURLSuffix}}{{else}}{{if $.root.TreePath}}/{{PathEscapeSegments $.root.TreePath}}{{end}}{{end}}"
15-
data-tag-url-prefix="{{if .tagURLPrefix}}{{.tagURLPrefix}}{{else if $release}}{{$.root.RepoLink}}/compare/{{else}}{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/tag/{{end}}"
16-
data-tag-url-suffix="{{if .tagURLSuffix}}{{.tagURLSuffix}}{{else if $release}}...{{if $release.IsDraft}}{{PathEscapeSegments $release.Target}}{{else}}{{if $release.TagName}}{{PathEscapeSegments $release.TagName}}{{else}}{{PathEscapeSegments $release.Sha1}}{{end}}{{end}}{{else}}{{if $.root.TreePath}}/{{PathEscapeSegments $.root.TreePath}}{{end}}{{end}}">
50+
<div class="ui floating filter dropdown custom">
1751
<button class="branch-dropdown-button gt-ellipsis ui basic small compact button gt-df" @click="menuVisible = !menuVisible" @keyup.enter="menuVisible = !menuVisible">
1852
<span class="text gt-df gt-ac gt-mr-2">
19-
{{if $release}}
20-
{{.root.locale.Tr "repo.release.compare"}}
21-
{{else}}
22-
<span :class="{visible: isViewTag}" v-if="isViewTag" {{if not (eq $type "tag")}}v-cloak{{end}}>{{svg "octicon-tag"}}</span>
23-
<span :class="{visible: isViewBranch}" v-if="isViewBranch" {{if not (eq $type "branch")}}v-cloak{{end}}>{{svg "octicon-git-branch"}}</span>
24-
<span :class="{visible: isViewTree}" v-if="isViewTree" {{if not (eq $type "tree")}}v-cloak{{end}}>{{svg "octicon-git-branch"}}</span>
25-
<strong ref="dropdownRefName" class="gt-ml-3">{{if and .root.IsViewTag (not .noTag)}}{{.root.TagName}}{{else if .root.IsViewBranch}}{{.root.BranchName}}{{else}}{{ShortSha .root.CommitID}}{{end}}</strong>
26-
{{end}}
53+
{{/* v-cloak is used to hide unnecessary elements before Vue componment is mounted */}}
54+
<span v-cloak v-if="release">${ textReleaseCompare }</span>
55+
<span :class="{visible: isViewTag}" v-if="isViewTag" {{if not (eq $type "tag")}}v-cloak{{end}}>{{svg "octicon-tag"}}</span>
56+
<span :class="{visible: isViewBranch}" v-if="isViewBranch" {{if not (eq $type "branch")}}v-cloak{{end}}>{{svg "octicon-git-branch"}}</span>
57+
<span :class="{visible: isViewTree}" v-if="isViewTree" {{if not (eq $type "tree")}}v-cloak{{end}}>{{svg "octicon-git-branch"}}</span>
58+
<strong ref="dropdownRefName" class="gt-ml-3">{{if and .root.IsViewTag (not .noTag)}}{{.root.TagName}}{{else if .root.IsViewBranch}}{{.root.BranchName}}{{else}}{{ShortSha .root.CommitID}}{{end}}</strong>
2759
</span>
2860
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
2961
</button>
30-
<div class="data gt-hidden" data-mode="{{if or .root.IsViewTag .isTag}}tags{{else}}branches{{end}}">
31-
{{if $showBranchesInDropdown}}
32-
{{range .root.Branches}}
33-
<div class="item branch {{if eq $defaultBranch .}}selected{{end}}" data-url="{{PathEscapeSegments .}}">{{.}}</div>
34-
{{end}}
35-
{{end}}
36-
{{if (not .noTag)}}
37-
{{range .root.Tags}}
38-
{{if $release}}
39-
<div class="item tag {{if eq $release.TagName .}}selected{{end}}" data-url="{{PathEscapeSegments .}}">{{.}}</div>
40-
{{else}}
41-
<div class="item tag {{if eq $defaultBranch .}}selected{{end}}" data-url="{{PathEscapeSegments .}}">{{.}}</div>
42-
{{end}}
43-
{{end}}
44-
{{end}}
45-
</div>
4662
<div class="menu transition" :class="{visible: menuVisible}" v-if="menuVisible" v-cloak>
4763
<div class="ui icon search input">
4864
<i class="icon gt-df gt-ac gt-jc gt-m-0">{{svg "octicon-filter" 16}}</i>
49-
<input name="search" ref="searchField" autocomplete="off" v-model="searchTerm" @keydown="keydown($event)" placeholder="{{if $.noTag}}{{.root.locale.Tr "repo.pulls.filter_branch"}}{{else if $showBranchesInDropdown}}{{.root.locale.Tr "repo.filter_branch_and_tag"}}{{else}}{{.root.locale.Tr "repo.find_tag"}}{{end}}...">
65+
<input name="search" ref="searchField" autocomplete="off" v-model="searchTerm" @keydown="keydown($event)" :placeholder="searchFieldPlaceholder">
5066
</div>
51-
{{if $showBranchesInDropdown}}
67+
<template v-if="showBranchesInDropdown">
5268
<div class="header branch-tag-choice">
5369
<div class="ui grid">
5470
<div class="two column row">
5571
<a class="reference column" href="#" @click="createTag = false; mode = 'branches'; focusSearchField()">
56-
<span class="text" :class="{black: mode == 'branches'}">
57-
{{svg "octicon-git-branch" 16 "gt-mr-2"}}{{.root.locale.Tr "repo.branches"}}
72+
<span class="text" :class="{black: mode === 'branches'}">
73+
{{svg "octicon-git-branch" 16 "gt-mr-2"}}${ textBranches }
5874
</span>
5975
</a>
60-
{{if not .noTag}}
76+
<template v-if="!noTag">
6177
<a class="reference column" href="#" @click="createTag = true; mode = 'tags'; focusSearchField()">
62-
<span class="text" :class="{black: mode == 'tags'}">
63-
{{svg "octicon-tag" 16 "gt-mr-2"}}{{.root.locale.Tr "repo.tags"}}
78+
<span class="text" :class="{black: mode === 'tags'}">
79+
{{svg "octicon-tag" 16 "gt-mr-2"}}${ textTags }
6480
</span>
6581
</a>
66-
{{end}}
82+
</template>
6783
</div>
6884
</div>
6985
</div>
70-
{{end}}
86+
</template>
7187
<div class="scrolling menu" ref="scrollContainer">
72-
<div v-for="(item, index) in filteredItems" :key="item.name" class="item" :class="{selected: item.selected, active: active == index}" @click="selectItem(item)" :ref="'listItem' + index">${ item.name }</div>
73-
<div class="item" v-if="showCreateNewBranch" :class="{active: active == filteredItems.length}" :ref="'listItem' + filteredItems.length">
88+
<div v-for="(item, index) in filteredItems" :key="item.name" class="item" :class="{selected: item.selected, active: active === index}" @click="selectItem(item)" :ref="'listItem' + index">${ item.name }</div>
89+
<div class="item" v-if="showCreateNewBranch" :class="{active: active === filteredItems.length}" :ref="'listItem' + filteredItems.length">
7490
<a href="#" @click="createNewBranch()">
7591
<div v-show="createTag">
7692
<i class="reference tags icon"></i>
77-
{{.root.locale.Tr "repo.tag.create_tag" `${ searchTerm }` | Safe}}
93+
<span v-html="textCreateTag.replace('%s', searchTerm)"></span>
7894
</div>
7995
<div v-show="!createTag">
8096
{{svg "octicon-git-branch"}}
81-
{{.root.locale.Tr "repo.branch.create_branch" `${ searchTerm }` | Safe}}
97+
<span v-html="textCreateBranch.replace('%s', searchTerm)"></span>
8298
</div>
8399
<div class="text small">
84-
{{if or .root.IsViewBranch $release}}
85-
{{.root.locale.Tr "repo.branch.create_from" .root.BranchName}}
86-
{{else if .root.IsViewTag}}
87-
{{.root.locale.Tr "repo.branch.create_from" .root.TagName}}
88-
{{else}}
89-
{{.root.locale.Tr "repo.branch.create_from" (ShortSha .root.CommitID)}}
90-
{{end}}
100+
<span v-if="isViewBranch || release">${ textCreateBranchFrom.replace('%s', branchName) }</span>
101+
<span v-else-if="isViewTag">${ textCreateBranchFrom.replace('%s', tagName) }</span>
102+
<span v-else>${ textCreateBranchFrom.replace('%s', commitIdShort) }</span>
91103
</div>
92104
</a>
93105
<form ref="newBranchForm" action="{{.root.RepoLink}}/branches/_new/{{.root.BranchNameSubURL}}" method="post">
94-
{{.root.CsrfTokenHtml}}
106+
<input type="hidden" name="_csrf" :value="csrfToken">
95107
<input type="hidden" name="new_branch_name" v-model="searchTerm">
96108
<input type="hidden" name="create_tag" v-model="createTag">
97-
{{if $.root.TreePath}}
98-
<input type="hidden" name="current_path" value="{{.root.TreePath}}">
99-
{{end}}
109+
<input type="hidden" name="current_path" v-model="treePath" v-if="treePath">
100110
</form>
101111
</div>
102112
</div>

templates/repo/commit_page.tmpl

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@
5252
data-modal-cherry-pick-type="revert"
5353
data-modal-cherry-pick-header="{{$.locale.Tr "repo.commit.revert-header" (ShortSha .CommitID)}}"
5454
data-modal-cherry-pick-content="{{$.locale.Tr "repo.commit.revert-content"}}"
55-
data-modal-cherry-pick-submit="{{.locale.Tr "repo.commit.revert"}}">{{.locale.Tr "repo.commit.revert"}}</a></div>
55+
data-modal-cherry-pick-submit="{{.locale.Tr "repo.commit.revert"}}">{{.locale.Tr "repo.commit.revert"}}</div>
5656
<div class="item cherry-pick-button show-modal"
5757
data-modal="#cherry-pick-modal"
5858
data-modal-cherry-pick-type="cherry-pick"
5959
data-modal-cherry-pick-header="{{$.locale.Tr "repo.commit.cherry-pick-header" (ShortSha .CommitID)}}"
6060
data-modal-cherry-pick-content="{{$.locale.Tr "repo.commit.cherry-pick-content"}}"
61-
data-modal-cherry-pick-submit="{{.locale.Tr "repo.commit.cherry-pick"}}">{{.locale.Tr "repo.commit.cherry-pick"}}</a></div>
61+
data-modal-cherry-pick-submit="{{.locale.Tr "repo.commit.cherry-pick"}}">{{.locale.Tr "repo.commit.cherry-pick"}}</div>
6262
<div class="ui basic modal" id="cherry-pick-modal">
6363
<div class="ui icon header">
6464
<span id="cherry-pick-header"></span>
6565
</div>
6666
<div class="content center">
6767
<p id="cherry-pick-content" class="branch-dropdown"></p>
6868
{{template "repo/branch_dropdown" dict "root" .
69-
"noTag" "true" "canCreateBranch" "false"
69+
"noTag" true "disableCreateBranch" true
7070
"branchForm" "branch-dropdown-form"
7171
"branchURLPrefix" (printf "%s/_cherrypick/%s/" $.RepoLink .CommitID) "branchURLSuffix" ""
72-
"setAction" "true" "submitForm" "true"}}
72+
"setAction" true "submitForm" true}}
7373
<form method="GET" action="{{$.RepoLink}}/_cherrypick/{{.CommitID}}/{{if $.BranchName}}{{PathEscapeSegments $.BranchName}}{{else}}{{PathEscapeSegments $.Repository.DefaultBranch}}{{end}}" id="branch-dropdown-form">
7474
<input type="hidden" name="ref" value="{{if $.BranchName}}{{$.BranchName}}{{else}}{{$.Repository.DefaultBranch}}{{end}}">
7575
<input type="hidden" name="refType" value="branch">

web_src/js/components/RepoBranchTagDropdown.js

+27-40
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,41 @@ import $ from 'jquery';
33
import {vueDelimiters} from './VueComponentLoader.js';
44

55
export function initRepoBranchTagDropdown(selector) {
6-
$(selector).each(function () {
7-
const $dropdown = $(this);
8-
const $data = $dropdown.find('.data');
6+
$(selector).each(function (dropdownIndex, elRoot) {
97
const data = {
8+
csrfToken: window.config.csrfToken,
109
items: [],
11-
mode: $data.data('mode'),
1210
searchTerm: '',
13-
refName: '',
14-
noResults: '',
15-
canCreateBranch: false,
1611
menuVisible: false,
1712
createTag: false,
13+
release: null,
14+
1815
isViewTag: false,
1916
isViewBranch: false,
2017
isViewTree: false,
18+
2119
active: 0,
22-
branchForm: '',
23-
branchURLPrefix: '',
24-
branchURLSuffix: '',
25-
tagURLPrefix: '',
26-
tagURLSuffix: '',
27-
setAction: false,
28-
submitForm: false,
20+
21+
...window.config.pageData.branchDropdownDataList[dropdownIndex],
2922
};
30-
$data.find('.item').each(function () {
31-
data.items.push({
32-
name: $(this).text(),
33-
url: $(this).data('url'),
34-
branch: $(this).hasClass('branch'),
35-
tag: $(this).hasClass('tag'),
36-
selected: $(this).hasClass('selected')
37-
});
38-
});
39-
$data.remove();
4023

41-
// eslint-disable-next-line unicorn/no-this-assignment
42-
const elRoot = this;
24+
// the "data.defaultBranch" is ambiguous, it could be "branch name" or "tag name"
25+
26+
if (data.showBranchesInDropdown && data.branches) {
27+
for (const branch of data.branches) {
28+
data.items.push({name: branch, url: branch, branch: true, tag: false, selected: branch === data.defaultBranch});
29+
}
30+
}
31+
if (!data.noTag && data.tags) {
32+
for (const tag of data.tags) {
33+
if (data.release) {
34+
data.items.push({name: tag, url: tag, branch: false, tag: true, selected: tag === data.release.tagName});
35+
} else {
36+
data.items.push({name: tag, url: tag, branch: false, tag: true, selected: tag === data.defaultBranch});
37+
}
38+
}
39+
}
40+
4341
const view = createApp({
4442
delimiters: vueDelimiters,
4543
data() {
@@ -60,7 +58,7 @@ export function initRepoBranchTagDropdown(selector) {
6058
return this.filteredItems.length === 0 && !this.showCreateNewBranch;
6159
},
6260
showCreateNewBranch() {
63-
if (!this.canCreateBranch || !this.searchTerm) {
61+
if (this.disableCreateBranch || !this.searchTerm) {
6462
return false;
6563
}
6664

@@ -77,10 +75,7 @@ export function initRepoBranchTagDropdown(selector) {
7775
},
7876

7977
beforeMount() {
80-
this.noResults = elRoot.getAttribute('data-no-results');
81-
this.canCreateBranch = elRoot.getAttribute('data-can-create-branch') === 'true';
82-
this.branchForm = elRoot.getAttribute('data-branch-form');
83-
switch (elRoot.getAttribute('data-view-type')) {
78+
switch (data.viewType) {
8479
case 'tree':
8580
this.isViewTree = true;
8681
break;
@@ -91,14 +86,6 @@ export function initRepoBranchTagDropdown(selector) {
9186
this.isViewBranch = true;
9287
break;
9388
}
94-
this.refName = elRoot.getAttribute('data-ref-name');
95-
this.branchURLPrefix = elRoot.getAttribute('data-branch-url-prefix');
96-
this.branchURLSuffix = elRoot.getAttribute('data-branch-url-suffix');
97-
this.tagURLPrefix = elRoot.getAttribute('data-tag-url-prefix');
98-
this.tagURLSuffix = elRoot.getAttribute('data-tag-url-suffix');
99-
this.setAction = elRoot.getAttribute('data-set-action') === 'true';
100-
this.submitForm = elRoot.getAttribute('data-submit-form') === 'true';
101-
10289

10390
document.body.addEventListener('click', (event) => {
10491
if (elRoot.contains(event.target)) return;
@@ -116,7 +103,7 @@ export function initRepoBranchTagDropdown(selector) {
116103
}
117104
item.selected = true;
118105
const url = (item.tag) ? this.tagURLPrefix + item.url + this.tagURLSuffix : this.branchURLPrefix + item.url + this.branchURLSuffix;
119-
if (this.branchForm === '') {
106+
if (!this.branchForm) {
120107
window.location.href = url;
121108
} else {
122109
this.isViewTree = false;

web_src/js/features/repo-legacy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ export function initRepository() {
485485
// File list and commits
486486
if ($('.repository.file.list').length > 0 || $('.branch-dropdown').length > 0 ||
487487
$('.repository.commits').length > 0 || $('.repository.release').length > 0) {
488-
initRepoBranchTagDropdown('.choose.reference .dropdown');
488+
initRepoBranchTagDropdown('.choose.reference .ui.dropdown');
489489
}
490490

491491
// Wiki

0 commit comments

Comments
 (0)