forked from jamie314159/Employee_Schedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_script.js
368 lines (324 loc) · 12.4 KB
/
table_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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use strict';
// COLORS_RGB holds one color for each name
// Colors are assigned to names in the order each name is first processed
var COLORS_RGB = ['rgb(231, 63, 63)','rgb(231, 231, 75)','rgb(0, 155, 155)','rgb(247, 108, 39)'];
var schedule = new XMLHttpRequest();
var NAMES = [];
var TIMES = [];
// Adujsts the Luminance of a color
// Used to darken cell colors for current day and time
//http://www.sitepoint.com/javascript-generate-lighter-darker-color/
function ColorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = '#', c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ('00'+c).substr(c.length);
}
return rgb;
}
// Function which converts one channel of rgb to one channel of hex
function hex(x) {
var hexDigits = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
return isNaN(x) ? '00' : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
// Converts rgb colors to hex colors for passing to ColorLuminance
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return '#' + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
// Uses TimeToString array to convert a number, stored as an int or string,
// to the corrisponding word to be used as class names
var TimeToString = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twentyone', 'twentytwo', 'twentythree', 'twentyfour'];
function getTimeName(n){
if(typeof n == 'string'){
n = parseInt(n.substring(0,2));
}
if(n){
return TimeToString[n];
}
else{
return '';
}
}
// Uses DayToString array to convert a number, stored as an int or string,
// to the corrisponding day name, to be usedwhen setting and referenceing css classes.
// ex: getDayName(1) = 'Monday'
var DayToName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
function getDayName(m) {
if(typeof m == 'string'){
m = parseInt(m);
}
if(m){
return DayToName[m];
}
else{
return '';
}
}
// Uses getDayName function to convert a day name, stored as an int or string,
// to the corrisponding day number to be used when setting and referenceing css classes.
function getDayNum(m) {
if(typeof m == 'number'){
m = m.toString();
}
// var d = DayToName.indexOf(m);
// if (DayToName[d] == m){
// return d;
// }
// return 0;
for (var i = 0; i < DayToName.length; i++) {
if(getDayName(i) == m){
return i;
}
}
return 0;
}
// Uses XMLHttpRequest to retrive schedule.csv from local storage
function getSchedule(){
schedule.open("get", "schedule2.json", false);
schedule.send();
}
//checks to see who is working what day of the week(***<MAY NEED WORK)
function isWorking(day , schedule){
//create a new array containing the days in the given schedule
daysWorking = schedule.map(function (d) {return d.day;});
//test when the given day is in the list of scheduled days
if( daysWorking.indexOf(day) !== -1){
return true;
}else{
return false;
}
}
//check to see if the time of work is correct to place into the grid (***MAY NEED WORK)
function validTime(time, schedule){
//create a new array containing the times of day worked on the given day
timeWorking = schedule.map(function (h) {return h.hours;});
for(var index = 0; index < timeWorking.length; index++){
if(time >= timeWorking[index].start && time < timeWorking[index].end){
return true;
}
}
return false;
}
// Reads in schedule2.json and creates a table with that information
function makeTable() {
// the string to parse as json.
// var schedule2 = schedule.responceText("schedule2.json");
var jsonData = JSON.parse(schedule.responseText);
//fill array NAMES with everyones names;
for(var n = 0; n < jsonData.length; n++){
NAMES[n] = jsonData[n].name;
}
//variables needed to create the table
var table = document.getElementById('table');
var tableHeading
var tableRow
var tableData
var columns = [ " ", " ", "Monday", " ", "Tuesday", " ", "Wednesday", " ", "Thursday", " ", "Friday"];
var rows = [" ", " ", "08", "09", "10", "11", "12", "13", "14", "15", "16", " "];
//loops throught the rows array to get all the time slots
for(var i = 0; i < rows.length; i++){
tableRow = document.createElement('tr');
//if i == 0 then the its the firt row which needs the table headings
if( i == 0){
tableHeading = document.createElement('th');
//sets the days of the week as table headings
for(var j = 0; j < columns.length; j++){
if(j == 0){
tableHeading = tableRow.insertCell(j);
tableHeading.innerHTML = columns[j];
tableHeading.setAttribute('class', 'time');
}else if(j % 2 != 0){
tableHeading = tableRow.insertCell(j);
tableHeading.setAttribute('class', 'divider');
}else{
tableHeading = tableRow.insertCell(j);
tableHeading.innerHTML = columns[j];
tableHeading.setAttribute('class', 'day');
tableHeading.setAttribute("colspan", "4");//colspan needs to change depending on how many people are working on that day.
//right now it hard coded to 4, so i could try to fill the grid.
tableRow.appendChild(tableHeading);
}
table.appendChild(tableRow);
}
//if its one or eleven then its a divider between the days of the week and the key at the bottom
}else if( i == 1 || i == 11){
tableData = document.createElement('td');
tableData = tableRow.insertCell(0);
tableData.innerHTML = rows[i];
tableData.setAttribute('class', 'divider2');
table.appendChild(tableData);
}
//any other time we need to build the table's rows with the person schedule
else{
tableData = document.createElement('td');
tableData = tableRow.insertCell(0);
tableData.innerHTML = rows[i];
tableData.setAttribute('class', 'time');
//adds time to the first slot of the row
tableRow.appendChild(tableData);
var integerTime = parseInt( rows[i], 10);
var count = 0;
//goes through every day of the week
for(var daysOfWeek = 0; daysOfWeek < columns.length; daysOfWeek++){
//makes sure were are only using valid days of the week
if( columns[daysOfWeek] !== " "){
//goes through every person and checks there schedule
for(var person = 0; person < jsonData.length; person++){
//if their schedule contains day and time then it will add color else null
if(isWorking(daysOfWeek, jsonData[person].schedule) && validTime(integerTime, jsonData[person].schedule)){
tableData = tableRow.insertCell(count);
tableData.innerHTML = jsonData[person].name;
count++;
}else{
tableData = tableRow.insertCell(count);
tableData.innerHTML = null;
count++;
}
tableRow.appendChild(tableData);
}
}
//more for asetics. Adds divividers between the days of the week to keep grid like structure
else{
tableData = tableRow.insertCell(count);
//tableData.setAttribute('class', 'divider2');
tableRow.appendChild(tableData);
count++;
}
table.appendChild(tableRow);
}
}
}
}
// Makes key
// Employees apeare in alphabetical order
function makeKey() {
var tr;
var td;
var div;
var dividertd;
var table = document.getElementById('key');
tr = document.createElement('tr');
tr.setAttribute('class', '');
for(var i = 0; i < NAMES.length; i++)
{
td = document.createElement('td');
div = document.createElement('div');
td.setAttribute('class', 'name ' + NAMES[i]);
div.innerHTML = '<p>' + NAMES[i] + '</p>';
td.appendChild(div);
tr.appendChild(td);
if(i != NAMES.length - 1){
dividertd = document.createElement('td');
dividertd.setAttribute('class', 'divider');
tr.appendChild(dividertd);
}
}
table.appendChild(tr);
}
// Darkens cell background color based on class names passed as arguments, i.e. day, time
function darken(day, time) {
var style;
var bcolor;
var nameList = [];
var classes = '';
for (var i = 0; i < arguments.length; i++) {
classes += '.' + arguments[i];
}
var element = document.querySelectorAll('.table ' + classes);
/* Will only darken color if it is in color set COLORS_RGB */
/* i.e. Will only darken a cell once */
for (i = 0; i < element.length; i++) {
var n = element[i].className.split(' ')[2];
if(n){
nameList[nameList.length] = n;
}
style = window.getComputedStyle(element[i]);
bcolor = style.getPropertyValue('background-color');
if(COLORS_RGB.indexOf(bcolor)>=0)
{
element[i].style.backgroundColor = new ColorLuminance(rgb2hex(bcolor), -0.25);
}
}
/* Darkens those working in Key */
for (i = 0; i < nameList.length; i++) {
classes = '.name.' + nameList[i];
element = document.querySelectorAll(classes);
style = window.getComputedStyle(element[0]);
bcolor = style.getPropertyValue('background-color');
if(COLORS_RGB.indexOf(bcolor)>=0)
{
element[0].style.backgroundColor = new ColorLuminance(rgb2hex(bcolor), -0.25);
}
}
}
//Sets default cell colors
function setColors() {
var i, j;
var classes = '';
var elements;
var element;
var trColors = ['#ffffff', '#848D82'];
var trBool = 0;
// Sets alternating dark and light row colors
for (i = 0; i < TIMES.length; i++) {
classes = TIMES[i];
if(classes !== null)
{
element = document.getElementById(classes);
if (element !== null){
element.style.backgroundColor = trColors[trBool];
if(trBool === 0)
trBool = 1;
else
trBool = 0;
}
}
}
// Set defalut colors of each employee
// Colors are assigned in order of their apearence in the COLORS array
// to employees in alphabetical order
// Main Table
for (i = 0; i < NAMES.length; i++) {
classes = '.' + NAMES[i];
elements = document.querySelectorAll('.table ' + classes);
for (j = 0; j < elements.length; j++) {
elements[j].style.backgroundColor = COLORS_RGB[i];
}
}
// Key
for (i = 0; i < NAMES.length; i++) {
classes = '.' + NAMES[i];
elements = document.querySelectorAll('.key ' + classes);
for (j = 0; j < elements.length; j++) {
elements[j].style.backgroundColor = COLORS_RGB[i];
}
}
}
//Calls darken for current date and time
function setByTime() {
var d = new Date();
var time = getTimeName(d.getHours());
var day = getDayName(d.getDay());
setColors();
darken(day, time);
}
//Calls creation functions and sets interval
function startTimer(){
//allows requests to be sent
getSchedule();
//reads in the json document and places results into the table
makeTable();
makeKey();
setColors();
setByTime();
var intervalID = window.setInterval(setByTime, 1000);
}