avatar.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {WebClientService} from '../services/webclient';
  18. export default [
  19. '$rootScope',
  20. '$timeout',
  21. 'WebClientService',
  22. function($rootScope: ng.IRootScopeService,
  23. $timeout: ng.ITimeoutService,
  24. webClientService: WebClientService) {
  25. return {
  26. restrict: 'E',
  27. scope: {},
  28. bindToController: {
  29. type: '=eeeType',
  30. receiver: '=eeeReceiver',
  31. resolution: '=eeeResolution',
  32. },
  33. controllerAs: 'ctrl',
  34. controller: [function() {
  35. this.highResolution = this.resolution === 'high';
  36. this.isLoading = this.highResolution;
  37. this.backgroundColor = this.receiver.color;
  38. let loadingPromise: ng.IPromise<any> = null;
  39. this.avatarClass = () => {
  40. return 'avatar-' + this.resolution + (this.isLoading ? ' is-loading' : '');
  41. };
  42. this.avatarExists = () => {
  43. if (this.receiver.avatar === undefined
  44. || this.receiver.avatar[this.resolution] === undefined) {
  45. return false;
  46. }
  47. this.isLoading = false;
  48. // Reset background color
  49. this.backgroundColor = null;
  50. return true;
  51. };
  52. this.getAvatar = () => {
  53. if (this.avatarExists()) {
  54. return this.receiver.avatar[this.resolution];
  55. } else if (this.highResolution
  56. && this.receiver.avatar !== undefined
  57. && this.receiver.avatar.low !== undefined) {
  58. return this.receiver.avatar.low;
  59. }
  60. return webClientService.defaults.getAvatar(this.type, this.highResolution);
  61. };
  62. this.requestAvatar = (inView: boolean) => {
  63. if (this.avatarExists()) {
  64. // do not request
  65. return;
  66. }
  67. if (inView) {
  68. if (loadingPromise === null) {
  69. // Do not wait on high resolution avatar
  70. let loadingTimeout = this.highResolution ? 0 : 500;
  71. loadingPromise = $timeout(() => {
  72. // show loading only on high res images!
  73. webClientService.requestAvatar({
  74. type: this.type,
  75. id: this.receiver.id,
  76. } as threema.Receiver, this.highResolution).then((avatar) => {
  77. $rootScope.$apply(() => {
  78. this.isLoading = false;
  79. });
  80. }).catch(() => {
  81. $rootScope.$apply(() => {
  82. this.isLoading = false;
  83. });
  84. });
  85. }, loadingTimeout);
  86. }
  87. } else if (loadingPromise !== null) {
  88. // Cancel pending avatar loading
  89. $timeout.cancel(loadingPromise);
  90. loadingPromise = null;
  91. }
  92. };
  93. }],
  94. template: `
  95. <div class="avatar" ng-class="ctrl.avatarClass()">
  96. <div class="avatar-loading" ng-if="ctrl.isLoading">
  97. <span></span>
  98. </div>
  99. <img
  100. ng-class="ctrl.avatarClass()"
  101. ng-style="{ 'background-color': ctrl.backgroundColor }"
  102. ng-src="{{ ctrl.getAvatar() }}"
  103. in-view="ctrl.requestAvatar($inview)"/>
  104. </div>
  105. `,
  106. };
  107. },
  108. ];