debian-mirror-gitlab/app/assets/javascripts/diffs/components/diff_file.vue

354 lines
9.4 KiB
Vue
Raw Normal View History

2018-11-08 19:23:39 +05:30
<script>
2018-12-05 23:21:45 +05:30
import { mapActions, mapGetters, mapState } from 'vuex';
2020-05-24 23:13:21 +05:30
import { escape } from 'lodash';
2021-01-29 00:20:46 +05:30
import { GlButton, GlLoadingIcon, GlSafeHtmlDirective as SafeHtml } from '@gitlab/ui';
2020-10-24 23:57:45 +05:30
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
2021-01-29 00:20:46 +05:30
import { sprintf } from '~/locale';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as createFlash } from '~/flash';
2020-03-13 15:44:24 +05:30
import { hasDiff } from '~/helpers/diffs_helper';
2021-01-29 00:20:46 +05:30
import notesEventHub from '../../notes/event_hub';
2018-11-08 19:23:39 +05:30
import DiffFileHeader from './diff_file_header.vue';
import DiffContent from './diff_content.vue';
2019-07-07 11:18:12 +05:30
import { diffViewerErrors } from '~/ide/constants';
2021-02-22 17:27:13 +05:30
import { collapsedType, isCollapsed } from '../utils/diff_file';
2021-01-29 00:20:46 +05:30
import {
DIFF_FILE_AUTOMATIC_COLLAPSE,
DIFF_FILE_MANUAL_COLLAPSE,
EVT_EXPAND_ALL_FILES,
EVT_PERF_MARK_DIFF_FILES_END,
EVT_PERF_MARK_FIRST_DIFF_FILE_SHOWN,
} from '../constants';
import { DIFF_FILE, GENERIC_ERROR } from '../i18n';
import eventHub from '../event_hub';
2018-11-08 19:23:39 +05:30
export default {
components: {
DiffFileHeader,
DiffContent,
2021-01-29 00:20:46 +05:30
GlButton,
2018-12-13 13:39:08 +05:30
GlLoadingIcon,
2018-11-08 19:23:39 +05:30
},
2020-11-24 15:15:51 +05:30
directives: {
SafeHtml,
},
2020-10-24 23:57:45 +05:30
mixins: [glFeatureFlagsMixin()],
2018-11-08 19:23:39 +05:30
props: {
file: {
type: Object,
required: true,
},
2021-01-29 00:20:46 +05:30
isFirstFile: {
type: Boolean,
required: false,
default: false,
},
isLastFile: {
type: Boolean,
required: false,
default: false,
},
2018-11-08 19:23:39 +05:30
canCurrentUserFork: {
type: Boolean,
required: true,
},
2019-02-15 15:39:39 +05:30
helpPagePath: {
type: String,
required: false,
default: '',
},
2020-07-28 23:09:34 +05:30
viewDiffsFileByFile: {
type: Boolean,
required: true,
},
2018-11-08 19:23:39 +05:30
},
data() {
return {
isLoadingCollapsedDiff: false,
forkMessageVisible: false,
2021-01-29 00:20:46 +05:30
isCollapsed: isCollapsed(this.file),
2018-11-08 19:23:39 +05:30
};
},
2021-01-29 00:20:46 +05:30
i18n: {
...DIFF_FILE,
genericError: GENERIC_ERROR,
},
2018-11-08 19:23:39 +05:30
computed: {
2018-12-05 23:21:45 +05:30
...mapState('diffs', ['currentDiffFileId']),
2018-12-13 13:39:08 +05:30
...mapGetters(['isNotesFetched']),
...mapGetters('diffs', ['getDiffFileDiscussions']),
2018-11-08 19:23:39 +05:30
viewBlobLink() {
return sprintf(
2021-01-29 00:20:46 +05:30
this.$options.i18n.blobView,
2018-11-08 19:23:39 +05:30
{
2020-05-24 23:13:21 +05:30
linkStart: `<a href="${escape(this.file.view_path)}">`,
2018-11-08 19:23:39 +05:30
linkEnd: '</a>',
},
false,
);
},
2018-11-20 20:47:30 +05:30
showLoadingIcon() {
return this.isLoadingCollapsedDiff || (!this.file.renderIt && !this.isCollapsed);
2018-11-08 19:23:39 +05:30
},
2019-12-26 22:10:19 +05:30
hasDiff() {
2020-03-13 15:44:24 +05:30
return hasDiff(this.file);
2018-12-13 13:39:08 +05:30
},
2019-07-07 11:18:12 +05:30
isFileTooLarge() {
return this.file.viewer.error === diffViewerErrors.too_large;
},
errorMessage() {
2021-01-29 00:20:46 +05:30
return !this.manuallyCollapsed ? this.file.viewer.error_message : '';
2019-07-07 11:18:12 +05:30
},
2019-09-30 21:07:59 +05:30
forkMessage() {
return sprintf(
2021-01-29 00:20:46 +05:30
this.$options.i18n.editInFork,
2019-09-30 21:07:59 +05:30
{
tag_start: '<span class="js-file-fork-suggestion-section-action">',
tag_end: '</span>',
},
false,
);
},
2021-01-29 00:20:46 +05:30
hasBodyClasses() {
const domParts = {
header: 'gl-rounded-base!',
contentByHash: '',
content: '',
};
if (this.showBody) {
domParts.header = 'gl-rounded-bottom-left-none gl-rounded-bottom-right-none';
domParts.contentByHash =
'gl-rounded-none gl-rounded-bottom-left-base gl-rounded-bottom-right-base gl-border-1 gl-border-t-0! gl-border-solid gl-border-gray-100';
domParts.content = 'gl-rounded-bottom-left-base gl-rounded-bottom-right-base';
2018-12-13 13:39:08 +05:30
}
2019-07-07 11:18:12 +05:30
2021-01-29 00:20:46 +05:30
return domParts;
2019-07-07 11:18:12 +05:30
},
2021-01-29 00:20:46 +05:30
automaticallyCollapsed() {
return collapsedType(this.file) === DIFF_FILE_AUTOMATIC_COLLAPSE;
},
manuallyCollapsed() {
return collapsedType(this.file) === DIFF_FILE_MANUAL_COLLAPSE;
},
showBody() {
return !this.isCollapsed || this.automaticallyCollapsed;
},
showWarning() {
return this.isCollapsed && (this.automaticallyCollapsed && !this.viewDiffsFileByFile);
},
showContent() {
return !this.isCollapsed && !this.isFileTooLarge;
},
},
watch: {
2020-10-24 23:57:45 +05:30
'file.file_hash': {
2021-01-29 00:20:46 +05:30
handler: function hashChangeWatch(newHash, oldHash) {
this.isCollapsed = isCollapsed(this.file);
if (newHash && oldHash && !this.hasDiff) {
this.requestDiff();
2020-10-24 23:57:45 +05:30
}
},
immediate: true,
},
2021-01-29 00:20:46 +05:30
'file.viewer.automaticallyCollapsed': {
handler: function autoChangeWatch(automaticValue) {
if (collapsedType(this.file) !== DIFF_FILE_MANUAL_COLLAPSE) {
this.isCollapsed = this.viewDiffsFileByFile ? false : automaticValue;
}
},
immediate: true,
},
'file.viewer.manuallyCollapsed': {
handler: function manualChangeWatch(manualValue) {
if (manualValue !== null) {
this.isCollapsed = manualValue;
}
},
immediate: true,
2018-12-13 13:39:08 +05:30
},
2018-11-08 19:23:39 +05:30
},
2019-02-15 15:39:39 +05:30
created() {
2021-01-29 00:20:46 +05:30
notesEventHub.$on(`loadCollapsedDiff/${this.file.file_hash}`, this.requestDiff);
eventHub.$on(EVT_EXPAND_ALL_FILES, this.expandAllListener);
},
mounted() {
if (this.hasDiff) {
this.postRender();
}
},
beforeDestroy() {
eventHub.$off(EVT_EXPAND_ALL_FILES, this.expandAllListener);
2019-02-15 15:39:39 +05:30
},
2018-11-08 19:23:39 +05:30
methods: {
2019-07-07 11:18:12 +05:30
...mapActions('diffs', [
'loadCollapsedDiff',
'assignDiscussionsToDiff',
'setRenderIt',
2021-01-29 00:20:46 +05:30
'setFileCollapsedByUser',
2019-07-07 11:18:12 +05:30
]),
2021-01-29 00:20:46 +05:30
expandAllListener() {
if (this.isCollapsed) {
this.handleToggle();
}
},
async postRender() {
const eventsForThisFile = [];
if (this.isFirstFile) {
eventsForThisFile.push(EVT_PERF_MARK_FIRST_DIFF_FILE_SHOWN);
}
if (this.isLastFile) {
eventsForThisFile.push(EVT_PERF_MARK_DIFF_FILES_END);
}
await this.$nextTick();
eventsForThisFile.forEach(event => {
eventHub.$emit(event);
});
},
2018-11-08 19:23:39 +05:30
handleToggle() {
2021-01-29 00:20:46 +05:30
const currentCollapsedFlag = this.isCollapsed;
this.setFileCollapsedByUser({
filePath: this.file.file_path,
collapsed: !currentCollapsedFlag,
});
if (!this.hasDiff && currentCollapsedFlag) {
this.requestDiff();
2018-11-08 19:23:39 +05:30
}
},
2021-01-29 00:20:46 +05:30
requestDiff() {
2018-11-08 19:23:39 +05:30
this.isLoadingCollapsedDiff = true;
this.loadCollapsedDiff(this.file)
.then(() => {
this.isLoadingCollapsedDiff = false;
2019-07-07 11:18:12 +05:30
this.setRenderIt(this.file);
2018-11-20 20:47:30 +05:30
})
.then(() => {
requestIdleCallback(
() => {
2021-01-29 00:20:46 +05:30
this.postRender();
2018-12-13 13:39:08 +05:30
this.assignDiscussionsToDiff(this.getDiffFileDiscussions(this.file));
2018-11-20 20:47:30 +05:30
},
{ timeout: 1000 },
);
2018-11-08 19:23:39 +05:30
})
.catch(() => {
this.isLoadingCollapsedDiff = false;
2021-01-29 00:20:46 +05:30
createFlash(this.$options.i18n.genericError);
2018-11-08 19:23:39 +05:30
});
},
showForkMessage() {
this.forkMessageVisible = true;
},
hideForkMessage() {
this.forkMessageVisible = false;
},
},
};
</script>
<template>
<div
2019-02-15 15:39:39 +05:30
:id="file.file_hash"
2018-12-05 23:21:45 +05:30
:class="{
2019-02-15 15:39:39 +05:30
'is-active': currentDiffFileId === file.file_hash,
2020-10-24 23:57:45 +05:30
'comments-disabled': Boolean(file.brokenSymlink),
2021-01-29 00:20:46 +05:30
'has-body': showBody,
2018-12-05 23:21:45 +05:30
}"
2020-04-22 19:07:51 +05:30
:data-path="file.new_path"
2021-01-29 00:20:46 +05:30
class="diff-file file-holder gl-border-none"
2018-11-08 19:23:39 +05:30
>
<diff-file-header
:can-current-user-fork="canCurrentUserFork"
:diff-file="file"
:collapsible="true"
:expanded="!isCollapsed"
:add-merge-request-buttons="true"
2020-07-28 23:09:34 +05:30
:view-diffs-file-by-file="viewDiffsFileByFile"
2021-01-29 00:20:46 +05:30
class="js-file-title file-title gl-border-1 gl-border-solid gl-border-gray-100"
:class="hasBodyClasses.header"
2018-11-08 19:23:39 +05:30
@toggleFile="handleToggle"
@showForkMessage="showForkMessage"
/>
2019-02-15 15:39:39 +05:30
<div v-if="forkMessageVisible" class="js-file-fork-suggestion-section file-fork-suggestion">
2020-11-24 15:15:51 +05:30
<span v-safe-html="forkMessage" class="file-fork-suggestion-note"></span>
2018-11-08 19:23:39 +05:30
<a
2019-02-15 15:39:39 +05:30
:href="file.fork_path"
2018-11-08 19:23:39 +05:30
class="js-fork-suggestion-button btn btn-grouped btn-inverted btn-success"
2021-01-29 00:20:46 +05:30
>{{ $options.i18n.fork }}</a
2018-11-08 19:23:39 +05:30
>
<button
class="js-cancel-fork-suggestion-button btn btn-grouped"
type="button"
@click="hideForkMessage"
>
2021-01-29 00:20:46 +05:30
{{ $options.i18n.cancel }}
2018-11-08 19:23:39 +05:30
</button>
</div>
2019-07-07 11:18:12 +05:30
<template v-else>
2021-01-29 00:20:46 +05:30
<div
:id="`diff-content-${file.file_hash}`"
:class="hasBodyClasses.contentByHash"
data-testid="content-area"
>
<gl-loading-icon
v-if="showLoadingIcon"
class="diff-content loading gl-my-0 gl-pt-3"
data-testid="loader-icon"
/>
<div v-else-if="errorMessage" class="diff-viewer">
2020-11-24 15:15:51 +05:30
<div v-safe-html="errorMessage" class="nothing-here-block"></div>
2019-07-07 11:18:12 +05:30
</div>
2020-04-08 14:13:33 +05:30
<template v-else>
2021-01-29 00:20:46 +05:30
<div
v-show="showWarning"
class="collapsed-file-warning gl-p-7 gl-bg-orange-50 gl-text-center gl-rounded-bottom-left-base gl-rounded-bottom-right-base"
>
<p class="gl-mb-8">
{{ $options.i18n.autoCollapsed }}
</p>
<gl-button
data-testid="expand-button"
category="secondary"
variant="warning"
@click.prevent="handleToggle"
>
{{ $options.i18n.expand }}
</gl-button>
2020-04-08 14:13:33 +05:30
</div>
<diff-content
2021-01-29 00:20:46 +05:30
v-show="showContent"
:class="hasBodyClasses.content"
2020-04-08 14:13:33 +05:30
:diff-file="file"
:help-page-path="helpPagePath"
/>
</template>
2019-07-07 11:18:12 +05:30
</div>
</template>
2018-11-08 19:23:39 +05:30
</div>
</template>
2018-12-05 23:21:45 +05:30
<style>
@keyframes shadow-fade {
from {
box-shadow: 0 0 4px #919191;
}
to {
box-shadow: 0 0 0 #dfdfdf;
}
}
.diff-file.is-active {
box-shadow: 0 0 0 #dfdfdf;
animation: shadow-fade 1.2s 0.1s 1;
}
</style>