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

210 lines
5.6 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';
2018-11-08 19:23:39 +05:30
import _ from 'underscore';
import { __, sprintf } from '~/locale';
import createFlash from '~/flash';
2019-02-15 15:39:39 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
import eventHub 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';
2018-11-08 19:23:39 +05:30
export default {
components: {
DiffFileHeader,
DiffContent,
2018-12-13 13:39:08 +05:30
GlLoadingIcon,
2018-11-08 19:23:39 +05:30
},
props: {
file: {
type: Object,
required: true,
},
canCurrentUserFork: {
type: Boolean,
required: true,
},
2019-02-15 15:39:39 +05:30
helpPagePath: {
type: String,
required: false,
default: '',
},
2018-11-08 19:23:39 +05:30
},
data() {
return {
isLoadingCollapsedDiff: false,
forkMessageVisible: false,
2019-07-07 11:18:12 +05:30
isCollapsed: this.file.viewer.collapsed || false,
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(
__('You can %{linkStart}view the blob%{linkEnd} instead.'),
{
2019-02-15 15:39:39 +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
},
2018-12-13 13:39:08 +05:30
hasDiffLines() {
2019-02-15 15:39:39 +05:30
return (
this.file.highlighted_diff_lines &&
this.file.parallel_diff_lines &&
this.file.parallel_diff_lines.length > 0
);
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() {
return this.file.viewer.error_message;
},
2018-12-13 13:39:08 +05:30
},
watch: {
2019-07-07 11:18:12 +05:30
isCollapsed: function fileCollapsedWatch(newVal, oldVal) {
2018-12-13 13:39:08 +05:30
if (!newVal && oldVal && !this.hasDiffLines) {
this.handleLoadCollapsedDiff();
}
2019-07-07 11:18:12 +05:30
this.setFileCollapsed({ filePath: this.file.file_path, collapsed: newVal });
},
'file.viewer.collapsed': function setIsCollapsed(newVal) {
this.isCollapsed = newVal;
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() {
eventHub.$on(`loadCollapsedDiff/${this.file.file_hash}`, this.handleLoadCollapsedDiff);
},
2018-11-08 19:23:39 +05:30
methods: {
2019-07-07 11:18:12 +05:30
...mapActions('diffs', [
'loadCollapsedDiff',
'assignDiscussionsToDiff',
'setRenderIt',
'setFileCollapsed',
]),
2018-11-08 19:23:39 +05:30
handleToggle() {
2018-12-13 13:39:08 +05:30
if (!this.hasDiffLines) {
2018-11-08 19:23:39 +05:30
this.handleLoadCollapsedDiff();
} else {
2019-07-07 11:18:12 +05:30
this.isCollapsed = !this.isCollapsed;
this.setRenderIt(this.file);
2018-11-08 19:23:39 +05:30
}
},
handleLoadCollapsedDiff() {
this.isLoadingCollapsedDiff = true;
this.loadCollapsedDiff(this.file)
.then(() => {
this.isLoadingCollapsedDiff = false;
2019-07-07 11:18:12 +05:30
this.isCollapsed = false;
this.setRenderIt(this.file);
2018-11-20 20:47:30 +05:30
})
.then(() => {
requestIdleCallback(
() => {
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;
createFlash(__('Something went wrong on our end. Please try again!'));
});
},
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,
2018-12-05 23:21:45 +05:30
}"
2018-11-08 19:23:39 +05:30
class="diff-file file-holder"
>
<diff-file-header
:can-current-user-fork="canCurrentUserFork"
:diff-file="file"
:collapsible="true"
:expanded="!isCollapsed"
:add-merge-request-buttons="true"
class="js-file-title file-title"
@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">
2018-11-08 19:23:39 +05:30
<span class="file-fork-suggestion-note">
2019-02-15 15:39:39 +05:30
You're not allowed to <span class="js-file-fork-suggestion-section-action">edit</span> files
in this project directly. Please fork this project, make your changes there, and submit a
merge request.
2018-11-08 19:23:39 +05:30
</span>
<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"
2019-02-15 15:39:39 +05:30
>Fork</a
2018-11-08 19:23:39 +05:30
>
<button
class="js-cancel-fork-suggestion-button btn btn-grouped"
type="button"
@click="hideForkMessage"
>
Cancel
</button>
</div>
2019-02-15 15:39:39 +05:30
<gl-loading-icon v-if="showLoadingIcon" class="diff-content loading" />
2019-07-07 11:18:12 +05:30
<template v-else>
<div :id="`diff-content-${file.file_hash}`">
<div v-if="errorMessage" class="diff-viewer">
<div class="nothing-here-block" v-html="errorMessage"></div>
</div>
<div v-else-if="isCollapsed" class="nothing-here-block diff-collapsed">
{{ __('This diff is collapsed.') }}
<a class="click-to-expand js-click-to-expand" href="#" @click.prevent="handleToggle">{{
__('Click to expand it.')
}}</a>
</div>
<diff-content
v-else
:class="{ hidden: isCollapsed || isFileTooLarge }"
:diff-file="file"
:help-page-path="helpPagePath"
/>
</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>