-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
117 lines (106 loc) · 2.88 KB
/
util.js
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
/**
* Shortcut for document.getElementById()
*/
function E(id) {
return document.getElementById(id);
}
/**
* createElement()
* @param parentNode {DOMElement} where to add the new element. May be null.
* @param tagname {String} <tagname>
* @param classname {String} class="classname"
* @param attributes {Array of String}
*/
function cE(parentNode, tagname, classname, attributes) {
var el = document.createElement(tagname);
if (classname) {
el.classList.add(classname);
}
for (var name in attributes) {
el.setAttribute(name, attributes[name]);
}
if (parentNode) {
parentNode.appendChild(el);
}
return el;
}
/**
* createTextNode()
*/
function cTN(text) {
return document.createTextNode(text);
}
/**
* Like parentElement.insertBefore(newElement, insertBefore), just insert
* after some other element.
*
* @param parentElement {node} Insert |newElement| as child of |parentElement|.
* @param newElement {node} new node that you want to insert
* @param insertAfterInfo {String or DOMElement} Element or ID of the node
* that should be before (left to) |newElement|.
* This must be a child of |parentElement|.
* If it does not exist, the |newElement| is added to the end.
* @returns {node} the node that was inserted
*/
function insertAfter(parentElement, newElement, insertAfterInfo) {
var afterEl = null;
if (insertAfterInfo) {
if (typeof(insertAfterInfo) == "string") {
afterEl = parentElement.ownerDocument.getElementById(insertAfterInfo);
} else if (insertAfterInfo.ownerDocument) {
afterEl = insertAfterInfo;
} else {
throw new NotReached("insertAfterInfo has the wrong type");
}
if (afterEl.parentNode != parentElement) {
throw new NotReached("insertAfterInfo has the wrong parent element");
}
}
if (afterEl && afterEl.nextSibling) {
parentElement.insertBefore(newElement, afterEl.nextSibling);
} else {
parentElement.appendChild(newElement);
}
return newElement;
}
/**
* Turns a DOM |NodeList| into a JS array that you can |.forEach| on
* - convenience
* - makes a copy, which is needed when you remove the elements
*/
function nodeListToArray(nodeList)
{
var result = [];
for (var i = 0, l = nodeList.length; i < l; i++) {
result.push(nodeList.item(i));
}
return result;
}
function cleanElement(el)
{
while (el.hasChildNodes()) {
el.removeChild(el.firstChild);
}
}
function removeElement(el)
{
if (el && el.parentNode) {
el.parentNode.removeChild(el);
}
}
/**
* Helper function that handles detecting the return key in a
* web page and calls the appropriate function
*/
function hookupReturnKey(element, onReturnKeypressCallback) {
element.addEventListener("keyup", function(event) {
if (event.keyCode == 13) {
onReturnKeypressCallback(event);
}
}, false);
}
function assert(test, errorMsg) {
if (!test) {
throw new Error(errorMsg);
}
}