-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
132 lines (107 loc) · 3.17 KB
/
script.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const containerHeight = 720;
const containerWidth = 600;
const minutesinDay = 60 * 12;
let collisions = [];
let width = [];
let leftOffSet = [];
// append one event to calendar
var createEvent = (height, top, left, units) => {
let node = document.createElement("DIV");
node.className = "event";
node.innerHTML =
"<span class='title'> Sample Item </span> \
<br><span class='location'> Sample Location </span>";
// Customized CSS to position each event
node.style.width = (containerWidth/units) + "px";
node.style.height = height + "px";
node.style.top = top + "px";
node.style.left = 100 + left + "px";
document.getElementById("events").appendChild(node);
}
/*
collisions is an array that tells you which events are in each 30 min slot
- each first level of array corresponds to a 30 minute slot on the calendar
- [[0 - 30mins], [ 30 - 60mins], ...]
- next level of array tells you which event is present and the horizontal order
- [0,0,1,2]
==> event 1 is not present, event 2 is not present, event 3 is at order 1, event 4 is at order 2
*/
function getCollisions (events) {
//resets storage
collisions = [];
for (var i = 0; i < 24; i ++) {
var time = [];
for (var j = 0; j < events.length; j++) {
time.push(0);
}
collisions.push(time);
}
events.forEach((event, id) => {
let end = event.end;
let start = event.start;
let order = 1;
while (start < end) {
timeIndex = Math.floor(start/30);
while (order < events.length) {
if (collisions[timeIndex].indexOf(order) === -1) {
break;
}
order ++;
}
collisions[timeIndex][id] = order;
start = start + 30;
}
collisions[Math.floor((end-1)/30)][id] = order;
});
};
/*
find width and horizontal position
width - number of units to divide container width by
horizontal position - pixel offset from left
*/
function getAttributes (events) {
//resets storage
width = [];
leftOffSet = [];
for (var i = 0; i < events.length; i++) {
width.push(0);
leftOffSet.push(0);
}
collisions.forEach((period) => {
// number of events in that period
let count = period.reduce((a,b) => {
return b ? a + 1 : a;
})
if (count > 1) {
period.forEach((event, id) => {
// max number of events it is sharing a time period with determines width
if (period[id]) {
if (count > width[id]) {
width[id] = count;
}
}
if (period[id] && !leftOffSet[id]) {
leftOffSet[id] = period[id];
}
})
}
});
};
var layOutDay = (events) => {
// clear any existing nodes
var myNode = document.getElementById("events");
myNode.innerHTML = '';
getCollisions(events);
getAttributes(events);
events.forEach((event, id) => {
let height = (event.end - event.start) / minutesinDay * containerHeight;
let top = event.start / minutesinDay * containerHeight;
let end = event.end;
let start = event.start;
let units = width[id];
if (!units) {units = 1};
let left = (containerWidth / width[id]) * (leftOffSet[id] - 1) + 10;
if (!left || left < 0) {left = 10};
createEvent(height, top, left, units);
});
}