-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimes.js
90 lines (75 loc) · 2.04 KB
/
times.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
function times_from_config(times) {
if (times.length == 0) {
for (var day = 0; day < 7; day++) {
for (var hour = 0; hour < 24; hour++) {
times.push(true);
}
}
}
return times;
}
function Config(times, onChange) {
function update_class(hour, active) {
var e = document.getElementById("hour-" + hour)
e.className = "hour";
if (active)
e.className += " active";
}
var onoff = true;
var edit = false;
function edit_start(hour) {
onoff = !times[hour];
edit_do(hour);
edit = true;
}
function edit_do(hour) {
times[hour] = onoff;
update_class(hour, onoff);
}
function edit_stop() {
edit = false;
}
function hour_from_elem(e) {
return parseInt(e.id.split("-")[1]);
}
function hourMouseDown(e) {
var hour = hour_from_elem(e.target);
edit_start(hour);
}
function hourMouseOver(e) {
if (!edit) return;
var hour = hour_from_elem(e.target);
edit_do(hour);
}
function bodyMouseUp() {
edit_stop();
onChange(times);
}
var root = document.getElementById('config')
console.log(times);
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
for (var hour = 0; hour < times.length; hour++) {
var active = times[hour];
if (hour % 24 == 0) {
var day_div = document.createElement("div");
var day_txt = document.createTextNode(days[Math.floor(hour/24)]);
day_div.appendChild(day_txt);
day_div.className = "day";
root.appendChild(day_div);
}
var hour_div = document.createElement("div");
var hour_txt = document.createTextNode(hour%24);
hour_div.appendChild(hour_txt);
hour_div.id = "hour-" + hour;
root.appendChild(hour_div);
update_class(hour, active);
hour_div.addEventListener("mousedown", hourMouseDown);
hour_div.addEventListener("mouseover", hourMouseOver);
if (hour % 24 == 23) {
var br = document.createElement("br");
root.appendChild(br);
}
}
document.addEventListener("mouseup", bodyMouseUp);
return {}
}