2020-03-09 09:07:45 +00:00
|
|
|
<script>
|
2020-08-17 21:09:56 +00:00
|
|
|
import { GlLoadingIcon } from '@gitlab/ui';
|
2020-03-09 09:07:45 +00:00
|
|
|
import BlobHeaderEdit from '~/blob/components/blob_edit_header.vue';
|
|
|
|
import BlobContentEdit from '~/blob/components/blob_edit_content.vue';
|
2020-07-17 09:09:43 +00:00
|
|
|
import { getBaseURL, joinPaths } from '~/lib/utils/url_utility';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { SNIPPET_BLOB_CONTENT_FETCH_ERROR } from '~/snippets/constants';
|
2020-08-20 09:09:55 +00:00
|
|
|
import { deprecatedCreateFlash as Flash } from '~/flash';
|
2020-07-17 09:09:43 +00:00
|
|
|
import { sprintf } from '~/locale';
|
|
|
|
|
2020-03-09 09:07:45 +00:00
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
BlobHeaderEdit,
|
|
|
|
BlobContentEdit,
|
2020-04-03 09:09:31 +00:00
|
|
|
GlLoadingIcon,
|
2020-03-09 09:07:45 +00:00
|
|
|
},
|
2020-04-01 09:07:45 +00:00
|
|
|
inheritAttrs: false,
|
2020-03-09 09:07:45 +00:00
|
|
|
props: {
|
2020-07-17 09:09:43 +00:00
|
|
|
blob: {
|
|
|
|
type: Object,
|
2020-08-18 18:10:10 +00:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
canDelete: {
|
|
|
|
type: Boolean,
|
2020-04-03 09:09:31 +00:00
|
|
|
required: false,
|
2020-08-18 18:10:10 +00:00
|
|
|
default: true,
|
2020-04-03 09:09:31 +00:00
|
|
|
},
|
2020-08-18 18:10:10 +00:00
|
|
|
showDelete: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false,
|
2020-03-09 09:07:45 +00:00
|
|
|
},
|
2020-08-18 18:10:10 +00:00
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
inputId() {
|
|
|
|
return `${this.blob.id}_file_path`;
|
2020-07-17 09:09:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
2020-08-18 18:10:10 +00:00
|
|
|
if (!this.blob.isLoaded) {
|
2020-07-17 09:09:43 +00:00
|
|
|
this.fetchBlobContent();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2020-08-18 18:10:10 +00:00
|
|
|
onDelete() {
|
|
|
|
this.$emit('delete');
|
|
|
|
},
|
2020-07-17 09:09:43 +00:00
|
|
|
notifyAboutUpdates(args = {}) {
|
2020-08-18 18:10:10 +00:00
|
|
|
this.$emit('blob-updated', args);
|
2020-07-17 09:09:43 +00:00
|
|
|
},
|
|
|
|
fetchBlobContent() {
|
|
|
|
const baseUrl = getBaseURL();
|
|
|
|
const url = joinPaths(baseUrl, this.blob.rawPath);
|
|
|
|
|
|
|
|
axios
|
2020-08-27 06:10:37 +00:00
|
|
|
.get(url, {
|
|
|
|
// This prevents axios from automatically JSON.parse response
|
|
|
|
transformResponse: [f => f],
|
|
|
|
})
|
2020-07-17 09:09:43 +00:00
|
|
|
.then(res => {
|
2020-08-18 18:10:10 +00:00
|
|
|
this.notifyAboutUpdates({ content: res.data });
|
2020-07-17 09:09:43 +00:00
|
|
|
})
|
2020-08-18 18:10:10 +00:00
|
|
|
.catch(e => this.flashAPIFailure(e));
|
2020-07-17 09:09:43 +00:00
|
|
|
},
|
|
|
|
flashAPIFailure(err) {
|
|
|
|
Flash(sprintf(SNIPPET_BLOB_CONTENT_FETCH_ERROR, { err }));
|
2020-04-01 09:07:45 +00:00
|
|
|
},
|
2020-03-09 09:07:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<template>
|
2020-08-12 21:09:54 +00:00
|
|
|
<div class="file-holder snippet">
|
|
|
|
<blob-header-edit
|
2020-08-18 18:10:10 +00:00
|
|
|
:id="inputId"
|
|
|
|
:value="blob.path"
|
2020-08-12 21:09:54 +00:00
|
|
|
data-qa-selector="file_name_field"
|
2020-08-18 18:10:10 +00:00
|
|
|
:can-delete="canDelete"
|
|
|
|
:show-delete="showDelete"
|
|
|
|
@input="notifyAboutUpdates({ path: $event })"
|
|
|
|
@delete="onDelete"
|
2020-08-12 21:09:54 +00:00
|
|
|
/>
|
|
|
|
<gl-loading-icon
|
2020-08-18 18:10:10 +00:00
|
|
|
v-if="!blob.isLoaded"
|
2020-08-12 21:09:54 +00:00
|
|
|
:label="__('Loading snippet')"
|
|
|
|
size="lg"
|
2020-08-21 00:10:44 +00:00
|
|
|
class="loading-animation prepend-top-20 gl-mb-6"
|
2020-08-12 21:09:54 +00:00
|
|
|
/>
|
2020-08-18 18:10:10 +00:00
|
|
|
<blob-content-edit
|
|
|
|
v-else
|
|
|
|
:value="blob.content"
|
|
|
|
:file-global-id="blob.id"
|
|
|
|
:file-name="blob.path"
|
|
|
|
@input="notifyAboutUpdates({ content: $event })"
|
|
|
|
/>
|
2020-03-09 09:07:45 +00:00
|
|
|
</div>
|
|
|
|
</template>
|