-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
568 lines (494 loc) · 18.7 KB
/
index.html
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<html>
<meta charset="UTF-8">
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/datepicker.min.css">
</head>
<body>
<div class="form-group">
<label for="natureOfDuty">Nature of Duty</label>
<input type="text" id="natureOfDuty" name="natureOfDuty"/>
<label for="">Organization</label>
<input type="text" id="organization" name="organization"/>
<label for="">From (Date)</label>
<input type="text" id="fromDate" name="fromDate"/>
<label for="">To (Date)</label>
<input type="text" id="toDate" name="toDate"/>
</div>
<div class="form-group">
<label for="">People per duty</label>
<input type="number" id="peoplePerDuty" name="peoplePerDuty" value="1" />
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="checkHighlightWeekend">
<label class="form-check-label" for="checkHighlightWeekend">Highlight Weekends</label>
</div>
<div class="form-group">
<label for="da6csv">Choose the DA6 CSV file to upload</label>
<input type="file" id="da6csv" name="da6csv"/>
</div>
<div class="form-group">
<button id="genDa6" type="button" class="btn btn-primary btn-lg">Generate DA6 Roster</button>
<button id="generateMemo" type="button" class="btn btn-primary btn-lg">Generate Memo</button>
<button id="generateFinalStatus" type="button" class="btn btn-primary btn-lg">Generate Final Duty Status</button>
</div>
<hr>
<div id="da6" class="da6">
<table id="da6Table" class="table table-bordered">
</table>
</div>
<div id="da6_memo" class="da6_memo">
<table id="da6_memo_table" class="table table-bordered">
</table>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/jquery.csv.min.js"></script>
<script src="js/luxon.min.js"></script>
<script src="js/datepicker.min.js"></script>
<script>
// Add 'edit mode' - have the ability to 'lock in' the da6 and make minor edits
var DateTime = luxon.DateTime
var Interval = luxon.Interval
var da6People = []
var exceptionArray = []
var da6Struct = []
// set the initial values of the date cells
let fromDateCell = document.getElementById('fromDate');
const fromDateCellPicker = datepicker(fromDateCell, {
formatter: (input, date, instance) => {
const value = date.toISOString().slice(0,10)
fromDateCell.value = value
}
})
fromDateCell.value = DateTime.local().toISODate();
let toDateCell = document.getElementById('toDate');
const toDateCellPicker= datepicker(toDateCell, {
formatter: (input, date, instance) => {
const value = date.toISOString().slice(0,10)
toDateCell.value = value
}
})
toDateCell.value = DateTime.local().plus({ days: 39 }).toISODate();
// Update the table when changing the number of people per task
document.querySelector('#peoplePerDuty').addEventListener('click',() => {
processDa6();
})
// allow interactive of highlighting weekends
document.querySelector('#checkHighlightWeekend').addEventListener('click',() => {
processDa6();
})
// generate roster button
document.querySelector('#genDa6').addEventListener('click',() => {
processDa6()
})
document.querySelector('#generateMemo').addEventListener('click',() => {
generateMemo(da6Struct);
})
// OUTPUT THE FINAL DUTY STATUS FOR IMPORT NEXT TIME
document.querySelector('#generateFinalStatus').addEventListener('click',() => {
outputFinalStatus(da6Struct);
})
// load the CSV to create the DA6
document.querySelector('#da6csv').addEventListener('change',event => {
var da6File = event.target.files[0]
var reader = new FileReader()
reader.onload = e => {
var da6group = $.csv.toObjects(e.target.result)
// filter out people that are blank
da6People = da6group.filter(person => person.Name.length > 0 )
// first sort by rank, then by name (lastname)
da6People = sortByRankAndName(da6People)
}
reader.readAsText(da6File)
})
function processDa6 () {
// get the date range, include this in the 'Duty' array of the da6_pers_list
let fromDate = $('#fromDate').val()
let toDate = $('#toDate').val()
let fromDateVal = ''
let toDateVal = ''
// if there is no date set, use the current day
// 2 options for setting the date
// 1 - date set in the page
// 2 - current date
if (fromDate !== '') {
fromDateVal = DateTime.fromISO(fromDate, { zone: 'utc' })
toDateVal = DateTime.fromISO(toDate, { zone: 'utc' })
} else {
fromDateVal = DateTime.local()
toDateVal = fromDateVal.plus({ days: 39 })
}
let dateArray = genDateArray(fromDateVal, toDateVal);
da6Struct = genDa6Struct(dateArray, da6People);
da6Struct = populateExceptions(da6Struct, exceptionArray);
// iterate through the days to assign duty
da6Struct.forEach((dutyDay, dayIndex) => {
// populate the days for the structure
if (dayIndex !== 0) {
da6Struct = populateDaysSinceDuty(da6Struct, dayIndex);
}
// get the available people for that day
let avail = getAvailable(da6Struct[dayIndex]['dutyInfo'])
// get the person that is available with the hightest number of days
let numToSelect = parseInt(document.querySelector('#peoplePerDuty').value);
// get an array of people that have the greatest days
let peopleForDuty = getTaskedPerson(avail, numToSelect)
// specify that this person has duty for this day
da6Struct = setDutyDay(da6Struct, dayIndex, peopleForDuty)
})
displayDa6(da6Struct);
}
function displayDa6 (da6) {
let da6Table = document.getElementById('da6Table')
// remove the current table
while(da6Table.hasChildNodes()) {
da6Table.removeChild(da6Table.firstChild);
}
let natureOfDuty = $('#natureOfDuty').val()
let organization = $('#organization').val()
// the first three rows are the administrative data
let firstRow = da6Table.insertRow(-1)
let dutyRosterCell = firstRow.insertCell(-1)
dutyRosterCell.innerHTML = 'DUTY ROSTER'
dutyRosterCell.colSpan = 2
let natureOfDutyCell = firstRow.insertCell(-1)
natureOfDutyCell.innerHTML = 'NATURE OF DUTY<br />' + natureOfDuty
natureOfDutyCell.colSpan = 12
let orgCell = firstRow.insertCell(-1)
orgCell.innerHTML = 'ORGANIZATION<br />' + organization
orgCell.colSpan = 14
let fromCell = firstRow.insertCell(-1)
let fromDateVal = da6[0]['date']
fromCell.innerHTML = 'FROM (Date)<br />' + fromDateVal.toFormat('dd MMM')
fromCell.colSpan = 8
let toCell = firstRow.insertCell(-1)
let dutyArrayLength = da6.length
let toDateVal = da6[dutyArrayLength-1]['date']
toCell.innerHTML = 'TO (Date)<br />' + toDateVal.toFormat('dd MMM')
toCell.colSpan = 7
let secondRow = da6Table.insertRow(-1);
let gradeCell = secondRow.insertCell(-1)
gradeCell.innerHTML = 'GRADE'
gradeCell.rowSpan = 2
gradeCell.style = 'vertical-align: middle'
let nameCell = secondRow.insertCell(-1)
nameCell.innerHTML = 'NAME'
nameCell.rowSpan = 2
nameCell.style = 'vertical-align: middle'
let date_cell = secondRow.insertCell(-1)
date_cell.innerHTML = 'Month'
// insert the second row for the date
let dayCell
let monthNameCell
let thirdRow = da6Table.insertRow(-1)
dayCell = thirdRow.insertCell(-1)
dayCell.innerHTML = 'Day'
let curMonth
let monthCellSpanWidth = 0
let monthIndex = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
da6.forEach((day, dayIndex, da6) => {
// come up with way to display the months on different columns
dayCell = thirdRow.insertCell(-1)
currentDay = day['date']
dayCell.innerHTML = currentDay.day
// adjust the spacing for the month cells
if (dayIndex === 0) {
monthNameCell = secondRow.insertCell(-1)
curMonth = currentDay.month
monthNameCell.innerHTML = monthIndex[parseInt(curMonth) - 1]
}
if (curMonth !== currentDay.month) {
curMonth = currentDay.month
// set the monthNameCell span and then update the month
monthNameCell.colSpan = monthCellSpanWidth
monthNameCell = secondRow.insertCell(-1)
monthCellSpanWidth = 0
monthNameCell.innerHTML = monthIndex[parseInt(curMonth) - 1]
}
if (dayIndex === da6.length - 1) {
// this is the last time through, set the final month spanwidth
monthCellSpanWidth = monthCellSpanWidth + 1;
monthNameCell.colSpan = monthCellSpanWidth;
}
monthCellSpanWidth = monthCellSpanWidth + 1;
if (document.getElementById('checkHighlightWeekend').checked) {
if (currentDay.weekday == 6 || currentDay.weekday == 7) {
dayCell.bgColor = 'Red'
}
}
day['dutyInfo'].forEach(person => {
// insert people here only if the dayIndex is 0
if (dayIndex === 0) {
let dutyRow = da6Table.insertRow(-1);
// set the id of the row for access later
dutyRow.id = person.Name;
let rankCell = dutyRow.insertCell(-1)
let nameCell = dutyRow.insertCell(-1)
rankCell.innerHTML = person.Rank
nameCell.colSpan = 2
nameCell.innerHTML = person.Name
}
// get the person row
personRow = document.getElementById(person.Name);
// now draw the duty rows out
dutyCell = personRow.insertCell(-1)
dutyCell.setAttribute('data-person',person.Name);
dutyCell.setAttribute('data-date',currentDay.toISODate());
dutyCell.setAttribute('data-exception',person.dutyStatus);
if (['A','U','D'].includes(person['dutyStatus'])) {
dutyCell.innerHTML = person['dutyStatus']
} else if (person['dutyStatus'] === 'T') {
dutyCell.innerHTML = '///'
dutyCell.bgColor = 'green'
} else if (person['daysSinceLastDuty'] == 0 && person.dutyStatus === '') {
dutyCell.innerHTML = '///'
} else {
dutyCell.innerHTML = person['daysSinceLastDuty']
}
dutyCell.onclick = function () {
toggleException(this)
}
if (dayIndex === da6.length - 1) {
for (let h = da6.length; h < 40; h = h + 1) {
let fillerCell = personRow.insertCell(-1);
}
}
})
})
// continue this array to finish drawing the columns
// only make 1 cell for the remainder of the space for the month block
if (da6.length < 40) {
monthCellSpanWidth = 0
monthNameCell = secondRow.insertCell(-1)
}
// draw day cells until there are 40, this is the width of the DA6
for (let h = da6.length; h < 40; h = h + 1) {
dayCell = thirdRow.insertCell(-1);
monthCellSpanWidth = monthCellSpanWidth + 1;
monthNameCell.colSpan = monthCellSpanWidth;
}
}
function toggleException (e) {
// get the exception in the cell, also get the person and the date
let oldException = e.getAttribute('data-exception')
let newException = ''
let cellDate = e.getAttribute('data-date')
let cellPerson = e.getAttribute('data-person')
if(oldException == 'A') {
newException = 'D';
} else if (oldException == 'D') {
newException = 'U';
} else if (oldException == 'U') {
newException = 'T';
} else if (oldException == 'T') {
newException = '';
} else if (oldException == '') {
newException = 'A';
}
exceptionArray = addException(exceptionArray, cellPerson, cellDate, newException);
processDa6()
}
// add the ability to see if there is a 'T' status, if yes, then return that one
function getTaskedPerson (people, numToSelect) {
// find the person that either has
// 'T' in the dutyStatus -> they are explicitly tasked
// highest days
let retArray = []
for (let i = 0; i < numToSelect; i = i + 1) {
// there may be too many people that have 'A' 'U' or 'D', handle this with try/catch
try {
let selectedPerson = people.reduce((prev, current) => {
if (prev.dutyStatus === 'T') {
retVal = prev
} else if (current.dutyStatus === 'T') {
retVal = current;
} else if (prev.daysSinceLastDuty >= current.daysSinceLastDuty) {
retVal = prev;
} else {
retVal = current;
}
return retVal;
})
retArray.push(selectedPerson)
// remove the value just returned from the array
people = people.filter(person => selectedPerson.Name !== person.Name && selectedPerson.Rank !== person.Rank)
} catch (err) { console.log(err); };
}
return retArray
}
function getAvailable (dutyDayEntry) {
return dutyDayEntry.filter(entry => !['A','U','D'].includes(entry.dutyStatus));
}
/*
function setDutyDay(da6Struct, dayIndex, targetPerson) {
da6Struct[dayIndex]['dutyInfo'].forEach((person, personIndex) => {
if (targetPerson.Name == person.Name) {
da6Struct[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = 0;
}
})
return da6Struct;
}
*/
function setDutyDay(da6Struct, dayIndex, targetPeople) {
targetPeople.forEach(targetPerson => {
da6Struct[dayIndex]['dutyInfo'].forEach((person, personIndex) => {
if (targetPerson.Name == person.Name) {
da6Struct[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = 0;
}
})
})
return da6Struct;
}
// return the value if a person has an exception for that day
function getPersonDayException (exceptionArray, person, date) {
return exceptionArray.filter(ex => {
return (ex.person === person.Name && ex.date === date )
})
}
// called when an array element is clicked on
function addException (exceptionArray, person, date, reason) {
// remove any exceptions from the exceptionArray for this person on this date
// before adding any new ones
exceptionArray = removeException(exceptionArray, person, date)
exceptionArray.push({
person: person,
date: date,
reason: reason,
});
return exceptionArray;
}
function removeException (exceptionArray, person, date) {
return exceptionArray.filter(exception => {
return !(exception.person === person && exception.date === date)
})
}
function populateDaysSinceDuty (dutyRoster, dayIndex) {
dutyRoster[dayIndex]['dutyInfo'].forEach((person, personIndex) => {
if (['D','U',''].includes(dutyRoster[dayIndex]['dutyInfo'][personIndex]['dutyStatus'])) {
dutyRoster[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = dutyRoster[dayIndex - 1]['dutyInfo'][personIndex]['daysSinceLastDuty'] + 1;
} else if (['A'].includes(dutyRoster[dayIndex]['dutyInfo'][personIndex]['dutyStatus'])) {
dutyRoster[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = dutyRoster[dayIndex - 1]['dutyInfo'][personIndex]['daysSinceLastDuty'];
} else if (['T'].includes(dutyRoster[dayIndex]['dutyInfo'][personIndex]['dutyStatus'])) {
dutyRoster[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = 0
}
})
return dutyRoster;
}
function populateExceptions(dutyRoster, exceptionArray) {
dutyRoster.forEach((day, dayIndex, dutyRoster) => {
dutyRoster[dayIndex]['dutyInfo'].forEach((person, personIndex) => {
// check if there is an exception, if so, then populate it
let personExceptions = getPersonDayException(exceptionArray, person, day.date.toISODate());
if (personExceptions.length > 0) {
dutyRoster[dayIndex]['dutyInfo'][personIndex]['dutyStatus'] = personExceptions[0].reason
}
})
})
return dutyRoster;
}
function genDa6Struct (dateArray, da6People) {
dateArray.forEach((day, dayIndex, dateArray) => {
dateArray[dayIndex]['dutyInfo'] = []
da6People.forEach((person, personIndex) => {
dateArray[dayIndex]['dutyInfo'][personIndex] = {}
dateArray[dayIndex]['dutyInfo'][personIndex]['Name'] = person.Name
dateArray[dayIndex]['dutyInfo'][personIndex]['Rank'] = person.Rank
if (dayIndex === 0) {
dateArray[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = parseInt(person.daysSinceLastDuty) + 1;
} else {
dateArray[dayIndex]['dutyInfo'][personIndex]['daysSinceLastDuty'] = 0;
}
dateArray[dayIndex]['dutyInfo'][personIndex]['dutyStatus'] = '';
})
})
return dateArray;
}
function genDateArray (start, end) {
const dateInterval = Interval.fromDateTimes(start, end);
const numDates = dateInterval.count('days');
let dateArray = Array.apply(null, Array(numDates));
dateArray.forEach((date, dateIndex) => dateArray[dateIndex] = { date: start.plus({ 'days': dateIndex })});
return dateArray;
}
// sorts the input with values Rank and Name according to rank (higher rank first)
// then name (alphabetically)
function sortByRankAndName (sort_file) {
// sort the pers list by rank and then name
let ordering = {};
let sortOrder = ['COL', 'LTC', 'MAJ', 'CPT', '1LT', '2LT', 'CSM', 'SGM', '1SG', 'MSG', 'SFC', 'SSG', 'SGT', 'CPL', 'SPC', 'PFC', 'PV2', 'PVT']
sortOrder.forEach((key, value) => { ordering[key] = value });
sort_file.sort((a, b) => {
return (ordering[a.Rank] - ordering[b.Rank]) || a.Name.localeCompare(b.Name)
})
return sort_file
}
// SIDE EFFECTS
function generateMemo (da6Struct) {
// detect if the table is generated, if not alert with a popup
if (da6Struct.length === 0) {
alert('Generate a DA6 before exporting the final status')
return
}
var da6_memo_table = document.getElementById('da6_memo_table')
// display a table with the date and names of personnel that have duty
// remove the current table
while(da6_memo_table.hasChildNodes()) {
da6_memo_table.removeChild(da6_memo_table.firstChild);
}
var headerRow = da6_memo_table.insertRow(-1)
var dateHeaderCell = headerRow.insertCell(-1)
dateHeaderCell.innerHTML = 'Date'
var nameHeaderCell = headerRow.insertCell(-1)
nameHeaderCell.innerHTML = 'Name'
let personRow = '';
let dateCell = '';
let personCell = '';
da6Struct.forEach(day => {
day['dutyInfo'].forEach(person => {
if (person['daysSinceLastDuty'] === 0) {
personRow = da6_memo_table.insertRow(-1);
dateCell = personRow.insertCell(-1);
dateCell.innerHTML = day.date.toISODate();
personCell = personRow.insertCell(-1);
personCell.innerHTML = person.Rank + ' ' + person.Name;
};
});
});
}
function outputFinalStatus (da6Struct) {
// detect if the table is generated, if not alert with a popup
if (da6Struct.length === 0) {
alert('Generate a DA6 before exporting the final status')
return
}
var exportList = exportFinalStatus(da6Struct)
const replacer = (key, value) => value === null ? '' : value;
const header = Object.keys(exportList[0]);
let csv = exportList.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','));
csv.unshift(header.join(','));
csv = csv.join('\r\n');
//Download the file as CSV
var downloadLink = document.createElement("a");
var blob = new Blob(["\ufeff", csv]);
var url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = "da6Status.csv"; //Name the file here
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
// output the Soldier name, rank and final status of the duty
function exportFinalStatus (da6Struct) {
var exportDa6Json = []
da6Struct[da6Struct.length-1]['dutyInfo'].forEach(person => {
exportDa6Json.push({
Name: person.Name,
Rank: person.Rank,
daysSinceLastDuty: person.daysSinceLastDuty,
})
})
return exportDa6Json
}
</script>
</body>
</html>