avatar.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. '$filter',
  22. 'WebClientService',
  23. function($rootScope: ng.IRootScopeService,
  24. $timeout: ng.ITimeoutService,
  25. $filter: ng.IFilterService,
  26. webClientService: WebClientService) {
  27. return {
  28. restrict: 'E',
  29. scope: {},
  30. bindToController: {
  31. type: '=eeeType',
  32. receiver: '=eeeReceiver',
  33. resolution: '=eeeResolution',
  34. },
  35. controllerAs: 'ctrl',
  36. controller: [function() {
  37. this.highResolution = this.resolution === 'high';
  38. this.isLoading = this.highResolution;
  39. this.backgroundColor = this.receiver.color;
  40. let loadingPromise: ng.IPromise<any> = null;
  41. this.avatarClass = () => {
  42. return 'avatar-' + this.resolution + (this.isLoading ? ' is-loading' : '');
  43. };
  44. this.avatarExists = () => {
  45. if (this.receiver.avatar === undefined
  46. || this.receiver.avatar === null
  47. || this.receiver.avatar[this.resolution] === undefined
  48. || this.receiver.avatar[this.resolution] === null) {
  49. return false;
  50. }
  51. this.isLoading = false;
  52. // Reset background color
  53. this.backgroundColor = null;
  54. return true;
  55. };
  56. /**
  57. * Return path to the default avatar.
  58. */
  59. this.getDefaultAvatarUri = (type: threema.ReceiverType, highResolution: boolean) => {
  60. switch (type) {
  61. case 'group':
  62. return highResolution ? 'img/ic_group_picture_big.png' : 'img/ic_group_t.png';
  63. case 'contact':
  64. case 'me':
  65. return highResolution ? 'img/ic_contact_picture_big.png' : 'img/ic_contact_picture_t.png';
  66. case 'distributionList':
  67. return highResolution ? 'img/ic_distribution_list_t.png' : 'img/ic_distribution_list_t.png';
  68. }
  69. return null;
  70. };
  71. this.avatarToUri = (data: ArrayBuffer) => {
  72. if (data === null || data === undefined) {
  73. return '';
  74. }
  75. return ($filter('bufferToUrl') as (data: ArrayBuffer, mime: string) => string)(data, 'image/png');
  76. };
  77. this.getAvatarUri = () => {
  78. if (this.avatarExists()) {
  79. return this.avatarToUri(this.receiver.avatar[this.resolution]);
  80. } else if (this.highResolution
  81. && this.receiver.avatar !== undefined
  82. && this.receiver.avatar.low !== undefined) {
  83. return this.avatarToUri(this.receiver.avatar.low);
  84. }
  85. return this.getDefaultAvatarUri(this.type, this.highResolution);
  86. };
  87. this.requestAvatar = (inView: boolean) => {
  88. if (this.avatarExists()) {
  89. // do not request
  90. return;
  91. }
  92. if (inView) {
  93. if (loadingPromise === null) {
  94. // Do not wait on high resolution avatar
  95. const loadingTimeout = this.highResolution ? 0 : 500;
  96. loadingPromise = $timeout(() => {
  97. // show loading only on high res images!
  98. webClientService.requestAvatar({
  99. type: this.type,
  100. id: this.receiver.id,
  101. } as threema.Receiver, this.highResolution).then((avatar) => {
  102. $rootScope.$apply(() => {
  103. this.isLoading = false;
  104. });
  105. }).catch(() => {
  106. $rootScope.$apply(() => {
  107. this.isLoading = false;
  108. });
  109. });
  110. }, loadingTimeout);
  111. }
  112. } else if (loadingPromise !== null) {
  113. // Cancel pending avatar loading
  114. $timeout.cancel(loadingPromise);
  115. loadingPromise = null;
  116. }
  117. };
  118. this.showWorkIndicator = () => {
  119. return this.type === 'contact'
  120. && !this.highResolution
  121. && (this.receiver as threema.ContactReceiver).identityType === threema.IdentityType.Work;
  122. };
  123. this.showBlocked = () => {
  124. return this.type === 'contact'
  125. && !this.highResolution
  126. && (this.receiver as threema.ContactReceiver).isBlocked;
  127. };
  128. }],
  129. template: `
  130. <div class="avatar" ng-class="ctrl.avatarClass()">
  131. <div class="avatar-loading" ng-if="ctrl.isLoading">
  132. <span></span>
  133. </div>
  134. <div class="work-indicator" ng-if="ctrl.showWorkIndicator()"
  135. translate-attr="{'aria-label': 'messenger.THREEMA_WORK_CONTACT',
  136. 'title': 'messenger.THREEMA_WORK_CONTACT'}">
  137. <img src="img/ic_work_round.svg" alt="Threema Work user">
  138. </div>
  139. <div class="blocked-indicator" ng-if="ctrl.showBlocked()"
  140. translate-attr="{'aria-label': 'messenger.THREEMA_BLOCKED_RECEIVER',
  141. 'title': 'messenger.THREEMA_BLOCKED_RECEIVER'}">
  142. <img src="img/ic_blocked_24px.svg" alt="blocked icon"/>
  143. </div>
  144. <img
  145. ng-class="ctrl.avatarClass()"
  146. ng-style="{ 'background-color': ctrl.backgroundColor }"
  147. ng-src="{{ ctrl.getAvatarUri() }}"
  148. in-view="ctrl.requestAvatar($inview)"/>
  149. </div>
  150. `,
  151. };
  152. },
  153. ];