click_action.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 {StateService as UiStateService} from '@uirouter/angularjs';
  18. import {UriService} from '../services/uri';
  19. import {WebClientService} from '../services/webclient';
  20. export default [
  21. '$timeout',
  22. '$state',
  23. 'UriService',
  24. 'WebClientService',
  25. function(
  26. $timeout: ng.ITimeoutService,
  27. $state: UiStateService,
  28. uriService: UriService,
  29. webClientService: WebClientService,
  30. ) {
  31. const validateThreemaId = (id: string): boolean => {
  32. return id !== undefined && id !== null && /^[0-9A-Z]{8}/.test(id);
  33. };
  34. const viewReceiver = (receiver: threema.Receiver) => {
  35. return function(e: Event) {
  36. if (!receiver) {
  37. return false;
  38. }
  39. $state.go('messenger.home.detail', receiver);
  40. };
  41. };
  42. const addAction = (params) => {
  43. return function(e: Event) {
  44. if (!validateThreemaId(params.id)) {
  45. return false;
  46. }
  47. // Verify the receiver already exists
  48. const contactReceiver = webClientService.contacts.get(params.id);
  49. if (contactReceiver) {
  50. return viewReceiver(contactReceiver)(e);
  51. }
  52. $state.go('messenger.home.create', {
  53. type: 'contact',
  54. initParams: {
  55. identity: params.id,
  56. },
  57. });
  58. };
  59. };
  60. const composeAction = (params) => {
  61. return function(e: Event) {
  62. if (!validateThreemaId(params.id)) {
  63. return false;
  64. }
  65. const text = params.text || '';
  66. $state.go('messenger.home.conversation', {
  67. type: 'contact',
  68. id: params.id,
  69. initParams: {
  70. text: text,
  71. },
  72. });
  73. };
  74. };
  75. const getThreemaActionHandler = (name: string) => {
  76. switch (name.toLowerCase()) {
  77. case 'add':
  78. return addAction;
  79. case 'compose':
  80. return composeAction;
  81. default:
  82. return null;
  83. }
  84. };
  85. return {
  86. restrict: 'A',
  87. scope: {},
  88. link(scope, el, attrs) {
  89. $timeout(() => {
  90. // tslint:disable-next-line: prefer-for-of (see #98)
  91. for (let i = 0; i < el[0].childNodes.length; i++) {
  92. const node: HTMLElement = el[0].childNodes[i];
  93. if (node.nodeType === Node.ELEMENT_NODE) {
  94. switch ( node.tagName.toLowerCase()) {
  95. case 'a':
  96. const link = (node as HTMLElement).innerText;
  97. if (link !== undefined && link.toLowerCase().startsWith('threema://')) {
  98. const matches = (/\bthreema:\/\/([a-z]+)\?([^\s]+)\b/gi).exec(link);
  99. if (matches !== null) {
  100. const handler = getThreemaActionHandler(matches[1]);
  101. const params = uriService.parseQueryParams(matches[2]);
  102. if (handler !== null && params !== null) {
  103. node.addEventListener('click', handler(params));
  104. }
  105. }
  106. }
  107. break;
  108. case 'span':
  109. // Support only id mentions (not all or me)
  110. const mentionCssClass = 'mention id';
  111. const cls = node.getAttribute('class');
  112. // Solved with the css classes, because angularJS removes
  113. // all other attributes from the DOMElement
  114. if (cls.substr(0, mentionCssClass.length) === mentionCssClass) {
  115. // Hack to extract the identity from class name
  116. const identity = cls.substring(mentionCssClass.length).trim();
  117. if (validateThreemaId(identity)) {
  118. const contactReceiver = webClientService.contacts.get(identity);
  119. node.addEventListener('click', viewReceiver(contactReceiver));
  120. node.setAttribute('class', cls + ' link');
  121. node.setAttribute('title', contactReceiver.displayName);
  122. }
  123. }
  124. default:
  125. // ignore
  126. }
  127. }
  128. }
  129. }, 0);
  130. },
  131. };
  132. },
  133. ];