-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
509 lines (370 loc) · 15.5 KB
/
main.ts
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
import { PigController } from "./PigController";
import { GreyPig, ChestnutPig, WhitePig, BlackPig } from "./Pigs";
//Group all subclasses of pig to make it easier to work
type PigType = GreyPig | ChestnutPig | WhitePig | BlackPig;
const controller = new PigController();
const pigCategorySelect = document.getElementById("pig-category") as HTMLSelectElement;
const addRowButton = document.getElementById('add-row') as HTMLButtonElement;
const hideInputButton = document.getElementById('hide-input') as HTMLButtonElement;
const tableBody = document.getElementById('table-body') as HTMLButtonElement;
const inputFormTemplate = document.getElementById('input-form-template') as HTMLButtonElement;
const saveInputButton = document.getElementById('save-input') as HTMLButtonElement;
const dynamicInput = document.getElementById('dynamicForm') as HTMLInputElement;
const title = document.getElementById("dynamic") as HTMLElement;
const breedDropdown = document.getElementById("pig-breed");
var restrict = 0;
var pig_id = 0;
//Load pig Id generator from local storage
const storedPigId = localStorage.getItem('pig_id');
if(storedPigId !== null){
pig_id = parseInt(storedPigId, 10);
}
addRowButton.addEventListener('click', () => showInputForm());
hideInputButton.addEventListener('click', () => hideInputForm());
saveInputButton.addEventListener('click', () => AddPigs());
dynamicInput.addEventListener('input', AdjustInput)
//Listener for changing input form
pigCategorySelect.addEventListener("change", function(event){
const target = event.target as HTMLSelectElement;
updateFormFields(target.value);
});
//Used to change the "more info" button to "less info" and vice versa
document.addEventListener('click', (event) => {
const target = event.target as HTMLButtonElement | null;
var graph = document.getElementById("hiddenGraph");
if(target?.tagName === 'BUTTON'){
if(target.textContent === 'More Info'){
target.textContent = 'Less Info';
target.style.color = 'red';
const expandedRows = document.querySelectorAll('.expanded');
expandedRows.forEach((row) => {
if(row !== target.closest('tr')){
const button = row.querySelector('button')!;
button.textContent = 'More Info';
button.style.color = 'green';
row.classList.remove('expanded');
}
});
target.closest('tr')!.classList.add('expanded');
if(graph){
graph.style.display = "block";
}
}
else if(target.textContent === 'Less Info'){
target.textContent = 'More Info';
target.style.color = 'green';
target.closest('tr')!.classList.remove('expanded');
if(graph){
graph.style.display = "none";
}
}
}
});
//Load pig objects from local storage
const stored_pigs = localStorage.getItem('pigs');
if(stored_pigs !== null){
const pigs = JSON.parse(stored_pigs);
for(const pig of pigs){
controller.addPig(pig);
}
controller.getPigs().sort(function (a, b){
var aCata = a.category;
var bCata = b.category;
var aName = a.name;
var bName = b.name;
if(aCata == bCata){
return (aName < bName) ? -1 : (aName > bName) ? 1 : 0;
}
else{
return (aCata < bCata) ? -1 : 1;
}
});
updateTable(controller);
}
//Take inputs from input form and turn into pig object
function AddPigs(){
const name = document.querySelector('input[name="name"]') as HTMLInputElement;
const breed = document.querySelector('select[name="breed"]') as HTMLSelectElement;
const height = document.querySelector('input[name="height"]') as HTMLInputElement;
const weight = document.querySelector('input[name="weight"]') as HTMLInputElement;
const personality = document.querySelector('input[name="personality"]') as HTMLInputElement;
const category = document.querySelector('select[name="category"]') as HTMLSelectElement;
const dynamic = document.getElementById('dynamicForm') as HTMLInputElement;
const breedOptions: { [key: string]: string[] } = {
"Grey": ["Yorkshire Pig", "Duroc Pig", "Landrace Pig"],
};
//Ensure all fields have inputs entered
const missingFields: string[] = [];
if (!name.value) missingFields.push("Name");
if (!height.value) missingFields.push("Height");
if (!weight.value) missingFields.push("Weight");
if (!personality.value) missingFields.push("Personality");
if (!dynamic.value) missingFields.push("Dynamic");
//Alert the user if any fields are missing
if (missingFields.length > 0) {
const missingFieldsStr = missingFields.join(', ');
alert(`Please fill in the following required fields: ${missingFieldsStr}`);
return;
}
if(category.value == "Grey"){
const greyPig = new GreyPig(pig_id, name.value, breed.value, Number(height.value), Number(weight.value), personality.value, category.value);
greyPig.swimmingAbility = Number(dynamic.value);
controller.addPig(greyPig);
}
else if(category.value == "Chestnut"){
const chestnutPig = new ChestnutPig(pig_id, name.value, breed.value, Number(height.value), Number(weight.value), personality.value, category.value);
chestnutPig.language = dynamic.value;
controller.addPig(chestnutPig);
}
else if(category.value == "White"){
const whitePig = new WhitePig(pig_id, name.value, breed.value, Number(height.value), Number(weight.value), personality.value, category.value);
whitePig.runningAbility = Number(dynamic.value);
controller.addPig(whitePig);
}
else if(category.value == "Black"){
const blackPig = new BlackPig(pig_id, name.value, breed.value, Number(height.value), Number(weight.value), personality.value, category.value);
blackPig.strengthAbility = Number(dynamic.value);
controller.addPig(blackPig);
}
//Store pig id into local storage
pig_id++;
localStorage.setItem('pig_id', JSON.stringify(pig_id));
//Sort pigs by colour, then by name
controller.getPigs().sort(function (a, b){
var aCata = a.category;
var bCata = b.category;
var aName = a.name;
var bName = b.name;
if(aCata == bCata){
return (aName < bName) ? -1 : (aName > bName) ? 1 : 0;
}
else{
return (aCata < bCata) ? -1 : 1;
}
});
updateTable(controller);
//Set default values for all inputs
name.value = '';
breed.selectedIndex= 0;
weight.value = '';
height.value = '';
personality.value = '';
category.selectedIndex = 0;
dynamic.value = '';
inputFormTemplate.style.display = "none";
if(dynamicInput){
dynamicInput.name = "swimming";
dynamicInput.type = "number";
dynamicInput.value = '';
restrict = 0;
}
if(title){
title.textContent = "Swimming (0-100)";
}
if(breedDropdown){
breedDropdown.innerHTML = "";
breedOptions["Grey"].forEach((optionText) => {
const option = document.createElement("option");
option.value = optionText;
option.textContent = optionText;
breedDropdown.appendChild(option);
});
}
}
//Used to update the main table after each new pig is added
function updateTable(pigController: PigController){
const tableBody = document.getElementById('table-body');
if(tableBody){
tableBody.innerHTML = '';
const pigs = pigController.getPigs();
for(const pig of pigs){
const newRow = document.createElement('tr');
const name_Cell = document.createElement('td');
name_Cell.textContent = pig.name;
const category_Cell = document.createElement('td');
category_Cell.textContent = pig.category;
const delete_Cell = document.createElement('td');
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('delete-button');
deleteButton.addEventListener('click', (event) => deleteRow(event, pig.id));
delete_Cell.appendChild(deleteButton);
const more_Cell = document.createElement('td');
const moreButton = document.createElement('button');
moreButton.textContent = 'More Info';
moreButton.classList.add('more-info-button');
moreButton.addEventListener('click', (event) => showForm(event, pig.id));
more_Cell.appendChild(moreButton);
newRow.appendChild(name_Cell);
newRow.appendChild(category_Cell);
newRow.appendChild(more_Cell);
newRow.appendChild(delete_Cell);
tableBody.appendChild(newRow);
}
localStorage.setItem('pigs', JSON.stringify(pigs));
var graph = document.getElementById("hiddenGraph");
if(graph){
graph.style.display = "none";
}
}
}
//Used to update input form depending on chosen pig colour
function updateFormFields(category: string){
//Key value list to hold all breeds, used ChatGPT to help generate this map
const breedOptions: { [key: string]: string[] } = {
"Grey": ["Yorkshire Pig", "Duroc Pig", "Landrace Pig"],
"Chestnut": ["Kune Kune Pig", "Tamworth Pig", "Mangalitsa Pig"],
"White": ["Hampshire Pig", "Meishan Pig", "Saddleback Pig"],
"Black": ["Potbelly Pig", "Large Black Pig", "Pietrain Pig"]
};
let titleText = "";
let fieldInputName = "";
let fieldType = "text";
switch(category){
case "Grey":
titleText = "Swimming (0-100)";
fieldInputName = "swimming";
fieldType = "number";
restrict = 0;
break;
case "Chestnut":
titleText = "Language";
fieldInputName = "language";
restrict = 2;
break;
case "White":
titleText = "Running (0-100)";
fieldInputName = "running";
fieldType = "number";
restrict = 0;
break;
case "Black":
titleText = "Strength (1-10)";
fieldInputName = "strength";
fieldType = "number";
restrict = 1;
break;
default:
break;
}
if(breedDropdown){
breedDropdown.innerHTML = "";
breedOptions[category].forEach((optionText) =>{
const option = document.createElement("option");
option.value = optionText;
option.textContent = optionText;
breedDropdown.appendChild(option);
});
}
if(title){
title.textContent = titleText;
}
if(dynamicInput){
dynamicInput.name = fieldInputName;
dynamicInput.type = fieldType;
dynamicInput.value = '';
}
}
//Ensures the inputs stay within range of the input type
function AdjustInput(){
if(restrict == 0){
if(Number(dynamicInput.value) > 100){
dynamicInput.value = "100";
}
if(Number(dynamicInput.value) < 0){
dynamicInput.value = "0";
}
}
if(restrict == 1){
if(Number(dynamicInput.value) > 10){
dynamicInput.value = "10";
}
if(Number(dynamicInput.value) < 1){
dynamicInput.value = "1";
}
}
}
//Used to delete a row after delete button is pressed
function deleteRow(event: MouseEvent, pigId: number) {
const confirmed = confirm("Are you sure you want to delete this pig?");
if (confirmed) {
controller.removePig(pigId);
updateTable(controller);
}
}
//Used to show the pig info after more info button is pressed
function showForm(event: MouseEvent, pigId: number){
const name = document.getElementById('nameCell') as HTMLElement;
const breed = document.getElementById('breedCell') as HTMLElement;
const height = document.getElementById('heightCell') as HTMLElement;
const weight = document.getElementById('weightCell') as HTMLElement;
const dynamic = document.getElementById('dynamicCell') as HTMLElement;
const dynamicHeader = document.getElementById('dynamicHeader') as HTMLElement;
const personality = document.getElementById('personalityCell') as HTMLElement;
const selectedPig = controller.getPigs().find(pig => pig.id == pigId);
switch(selectedPig?.category){
case "Grey":
dynamicHeader.textContent = "Swimming (0-100)";
dynamic.textContent = (selectedPig as GreyPig).swimmingAbility.toString();
break;
case "Chestnut":
dynamicHeader.textContent = "Language";
dynamic.textContent = (selectedPig as ChestnutPig).language;
break;
case "White":
dynamicHeader.textContent = "Running (0-100)";
dynamic.textContent = (selectedPig as WhitePig).runningAbility.toString();
break;
case "Black":
dynamic.textContent = (selectedPig as BlackPig).strengthAbility.toString();
dynamicHeader.textContent = "Strength (0-10)";
break;
default:
break;
}
if(selectedPig?.name){
name.textContent = selectedPig?.name;
}
if(selectedPig?.breed){
breed.textContent = selectedPig?.breed;
}
if(selectedPig?.height){
height.textContent = selectedPig?.height.toString();
}
if(selectedPig?.weight){
weight.textContent = selectedPig?.weight.toString();
}
if(selectedPig?.personality){
personality.textContent = selectedPig?.personality;
}
}
function showInputForm(){
inputFormTemplate.style.display = "block";
}
//Used to hide and clear info form when the less info button is pressed
function hideInputForm(){
const name = document.querySelector('input[name="name"]') as HTMLInputElement;
const breed = document.querySelector('select[name="breed"]') as HTMLSelectElement;
const height = document.querySelector('input[name="height"]') as HTMLInputElement;
const weight = document.querySelector('input[name="weight"]') as HTMLInputElement;
const personality = document.querySelector('input[name="personality"]') as HTMLInputElement;
const category = document.querySelector('select[name="category"]') as HTMLSelectElement;
const dynamic = document.getElementById('dynamicForm') as HTMLInputElement;
name.value = '';
breed.selectedIndex = 0;
weight.value = '';
height.value = '';
personality.value = '';
category.selectedIndex = 0;
dynamic.value = '';
inputFormTemplate.style.display = "none";
if(dynamicInput){
dynamicInput.name = "swimming";
dynamicInput.type = "number";
dynamicInput.value = '';
restrict = 0;
}
if(title){
title.textContent = "Swimming (0-100)";
}
}