-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
132 lines (112 loc) · 4.14 KB
/
Copy pathutils.js
File metadata and controls
132 lines (112 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
// Source: https://stackoverflow.com/questions/2998784/how-to-output-numbers-with-leading-zeros-in-javascript
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
// Source: https://www.w3schools.com/js/js_cookies.asp
// Modified
function setCookie(cname, cvalue, exdays, plain = false) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
let cookieString = cname + "=" + (plain ? ("!" + cvalue) : window.btoa(cvalue)) + ";" + expires + ";samesite=lax;path=/";
//console.log("setting cookie: " + cookieString);
document.cookie = cookieString;
//cname + "=" + cvalue + ";" + expires + ";SameSite:strict;path=/";
}
// Source: https://www.w3schools.com/js/js_cookies.asp
// Modified
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
//console.log("decoding " + c.substring(name.length, c.length));
let val = c.substring(name.length, c.length);
if (val.startsWith("!"))
return val.substring(1);
else
return window.atob(val);
}
}
return null;
}
function saveObject(name, value, exdays = 36500) {
setCookie(name, JSON.stringify(value), exdays);
}
function loadObject(name) {
let v = getCookie(name);
if (v != null)
return JSON.parse(v);
else
return null;
}
let instantiationIndex = 0;
function createTemplateInstance(template, container, activeOnAdd = false) {
let el;
if (typeof (template) == "string") {
el = $(`#${template}`)[0].cloneNode(true);
} else {
el = template.cloneNode(true);
}
let modifyNodes = $("[id]", el).toArray();
if (el.hasAttribute("id"))
modifyNodes.push(el);
for (let modifyNode of modifyNodes) {
let origID = modifyNode.getAttribute("id");
modifyNode.setAttribute("data-id", origID);
let newID = origID + "-" + instantiationIndex.toString();
instantiationIndex += 1;
modifyNode.setAttribute("id", newID);
modifyNode.id = newID;
$(`[marker-end=\"url(#${origID})\"]`).attr("marker-end", `url(#${newID})`);
$(`[href=\"#${origID}\"]`).attr("href", `#${newID}`);
$(`[for=\"${origID}\"]`).attr("for", `${newID}`);
}
if (!activeOnAdd)
el.classList.add("template-staging");
el.classList.remove("template");
if (container != null)
container.appendChild(el);
return el;
}
function activateTemplateInstance(el) {
el.classList.remove("template-staging");
}
// Source: https://gomakethings.com/automatically-expand-a-textarea-as-the-user-types-using-vanilla-javascript/
// Modified
var autoExpand = function (field, heightHolder) {
if (heightHolder != null)
heightHolder.style.height = field.style.height;
// Reset field height
field.style.height = 'inherit';
// Get the computed styles for the element
var computed = window.getComputedStyle(field);
// Calculate the height
var height = parseInt(computed.getPropertyValue('border-top-width'), 10)
+ parseInt(computed.getPropertyValue('padding-top'), 10)
+ field.scrollHeight
+ parseInt(computed.getPropertyValue('padding-bottom'), 10)
+ parseInt(computed.getPropertyValue('border-bottom-width'), 10);
field.style.height = height + 'px';
if (heightHolder != null)
heightHolder.style.height = "0px";
};
// Source: https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518
// by Mark Amery
/**
* @param {String} HTML representing a single element
* @return {Element}
*/
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}