debian-mirror-gitlab/app/assets/javascripts/snippets/components/snippet_blob_view.vue

117 lines
3.2 KiB
Vue
Raw Normal View History

2020-03-13 15:44:24 +05:30
<script>
import BlobEmbeddable from '~/blob/components/blob_embeddable.vue';
import { SNIPPET_VISIBILITY_PUBLIC } from '../constants';
import BlobHeader from '~/blob/components/blob_header.vue';
import BlobContent from '~/blob/components/blob_content.vue';
2020-04-22 19:07:51 +05:30
import CloneDropdownButton from '~/vue_shared/components/clone_dropdown.vue';
2020-03-13 15:44:24 +05:30
import GetBlobContent from '../queries/snippet.blob.content.query.graphql';
2020-05-24 23:13:21 +05:30
import {
SIMPLE_BLOB_VIEWER,
RICH_BLOB_VIEWER,
BLOB_RENDER_EVENT_LOAD,
BLOB_RENDER_EVENT_SHOW_SOURCE,
} from '~/blob/components/constants';
2020-03-13 15:44:24 +05:30
export default {
components: {
BlobEmbeddable,
BlobHeader,
BlobContent,
2020-04-22 19:07:51 +05:30
CloneDropdownButton,
2020-03-13 15:44:24 +05:30
},
apollo: {
blobContent: {
query: GetBlobContent,
variables() {
return {
ids: this.snippet.id,
rich: this.activeViewerType === RICH_BLOB_VIEWER,
};
},
update: data =>
data.snippets.edges[0].node.blob.richData || data.snippets.edges[0].node.blob.plainData,
2020-05-24 23:13:21 +05:30
result() {
if (this.activeViewerType === RICH_BLOB_VIEWER) {
this.blob.richViewer.renderError = null;
} else {
this.blob.simpleViewer.renderError = null;
}
},
skip() {
return this.viewer.renderError;
},
2020-03-13 15:44:24 +05:30
},
},
props: {
snippet: {
type: Object,
required: true,
},
},
data() {
return {
2020-04-22 19:07:51 +05:30
blob: this.snippet.blob,
2020-03-13 15:44:24 +05:30
blobContent: '',
2020-04-22 19:07:51 +05:30
activeViewerType:
this.snippet.blob?.richViewer && !window.location.hash
? RICH_BLOB_VIEWER
: SIMPLE_BLOB_VIEWER,
2020-03-13 15:44:24 +05:30
};
},
computed: {
embeddable() {
return this.snippet.visibilityLevel === SNIPPET_VISIBILITY_PUBLIC;
},
isContentLoading() {
return this.$apollo.queries.blobContent.loading;
},
viewer() {
const { richViewer, simpleViewer } = this.blob;
return this.activeViewerType === RICH_BLOB_VIEWER ? richViewer : simpleViewer;
},
2020-04-22 19:07:51 +05:30
canBeCloned() {
return this.snippet.sshUrlToRepo || this.snippet.httpUrlToRepo;
},
2020-03-13 15:44:24 +05:30
},
methods: {
2020-04-22 19:07:51 +05:30
switchViewer(newViewer) {
2020-05-24 23:13:21 +05:30
this.activeViewerType = newViewer || SIMPLE_BLOB_VIEWER;
},
forceQuery() {
this.$apollo.queries.blobContent.skip = false;
this.$apollo.queries.blobContent.refetch();
2020-03-13 15:44:24 +05:30
},
},
2020-05-24 23:13:21 +05:30
BLOB_RENDER_EVENT_LOAD,
BLOB_RENDER_EVENT_SHOW_SOURCE,
2020-03-13 15:44:24 +05:30
};
</script>
<template>
<div>
<blob-embeddable v-if="embeddable" class="mb-3" :url="snippet.webUrl" />
2020-04-22 19:07:51 +05:30
<article class="file-holder snippet-file-content">
<blob-header :blob="blob" :active-viewer-type="viewer.type" @viewer-changed="switchViewer">
<template #actions>
<clone-dropdown-button
v-if="canBeCloned"
2020-05-24 23:13:21 +05:30
class="mr-2"
2020-04-22 19:07:51 +05:30
:ssh-link="snippet.sshUrlToRepo"
:http-link="snippet.httpUrlToRepo"
2020-05-24 23:13:21 +05:30
data-qa-selector="clone_button"
2020-04-22 19:07:51 +05:30
/>
</template>
</blob-header>
2020-05-24 23:13:21 +05:30
<blob-content
:loading="isContentLoading"
:content="blobContent"
:active-viewer="viewer"
:blob="blob"
@[$options.BLOB_RENDER_EVENT_LOAD]="forceQuery"
@[$options.BLOB_RENDER_EVENT_SHOW_SOURCE]="switchViewer"
/>
2020-03-13 15:44:24 +05:30
</article>
</div>
</template>