forked from johreh/gloomycompanion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
106 lines (88 loc) · 2.22 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
function shuffle_list(l)
{
for (var i = 0; i < l.length-1; i++)
{
// Based on https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Implementation_errors
var switch_index = i + Math.floor(Math.random() * (l.length - i));
var tmp = l[switch_index];
l[switch_index] = l[i];
l[i] = tmp;
}
}
function toggle_class(element, class_name, enable_class)
{
if (enable_class)
{
element.classList.add(class_name);
}
else
{
element.classList.remove(class_name);
}
}
function remove_child(myNode)
{
while (myNode.firstChild)
{
myNode.removeChild(myNode.firstChild);
}
}
function create_input(type, name, value, text)
{
var input = document.createElement("input");
input.type = type;
input.name = name;
input.value = value;
var textnode = document.createTextNode(text);
var label = document.createElement("label");
label.appendChild(input);
label.appendChild(textnode);
return {'root': label, 'input': input};
}
function create_button(type, id, value)
{
var button = document.createElement("input");
button.type = type;
button.id = id;
button.value = value;
return button;
}
function dict_values(dict)
{
var values = [];
for (key in dict) {
values.push(dict[key]);
}
return values;
}
function concat_arrays(arrays)
{
return Array.prototype.concat.apply([], arrays);
}
function is_checked(input)
{
return (('checked' in input) ? input.checked : false);
}
function input_value(input)
{
return (('value' in input) ? input.value : '');
}
function remove_empty_strings(array)
{
return array.filter(Boolean);
}
function write_to_storage(name, value) {
try { localStorage.setItem(name, value); } catch (e) { console.error('Local storage is required'); return; }
// console.info("Local storage write:", name, value);
}
function get_from_storage(name) {
try { return localStorage.getItem(name); } catch (e) { console.error('Local storage is required'); return; }
}
function find_in_discard(discard, id) {
for (var i=0; i < discard.length; i++) {
if (discard[i].id === id) {
return discard[i];
}
}
return null;
}