Refactor from Vue2 to Vue3 (#20044)

Close #19902
This commit is contained in:
André Jaenisch 2022-10-01 16:26:38 +02:00 committed by GitHub
parent 726afe8a9e
commit 04e97b8311
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 384 additions and 606 deletions

View file

@ -15,7 +15,7 @@
</div>
</template>
<script>
import {CalendarHeatmap} from 'vue-calendar-heatmap';
import {CalendarHeatmap} from 'vue3-calendar-heatmap';
export default {
name: 'ActivityHeatmap',

View file

@ -1,5 +1,5 @@
<template>
<div>
<div ref="root">
<div v-if="loading" class="ui active centered inline loader"/>
<div v-if="!loading && issue !== null">
<p><small>{{ issue.repository.full_name }} on {{ createdAt }}</small></p>
@ -109,15 +109,16 @@ export default {
},
mounted() {
this.$root.$on('load-context-popup', (data, callback) => {
this.$refs.root.addEventListener('us-load-context-popup', (e) => {
const data = e.detail;
if (!this.loading && this.issue === null) {
this.load(data, callback);
this.load(data);
}
});
},
methods: {
load(data, callback) {
load(data) {
this.loading = true;
this.i18nErrorMessage = null;
$.get(`${appSubUrl}/${data.owner}/${data.repo}/issues/${data.index}/info`).done((issue) => {
@ -130,9 +131,6 @@ export default {
}
}).always(() => {
this.loading = false;
if (callback) {
this.$nextTick(callback);
}
});
}
}

View file

@ -1,12 +1,12 @@
import Vue from 'vue';
import {createApp, nextTick} from 'vue';
import $ from 'jquery';
import {initVueSvg, vueDelimiters} from './VueComponentLoader.js';
import {initTooltip} from '../modules/tippy.js';
const {appSubUrl, assetUrlPrefix, pageData} = window.config;
function initVueComponents() {
Vue.component('repo-search', {
function initVueComponents(app) {
app.component('repo-search', {
delimiters: vueDelimiters,
props: {
searchLimit: {
@ -138,13 +138,14 @@ function initVueComponents() {
},
mounted() {
const el = document.getElementById('dashboard-repo-list');
this.changeReposFilter(this.reposFilter);
for (const el of this.$el.querySelectorAll('.tooltip')) {
initTooltip(el);
for (const elTooltip of el.querySelectorAll('.tooltip')) {
initTooltip(elTooltip);
}
$(this.$el).find('.dropdown').dropdown();
$(el).find('.dropdown').dropdown();
this.setCheckboxes();
Vue.nextTick(() => {
nextTick(() => {
this.$refs.search.focus();
});
},
@ -192,7 +193,7 @@ function initVueComponents() {
this.reposFilter = filter;
this.repos = [];
this.page = 1;
Vue.set(this.counts, `${filter}:${this.archivedFilter}:${this.privateFilter}`, 0);
this.counts[`${filter}:${this.archivedFilter}:${this.privateFilter}`] = 0;
this.searchRepos();
},
@ -261,7 +262,7 @@ function initVueComponents() {
this.page = 1;
this.repos = [];
this.setCheckboxes();
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, 0);
this.counts[`${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`] = 0;
this.searchRepos();
},
@ -283,7 +284,7 @@ function initVueComponents() {
this.page = 1;
this.repos = [];
this.setCheckboxes();
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, 0);
this.counts[`${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`] = 0;
this.searchRepos();
},
@ -297,7 +298,7 @@ function initVueComponents() {
this.page = 1;
}
this.repos = [];
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, 0);
this.counts[`${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`] = 0;
this.searchRepos();
},
@ -331,7 +332,7 @@ function initVueComponents() {
if (searchedQuery === '' && searchedMode === '' && this.archivedFilter === 'both') {
this.reposTotalCount = count;
}
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, count);
this.counts[`${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`] = count;
this.finalPage = Math.ceil(count / this.searchLimit);
this.updateHistory();
this.isLoading = false;
@ -352,22 +353,20 @@ function initVueComponents() {
}
return 'octicon-repo';
}
}
},
template: document.getElementById('dashboard-repo-list-template'),
});
}
export function initDashboardRepoList() {
const el = document.getElementById('dashboard-repo-list');
const dashboardRepoListData = pageData.dashboardRepoList || null;
if (!el || !dashboardRepoListData) return;
initVueSvg();
initVueComponents();
new Vue({
el,
const app = createApp({
delimiters: vueDelimiters,
data: () => {
data() {
return {
searchLimit: dashboardRepoListData.searchLimit || 0,
subUrl: appSubUrl,
@ -375,4 +374,7 @@ export function initDashboardRepoList() {
};
},
});
initVueSvg(app);
initVueComponents(app);
app.mount(el);
}

View file

@ -1,5 +1,5 @@
<template>
<ol class="diff-detail-box diff-stats m-0" id="diff-files" v-if="fileListIsVisible">
<ol class="diff-detail-box diff-stats m-0" ref="root" v-if="fileListIsVisible">
<li v-for="file in files" :key="file.NameHash">
<div class="bold df ac pull-right">
<span v-if="file.IsBin" class="ml-1 mr-3">{{ binaryFileMessage }}</span>
@ -37,7 +37,7 @@ export default {
fileListIsVisible(newValue) {
if (newValue === true) {
this.$nextTick(() => {
for (const el of this.$el.querySelectorAll('.tooltip')) {
for (const el of this.$refs.root.querySelectorAll('.tooltip')) {
initTooltip(el);
}
});

View file

@ -1,11 +1,10 @@
<template>
<div
v-show="fileTreeIsVisible"
id="diff-file-tree"
v-if="fileTreeIsVisible"
class="mr-3 mt-3 diff-detail-box"
>
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
<div class="ui list" v-if="fileTreeIsVisible">
<div class="ui list">
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item" />
</div>
<div v-if="isIncomplete" id="diff-too-many-files-stats" class="pt-2">
@ -117,6 +116,9 @@ export default {
const [toShow, toHide] = document.querySelectorAll('.diff-toggle-file-tree-button .icon');
toShow.classList.toggle('hide', visible); // hide the toShow icon if the tree is visible
toHide.classList.toggle('hide', !visible); // similarly
const diffTree = document.getElementById('diff-file-tree');
diffTree.classList.toggle('hide', !visible);
},
loadMoreData() {
this.isLoadingNewData = true;

View file

@ -1,4 +1,4 @@
import Vue from 'vue';
import {createApp, nextTick} from 'vue';
import $ from 'jquery';
import {vueDelimiters} from './VueComponentLoader.js';
@ -37,10 +37,14 @@ export function initRepoBranchTagDropdown(selector) {
});
});
$data.remove();
new Vue({
el: this,
// eslint-disable-next-line unicorn/no-this-assignment
const elRoot = this;
const view = createApp({
delimiters: vueDelimiters,
data,
data() {
return data;
},
computed: {
filteredItems() {
const items = this.items.filter((item) => {
@ -73,10 +77,10 @@ export function initRepoBranchTagDropdown(selector) {
},
beforeMount() {
this.noResults = this.$el.getAttribute('data-no-results');
this.canCreateBranch = this.$el.getAttribute('data-can-create-branch') === 'true';
this.branchForm = this.$el.getAttribute('data-branch-form');
switch (this.$el.getAttribute('data-view-type')) {
this.noResults = elRoot.getAttribute('data-no-results');
this.canCreateBranch = elRoot.getAttribute('data-can-create-branch') === 'true';
this.branchForm = elRoot.getAttribute('data-branch-form');
switch (elRoot.getAttribute('data-view-type')) {
case 'tree':
this.isViewTree = true;
break;
@ -87,19 +91,19 @@ export function initRepoBranchTagDropdown(selector) {
this.isViewBranch = true;
break;
}
this.refName = this.$el.getAttribute('data-ref-name');
this.branchURLPrefix = this.$el.getAttribute('data-branch-url-prefix');
this.branchURLSuffix = this.$el.getAttribute('data-branch-url-suffix');
this.tagURLPrefix = this.$el.getAttribute('data-tag-url-prefix');
this.tagURLSuffix = this.$el.getAttribute('data-tag-url-suffix');
this.setAction = this.$el.getAttribute('data-set-action') === 'true';
this.submitForm = this.$el.getAttribute('data-submit-form') === 'true';
this.refName = elRoot.getAttribute('data-ref-name');
this.branchURLPrefix = elRoot.getAttribute('data-branch-url-prefix');
this.branchURLSuffix = elRoot.getAttribute('data-branch-url-suffix');
this.tagURLPrefix = elRoot.getAttribute('data-tag-url-prefix');
this.tagURLSuffix = elRoot.getAttribute('data-tag-url-suffix');
this.setAction = elRoot.getAttribute('data-set-action') === 'true';
this.submitForm = elRoot.getAttribute('data-submit-form') === 'true';
document.body.addEventListener('click', (event) => {
if (this.$el.contains(event.target)) return;
if (elRoot.contains(event.target)) return;
if (this.menuVisible) {
Vue.set(this, 'menuVisible', false);
this.menuVisible = false;
}
});
},
@ -135,7 +139,7 @@ export function initRepoBranchTagDropdown(selector) {
if (this.submitForm) {
$(`#${this.branchForm}`).trigger('submit');
}
Vue.set(this, 'menuVisible', false);
this.menuVisible = false;
}
},
createNewBranch() {
@ -143,7 +147,7 @@ export function initRepoBranchTagDropdown(selector) {
$(this.$refs.newBranchForm).trigger('submit');
},
focusSearchField() {
Vue.nextTick(() => {
nextTick(() => {
this.$refs.searchField.focus();
});
},
@ -213,5 +217,6 @@ export function initRepoBranchTagDropdown(selector) {
}
}
});
view.mount(this);
});
}

View file

@ -1,4 +1,4 @@
import Vue from 'vue';
import {createApp} from 'vue';
import {svgs} from '../svg.js';
export const vueDelimiters = ['${', '}'];
@ -8,13 +8,14 @@ export function initVueEnv() {
if (vueEnvInited) return;
vueEnvInited = true;
const isProd = window.config.runModeIsProd;
Vue.config.productionTip = false;
Vue.config.devtools = !isProd;
// As far as I could tell, this is no longer possible.
// But there seem not to be a guide what to do instead.
// const isProd = window.config.runModeIsProd;
// Vue.config.devtools = !isProd;
}
let vueSvgInited = false;
export function initVueSvg() {
export function initVueSvg(app) {
if (vueSvgInited) return;
vueSvgInited = true;
@ -24,7 +25,7 @@ export function initVueSvg() {
.replace(/height="[0-9]+"/, 'v-bind:height="size"')
.replace(/width="[0-9]+"/, 'v-bind:width="size"');
Vue.component(name, {
app.component(name, {
props: {
size: {
type: String,
@ -42,8 +43,7 @@ export function initVueApp(el, opts = {}) {
}
if (!el) return null;
return new Vue(Object.assign({
el,
delimiters: vueDelimiters,
}, opts));
return createApp(
Object.assign({delimiters: vueDelimiters}, opts)
).mount(el);
}