gitlab-org--gitlab-foss/app/assets/javascripts/diffs/components/diff_file.vue

359 lines
9.5 KiB
Vue
Raw Normal View History

2018-06-21 12:22:40 +00:00
<script>
import { mapActions, mapGetters, mapState } from 'vuex';
import { escape } from 'lodash';
import { GlButton, GlLoadingIcon, GlSafeHtmlDirective as SafeHtml } from '@gitlab/ui';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { sprintf } from '~/locale';
import { deprecatedCreateFlash as createFlash } from '~/flash';
import { hasDiff } from '~/helpers/diffs_helper';
import notesEventHub from '../../notes/event_hub';
2018-06-21 12:22:40 +00:00
import DiffFileHeader from './diff_file_header.vue';
import DiffContent from './diff_content.vue';
import { diffViewerErrors } from '~/ide/constants';
import { collapsedType, isCollapsed } from '../utils/diff_file';
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-06-21 12:22:40 +00:00
export default {
components: {
DiffFileHeader,
DiffContent,
GlButton,
GlLoadingIcon,
2018-06-21 12:22:40 +00:00
},
directives: {
SafeHtml,
},
mixins: [glFeatureFlagsMixin()],
2018-06-21 12:22:40 +00:00
props: {
file: {
type: Object,
required: true,
},
reviewed: {
type: Boolean,
required: false,
default: false,
},
isFirstFile: {
type: Boolean,
required: false,
default: false,
},
isLastFile: {
type: Boolean,
required: false,
default: false,
},
canCurrentUserFork: {
type: Boolean,
2018-06-21 12:22:40 +00:00
required: true,
},
helpPagePath: {
type: String,
required: false,
default: '',
},
viewDiffsFileByFile: {
type: Boolean,
required: true,
},
2018-06-21 12:22:40 +00:00
},
data() {
return {
isLoadingCollapsedDiff: false,
forkMessageVisible: false,
isCollapsed: isCollapsed(this.file),
2018-06-21 12:22:40 +00:00
};
},
i18n: {
...DIFF_FILE,
genericError: GENERIC_ERROR,
},
2018-06-21 12:22:40 +00:00
computed: {
...mapState('diffs', ['currentDiffFileId']),
...mapGetters(['isNotesFetched']),
...mapGetters('diffs', ['getDiffFileDiscussions']),
2018-06-21 12:22:40 +00:00
viewBlobLink() {
return sprintf(
this.$options.i18n.blobView,
2018-06-21 12:22:40 +00:00
{
linkStart: `<a href="${escape(this.file.view_path)}">`,
2018-06-21 12:22:40 +00:00
linkEnd: '</a>',
},
false,
);
},
showLoadingIcon() {
return this.isLoadingCollapsedDiff || (!this.file.renderIt && !this.isCollapsed);
},
hasDiff() {
return hasDiff(this.file);
},
isFileTooLarge() {
return this.file.viewer.error === diffViewerErrors.too_large;
},
errorMessage() {
return !this.manuallyCollapsed ? this.file.viewer.error_message : '';
},
2019-07-16 02:48:40 +00:00
forkMessage() {
return sprintf(
this.$options.i18n.editInFork,
2019-07-16 02:48:40 +00:00
{
tag_start: '<span class="js-file-fork-suggestion-section-action">',
tag_end: '</span>',
},
false,
);
},
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';
}
return domParts;
},
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: {
'file.file_hash': {
handler: function hashChangeWatch(newHash, oldHash) {
this.isCollapsed = isCollapsed(this.file);
if (newHash && oldHash && !this.hasDiff) {
this.requestDiff();
}
},
immediate: true,
},
'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-06-21 12:22:40 +00:00
},
created() {
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);
},
2018-06-21 12:22:40 +00:00
methods: {
...mapActions('diffs', [
'loadCollapsedDiff',
'assignDiscussionsToDiff',
'setRenderIt',
'setFileCollapsedByUser',
]),
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-06-21 12:22:40 +00:00
handleToggle() {
const currentCollapsedFlag = this.isCollapsed;
this.setFileCollapsedByUser({
filePath: this.file.file_path,
collapsed: !currentCollapsedFlag,
});
if (!this.hasDiff && currentCollapsedFlag) {
this.requestDiff();
2018-06-21 12:22:40 +00:00
}
},
requestDiff() {
2018-06-21 12:22:40 +00:00
this.isLoadingCollapsedDiff = true;
this.loadCollapsedDiff(this.file)
.then(() => {
this.isLoadingCollapsedDiff = false;
this.setRenderIt(this.file);
2018-06-21 12:22:40 +00:00
})
.then(() => {
requestIdleCallback(
() => {
this.postRender();
this.assignDiscussionsToDiff(this.getDiffFileDiscussions(this.file));
},
{ timeout: 1000 },
);
})
2018-06-21 12:22:40 +00:00
.catch(() => {
this.isLoadingCollapsedDiff = false;
createFlash(this.$options.i18n.genericError);
2018-06-21 12:22:40 +00:00
});
},
showForkMessage() {
this.forkMessageVisible = true;
},
hideForkMessage() {
this.forkMessageVisible = false;
},
},
};
</script>
<template>
<div
:id="file.file_hash"
:class="{
'is-active': currentDiffFileId === file.file_hash,
'comments-disabled': Boolean(file.brokenSymlink),
'has-body': showBody,
}"
:data-path="file.new_path"
class="diff-file file-holder gl-border-none"
2018-06-21 12:22:40 +00:00
>
<diff-file-header
:can-current-user-fork="canCurrentUserFork"
2018-06-21 12:22:40 +00:00
:diff-file="file"
:collapsible="true"
:expanded="!isCollapsed"
:add-merge-request-buttons="true"
:view-diffs-file-by-file="viewDiffsFileByFile"
class="js-file-title file-title gl-border-1 gl-border-solid gl-border-gray-100"
:class="hasBodyClasses.header"
2018-06-21 12:22:40 +00:00
@toggleFile="handleToggle"
@showForkMessage="showForkMessage"
/>
2018-11-16 20:07:38 +00:00
<div v-if="forkMessageVisible" class="js-file-fork-suggestion-section file-fork-suggestion">
<span v-safe-html="forkMessage" class="file-fork-suggestion-note"></span>
2018-06-21 12:22:40 +00:00
<a
:href="file.fork_path"
2018-06-21 12:22:40 +00:00
class="js-fork-suggestion-button btn btn-grouped btn-inverted btn-success"
>{{ $options.i18n.fork }}</a
2018-06-21 12:22:40 +00:00
>
<button
class="js-cancel-fork-suggestion-button btn btn-grouped"
type="button"
@click="hideForkMessage"
>
{{ $options.i18n.cancel }}
2018-06-21 12:22:40 +00:00
</button>
</div>
<template v-else>
<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">
<div v-safe-html="errorMessage" class="nothing-here-block"></div>
</div>
<template v-else>
<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>
</div>
<diff-content
v-show="showContent"
:class="hasBodyClasses.content"
:diff-file="file"
:help-page-path="helpPagePath"
/>
</template>
</div>
</template>
2018-06-21 12:22:40 +00:00
</div>
</template>
<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>