debian-mirror-gitlab/app/assets/javascripts/badges/components/badge.vue

122 lines
2.2 KiB
Vue
Raw Normal View History

2018-05-09 12:01:36 +05:30
<script>
import Icon from '~/vue_shared/components/icon.vue';
import Tooltip from '~/vue_shared/directives/tooltip';
2018-12-13 13:39:08 +05:30
import { GlLoadingIcon } from '@gitlab-org/gitlab-ui';
2018-05-09 12:01:36 +05:30
export default {
name: 'Badge',
components: {
Icon,
Tooltip,
2018-12-13 13:39:08 +05:30
GlLoadingIcon,
2018-05-09 12:01:36 +05:30
},
directives: {
Tooltip,
},
props: {
imageUrl: {
type: String,
required: true,
},
linkUrl: {
type: String,
required: true,
},
},
data() {
return {
hasError: false,
isLoading: true,
numRetries: 0,
};
},
computed: {
imageUrlWithRetries() {
if (this.numRetries === 0) {
return this.imageUrl;
}
return `${this.imageUrl}#retries=${this.numRetries}`;
},
},
watch: {
imageUrl() {
this.hasError = false;
this.isLoading = true;
this.numRetries = 0;
},
},
methods: {
onError() {
this.isLoading = false;
this.hasError = true;
},
onLoad() {
this.isLoading = false;
},
reloadImage() {
this.hasError = false;
this.isLoading = true;
this.numRetries += 1;
},
},
};
</script>
<template>
<div>
<a
v-show="!isLoading && !hasError"
:href="linkUrl"
target="_blank"
rel="noopener noreferrer"
>
<img
:src="imageUrlWithRetries"
2018-11-08 19:23:39 +05:30
class="project-badge"
aria-hidden="true"
2018-05-09 12:01:36 +05:30
@load="onLoad"
@error="onError"
/>
</a>
2018-12-05 23:21:45 +05:30
<gl-loading-icon
2018-05-09 12:01:36 +05:30
v-show="isLoading"
:inline="true"
/>
<div
v-show="hasError"
class="btn-group"
>
2018-11-08 19:23:39 +05:30
<div class="btn btn-default btn-sm disabled">
2018-05-09 12:01:36 +05:30
<icon
2018-11-08 19:23:39 +05:30
:size="16"
2018-05-09 12:01:36 +05:30
class="prepend-left-8 append-right-8"
2018-11-18 11:00:15 +05:30
name="doc-image"
2018-05-09 12:01:36 +05:30
aria-hidden="true"
/>
</div>
<div
2018-11-08 19:23:39 +05:30
class="btn btn-default btn-sm disabled"
2018-05-09 12:01:36 +05:30
>
<span class="prepend-left-8 append-right-8">{{ s__('Badges|No badge image') }}</span>
</div>
</div>
<button
2018-11-08 19:23:39 +05:30
v-show="hasError"
2018-12-05 23:21:45 +05:30
v-tooltip
2018-05-09 12:01:36 +05:30
:title="s__('Badges|Reload badge image')"
2018-11-08 19:23:39 +05:30
class="btn btn-transparent btn-sm text-primary"
type="button"
2018-05-09 12:01:36 +05:30
@click="reloadImage"
>
<icon
:size="16"
2018-11-08 19:23:39 +05:30
name="retry"
2018-05-09 12:01:36 +05:30
/>
</button>
</div>
</template>