filters.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 {filter} from './helpers';
  18. import {MimeService} from './services/mime';
  19. import {WebClientService} from './services/webclient';
  20. angular.module('3ema.filters', [])
  21. /**
  22. * Escape HTML by replacing special characters with HTML entities.
  23. */
  24. .filter('escapeHtml', function() {
  25. const map = {
  26. '&': '&amp;',
  27. '<': '&lt;',
  28. '>': '&gt;',
  29. '"': '&quot;',
  30. "'": '&#039;',
  31. };
  32. return (text: string) => {
  33. if (text === undefined || text === null) {
  34. text = '';
  35. }
  36. return text.replace(/[&<>"']/g, (m) => map[m]);
  37. };
  38. })
  39. /**
  40. * Convert newline characters with a <br> tag.
  41. */
  42. .filter('nlToBr', function() {
  43. return (text, enabled: boolean) => {
  44. if (enabled) {
  45. text = text.replace(/\n/g, '<br>');
  46. }
  47. return text;
  48. };
  49. })
  50. /**
  51. * Replace a undefined/null or empty string with a placeholder
  52. */
  53. .filter('emptyToPlaceholder', function() {
  54. return (text, placeholder: string = '-') => {
  55. if (text === null || text === undefined || text.trim().length === 0) {
  56. return placeholder;
  57. }
  58. return text;
  59. };
  60. })
  61. /**
  62. * Convert links in text to <a> tags.
  63. */
  64. .filter('linkify', function() {
  65. const autolinker = new Autolinker({
  66. // Open links in new window
  67. newWindow: true,
  68. // Don't strip protocol prefix
  69. stripPrefix: false,
  70. // Don't truncate links
  71. truncate: 99999,
  72. // Add class name to linked links
  73. className: 'autolinked',
  74. // Link urls
  75. urls: true,
  76. // Link e-mails
  77. email: true,
  78. // Don't link phone numbers (doesn't work reliably)
  79. phone: false,
  80. // Don't link twitter handles
  81. twitter: false,
  82. // Don't link hashtags
  83. hashtag: false,
  84. });
  85. return (text) => autolinker.link(text);
  86. })
  87. /**
  88. * Convert emoji unicode characters to images.
  89. * Reference: http://git.emojione.com/demos/latest/index.html#js
  90. *
  91. * Set the `imgTag` parameter to `true` to use inline PNGs instead of sprites.
  92. */
  93. .filter('emojify', function() {
  94. return function(text, imgTag = false, greedyMatch = true) {
  95. if (text !== null) {
  96. emojione.sprites = imgTag !== true;
  97. emojione.emojiSize = '32';
  98. emojione.imagePathPNG = 'img/e1/';
  99. emojione.greedyMatch = greedyMatch;
  100. return emojione.unicodeToImage(text);
  101. } else {
  102. return text;
  103. }
  104. };
  105. })
  106. /**
  107. * Convert markdown elements to html elements
  108. */
  109. .filter('markify', function() {
  110. return function(text) {
  111. if (text !== null) {
  112. text = text.replace(/\B\*([^\r\n]+?)\*\B/g, '<span class="text-bold">$1</span>');
  113. text = text.replace(/\b_([^\r\n]+?)_\b/g, '<span class="text-italic">$1</span>');
  114. text = text.replace(/\B~([^\r\n]+?)~\B/g, '<span class="text-strike">$1</span>');
  115. return text;
  116. }
  117. return text;
  118. };
  119. })
  120. /**
  121. * Reverse an array.
  122. */
  123. .filter('reverse', function() {
  124. return (list) => list.slice().reverse();
  125. })
  126. /**
  127. * Return whether receiver has corresponding data.
  128. */
  129. .filter('hasData', function() {
  130. return function(obj, receivers) {
  131. const valid = (receiver) => receivers.get(receiver.type).has(receiver.id);
  132. return filter(obj, valid);
  133. };
  134. })
  135. /**
  136. * Return whether item has a corresponding contact.
  137. */
  138. .filter('hasContact', function() {
  139. return function(obj, contacts) {
  140. const valid = (item) => contacts.has(item.id);
  141. return filter(obj, valid);
  142. };
  143. })
  144. /**
  145. * Return whether contact is not me.
  146. */
  147. .filter('isNotMe', ['WebClientService', function(webClientService: WebClientService) {
  148. return function(obj: threema.Receiver) {
  149. const valid = (contact: threema.Receiver) => contact.id !== webClientService.receivers.me.id;
  150. return filter(obj, valid);
  151. };
  152. }])
  153. /**
  154. * Filter for duration formatting.
  155. */
  156. .filter('duration', function() {
  157. return function(seconds) {
  158. const left = Math.floor(seconds / 60);
  159. const right = seconds % 60;
  160. const padLeft = left < 10 ? '0' : '';
  161. const padRight = right < 10 ? '0' : '';
  162. return padLeft + left + ':' + padRight + right;
  163. };
  164. })
  165. .filter('bufferToUrl', ['$sce', '$log', function($sce, $log) {
  166. const logTag = '[filters.bufferToUrl]';
  167. return function(buffer: ArrayBuffer, mimeType: string, trust: boolean = true) {
  168. if (!buffer) {
  169. $log.error(logTag, 'Could not apply bufferToUrl filter: buffer is', buffer);
  170. return '';
  171. }
  172. let binary = '';
  173. const bytes = new Uint8Array(buffer);
  174. const len = bytes.byteLength;
  175. for (let i = 0; i < len; i++) {
  176. binary += String.fromCharCode(bytes[i]);
  177. }
  178. const uri = 'data:' + mimeType + ';base64,' + btoa(binary);
  179. if (trust) {
  180. return $sce.trustAsResourceUrl(uri);
  181. } else {
  182. return uri;
  183. }
  184. };
  185. }])
  186. .filter('mapLink', function() {
  187. return function(location: threema.LocationInfo) {
  188. return 'https://www.openstreetmap.org/?mlat='
  189. + location.lat + '&mlon='
  190. + location.lon;
  191. };
  192. })
  193. /**
  194. * Convert message state to material icon class.
  195. */
  196. .filter('messageStateIcon', function() {
  197. return (message: threema.Message) => {
  198. if (!message) {
  199. return '';
  200. }
  201. if (!message.isOutbox) {
  202. switch (message.state) {
  203. case 'user-ack':
  204. return 'thumb_up';
  205. case 'user-dec':
  206. return 'thumb_down';
  207. default:
  208. return 'reply';
  209. }
  210. }
  211. switch (message.state) {
  212. case 'pending':
  213. case 'sending':
  214. return 'file_upload';
  215. case 'sent':
  216. return 'email';
  217. case 'delivered':
  218. return 'move_to_inbox';
  219. case 'read':
  220. return 'visibility';
  221. case 'send-failed':
  222. return 'report_problem';
  223. case 'user-ack':
  224. return 'thumb_up';
  225. case 'user-dec':
  226. return 'thumb_down';
  227. default:
  228. return '';
  229. }
  230. };
  231. })
  232. .filter('fileSize', function() {
  233. return (size: number) => {
  234. if (!size) {
  235. return '';
  236. }
  237. let i = Math.floor( Math.log(size) / Math.log(1024) );
  238. let x = (size / Math.pow(1024, i)).toFixed(2);
  239. return (x + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]);
  240. };
  241. })
  242. /**
  243. * Return the MIME type label.
  244. */
  245. .filter('mimeTypeLabel', ['MimeService', function(mimeService: MimeService) {
  246. return (mimeType: string) => mimeService.getLabel(mimeType);
  247. }])
  248. /**
  249. * Return the MIME type icon URL.
  250. */
  251. .filter('mimeTypeIcon', ['MimeService', function(mimeService: MimeService) {
  252. return (mimeType: string) => mimeService.getIconUrl(mimeType);
  253. }])
  254. /**
  255. * Convert ID-Array to (Display-)Name-String, separated by ','
  256. */
  257. .filter('idsToNames', ['WebClientService', function (webClientService: WebClientService) {
  258. return(ids: string[]) => {
  259. let names: string[] = [];
  260. for (let id of ids) {
  261. this.contactReceiver = webClientService.contacts.get(id);
  262. names.push(this.contactReceiver.displayName);
  263. }
  264. return names.join(', ');
  265. };
  266. }])
  267. ;