Display "brief" datestamps in mailbox: date if not today, time if today.
[citadel.git] / webcit-ng / static / js / util.js
1 // Copyright (c) 2016-2022 by the citadel.org team
2 //
3 // This program is open source software.  Use, duplication, or
4 // disclosure are subject to the GNU General Public License v3.
5
6
7 // Function to encode data in quoted-printable format
8 // Written by Theriault and Brett Zamir [https://locutus.io/php/quoted_printable_encode/]
9 function quoted_printable_encode(str) {
10         const hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
11         const RFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm
12         const RFC2045Encode1OUT = function (sMatch) {
13                 // Encode space before CRLF sequence to prevent spaces from being stripped
14                 // Keep hard line breaks intact; CRLF sequences
15                 if (sMatch.length > 1) {
16                         return sMatch.replace(' ', '=20');
17                 }
18                 // Encode matching character
19                 const chr = sMatch.charCodeAt(0);
20                 return '=' + hexChars[((chr >>> 4) & 15)] + hexChars[(chr & 15)];
21         }
22         // Split lines to 75 characters; the reason it's 75 and not 76 is because softline breaks are
23         // preceeded by an equal sign; which would be the 76th character. However, if the last line/string
24         // was exactly 76 characters, then a softline would not be needed. PHP currently softbreaks
25         // anyway; so this function replicates PHP.
26         const RFC2045Encode2IN = /.{1,72}(?!\r\n)[^=]{0,3}/g
27         const RFC2045Encode2OUT = function (sMatch) {
28                 if (sMatch.substr(sMatch.length - 2) === '\r\n') {
29                         return sMatch;
30                 }
31                 return sMatch + '=\r\n';
32         }
33         str = str.replace(RFC2045Encode1IN, RFC2045Encode1OUT).replace(RFC2045Encode2IN, RFC2045Encode2OUT);
34         // Strip last softline break
35         return str.substr(0, str.length - 3)
36 }
37
38
39 // Generate a random string of the specified length
40 // Useful for generating one-time-use div names
41 function randomString() {
42         return Math.random().toString(36).replace('0.','ctdl_' || '');
43 }
44
45
46 // string escape for html display
47 function escapeHTML(text) {
48         'use strict';
49         return text.replace(/[\"&<>]/g, function (a) {
50                 return {
51                         '"': '&quot;',
52                         '&': '&amp;',
53                         '<': '&lt;',
54                         '>': '&gt;'
55                 }[a];
56         });
57 }
58
59
60 // string escape for html display
61 function escapeHTMLURI(text) {
62         'use strict';
63         return text.replace(/./g, function (a) {
64                 return '%' + a.charCodeAt(0).toString(16);
65         });
66 }
67
68
69 // string escape for JavaScript string
70 //
71 function escapeJS(text) {
72         'use strict';
73         return text.replace(/[\"\']/g, function (a) {
74                 return '\\' + a ;
75         });
76 }
77
78
79 // Convert a UNIX timestamp to the browser's local time
80 // See also: https://timestamp.online/article/how-to-convert-timestamp-to-datetime-in-javascript
81 // format should be: 0=full (date+time), 1=brief (date if not today, time if today)
82 function string_timestamp(timestamp, format) {
83         var ts = new Date(timestamp * 1000);
84         if (format == 1) {
85                 var now_ts = new Date(Date.now());
86                 if (ts.toLocaleDateString() == now_ts.toLocaleDateString()) {
87                         return(ts.toLocaleTimeString());
88                 }
89                 else {
90                         return(ts.toLocaleDateString());
91                 }
92         }
93         else {
94                 return(ts.toLocaleString());
95         }
96 }
97
98
99 // An old version of string_timestamp() did it the hard way.
100 // It used https://gist.github.com/kmaida/6045266 as a reference.
101 // check git history prior to 2022-jul-03 if you want to see it.
102
103
104 // Get the value of a cookie from the HTTP session
105 // Shamelessly swiped from https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript
106 const getCookieValue = (name) => (
107         document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || ''
108 )
109
110