mirror of
https://github.com/flarum/core.git
synced 2025-08-08 01:16:52 +02:00
update: forum/components/AvatarEditor
This commit is contained in:
committed by
Franz Liedke
parent
c17d7cd23f
commit
784b5cc03c
@@ -3,6 +3,7 @@ import avatar from '../../common/helpers/avatar';
|
|||||||
import icon from '../../common/helpers/icon';
|
import icon from '../../common/helpers/icon';
|
||||||
import listItems from '../../common/helpers/listItems';
|
import listItems from '../../common/helpers/listItems';
|
||||||
import ItemList from '../../common/utils/ItemList';
|
import ItemList from '../../common/utils/ItemList';
|
||||||
|
import classList from '../../common/utils/classList';
|
||||||
import Button from '../../common/components/Button';
|
import Button from '../../common/components/Button';
|
||||||
import LoadingIndicator from '../../common/components/LoadingIndicator';
|
import LoadingIndicator from '../../common/components/LoadingIndicator';
|
||||||
|
|
||||||
@@ -16,7 +17,9 @@ import LoadingIndicator from '../../common/components/LoadingIndicator';
|
|||||||
* - `user`
|
* - `user`
|
||||||
*/
|
*/
|
||||||
export default class AvatarEditor extends Component {
|
export default class AvatarEditor extends Component {
|
||||||
init() {
|
oninit(vnode) {
|
||||||
|
super.oninit(vnode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether or not an avatar upload is in progress.
|
* Whether or not an avatar upload is in progress.
|
||||||
*
|
*
|
||||||
@@ -32,17 +35,11 @@ export default class AvatarEditor extends Component {
|
|||||||
this.isDraggedOver = false;
|
this.isDraggedOver = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static initProps(props) {
|
|
||||||
super.initProps(props);
|
|
||||||
|
|
||||||
props.className = props.className || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
view() {
|
view() {
|
||||||
const user = this.props.user;
|
const user = this.attrs.user;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={'AvatarEditor Dropdown ' + this.props.className + (this.loading ? ' loading' : '') + (this.isDraggedOver ? ' dragover' : '')}>
|
<div className={classList(['AvatarEditor', 'Dropdown', this.attrs.className, this.loading && 'loading', this.isDraggedOver && 'dragover'])}>
|
||||||
{avatar(user)}
|
{avatar(user)}
|
||||||
<a
|
<a
|
||||||
className={user.avatarUrl() ? 'Dropdown-toggle' : 'Dropdown-toggle AvatarEditor--noAvatar'}
|
className={user.avatarUrl() ? 'Dropdown-toggle' : 'Dropdown-toggle AvatarEditor--noAvatar'}
|
||||||
@@ -55,7 +52,7 @@ export default class AvatarEditor extends Component {
|
|||||||
ondragend={this.disableDragover.bind(this)}
|
ondragend={this.disableDragover.bind(this)}
|
||||||
ondrop={this.dropUpload.bind(this)}
|
ondrop={this.dropUpload.bind(this)}
|
||||||
>
|
>
|
||||||
{this.loading ? LoadingIndicator.component() : user.avatarUrl() ? icon('fas fa-pencil-alt') : icon('fas fa-plus-circle')}
|
{this.loading ? <LoadingIndicator /> : user.avatarUrl() ? icon('fas fa-pencil-alt') : icon('fas fa-plus-circle')}
|
||||||
</a>
|
</a>
|
||||||
<ul className="Dropdown-menu Menu">{listItems(this.controlItems().toArray())}</ul>
|
<ul className="Dropdown-menu Menu">{listItems(this.controlItems().toArray())}</ul>
|
||||||
</div>
|
</div>
|
||||||
@@ -72,20 +69,16 @@ export default class AvatarEditor extends Component {
|
|||||||
|
|
||||||
items.add(
|
items.add(
|
||||||
'upload',
|
'upload',
|
||||||
Button.component({
|
<Button icon="fas fa-upload" onclick={this.openPicker.bind(this)}>
|
||||||
icon: 'fas fa-upload',
|
{app.translator.trans('core.forum.user.avatar_upload_button')}
|
||||||
children: app.translator.trans('core.forum.user.avatar_upload_button'),
|
</Button>
|
||||||
onclick: this.openPicker.bind(this),
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
items.add(
|
items.add(
|
||||||
'remove',
|
'remove',
|
||||||
Button.component({
|
<Button icon="fas fa-times" onclick={this.remove.bind(this)}>
|
||||||
icon: 'fas fa-times',
|
{app.translator.trans('core.forum.user.avatar_remove_button')}
|
||||||
children: app.translator.trans('core.forum.user.avatar_remove_button'),
|
</Button>
|
||||||
onclick: this.remove.bind(this),
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
@@ -134,7 +127,7 @@ export default class AvatarEditor extends Component {
|
|||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
*/
|
*/
|
||||||
quickUpload(e) {
|
quickUpload(e) {
|
||||||
if (!this.props.user.avatarUrl()) {
|
if (!this.attrs.user.avatarUrl()) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
this.openPicker();
|
this.openPicker();
|
||||||
@@ -149,7 +142,6 @@ export default class AvatarEditor extends Component {
|
|||||||
|
|
||||||
// Create a hidden HTML input element and click on it so the user can select
|
// Create a hidden HTML input element and click on it so the user can select
|
||||||
// an avatar file. Once they have, we will upload it via the API.
|
// an avatar file. Once they have, we will upload it via the API.
|
||||||
const user = this.props.user;
|
|
||||||
const $input = $('<input type="file">');
|
const $input = $('<input type="file">');
|
||||||
|
|
||||||
$input
|
$input
|
||||||
@@ -169,7 +161,7 @@ export default class AvatarEditor extends Component {
|
|||||||
upload(file) {
|
upload(file) {
|
||||||
if (this.loading) return;
|
if (this.loading) return;
|
||||||
|
|
||||||
const user = this.props.user;
|
const user = this.attrs.user;
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
data.append('avatar', file);
|
data.append('avatar', file);
|
||||||
|
|
||||||
@@ -179,9 +171,9 @@ export default class AvatarEditor extends Component {
|
|||||||
app
|
app
|
||||||
.request({
|
.request({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: app.forum.attribute('apiUrl') + '/users/' + user.id() + '/avatar',
|
url: `${app.forum.attribute('apiUrl')}/users/${user.id()}/avatar`,
|
||||||
serialize: (raw) => raw,
|
serialize: (raw) => raw,
|
||||||
data,
|
body: data,
|
||||||
})
|
})
|
||||||
.then(this.success.bind(this), this.failure.bind(this));
|
.then(this.success.bind(this), this.failure.bind(this));
|
||||||
}
|
}
|
||||||
@@ -190,7 +182,7 @@ export default class AvatarEditor extends Component {
|
|||||||
* Remove the user's avatar.
|
* Remove the user's avatar.
|
||||||
*/
|
*/
|
||||||
remove() {
|
remove() {
|
||||||
const user = this.props.user;
|
const user = this.attrs.user;
|
||||||
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
m.redraw();
|
m.redraw();
|
||||||
@@ -198,7 +190,7 @@ export default class AvatarEditor extends Component {
|
|||||||
app
|
app
|
||||||
.request({
|
.request({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
url: app.forum.attribute('apiUrl') + '/users/' + user.id() + '/avatar',
|
url: `${app.forum.attribute('apiUrl')}/users/${user.id()}/avatar`,
|
||||||
})
|
})
|
||||||
.then(this.success.bind(this), this.failure.bind(this));
|
.then(this.success.bind(this), this.failure.bind(this));
|
||||||
}
|
}
|
||||||
@@ -212,7 +204,7 @@ export default class AvatarEditor extends Component {
|
|||||||
*/
|
*/
|
||||||
success(response) {
|
success(response) {
|
||||||
app.store.pushPayload(response);
|
app.store.pushPayload(response);
|
||||||
delete this.props.user.avatarColor;
|
delete this.attrs.user.avatarColor;
|
||||||
|
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
m.redraw();
|
m.redraw();
|
||||||
|
Reference in New Issue
Block a user