/** * This file is part of Threema Web. * * Threema Web is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero * General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Threema Web. If not, see . */ // tslint:disable:max-line-length import {bufferToUrl} from '../helpers'; import {LogService} from '../services/log'; import {WebClientService} from '../services/webclient'; /** * Support uploading and resizing avatar */ export default [ '$rootScope', '$mdDialog', 'LogService', 'WebClientService', function($rootScope: ng.IRootScopeService, $mdDialog: ng.material.IDialogService, logService: LogService, webClientService: WebClientService) { const log = logService.getLogger('AvatarArea-C'); return { restrict: 'EA', scope: true, bindToController: { onChange: '=', loadAvatar: '=', enableClear: '=?', color: '=?', }, controllerAs: 'ctrl', controller: [function() { this.isLoading = false; this.avatar = null; // String const avatarFormat = webClientService.appCapabilities.imageFormat.avatar; this.$onInit = function() { this.setAvatar = (avatarBytes: ArrayBuffer) => { this.avatar = (avatarBytes === null) ? null : bufferToUrl(avatarBytes, avatarFormat, log); }; this.imageChanged = (image: ArrayBuffer, notify = true) => { this.isLoading = true; if (notify === true && this.onChange !== undefined) { this.onChange(image); } this.setAvatar(image); this.isLoading = false; }; if (this.loadAvatar !== undefined) { this.isLoading = true; (this.loadAvatar as Promise) .then((image: ArrayBuffer) => { $rootScope.$apply(() => { this.setAvatar(image); this.isLoading = false; }); }) .catch(() => { $rootScope.$apply(() => { this.isLoading = false; }); }); } this.delete = () => { this.imageChanged(null, true); }; // show editor in a dialog this.modify = (ev) => { $mdDialog.show({ controllerAs: 'ctrl', controller: function() { this.avatar = null; this.apply = () => $mdDialog.hide(this.avatar); this.cancel = () => $mdDialog.cancel(); this.changeAvatar = (image: ArrayBuffer) => this.avatar = image; }, template: `

messenger.UPLOAD_AVATAR

common.CANCEL common.OK
`, parent: angular.element(document.body), targetEvent: ev, clickOutsideToClose: true, }) .then((newImage: ArrayBuffer) => { // update image only if a image was set or if enable clear is true if (this.enableClear === true || newImage !== null) { this.imageChanged(newImage, true); } }, () => null); }; }; }], template: `
common.DELETE messenger.UPLOAD_AVATAR
`, }; }, ];