Skip to content

Commit 857abed

Browse files
authored
Move RepoBranchTagSelector init outside the SFC (#32890)
SFCs shouldn't export anything besides their component, and this eliminates one issue with tsc, while apparently also solving a hack. It seems to work as before, also when multiples are on the same page.
1 parent 2beaedc commit 857abed

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

web_src/js/components/RepoBranchTagSelector.vue

+30-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import {createApp, nextTick} from 'vue';
2+
import {nextTick} from 'vue';
33
import {SvgIcon} from '../svg.ts';
44
import {showErrorToast} from '../modules/toast.ts';
55
import {GET} from '../modules/fetch.ts';
@@ -17,11 +17,11 @@ type SelectedTab = 'branches' | 'tags';
1717
1818
type TabLoadingStates = Record<SelectedTab, '' | 'loading' | 'done'>
1919
20-
let currentElRoot: HTMLElement;
21-
2220
const sfc = {
2321
components: {SvgIcon},
24-
22+
props: {
23+
elRoot: HTMLElement,
24+
},
2525
computed: {
2626
searchFieldPlaceholder() {
2727
return this.selectedTab === 'branches' ? this.textFilterBranch : this.textFilterTag;
@@ -55,18 +55,15 @@ const sfc = {
5555
return `${this.currentRepoLink}/branches/_new/${this.currentRefType}/${pathEscapeSegments(this.currentRefShortName)}`;
5656
},
5757
},
58-
5958
watch: {
60-
menuVisible(visible) {
59+
menuVisible(visible: boolean) {
6160
if (!visible) return;
6261
this.focusSearchField();
6362
this.loadTabItems();
6463
},
6564
},
66-
6765
data() {
68-
const elRoot = currentElRoot;
69-
const shouldShowTabBranches = elRoot.getAttribute('data-show-tab-branches') === 'true';
66+
const shouldShowTabBranches = this.elRoot.getAttribute('data-show-tab-branches') === 'true';
7067
return {
7168
csrfToken: window.config.csrfToken,
7269
allItems: [] as ListItem[],
@@ -76,37 +73,35 @@ const sfc = {
7673
activeItemIndex: 0,
7774
tabLoadingStates: {} as TabLoadingStates,
7875
79-
textReleaseCompare: elRoot.getAttribute('data-text-release-compare'),
80-
textBranches: elRoot.getAttribute('data-text-branches'),
81-
textTags: elRoot.getAttribute('data-text-tags'),
82-
textFilterBranch: elRoot.getAttribute('data-text-filter-branch'),
83-
textFilterTag: elRoot.getAttribute('data-text-filter-tag'),
84-
textDefaultBranchLabel: elRoot.getAttribute('data-text-default-branch-label'),
85-
textCreateTag: elRoot.getAttribute('data-text-create-tag'),
86-
textCreateBranch: elRoot.getAttribute('data-text-create-branch'),
87-
textCreateRefFrom: elRoot.getAttribute('data-text-create-ref-from'),
88-
textNoResults: elRoot.getAttribute('data-text-no-results'),
89-
textViewAllBranches: elRoot.getAttribute('data-text-view-all-branches'),
90-
textViewAllTags: elRoot.getAttribute('data-text-view-all-tags'),
76+
textReleaseCompare: this.elRoot.getAttribute('data-text-release-compare'),
77+
textBranches: this.elRoot.getAttribute('data-text-branches'),
78+
textTags: this.elRoot.getAttribute('data-text-tags'),
79+
textFilterBranch: this.elRoot.getAttribute('data-text-filter-branch'),
80+
textFilterTag: this.elRoot.getAttribute('data-text-filter-tag'),
81+
textDefaultBranchLabel: this.elRoot.getAttribute('data-text-default-branch-label'),
82+
textCreateTag: this.elRoot.getAttribute('data-text-create-tag'),
83+
textCreateBranch: this.elRoot.getAttribute('data-text-create-branch'),
84+
textCreateRefFrom: this.elRoot.getAttribute('data-text-create-ref-from'),
85+
textNoResults: this.elRoot.getAttribute('data-text-no-results'),
86+
textViewAllBranches: this.elRoot.getAttribute('data-text-view-all-branches'),
87+
textViewAllTags: this.elRoot.getAttribute('data-text-view-all-tags'),
9188
92-
currentRepoDefaultBranch: elRoot.getAttribute('data-current-repo-default-branch'),
93-
currentRepoLink: elRoot.getAttribute('data-current-repo-link'),
94-
currentTreePath: elRoot.getAttribute('data-current-tree-path'),
95-
currentRefType: elRoot.getAttribute('data-current-ref-type'),
96-
currentRefShortName: elRoot.getAttribute('data-current-ref-short-name'),
89+
currentRepoDefaultBranch: this.elRoot.getAttribute('data-current-repo-default-branch'),
90+
currentRepoLink: this.elRoot.getAttribute('data-current-repo-link'),
91+
currentTreePath: this.elRoot.getAttribute('data-current-tree-path'),
92+
currentRefType: this.elRoot.getAttribute('data-current-ref-type'),
93+
currentRefShortName: this.elRoot.getAttribute('data-current-ref-short-name'),
9794
98-
refLinkTemplate: elRoot.getAttribute('data-ref-link-template'),
99-
refFormActionTemplate: elRoot.getAttribute('data-ref-form-action-template'),
100-
dropdownFixedText: elRoot.getAttribute('data-dropdown-fixed-text'),
95+
refLinkTemplate: this.elRoot.getAttribute('data-ref-link-template'),
96+
refFormActionTemplate: this.elRoot.getAttribute('data-ref-form-action-template'),
97+
dropdownFixedText: this.elRoot.getAttribute('data-dropdown-fixed-text'),
10198
showTabBranches: shouldShowTabBranches,
102-
showTabTags: elRoot.getAttribute('data-show-tab-tags') === 'true',
103-
allowCreateNewRef: elRoot.getAttribute('data-allow-create-new-ref') === 'true',
104-
showViewAllRefsEntry: elRoot.getAttribute('data-show-view-all-refs-entry') === 'true',
105-
106-
enableFeed: elRoot.getAttribute('data-enable-feed') === 'true',
99+
showTabTags: this.elRoot.getAttribute('data-show-tab-tags') === 'true',
100+
allowCreateNewRef: this.elRoot.getAttribute('data-allow-create-new-ref') === 'true',
101+
showViewAllRefsEntry: this.elRoot.getAttribute('data-show-view-all-refs-entry') === 'true',
102+
enableFeed: this.elRoot.getAttribute('data-enable-feed') === 'true',
107103
};
108104
},
109-
110105
beforeMount() {
111106
document.body.addEventListener('click', (e) => {
112107
if (this.$el.contains(e.target)) return;
@@ -219,16 +214,6 @@ const sfc = {
219214
},
220215
};
221216
222-
export function initRepoBranchTagSelector(selector) {
223-
for (const elRoot of document.querySelectorAll(selector)) {
224-
// it is very hacky, but it is the only way to pass the elRoot to the "data()" function
225-
// it could be improved in the future to do more rewriting.
226-
currentElRoot = elRoot;
227-
const comp = {...sfc};
228-
createApp(comp).mount(elRoot);
229-
}
230-
}
231-
232217
export default sfc; // activate IDE's Vue plugin
233218
</script>
234219
<template>

web_src/js/features/repo-legacy.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
initRepoPullRequestUpdate,
88
} from './repo-issue.ts';
99
import {initUnicodeEscapeButton} from './repo-unicode-escape.ts';
10-
import {initRepoBranchTagSelector} from '../components/RepoBranchTagSelector.vue';
1110
import {initRepoCloneButtons} from './repo-common.ts';
1211
import {initCitationFileCopyContent} from './citation.ts';
1312
import {initCompLabelEdit} from './comp/LabelEdit.ts';
@@ -20,6 +19,14 @@ import {hideElem, queryElemChildren, showElem} from '../utils/dom.ts';
2019
import {initRepoIssueCommentEdit} from './repo-issue-edit.ts';
2120
import {initRepoMilestone} from './repo-milestone.ts';
2221
import {initRepoNew} from './repo-new.ts';
22+
import {createApp} from 'vue';
23+
import RepoBranchTagSelector from '../components/RepoBranchTagSelector.vue';
24+
25+
function initRepoBranchTagSelector(selector: string) {
26+
for (const elRoot of document.querySelectorAll(selector)) {
27+
createApp(RepoBranchTagSelector, {elRoot}).mount(elRoot);
28+
}
29+
}
2330

2431
export function initBranchSelectorTabs() {
2532
const elSelectBranch = document.querySelector('.ui.dropdown.select-branch');

web_src/js/globals.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ declare module '*.vue' {
1414
export default component;
1515
// List of named exports from vue components, used to make `tsc` output clean.
1616
// To actually lint .vue files, `vue-tsc` is used because `tsc` can not parse them.
17-
export function initRepoBranchTagSelector(selector: string): void;
1817
export function initDashboardRepoList(): void;
1918
export function initRepositoryActionView(): void;
2019
}

0 commit comments

Comments
 (0)