2020-06-03 13:53:52 +02:00
|
|
|
import { CustomElement } from './element.js';
|
2021-03-24 11:59:09 +01:00
|
|
|
import { __ } from 'i18n';
|
2021-06-25 10:38:38 +02:00
|
|
|
import { api } from "@converse/headless/core";
|
2021-04-14 22:56:59 +02:00
|
|
|
import { html } from 'lit';
|
2020-06-03 13:53:52 +02:00
|
|
|
|
2021-06-25 10:38:38 +02:00
|
|
|
const i18n_profile_picture = __('Your profile picture');
|
2020-06-03 13:53:52 +02:00
|
|
|
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
export default class ImagePicker extends CustomElement {
|
2020-06-03 13:53:52 +02:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
|
|
|
'height': { type: Number },
|
2021-11-19 11:45:30 +01:00
|
|
|
'data': { type: Object},
|
2020-06-03 13:53:52 +02:00
|
|
|
'width': { type: Number },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return html`
|
2021-06-25 10:38:38 +02:00
|
|
|
<a class="change-avatar" @click=${this.openFileSelection} title="${i18n_profile_picture}">
|
2021-11-19 11:45:30 +01:00
|
|
|
<converse-avatar class="avatar" .data=${this.data} height="${this.height}" width="${this.width}"></converse-avatar>
|
2020-06-03 13:53:52 +02:00
|
|
|
</a>
|
|
|
|
<input @change=${this.updateFilePreview} class="hidden" name="image" type="file"/>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
openFileSelection (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.querySelector('input[type="file"]').click();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFilePreview (ev) {
|
|
|
|
const file = ev.target.files[0];
|
|
|
|
const reader = new FileReader();
|
2021-11-19 11:45:30 +01:00
|
|
|
reader.onloadend = () => {
|
|
|
|
this.data = {
|
|
|
|
'data_uri': reader.result,
|
|
|
|
'image_type': file.type
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 13:53:52 +02:00
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
api.elements.define('converse-image-picker', ImagePicker);
|