-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule_table.js
116 lines (103 loc) · 3.03 KB
/
schedule_table.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
var ScheduleTable = function(height, width) {
this.height = height;
this.width = width;
this.days = new Array(this.width);
// make an empty checkerboard
for (var i=0; i < this.width; i++){
this.days[i] = [];
}
this.paint = function(day, hour, color) {
if (this.days[day][hour] == color) {
delete this.days[day][hour];
this.dispatchEvent("paint", {day:day, hour:hour, newColor:"blank"});
} else {
this.days[day][hour] = color;
this.dispatchEvent("paint", {day:day, hour:hour, newColor:color});
}
}
this.clear = function() {
for (var i = 0; i < this.width; i ++) {
for (var j = 0; j < this.height; j++) {
if(this.days[i][j] != null) {
delete this.days[i][j];
this.dispatchEvent("paint", {day:i, hour:j, newColor:"blank"});
}
}
}
}
this.getTimeblocks = function() {
var timeblocks = [];
var inTimeblock = false;
var timeblockStart;
for (var i = 0; i < this.width; i ++) {
for (var j = 0; j < this.height; j++) {
if (this.getColor(i, j) == "blank") {
if (inTimeblock) {
timeblocks.push({start:timeblockStart, end:{day:i,hour:j}});
inTimeblock = false;
}
} else {
if (!inTimeblock) {
inTimeblock = true;
timeblockStart = {day:i,hour:j};
}
}
}
}
return timeblocks;
}
this.getPostableTimeblocks = function(earliestDateOnSchedule) {
var time = [];
var timeblocks = this.getTimeblocks();
for (var i = 0; i < timeblocks.length; i++) {
var block = timeblocks[i];
var startDate = new Date();
startDate.setDate(earliestDateOnSchedule.getDate() + block.start.day);
startDate.setHours(block.start.hour);
var endDate = new Date();
endDate.setDate(earliestDateOnSchedule.getDate() + block.end.day);
endDate.setHours(block.end.hour);
time.push(startDate.getFullYear() + " " + startDate.getMonth() + " " +
startDate.getDate() + " " + startDate.getHours() + " " +
endDate.getFullYear() + " " + endDate.getMonth() + " " +
endDate.getDate() + " " + endDate.getHours());
}
return time;
}
this.getColor = function(day, hour) {
if (this.days[day][hour] == null) {
return "blank";
}
return this.days[day][hour];
}
this.allHandlers = new Array();
/**
* Dispatch a new event to all the event listeners of a given event type
*/
this.dispatchEvent = function(type, details){
var newEvent = {type:type, details:details};
if (this.allHandlers[type]){
for (var i in this.allHandlers[type]){
this.allHandlers[type][i](newEvent);
}
}
}
/**
* Add a new event listener for a given event type
* the parameter 'handler' has to be a function with one parameter which is an event object
*/
this.addEventListener = function(eventType, handler){
if (!this.allHandlers[eventType])
this.allHandlers[eventType] = [];
this.allHandlers[eventType].push(handler);
}
this.clearAll = function() {
for (var i = 0; i < this.width; i ++) {
for (var j = 0; j < this.height; j++) {
if (this.getColor(i, j) != "blank") {
this.paint(i, j, "blank");
}
}
}
}
}