| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function reloadApp() { | 
					
						
							|  |  |  |     window.location.reload(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function parseDate(str) { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         return new Date(Date.parse(str)); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     catch (e) { | 
					
						
							|  |  |  |         throw new Error("Can't parse date from " + str + ": " + e.stack); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function padNum(num) { | 
					
						
							|  |  |  |     return (num <= 9 ? "0" : "") + num; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function formatTime(date) { | 
					
						
							|  |  |  |     return padNum(date.getHours()) + ":" + padNum(date.getMinutes()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function formatTimeWithSeconds(date) { | 
					
						
							|  |  |  |     return padNum(date.getHours()) + ":" + padNum(date.getMinutes()) + ":" + padNum(date.getSeconds()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 22:03:30 +01:00
										 |  |  | // this is producing local time!
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function formatDate(date) { | 
					
						
							| 
									
										
										
										
											2018-08-15 08:48:16 +02:00
										 |  |  | //    return padNum(date.getDate()) + ". " + padNum(date.getMonth() + 1) + ". " + date.getFullYear();
 | 
					
						
							|  |  |  |     // instead of european format we'll just use ISO as that's pretty unambiguous
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return formatDateISO(date); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-23 12:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 22:03:30 +01:00
										 |  |  | // this is producing local time!
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function formatDateISO(date) { | 
					
						
							|  |  |  |     return date.getFullYear() + "-" + padNum(date.getMonth() + 1) + "-" + padNum(date.getDate()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-23 12:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function formatDateTime(date) { | 
					
						
							|  |  |  |     return formatDate(date) + " " + formatTime(date); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-28 19:00:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function now() { | 
					
						
							|  |  |  |     return formatTimeWithSeconds(new Date()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-22 23:18:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function isElectron() { | 
					
						
							| 
									
										
										
										
											2019-02-09 19:25:55 +01:00
										 |  |  |     return !!(window && window.process && window.process.type); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-02 14:04:53 +01:00
										 |  |  | function isMac() { | 
					
						
							|  |  |  |     return navigator.platform.indexOf('Mac') > -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function assertArguments() { | 
					
						
							|  |  |  |     for (const i in arguments) { | 
					
						
							|  |  |  |         if (!arguments[i]) { | 
					
						
							| 
									
										
										
										
											2018-08-06 22:29:03 +02:00
										 |  |  |             console.trace(`Argument idx#${i} should not be falsy: ${arguments[i]}`); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function escapeHtml(str) { | 
					
						
							|  |  |  |     return $('<div/>').text(str).html(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function stopWatch(what, func) { | 
					
						
							|  |  |  |     const start = new Date(); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const ret = await func(); | 
					
						
							| 
									
										
										
										
											2018-02-04 20:23:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:36:25 +01:00
										 |  |  |     const tookMs = Date.now() - start.getTime(); | 
					
						
							| 
									
										
										
										
											2018-02-04 20:23:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     console.log(`${what} took ${tookMs}ms`); | 
					
						
							| 
									
										
										
										
											2018-02-04 20:23:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-19 22:02:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function formatValueWithWhitespace(val) { | 
					
						
							|  |  |  |     return /[^\w_-]/.test(val) ? '"' + val + '"' : val; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-21 20:30:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  | function formatLabel(label) { | 
					
						
							|  |  |  |     let str = "@" + formatValueWithWhitespace(label.name); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |     if (label.value !== "") { | 
					
						
							|  |  |  |         str += "=" + formatValueWithWhitespace(label.value); | 
					
						
							| 
									
										
										
										
											2018-02-19 22:02:03 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getHost() { | 
					
						
							|  |  |  |     const url = new URL(window.location.href); | 
					
						
							|  |  |  |     return url.protocol + "//" + url.hostname + ":" + url.port; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function download(url) { | 
					
						
							|  |  |  |     if (isElectron()) { | 
					
						
							|  |  |  |         const remote = require('electron').remote; | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         remote.getCurrentWebContents().downloadURL(url); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     else { | 
					
						
							|  |  |  |         window.location.href = url; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-04 23:28:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function toObject(array, fn) { | 
					
						
							|  |  |  |     const obj = {}; | 
					
						
							| 
									
										
										
										
											2018-03-04 23:28:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     for (const item of array) { | 
					
						
							| 
									
										
										
										
											2018-03-25 23:25:17 -04:00
										 |  |  |         const [key, value] = fn(item); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:37:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 23:25:17 -04:00
										 |  |  |         obj[key] = value; | 
					
						
							| 
									
										
										
										
											2018-03-04 23:28:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return obj; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-12 23:27:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function randomString(len) { | 
					
						
							|  |  |  |     let text = ""; | 
					
						
							|  |  |  |     const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | 
					
						
							| 
									
										
										
										
											2018-03-12 23:27:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     for (let i = 0; i < len; i++) { | 
					
						
							|  |  |  |         text += possible.charAt(Math.floor(Math.random() * possible.length)); | 
					
						
							| 
									
										
										
										
											2018-03-12 23:27:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return text; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 19:49:33 -04:00
										 |  |  | function bindShortcut(keyboardShortcut, handler) { | 
					
						
							| 
									
										
										
										
											2018-12-24 18:39:31 +01:00
										 |  |  |     if (isDesktop()) { | 
					
						
							| 
									
										
										
										
											2019-01-09 21:42:16 +01:00
										 |  |  |         if (isMac()) { | 
					
						
							|  |  |  |             // use CMD (meta) instead of CTRL for all shortcuts
 | 
					
						
							|  |  |  |             keyboardShortcut = keyboardShortcut.replace("ctrl", "meta"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-24 18:39:31 +01:00
										 |  |  |         $(document).bind('keydown', keyboardShortcut, e => { | 
					
						
							|  |  |  |             handler(); | 
					
						
							| 
									
										
										
										
											2018-03-25 19:49:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-24 18:39:31 +01:00
										 |  |  |             e.preventDefault(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 19:49:33 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-24 10:10:36 +01:00
										 |  |  | function isMobile() { | 
					
						
							| 
									
										
										
										
											2019-01-14 23:50:45 +01:00
										 |  |  |     return window.device === "mobile" | 
					
						
							|  |  |  |         // window.device is not available in setup
 | 
					
						
							|  |  |  |         || (!window.device && /Mobi/.test(navigator.userAgent)); | 
					
						
							| 
									
										
										
										
											2018-12-24 10:10:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isDesktop() { | 
					
						
							| 
									
										
										
										
											2019-01-14 23:50:45 +01:00
										 |  |  |     return window.device === "desktop" | 
					
						
							|  |  |  |         // window.device is not available in setup
 | 
					
						
							|  |  |  |         || (!window.device && !/Mobi/.test(navigator.userAgent)); | 
					
						
							| 
									
										
										
										
											2018-12-24 10:10:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 21:53:09 +01:00
										 |  |  | // cookie code below works for simple use cases only - ASCII only
 | 
					
						
							|  |  |  | // not setting path so that cookies do not leak into other websites if multiplexed with reverse proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-29 00:09:16 +01:00
										 |  |  | function setCookie(name, value) { | 
					
						
							|  |  |  |     const date = new Date(Date.now() + 10 * 365 * 24 * 60 * 60 * 1000); | 
					
						
							|  |  |  |     const expires = "; expires=" + date.toUTCString(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 21:53:09 +01:00
										 |  |  |     document.cookie = name + "=" + (value || "")  + expires + ";"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function setSessionCookie(name, value) { | 
					
						
							|  |  |  |     document.cookie = name + "=" + (value || "") + ";"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getCookie(name) { | 
					
						
							|  |  |  |     const valueMatch = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)'); | 
					
						
							|  |  |  |     return valueMatch ? valueMatch[2] : null; | 
					
						
							| 
									
										
										
										
											2018-12-29 00:09:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 21:42:37 +01:00
										 |  |  | function getNoteTypeClass(type) { | 
					
						
							|  |  |  |     return "type-" + type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getMimeTypeClass(mime) { | 
					
						
							|  |  |  |     const semicolonIdx = mime.indexOf(';'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (semicolonIdx !== -1) { | 
					
						
							|  |  |  |         // stripping everything following the semicolon
 | 
					
						
							|  |  |  |         mime = mime.substr(0, semicolonIdx); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 'mime-' + mime.toLowerCase().replace(/[\W_]+/g,"-"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     reloadApp, | 
					
						
							|  |  |  |     parseDate, | 
					
						
							|  |  |  |     padNum, | 
					
						
							|  |  |  |     formatTime, | 
					
						
							|  |  |  |     formatTimeWithSeconds, | 
					
						
							|  |  |  |     formatDate, | 
					
						
							|  |  |  |     formatDateISO, | 
					
						
							|  |  |  |     formatDateTime, | 
					
						
							|  |  |  |     now, | 
					
						
							|  |  |  |     isElectron, | 
					
						
							| 
									
										
										
										
											2018-12-02 14:04:53 +01:00
										 |  |  |     isMac, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     assertArguments, | 
					
						
							|  |  |  |     escapeHtml, | 
					
						
							|  |  |  |     stopWatch, | 
					
						
							|  |  |  |     formatValueWithWhitespace, | 
					
						
							|  |  |  |     formatLabel, | 
					
						
							|  |  |  |     getHost, | 
					
						
							|  |  |  |     download, | 
					
						
							|  |  |  |     toObject, | 
					
						
							| 
									
										
										
										
											2018-03-25 19:49:33 -04:00
										 |  |  |     randomString, | 
					
						
							| 
									
										
										
										
											2018-12-24 10:10:36 +01:00
										 |  |  |     bindShortcut, | 
					
						
							|  |  |  |     isMobile, | 
					
						
							| 
									
										
										
										
											2018-12-29 00:09:16 +01:00
										 |  |  |     isDesktop, | 
					
						
							| 
									
										
										
										
											2019-01-28 21:42:37 +01:00
										 |  |  |     setCookie, | 
					
						
							| 
									
										
										
										
											2019-03-13 21:53:09 +01:00
										 |  |  |     setSessionCookie, | 
					
						
							|  |  |  |     getCookie, | 
					
						
							| 
									
										
										
										
											2019-01-28 21:42:37 +01:00
										 |  |  |     getNoteTypeClass, | 
					
						
							|  |  |  |     getMimeTypeClass | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; |