/* global phpbb */
(function($) { // Avoid conflicts with other libraries
'use strict';
/**
* phpBB Avatars namespace.
*
* Handles cropping for local file uploads.
*/
phpbb.avatars = {
cropper: null,
image: null,
/** @type {jQuery} */
$form: null,
/** @type {jQuery} */
$buttons: $('#avatar-cropper-buttons'),
/** @type {jQuery} */
$box: $('.c-avatar-box'),
/** @type {jQuery} */
$data: $('#avatar-cropper-data'),
/** @type {jQuery} */
$input: $('#avatar_upload_file'),
/** @type {jQuery} */
$driver: $('#avatar_driver'),
/** @type {string} */
driverUpload: 'avatar_driver_upload',
/**
* Initialise avatar cropping.
*/
init() {
// If the cropper library is not available
if (!$.isFunction($.fn.cropper)) {
return;
}
// Correctly position the cropper buttons
this.$buttons.appendTo(this.$box);
// Ensure we have an img for the cropping
if (this.$box.children('img').length === 0) {
const $avatarImg = $('
');
$avatarImg.setAttribute('width', phpbb.avatars.$data.data().maxWidth);
$avatarImg.setAttribute('height', phpbb.avatars.$data.data().maxHeight);
$avatarImg.addClass('avatar');
this.image = $avatarImg;
this.$box.prepend($avatarImg);
} else {
this.image = this.$box.children('img');
}
this.bindInput();
this.bindSelect();
this.bindSubmit();
},
/**
* Destroy (undo) any initialisation.
*/
destroy() {
this.$buttons.find('[data-cropper-action]').off('click.phpbb.avatars');
this.image.off('crop.phpbb.avatars');
this.$form.off('submit');
this.$data.val('');
this.$buttons.hide();
this.$box.removeClass('c-cropper-avatar-box');
if (this.cropper !== null) {
this.cropper.destroy();
}
},
/**
* Bind a function to the avatar driver