(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', /** * 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(); if (this.files[0].type.match('image.*')) { fileReader.readAsDataURL(this.files[0]); fileReader.onload = function() { phpbb.avatars.image.cropper('destroy').attr('src', this.result).addClass('avatar'); phpbb.avatars.initCropper(); phpbb.avatars.initButtons(); }; } else { phpbb.avatars.destroy(); } }); }, /** * Bind a function to all the cropper