-
Notifications
You must be signed in to change notification settings - Fork 0
/
babysitter_edit_schedule.js
242 lines (214 loc) · 7.84 KB
/
babysitter_edit_schedule.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//the times on the schedule to display (controlled by the slider)
var earliestTimeOnSchedule = 13;
var latestTimeOnSchedule = 24;
//color to paint in, currently always green
var currentColor = "green";
//whether the mouse button is down
var painting = false;
//the first date to display (currently always today)
var earliestDateOnSchedule = new Date();
//representation of which things are colored
var babysitterScheduleTable = new ScheduleTable(24, 7);
//update the displayed table with new information if stuff gets painted
babysitterScheduleTable.addEventListener('paint',function (e) {
if (e.details.newColor == "green") {
$( "#" + e.details.day + "_" + e.details.hour + "_square").attr('class', 'greenScheduleTableSquare');
} else if (e.details.newColor == "blank") {
$( "#" + e.details.day + "_" + e.details.hour + "_square").attr('class', 'blankScheduleTableSquare');
}
},true);
//initialization things
$(function() {
// add all the rows for the various hours of days to the table you can paint
for (var i = earliestTimeOnSchedule; i < latestTimeOnSchedule; i ++){
$("#babysitterScheduleTable > tbody > tr:last").after(genRow(i));
}
// make the labels for the columns
for (var i = 0; i < 7; i++) {
var tempDate = new Date();
tempDate.setDate(earliestDateOnSchedule.getDate() + i);
var dateString = tempDate.toDateString();
dateString = dateString.substring(0, dateString.length - 4);
$("#day" + i).text(dateString);
}
updateEditingSchedule();
// initialize the slider for which times are showing
makeSlider();
});
// track whether the mouse button is down
document.onmousedown = function() {
painting = true;
}
document.onmouseup = function() {
painting = false;
}
//all schedule stuff is same-each-week.
function updateEditingSchedule() {
$.get("schedule/jimmy/", function(data) {
var lines = data.split("\n");
for (line in lines) {
var fields = lines[line].split(" ");
if (fields.length == 4) { //validate it's a real line
paintRange(parseInt(fields[0]), parseInt(fields[1]), parseInt(fields[2]), parseInt(fields[3]));
}
}
});
}
function paintRange(startDay, startHour, endDay, endHour) {
if (startDay != endDay) {
paintRange(startDay, startHour, startDay, 24);
paintRange((startDay + 1)%7, 0, endDay, endHour);
} else {
if (startHour < endHour){
var relDay = (startDay - earliestDateOnSchedule.getDay());
if (relDay < 0) {
relDay = relDay + 7;
}
babysitterScheduleTable.paint(relDay, startHour, "green");
paintRange(startDay, startHour + 1, endDay, endHour);
}
}
}
//initialize the slider for which times are showing.
function makeSlider() {
$( "#timerange-slider" ).slider({range: true, min: 0, max:24,
orientation:"vertical",
values:[ 24 - latestTimeOnSchedule,
24 - earliestTimeOnSchedule ],
slide: function( event, ui ) {
updateTimerange( 24 - ui.values[1],
24 - ui.values[0]);
}}).height("300px");
$( "#timerange-slider > a:first").text(asShort12HourTime(latestTimeOnSchedule)).addClass("sliderLabel");
$( "#timerange-slider > a:last").text(asShort12HourTime(earliestTimeOnSchedule)).addClass("sliderLabel");
}
// on mouseover to a table cell, paint it if the mouse button's down
function paintIfClicking(event, day, time) {
if (painting) {
babysitterScheduleTable.paint(day, time, currentColor);
}
}
// make a row for the table: makes a bunch of table cells with appropriate event handlers
function genRow(time) {
var row = "<tr class='scheduleTableRow' id='" + as12HourTime(time) + "row" +
"'>\n<td class='scheduleRowLabel'>";
row += as12HourTime(time) + "</td>\n";
for (var day = 0; day < babysitterScheduleTable.width; day ++) {
row += "<td class='";
row += babysitterScheduleTable.getColor(day,time) + "ScheduleTableSquare";
row += "' id='" + day + "_" + time + "_square' ";
row += "onmousedown='babysitterScheduleTable.paint(" + day + "," + time + ", currentColor)' ";
row += "onmouseover='paintIfClicking(event," + day + "," + time + ")'/>\n";
}
row += "</tr>\n"
return row;
}
// scale the table appropriately when the slider moves
function updateTimerange(startTime, endTime){
$( "#startTime").text(as12HourTime(startTime));
$( "#endTime").text(as12HourTime(endTime));
$( "#timerange-slider > a:first").text(asShort12HourTime(endTime));
$( "#timerange-slider > a:last").text(asShort12HourTime(startTime));
for (var i = earliestTimeOnSchedule; i < startTime; i++) {
$("#" + as12HourTime(i) + "row").remove();
}
for (var i = startTime; i < endTime; i ++){
if (i < earliestTimeOnSchedule || i >= latestTimeOnSchedule) {
$("#babysitterScheduleTable > tbody > tr").eq(i - startTime).after(genRow(i));
}
}
for (var i = endTime; i < latestTimeOnSchedule; i++) {
$("#" + as12HourTime(i) + "row").remove();
}
earliestTimeOnSchedule = startTime;
latestTimeOnSchedule = endTime;
}
// convert a number to a nice time like "5am"
function as12HourTime(number) {
if (number == 0 || number == 24) {
return "midnight";
}
if (number < 12) {
return number + "am";
}
if (number == 12) {
return "noon";
} else {
return (number - 12) + "pm";
}
}
// convert a number to a short time like "5"
function asShort12HourTime(number) {
if (number == 0) {
return 12;
}
if (number <= 12) {
return number;
}
return number - 12;
}
// close the dialog, submit to the server
function submitSchedule() {
$.post("schedule/jimmy/", {times:babysitterScheduleTable.getPostableTimeblocks(earliestDateOnSchedule)},
function(data){updateCalendar();});
$("#hidingDiv").append($("#editSchedulePopup").remove());
$("#editSchedulePopup").dialog('close');
}
// show the schedule editing popup
function showEditSchedule(){
$("#hidingDiv").append($("#editSchedulePageOne").detach());
$("#hidingDiv").append($("#editSchedulePageTwo").detach());
dialog = $("#editSchedulePopup").dialog({title:"Edit Your Schedule",
modal:true,
draggable:false,
width:800,
height:600}).addClass("bodyFont").append($("#editSchedulePageOne"));
makeSlider();
}
// clears the schedule in the dialog.
function clearSchedule() {
var timeblocks = babysitterScheduleTable.getTimeblocks();
for (var i = 0; i < timeblocks.length; i++) {
var block = timeblocks[i];
for(var day = block.start.day; day <= block.end.day; day++) {
for(var time = earliestTimeOnSchedule; time <= latestTimeOnSchedule; time++) {
babysitterScheduleTable.paint(day, time, "blank");
}
}
}
}
// show the contacts managing popup
function showManageContacts(){
$("#manageContactsPopup").dialog({title:"Manage Contacts",
modal:true,
draggable:false,
width:800,
height:600}).addClass("bodyFont");
getParentList(function(parents) {
var lines = [];
for (var parent in parents) {
lines.push(parents[parent].username + " (" + parents[parent].name + ")<br/>");
}
$('#contactList').html(lines.join('\n'));
});
}
function getParentList(callback) {
$.get("contacts/jimmy", function(data) {
var list = data.split('\n');
var finalList = [];
for (var parent in list){
var info = list[parent].split(',');
finalList.push({username:info[0], name:info[1]});
}
callback(finalList);
});
}
function showApplyJobPopup(jobId){
$.get("jobApplicationPopup/jimmy/" + jobId + "/", function(data){$("#applyJobPopup").html(data);$("#applyJobPopup").dialog({title:"Apply for this job?"});});
}
function showUnApplyJobPopup(jobId){
$.get("jobUnApplicationPopup/jimmy/" + jobId + "/", function(data){$("#applyJobPopup").html(data);$("#applyJobPopup").dialog({title:"Withdraw your application?"});});
}
function clearSchedule(){
babysitterScheduleTable.clearAll();
}