-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.js
174 lines (160 loc) · 4.51 KB
/
scheduler.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
let standardizeDay = function (day) {
let standardDay;
// If cannot be converted into a number, process as string
if(isNaN(parseInt(day))) {
if(typeof day === "string") {
const dayCode = day.substring(0, 3).toLowerCase();
switch (dayCode) {
case "sun":
standardDay = 0;
break;
case "mon":
standardDay = 1;
break;
case "tue":
standardDay = 2;
break;
case "wed":
standardDay = 3;
break;
case "thu":
standardDay = 4;
break;
case "fri":
standardDay = 5;
break;
case "sat":
standardDay = 6;
break;
default:
throw new Meteor.Error(400, 'Error 400: Bad Request', "One of the 'day' values is not given or is in an unrecognized format. The script will continue execution using the default value.");
}
} else {
throw new Meteor.Error(400, 'Error 400: Bad Request', "One of the 'day' values is not given or is in an unrecognized format. The script will continue execution using the default value.");
}
}
// If day can be converted to a number, process as number
else {
standardDay = parseInt(day) % 7;
}
return standardDay;
}
var standardizeHour = function (hour) {
let standardHour = parseInt(hour);
if (isNaN(standardHour)) {
throw new Meteor.Error(400, 'Error 400: Bad Request', "One of the 'hour' values is not given or is in an unrecognized format. The script will continue execution using the default value.");
} else {
return standardHour;
}
}
let stardardDayToText = function (standardDay) {
switch (standardDay) {
case 0:
return "Sunday";
break;
case 1:
return "Monday";
break;
case 2:
return "Tuesday";
break;
case 3:
return "Wednesday";
break;
case 4:
return "Thursday";
break;
case 5:
return "Friday";
break;
case 6:
return "Saturday";
break;
default:
break;
}
};
let stardardDayToShortText = function (standardDay) {
return stardardDayToText(standardDay).substring(0,3);
};
let stardardDayToLetter = function (standardDay) {
return stardardDayToText(standardDay).substring(0,1);
};
var countInBetweenDays = function (startDay, endDay) {
let temp = standardizeDay(endDay) - standardizeDay(startDay) + 1;
return temp > 0 ? temp : temp + 7;
};
var countInBetweenHours = function (startHour, endHour) {
return Math.abs(standardizeHour(endHour) - standardizeHour(startHour)) + 1;
};
var generateWeekTable = function (options) {
let inBetweenHours = countInBetweenHours(options.dayStart, options.dayEnd);
let standardDayStart = standardizeHour(options.dayStart);
let table = document.createElement('table');
table.dataset.template = "week";
if(options.id) {
table.id = options.id;
}
if(options.class) {
table.className = options.class;
}
let tr = table.insertRow(0);
tr.insertCell(0);
for (let i = 0; i < inBetweenHours; i++) {
let td = tr.insertCell(i + 1);
td.appendChild(document.createTextNode(standardDayStart + i));
}
for (let i = 0; i < countInBetweenDays(options.weekStart, options.weekEnd); i++) {
let tr = table.insertRow(i + 1);
let td = tr.insertCell(0);
let standardizedWeekStart = standardizeDay(options.weekStart + i);
td.appendChild(document.createTextNode(stardardDayToShortText(standardizedWeekStart)));
for (let j = 0; j < inBetweenHours; j++) {
let td = tr.insertCell(j + 1);
if(options.buttons) {
let toggleButton = document.createElement('button');
toggleButton.className = "scheduler__button";
toggleButton.dataset.day = standardizedWeekStart;
toggleButton.dataset.hour = standardDayStart + j;
td.appendChild(toggleButton);
}
}
}
return table;
}
Scheduler = class Scheduler {
constructor (options) {
if (options.weekStart) {
options.weekStart = standardizeDay(options.weekStart)
}
if (options.weekEnd) {
options.weekEnd = standardizeDay(options.weekEnd)
}
this.options = options;
}
static getData (table) {
let data = {};
if (table.dataset.template === "week") {
let buttons = table.getElementsByClassName('scheduler__button');
for (let i = 0; i < buttons.length; i++) {
let day = buttons[i].dataset.day;
let hour = buttons[i].dataset.hour;
if (!data[day]) {
data[day] = {};
}
data[day][hour] = ((" " + buttons[i].className + " " ).indexOf("schedular__active") > -1);
}
}
return data;
}
generateTable () {
if (this.options.template === "week") {
return generateWeekTable(this.options);
}
return "This template is not supported yet.";
}
generateTableAsHTML () {
let tableObj = this.generateTable();
return tableObj.outerHTML;
}
}