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

112 lines
2.4 KiB
Vue
Raw Normal View History

2018-05-09 12:01:36 +05:30
<script>
2021-01-29 00:20:46 +05:30
import { GlLoadingIcon, GlTooltipDirective, GlIcon, GlButton } from '@gitlab/ui';
2021-04-29 21:17:54 +05:30
import { s__ } from '~/locale';
2018-05-09 12:01:36 +05:30
export default {
2021-04-29 21:17:54 +05:30
i18n: {
buttonLabel: s__('Badges|Reload badge image'),
},
2019-10-12 21:52:04 +05:30
// name: 'Badge' is a false positive: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/25
2020-04-22 19:07:51 +05:30
// eslint-disable-next-line @gitlab/require-i18n-strings
2018-05-09 12:01:36 +05:30
name: 'Badge',
components: {
2020-11-24 15:15:51 +05:30
GlIcon,
2018-12-13 13:39:08 +05:30
GlLoadingIcon,
2021-01-29 00:20:46 +05:30
GlButton,
2018-05-09 12:01:36 +05:30
},
directives: {
2019-02-15 15:39:39 +05:30
GlTooltip: GlTooltipDirective,
2018-05-09 12:01:36 +05:30
},
props: {
2020-01-01 13:55:28 +05:30
name: {
type: String,
required: false,
default: '',
},
2018-05-09 12:01:36 +05:30
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>
2019-02-15 15:39:39 +05:30
<a v-show="!isLoading && !hasError" :href="linkUrl" target="_blank" rel="noopener noreferrer">
2018-05-09 12:01:36 +05:30
<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>
2021-09-30 23:02:18 +05:30
<gl-loading-icon v-show="isLoading" size="sm" :inline="true" />
2018-05-09 12:01:36 +05:30
2019-02-15 15:39:39 +05:30
<div v-show="hasError" class="btn-group">
2018-11-08 19:23:39 +05:30
<div class="btn btn-default btn-sm disabled">
2021-02-22 17:27:13 +05:30
<gl-icon :size="16" class="gl-ml-3 gl-mr-3" name="doc-image" />
2018-05-09 12:01:36 +05:30
</div>
2019-02-15 15:39:39 +05:30
<div class="btn btn-default btn-sm disabled">
2020-06-23 00:09:42 +05:30
<span class="gl-ml-3 gl-mr-3">{{ s__('Badges|No badge image') }}</span>
2018-05-09 12:01:36 +05:30
</div>
</div>
2021-01-29 00:20:46 +05:30
<gl-button
2018-11-08 19:23:39 +05:30
v-show="hasError"
2019-02-15 15:39:39 +05:30
v-gl-tooltip.hover
2021-04-29 21:17:54 +05:30
:title="$options.i18n.buttonLabel"
:aria-label="$options.i18n.buttonLabel"
2021-01-29 00:20:46 +05:30
category="tertiary"
2021-04-17 20:07:23 +05:30
variant="confirm"
2018-11-08 19:23:39 +05:30
type="button"
2021-01-29 00:20:46 +05:30
icon="retry"
size="small"
2018-05-09 12:01:36 +05:30
@click="reloadImage"
2021-01-29 00:20:46 +05:30
/>
2018-05-09 12:01:36 +05:30
</div>
</template>