-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
70 lines (62 loc) · 1.79 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
// DOM util functions
var _ = {
// now $ can get elements by id
$: function(id) {
return document.getElementById(id);
},
all: function(selectors) {
return document.querySelectorAll(selectors);
},
removeClass: function(selectors, cssClass) {
var nodes = document.querySelectorAll(selectors);
var l = nodes.length;
for (i = 0; i < l; i++) {
var el = nodes[i];
// Bootstrap compatibility
el.className = el.className.replace(cssClass, "");
}
},
addClass: function(selectors, cssClass) {
var nodes = document.querySelectorAll(selectors);
var l = nodes.length;
for (i = 0; i < l; i++) {
var el = nodes[i];
// Bootstrap compatibility
if (-1 == el.className.indexOf(cssClass)) {
el.className += " " + cssClass;
}
}
},
show: function(selectors) {
this.removeClass(selectors, "hidden");
},
hide: function(selectors) {
this.addClass(selectors, "hidden");
},
toggle: function(selectors, cssClass) {
var cssClass = cssClass || "hidden";
var nodes = document.querySelectorAll(selectors);
var l = nodes.length;
for (i = 0; i < l; i++) {
var el = nodes[i];
// Bootstrap compatibility
if (-1 !== el.className.indexOf(cssClass)) {
el.className = el.className.replace(cssClass, "");
} else {
el.className += " " + cssClass;
}
}
}
};
// return a point on the same line and in the same direction as (currentX, currentY),
// 12000 units away from (centerX, centerY)
function getNewPoint(currentX, currentY, centerX, centerY) {
var distance = Math.sqrt(
Math.pow(currentX - centerX, 2) + Math.pow(currentY - centerY, 2)
);
var t = 20000 / distance;
return {
x: (1 - t) * centerX + t * currentX,
y: (1 - t) * centerY + t * currentY
};
}