Skip to content

Commit 926ef61

Browse files
authored
1 parent 2535e0c commit 926ef61

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

utilities.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Utilities for undercards.js
2+
function debug() {
3+
if (localStorage.getItem("debugging") !== "true") return;
4+
console.log.apply(console, arguments);
5+
}
6+
var fn = { // Not used
7+
each: function(o, f, t) {
8+
if (!o) return;
9+
Object.keys(o).forEach(function(x) {
10+
if (!o[x]) return;
11+
f.call(t, o[x], x, o); // "this", value, key, object
12+
});
13+
},
14+
};
15+
class Hotkey {
16+
constructor(name) {
17+
this.name = name;
18+
this.keys = [];
19+
this.clicks = [];
20+
this.fn = null;
21+
}
22+
23+
_has(x, a) {
24+
var h = false;
25+
a.some(function (v, i) {
26+
//if (v === x) h = i;
27+
return h = v === x;
28+
});
29+
return h;
30+
}
31+
32+
_del(x, a) {
33+
a.some(function (v, i) {
34+
if (x === v) {
35+
a.splice(i, 1);
36+
return true;
37+
}
38+
return false;
39+
});
40+
}
41+
42+
bindKey(x) {
43+
if (!this.keybound(x)) {
44+
this.keys.push(x);
45+
}
46+
return this;
47+
}
48+
49+
unbindKey(x) {
50+
this._del(x, this.keys);
51+
return this;
52+
}
53+
54+
keybound(x) {
55+
return this._has(x, this.keys);
56+
}
57+
58+
bindClick(x) {
59+
if (!this.clickbound(x)) {
60+
this.clicks.push(x);
61+
}
62+
return this;
63+
}
64+
65+
unbindClick(x) {
66+
this._del(x, this.clicks);
67+
return this;
68+
}
69+
70+
clickbound(x) {
71+
return this._has(x, this.clicks);
72+
}
73+
74+
run(x) {
75+
if (typeof x === "function") { // Set the function
76+
this.fn = x;
77+
return this; // Allow inline constructing
78+
} else if (this.fn) { // All clear (x is the event)
79+
this.fn(x);
80+
}
81+
}
82+
83+
toString() {
84+
return `${this.name || "Hotkey"}: Bind{Keys:${JSON.stringify(this.keys)}, Clicks:${JSON.stringify(this.clicks)}}, FN:${this.fn!==null}`;
85+
}
86+
}

0 commit comments

Comments
 (0)