2020-08-02 19:17:29 +02:00
|
|
|
<template>
|
2021-06-24 01:24:57 +02:00
|
|
|
<card :title="$t('user.settings.avatar.title')">
|
2021-01-17 18:57:57 +01:00
|
|
|
<div class="control mb-4">
|
|
|
|
<label class="radio">
|
|
|
|
<input name="avatarProvider" type="radio" v-model="avatarProvider" value="default"/>
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('misc.default') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</label>
|
|
|
|
<label class="radio">
|
|
|
|
<input name="avatarProvider" type="radio" v-model="avatarProvider" value="initials"/>
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('user.settings.avatar.initials') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</label>
|
|
|
|
<label class="radio">
|
|
|
|
<input name="avatarProvider" type="radio" v-model="avatarProvider" value="gravatar"/>
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('user.settings.avatar.gravatar') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</label>
|
|
|
|
<label class="radio">
|
|
|
|
<input name="avatarProvider" type="radio" v-model="avatarProvider" value="upload"/>
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('user.settings.avatar.upload') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</label>
|
|
|
|
</div>
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2021-01-17 18:57:57 +01:00
|
|
|
<template v-if="avatarProvider === 'upload'">
|
|
|
|
<input
|
|
|
|
@change="cropAvatar"
|
|
|
|
accept="image/*"
|
|
|
|
class="is-hidden"
|
|
|
|
ref="avatarUploadInput"
|
|
|
|
type="file"
|
|
|
|
/>
|
|
|
|
<x-button
|
|
|
|
:loading="avatarService.loading || loading"
|
|
|
|
@click="$refs.avatarUploadInput.click()"
|
|
|
|
v-if="!isCropAvatar">
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('user.settings.avatar.uploadAvatar') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
|
|
|
<template v-else>
|
|
|
|
<cropper
|
|
|
|
:src="avatarToCrop"
|
|
|
|
:stencil-props="{aspectRatio: 1}"
|
|
|
|
@ready="() => loading = false"
|
2021-02-05 19:51:06 +01:00
|
|
|
class="mb-4 cropper"
|
|
|
|
ref="cropper"
|
|
|
|
/>
|
2021-01-17 18:57:57 +01:00
|
|
|
<x-button
|
|
|
|
:loading="avatarService.loading || loading"
|
|
|
|
@click="uploadAvatar"
|
|
|
|
>
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('user.settings.avatar.uploadAvatar') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
2020-08-02 19:17:29 +02:00
|
|
|
</template>
|
2021-01-17 18:57:57 +01:00
|
|
|
</template>
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2021-01-17 18:57:57 +01:00
|
|
|
<div class="mt-2" v-if="avatarProvider !== 'upload'">
|
|
|
|
<x-button
|
|
|
|
:loading="avatarService.loading || loading"
|
|
|
|
@click="updateAvatarStatus()"
|
|
|
|
class="is-fullwidth"
|
|
|
|
>
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('misc.save') }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
2020-08-02 19:17:29 +02:00
|
|
|
</div>
|
2021-01-17 18:57:57 +01:00
|
|
|
</card>
|
2020-08-02 19:17:29 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-09-05 22:35:52 +02:00
|
|
|
import {Cropper} from 'vue-advanced-cropper'
|
2021-02-05 19:51:06 +01:00
|
|
|
import 'vue-advanced-cropper/dist/style.css'
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
import AvatarService from '../../services/avatar'
|
|
|
|
import AvatarModel from '../../models/avatar'
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
export default {
|
|
|
|
name: 'avatar-settings',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
avatarProvider: '',
|
|
|
|
avatarService: AvatarService,
|
|
|
|
isCropAvatar: false,
|
|
|
|
avatarToCrop: null,
|
|
|
|
loading: false, // Seperate variable because some things we're doing in browser take a bit
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.avatarService = new AvatarService()
|
|
|
|
this.avatarStatus()
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
Cropper,
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
avatarStatus() {
|
|
|
|
this.avatarService.get({})
|
|
|
|
.then(r => {
|
|
|
|
this.avatarProvider = r.avatarProvider
|
|
|
|
})
|
2021-08-25 12:28:29 +02:00
|
|
|
.catch(e => this.$message.error(e))
|
2020-08-02 19:17:29 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
updateAvatarStatus() {
|
|
|
|
const avatarStatus = new AvatarModel({avatarProvider: this.avatarProvider})
|
|
|
|
this.avatarService.update(avatarStatus)
|
|
|
|
.then(() => {
|
2021-08-25 12:28:29 +02:00
|
|
|
this.$message.success({message: this.$t('user.settings.avatar.statusUpdateSuccess')})
|
2020-09-05 22:35:52 +02:00
|
|
|
this.$store.commit('auth/reloadAvatar')
|
|
|
|
})
|
2021-08-25 12:28:29 +02:00
|
|
|
.catch(e => this.$message.error(e))
|
2020-08-02 19:17:29 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
uploadAvatar() {
|
|
|
|
this.loading = true
|
|
|
|
const {canvas} = this.$refs.cropper.getResult()
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
if (canvas) {
|
|
|
|
canvas.toBlob(blob => {
|
|
|
|
this.avatarService.create(blob)
|
|
|
|
.then(() => {
|
2021-08-25 12:28:29 +02:00
|
|
|
this.$message.success({message: this.$t('user.settings.avatar.setSuccess')})
|
2020-09-05 22:35:52 +02:00
|
|
|
this.$store.commit('auth/reloadAvatar')
|
|
|
|
})
|
2021-08-25 12:28:29 +02:00
|
|
|
.catch(e => this.$message.error(e))
|
2020-09-05 22:35:52 +02:00
|
|
|
.finally(() => {
|
|
|
|
this.loading = false
|
|
|
|
this.isCropAvatar = false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.loading = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cropAvatar() {
|
|
|
|
const avatar = this.$refs.avatarUploadInput.files
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
if (avatar.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2020-08-02 19:17:29 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
this.loading = true
|
|
|
|
const reader = new FileReader()
|
|
|
|
reader.onload = e => {
|
|
|
|
this.avatarToCrop = e.target.result
|
|
|
|
this.isCropAvatar = true
|
|
|
|
}
|
|
|
|
reader.onloadend = () => this.loading = false
|
|
|
|
reader.readAsDataURL(avatar[0])
|
2020-08-02 19:17:29 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
}
|
2020-08-02 19:17:29 +02:00
|
|
|
</script>
|
2021-02-05 19:51:06 +01:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.cropper {
|
|
|
|
height: 80vh;
|
|
|
|
background: transparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
.vue-advanced-cropper__background {
|
|
|
|
background: $white;
|
|
|
|
}
|
|
|
|
</style>
|