diff --git a/phpBB/assets/javascript/phpbb-avatars.js b/phpBB/assets/javascript/phpbb-avatars.js index 41f14112f0..147c2d9064 100644 --- a/phpBB/assets/javascript/phpbb-avatars.js +++ b/phpBB/assets/javascript/phpbb-avatars.js @@ -1,184 +1,189 @@ -(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} */ - $buttons: $('#avatar-cropper-buttons'), - - /** @type {jQuery} */ - $box: $('#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', +/* global phpbb */ +(function($) { // Avoid conflicts with other libraries + 'use strict'; /** - * Initialise avatar cropping. - */ - init: function() { - // If the cropper library is not available - if (!$.isFunction($.fn.cropper)) { - return; - } - - // Correctly position the cropper buttons - this.$buttons.appendTo(this.$box); - - this.image = this.$box.children('img'); - - this.bindInput(); - this.bindSelect(); - }, - - /** - * Destroy (undo) any initialisation. - */ - destroy: function() { - this.$buttons.find('[data-cropper-action]').off('click.phpbb.avatars'); - this.image.off('crop.phpbb.avatars'); - - this.$data.val(''); - this.$buttons.hide(); - - if (this.cropper !== null) { - this.cropper.destroy(); - } - }, - - /** - * Bind a function to the avatar driver element. - * - * If a file was chosen and it is a valid image file, the cropper is initialised. - * Otherwise the cropper is destroyed. - */ - bindInput: function() { - this.$input.on('change', function() { - const fileReader = new FileReader(); + // Correctly position the cropper buttons + this.$buttons.appendTo(this.$box); - if (this.files[0].type.match('image.*')) { - fileReader.readAsDataURL(this.files[0]); + this.image = this.$box.children('img'); - fileReader.onload = function() { - phpbb.avatars.image.cropper('destroy').attr('src', this.result).addClass('avatar'); - phpbb.avatars.initCropper(); - phpbb.avatars.initButtons(); - }; - } else { - phpbb.avatars.destroy(); + this.bindInput(); + this.bindSelect(); + }, + + /** + * Destroy (undo) any initialisation. + */ + destroy() { + this.$buttons.find('[data-cropper-action]').off('click.phpbb.avatars'); + this.image.off('crop.phpbb.avatars'); + + this.$data.val(''); + this.$buttons.hide(); + + if (this.cropper !== null) { + this.cropper.destroy(); } - }); - }, + }, - /** - * Bind a function to all the cropper