message_icon.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 {hasValue} from '../helpers';
  18. export default [
  19. function() {
  20. return {
  21. restrict: 'EA',
  22. scope: {},
  23. bindToController: {
  24. message: '=eeeMessage',
  25. },
  26. link: function(scope, elem, attrs) {
  27. scope.$watch(
  28. () => scope.ctrl.message.id,
  29. (newId, oldId) => {
  30. // Register for message changes. When the ID changes, update the icon.
  31. // This prevents processing the message more than once.
  32. if (hasValue(newId) && newId !== oldId) {
  33. scope.ctrl.update();
  34. }
  35. },
  36. );
  37. },
  38. controllerAs: 'ctrl',
  39. controller: [function() {
  40. // Return icon depending on message type.
  41. const getIcon = (msgType: threema.MessageType) => {
  42. switch (msgType) {
  43. case 'image':
  44. return 'ic_image_24px.svg';
  45. case 'video':
  46. return 'ic_movie_24px.svg';
  47. case 'audio':
  48. return 'ic_mic_24px.svg';
  49. case 'location':
  50. return 'ic_location_on_24px.svg';
  51. case 'file':
  52. if (this.message.file.type === 'image/gif') {
  53. return 'ic_image_24px.svg';
  54. }
  55. return 'ic_insert_drive_file_24px.svg';
  56. case 'ballot':
  57. return 'ic_poll_24px.svg';
  58. default:
  59. return null;
  60. }
  61. };
  62. this.update = () => {
  63. this.icon = getIcon(this.message.type);
  64. this.altText = this.message.type + ' icon';
  65. };
  66. this.$onInit = function() {
  67. this.update();
  68. };
  69. }],
  70. template: `
  71. <img ng-if="ctrl.icon !== null" ng-src="img/{{ ctrl.icon }}" alt="{{ ctrl.altText }}">
  72. `,
  73. };
  74. },
  75. ];