66 lines
2.0 KiB
Vue
66 lines
2.0 KiB
Vue
<template>
|
||
<div class="icon-upload field">
|
||
<label :for="inputId"><span>Иконка кейса (PNG)</span></label>
|
||
<div class="icon-upload-controls">
|
||
<CaseIcon :src="value" /><input
|
||
:id="inputId"
|
||
ref="file"
|
||
type="file"
|
||
accept="image/png,.png"
|
||
:disabled="disabled"
|
||
@change="select"
|
||
/><button v-if="value || error" class="button button-sm" type="button" :disabled="disabled" @click="remove">
|
||
Убрать
|
||
</button>
|
||
</div>
|
||
<p class="muted">До 1 МБ, не более 1024 × 1024 пикселей.</p>
|
||
<div v-if="error" class="form-error" role="alert">{{ error }}</div>
|
||
</div>
|
||
</template>
|
||
<script>
|
||
export default {
|
||
props: { value: { type: String, default: null }, disabled: Boolean },
|
||
data() {
|
||
return { error: '', inputId: 'case-icon-file' }
|
||
},
|
||
methods: {
|
||
select(event) {
|
||
this.error = ''
|
||
event.target.setCustomValidity('')
|
||
const file = event.target.files[0]
|
||
if (!file) return
|
||
if (file.type !== 'image/png' || file.size > 1024 * 1024) {
|
||
this.fail('Выберите PNG-файл размером до 1 МБ.')
|
||
return
|
||
}
|
||
event.target.setCustomValidity('Дождитесь загрузки иконки.')
|
||
const reader = new FileReader()
|
||
reader.onerror = () => this.fail('Не удалось прочитать файл.')
|
||
reader.onload = () => {
|
||
const image = new Image()
|
||
image.onload = () => {
|
||
if (image.width > 1024 || image.height > 1024) this.fail('Размер иконки — не более 1024 × 1024 пикселей.')
|
||
else {
|
||
event.target.setCustomValidity('')
|
||
this.$emit('input', reader.result)
|
||
}
|
||
}
|
||
image.onerror = () => this.fail('Не удалось прочитать PNG-файл.')
|
||
image.src = reader.result
|
||
}
|
||
reader.readAsDataURL(file)
|
||
},
|
||
fail(message) {
|
||
this.error = message
|
||
this.$refs.file.setCustomValidity(message)
|
||
},
|
||
remove() {
|
||
this.$emit('input', null)
|
||
this.error = ''
|
||
this.$refs.file.value = ''
|
||
this.$refs.file.setCustomValidity('')
|
||
},
|
||
},
|
||
}
|
||
</script>
|