-
Notifications
You must be signed in to change notification settings - Fork 0
/
winelist.js
755 lines (689 loc) · 22.5 KB
/
winelist.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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
"use strict";
// Allowed: default, count_{asc,desc}, price_{asc,desc}, rating_{asc,desc},
// year_{asc,desc}, value_{asc,desc}, sweetness_{asc,desc}, age_{asc,desc}
var g_viewmode = "default";
var g_edit_mode = false;
var g_stock_mode = false;
var kAges = {
0: "unbekannt",
1: "zu jung",
2: "wird noch besser",
3: "genau richtig",
4: "muss weg",
5: "zu alt",
}
var kLang = {
checkmark: "\u2713",
save_button_text: "\u270E",
save_string: "Speichern",
plus_button_text: "+",
minus_button_text: "−",
delete_button_text: "Lö",
invalid_count: "Anzahl fehlt/ungültig!",
invalid_price: "Ungültiger Preis!",
invalid_vineyard: "Weingut fehlt!",
invalid_wine: "Wein fehlt!",
invalid_year: "Jahr fehlt/ungültig!",
}
var kPricePattern = "[0-9]{1,3}([\.|,][0-9]{1,2})?";
function FormatAge(int) {
return kAges[int];
}
function ShowOnlyExisting() {
return document.getElementById("show_only_existing").checked ? 1 : 0;
}
function ReasonAdd() {
return document.getElementById("default_reason_add").value;
}
function IsEditMode() {
return g_edit_mode;
}
function IsStockMode() {
return g_stock_mode;
}
function ClickVineyardHeader() {
g_viewmode = "default";
PopulateList();
}
function SetViewMode(primary, secondary) {
if (g_viewmode === primary) {
g_viewmode = secondary;
} else {
g_viewmode = primary;
}
PopulateList();
}
function ClickYearHeader() {
SetViewMode("year_asc", "year_desc");
}
function ClickCountHeader() {
SetViewMode("count_desc", "count_asc");
}
function ClickPriceHeader() {
SetViewMode("price_asc", "price_desc");
}
function ClickRatingHeader() {
SetViewMode("rating_desc", "rating_asc");
}
function ClickValueHeader() {
SetViewMode("value_desc", "value_asc");
}
function ClickSweetnessHeader() {
SetViewMode("sweetness_desc", "sweetness_asc");
}
function ClickAgeHeader() {
SetViewMode("age_asc", "age_desc");
}
function ClickPlus(event) {
var yearid = event.target.parentElement.parentElement.year_id;
if (IsStockMode()) {
SendPost("add_stock", ClickPlus_Callback, {yearid});
} else {
var reason = document.getElementById("default_reason_add").value;
SendPost("add_bottle", ClickPlus_Callback, {yearid, reason});
}
}
function ClickMinus(event) {
var yearid = event.target.parentElement.parentElement.year_id;
if (IsStockMode()) {
SendPost("remove_stock", ClickPlus_Callback, {yearid})
} else {
var reason = document.getElementById("default_reason_delete").value;
SendPost("remove_bottle", ClickPlus_Callback, {yearid, reason});
}
}
function ClickDelete(event) {
var year_id = event.target.parentElement.parentElement.year_id;
if (!confirm("Diesen Wein (samt Preis, Kommentar, Bewertung) löschen?")) {
return;
}
SendPost("delete_year", PopulateList, {year_id});
}
function UpdateMinusButton(minus_button, count, opt_real_count) {
if (IsStockMode()) {
SetInnerText(minus_button, kLang.minus_button_text);
if (count === 0) {
minus_button.disabled = true;
} else {
minus_button.disabled = false;
minus_button.onclick = ClickMinus;
}
var td = minus_button.parentNode;
if (opt_real_count !== undefined) {
minus_button.real_count = opt_real_count;
var real_count = document.createTextNode(" (" + opt_real_count + ")");
if (minus_button.nextSibling === null) {
td.appendChild(real_count);
} else {
td.replaceChild(real_count, minus_button.nextSibling);
}
}
if (minus_button.real_count === count) {
td.setAttribute("class", "");
} else {
td.setAttribute("class", "highlight");
}
} else {
minus_button.disabled = false;
if (count === 0) {
minus_button.onclick = ClickDelete;
SetInnerText(minus_button, kLang.delete_button_text)
} else {
minus_button.onclick = ClickMinus;
SetInnerText(minus_button, kLang.minus_button_text);
}
}
}
function ClickPlus_Callback() {
var update = GetResponse(this);
var tr = document.getElementById("year_" + update.yearid);
// vineyard -> wine -> year -> count
var td = tr.firstChild.nextSibling.nextSibling.nextSibling;
var minus_button = td.firstChild.nextSibling.nextSibling;
var count = IsStockMode() ? update.stock : update.count;
SetInnerText(td, count);
UpdateMinusButton(minus_button, count);
if (!IsStockMode()) {
PopulateLog();
UpdateTotals();
}
}
function ClickEdit(event) {
SetInnerText(event.target, kLang.save_string);
event.target.onclick = ClickSave;
var td_comment = event.target.parentElement.previousSibling;
ReplaceTextWithInput(td_comment, 0, KeyUp);
var td_price = td_comment.previousSibling;
var price_input = ReplaceTextWithInput(td_price, 4, KeyUp);
price_input.pattern = kPricePattern;
}
function KeyUp(event) {
if (event.key === "Enter") {
var e = event.target.parentElement.nextSibling;
while (e.firstChild.nodeName !== "BUTTON") e = e.nextSibling;
ClickSaveButton(e.firstChild, true);
event.preventDefault();
} else if (event.key === "Escape") {
var e = event.target.parentElement.nextSibling;
while (e.firstChild.nodeName !== "BUTTON") e = e.nextSibling;
ClickSaveButton(e.firstChild, false);
event.preventDefault();
}
}
function ClickSave(event) {
ClickSaveButton(event.target, true);
}
function ClickSaveButton(button, actually_save) {
var year_id = button.parentElement.parentElement.year_id;
var td_comment = button.parentElement.previousSibling;
var td_price = td_comment.previousSibling;
if (actually_save) {
var price_input = td_price.firstChild;
if (!price_input.reportValidity()) {
alert(kLang.invalid_price);
return;
}
}
SetInnerText(button, kLang.save_button_text);
button.onclick = ClickEdit;
if (actually_save) {
var comment = ReplaceInputWithText(td_comment);
var price = ParsePrice(ReplaceInputWithText(td_price));
SendPost("update", null, {year_id, price, comment});
} else {
ReplaceInputWithText(td_comment, true);
ReplaceInputWithText(td_price, true);
}
}
function ClickApplyYear(event) {
var yearid = event.target.parentNode.parentNode.year_id;
SendPost("apply_stock", ApplyStock_Callback, {yearid});
}
function ClickApplyWine(event) {
event.stopPropagation();
var wineid = event.target.parentNode.wine_id;
SendPost("apply_stock_wine", ApplyStockWine_Callback, {wineid});
}
function ClickApplyVineyard(event) {
event.stopPropagation();
var vineyard_id = event.target.parentNode.vineyard_id;
SendPost("apply_stock_vineyard", ApplyStockWine_Callback, {vineyard_id});
}
function ClickApplyAll(event) {
if (!confirm("Dies überschreibt den Bestand aller Weine mit den " +
"Inventur-Daten. Sicher?")) {
return;
}
SendPost("apply_stock_all", ApplyStockWine_Callback);
}
function ClickResetAllStock(event) {
if (!confirm("Dies setzt alle Inventur-Daten auf 0 zurück. Sicher?")) {
return;
}
SendPost("reset_stock_all", ResetAllStock_Callback)
}
function ApplyStock(year_id, count) {
var tr = document.getElementById("year_" + year_id);
var count_td = tr.firstChild.nextSibling.nextSibling.nextSibling;
SetInnerText(count_td, count);
var minus_button = count_td.firstChild.nextSibling.nextSibling;
UpdateMinusButton(minus_button, count, count);
}
function ApplyStock_Callback() {
var data = GetResponse(this);
ApplyStock(data.yearid, data.count);
}
function ApplyStockWine_Callback() {
var data = GetResponse(this);
for (var year_id in data) {
var count = data[year_id].count;
ApplyStock(year_id, count);
}
}
function ResetAllStock_Callback() {
var winelist = document.getElementById("winelist");
for (var tr = winelist.firstChild; tr !== null; tr = tr.nextSibling) {
var count_td = tr.firstChild.nextSibling.nextSibling.nextSibling;
SetInnerText(count_td, 0)
var minus_button = count_td.firstChild.nextSibling.nextSibling;
UpdateMinusButton(minus_button, 0);
}
}
function ToggleShowExisting() {
PopulateList();
}
function ToggleEditMode() {
let checkbox = document.getElementById("edit_mode");
g_edit_mode = checkbox.checked;
let container = checkbox.parentNode.parentNode;
if (IsEditMode()) {
container.setAttribute("class", "setting checked");
} else {
container.setAttribute("class", "setting");
}
PopulateList();
}
function ToggleStockMode() {
let checkbox = document.getElementById("stock_mode");
g_stock_mode = checkbox.checked;
let container = checkbox.parentNode.parentNode;
let controls = document.getElementById("stock_mode_controls");
if (IsStockMode()) {
container.setAttribute("class", "setting checked");
controls.style.display = "block";
} else {
container.setAttribute("class", "setting");
controls.style.display = "none";
}
PopulateList();
}
function CollectInputValues(data, year_input, count_input, price_input,
comment_input) {
data.year = year_input.value;
data.count = count_input.value;
data.price = ParsePrice(price_input.value);
data.comment = comment_input.value;
data.reason = ReasonAdd();
data.only_existing = ShowOnlyExisting();
if (data.year === "") {
alert(kLang.invalid_year);
return false;
}
if (data.count === "") {
alert(kLang.invalid_count);
return false;
}
if (!price_input.reportValidity()) {
alert(kLang.invalid_price);
return false;
}
return true;
}
function CollectFields(wine_td, obj) {
var year_td = wine_td.nextSibling;
var count_td = year_td.nextSibling;
var price_td = count_td.nextSibling;
var comment_td = price_td.nextSibling;
return CollectInputValues(obj, year_td.firstChild, count_td.firstChild,
price_td.firstChild, comment_td.firstChild);
}
function ClickAddYear(event) {
var tr = event.target.parentNode.parentNode;
var wine_id = tr.wine_id;
var vineyard_td = tr.firstChild;
var wine_td = vineyard_td.nextSibling;
var options = {wine_id};
if (CollectFields(wine_td, options)) {
SendPost("add_year", PopulateList_Callback, options);
}
}
function ClickAddWine(event) {
var tr = event.target.parentNode.parentNode;
var vineyard_id = tr.vineyard_id;
var vineyard_td = tr.firstChild;
var wine_td = vineyard_td.nextSibling;
var wine = wine_td.firstChild.value;
var options = {vineyard_id, wine};
if (wine === "") {
alert(kLang.invalid_wine);
return;
}
if (CollectFields(wine_td, options)) {
SendPost("add_wine", PopulateList_Callback, options);
}
}
function ClickRating(event) {
return ClickRatingGeneric(event, "rating");
}
function ClickValue(event) {
return ClickRatingGeneric(event, "value");
}
function ClickSweetness(event) {
return ClickRatingGeneric(event, "sweetness");
}
function ClickRatingGeneric(event, what) {
var input = event.target.previousSibling;
input.checked = true;
var val = input.value;
// label -> fieldset -> td -> tr
var tr = event.target.parentNode.parentNode.parentNode;
var year_id = tr.year_id;
SendPost("update_rating", null, {year_id, what, val});
}
function ClickAge(event) {
var select = event.target;
var td = select.parentElement;
var tr = td.parentElement;
if (td !== tr.lastChild) console.log("Error: age must be last child of tr");
var year_id = tr.year_id;
var val = select.value;
var what = "age";
SendPost("update_rating", AgeChange_Callback, {year_id, what, val});
}
function AgeChange_Callback() {
var data = GetResponse(this);
var tr = document.getElementById("year_" + data.yearid);
var td = tr.lastChild;
SetInnerText(td, FormatAge(data.age));
}
function PopulateList() {
var only_existing = ShowOnlyExisting();
if (g_viewmode === "default") {
SendGet("get_all", PopulateList_Callback, {only_existing});
} else {
var sortby = g_viewmode;
SendGet("get_sorted", PopulateList_Sorted, {only_existing, sortby});
}
}
function MaybeAddApplyButton(td, callback) {
if (!IsStockMode()) return;
var apply_button = document.createElement("button");
apply_button.appendChild(document.createTextNode(kLang.checkmark));
apply_button.onclick = callback;
apply_button.setAttribute("class", "apply");
td.appendChild(apply_button);
}
function MakeVineyardTd(name, vineyard_id, region) {
var td = document.createElement("td");
if (name) {
td.appendChild(document.createTextNode(name));
td.onclick = ShowVineyardEdit;
td.vineyard_id = vineyard_id;
td.setAttribute("class", "vineyard");
td.setAttribute("title", region);
td.id = "vineyard_" + vineyard_id;
MaybeAddApplyButton(td, ClickApplyVineyard);
}
return td;
}
function MakeWineTd(name, wine_id, grape) {
var td = document.createElement("td");
if (name) {
td.appendChild(document.createTextNode(name));
td.onclick = ShowWineEdit;
td.wine_id = wine_id;
td.setAttribute("class", "wine");
td.setAttribute("title", grape);
td.id = "wine_" + wine_id;
MaybeAddApplyButton(td, ClickApplyWine);
}
return td;
}
function MakeYearTd(year) {
var td = document.createElement("td");
td.appendChild(document.createTextNode(year));
MaybeAddApplyButton(td, ClickApplyYear);
return td;
}
function MakeCountTd(count, real_count) {
var td = document.createElement("td");
td.appendChild(document.createTextNode(count));
var plus_button = document.createElement("button");
plus_button.appendChild(document.createTextNode(kLang.plus_button_text));
plus_button.onclick = ClickPlus;
plus_button.setAttribute("class", "plus");
td.appendChild(plus_button);
var minus_button = document.createElement("button");
td.appendChild(minus_button);
minus_button.appendChild(document.createTextNode("")); // For replacing.
UpdateMinusButton(minus_button, count, real_count);
minus_button.setAttribute("class", "minus");
return td;
}
function MakeButtonsTd() {
var td = document.createElement("td");
var button_edit = document.createElement("button");
button_edit.onclick = ClickEdit;
button_edit.appendChild(document.createTextNode(kLang.save_button_text));
button_edit.setAttribute("class", "edit");
td.appendChild(button_edit);
return td;
}
function MakeRatingTd(year_id, current) {
var labels = ["Herausragend", "Sehr gut", "Solide", "Mäßig", "Schlecht"];
return MakeGenericRatingTd(labels, "rating", ClickRating, year_id, current);
}
function MakeValueTd(year_id, current) {
var labels = ["Herausragend", "Sehr gut", "Solide", "Mäßig", "Schlecht"];
return MakeGenericRatingTd(labels, "value", ClickValue, year_id, current);
}
function MakeSweetnessTd(year_id, current) {
var labels = ["Dessertwein", "feinherb", "fruchtig", "trocken", "sauer"];
return MakeGenericRatingTd(labels, "sweetness", ClickSweetness, year_id,
current);
}
function MakeGenericRatingTd(labels, radioname, callback, id, current) {
var td = document.createElement("td");
var fieldset = document.createElement("fieldset");
fieldset.setAttribute("class", "rating");
var count = labels.length;
for (var i = 0; i < count; i++) {
var input = document.createElement("input");
var name = radioname + id;
var input_id = name + i;
input.id = input_id;
input.type = "radio";
input.name = name;
input.value = (count - i);
if (current === (count - i)) input.checked = true;
if (i === 0) input.setAttribute("class", "fivestar");
var label = document.createElement("label");
label.for = input_id;
label.title = labels[i];
label.onclick = callback;
fieldset.appendChild(input);
fieldset.appendChild(label);
}
td.appendChild(fieldset);
return td;
}
function MakeAgeTd(current) {
var td = document.createElement("td");
td.appendChild(document.createTextNode(FormatAge(current)));
var age_select = document.createElement("select");
age_select.setAttribute("class", "reason");
age_select.onchange = ClickAge;
for (var i in kAges) {
var option = document.createElement("option");
option.value = i;
option.appendChild(document.createTextNode(FormatAge(i)));
if (i == current) option.setAttribute("selected", true);
age_select.appendChild(option);
}
td.appendChild(age_select);
return td;
}
function AppendEditModeRow(edit_mode, winelist, what, parent_id) {
if (!edit_mode) return;
var tr = document.createElement("tr");
AppendTextTd(tr, ""); // Vineyard.
var callback;
if (what === "year") {
tr.wine_id = parent_id;
callback = ClickAddYear;
AppendTextTd(tr, ""); // Wine.
} else {
tr.vineyard_id = parent_id;
callback = ClickAddWine;
AppendInputTd(tr, "neuer Wein");
}
AppendInputTd(tr, "Jahr", 1900, 2200, 1);
AppendInputTd(tr, "Anzahl", 0, 9999, 1);
var price_input = AppendInputTd(tr, "Preis");
price_input.pattern = kPricePattern;
AppendInputTd(tr, "Kommentar");
var button_td = document.createElement("td");
var button_add = document.createElement("button");
button_add.appendChild(document.createTextNode(kLang.checkmark));
button_add.setAttribute("class", "add");
button_add.onclick = callback;
button_td.appendChild(button_add);
tr.appendChild(button_td);
winelist.appendChild(tr);
}
function PopulateList_Callback() {
var winelist = document.getElementById("winelist");
DropAllChildren(winelist);
var edit_mode = IsEditMode();
var all_wines = GetResponse(this);
for (var vineyard in all_wines) {
var first_wine = true;
var vineyard_data = all_wines[vineyard];
var wines = vineyard_data.wines;
for (var wine in wines) {
var first_year = true;
var wine_data = wines[wine];
var years = wine_data.years;
for (var year in years) {
var data = years[year];
var tr = document.createElement("tr");
tr.id = "year_" + data.year_id;
tr.year_id = data.year_id;
// Vineyard.
if (first_wine) {
tr.appendChild(
MakeVineyardTd(vineyard, vineyard_data.id, vineyard_data.region));
} else {
tr.appendChild(MakeVineyardTd(null, null, null));
}
// Wine.
if (first_year) {
tr.appendChild(MakeWineTd(wine, wine_data.id, wine_data.grape));
} else {
tr.appendChild(MakeWineTd(null, null, null));
}
// Year.
tr.appendChild(MakeYearTd(year));
// Count.
let count = IsStockMode() ? data.stock : data.count;
tr.appendChild(MakeCountTd(count, data.count));
// Price, comment.
AppendTextTd(tr, FormatPrice(data.price));
AppendTextTd(tr, data.comment);
// Buttons.
tr.appendChild(MakeButtonsTd());
// Ratings.
tr.appendChild(MakeRatingTd(data.year_id, data.rating));
tr.appendChild(MakeValueTd(data.year_id, data.value));
tr.appendChild(MakeSweetnessTd(data.year_id, data.sweetness));
tr.appendChild(MakeAgeTd(data.age));
winelist.appendChild(tr);
first_wine = false;
first_year = false;
} // for year in years
AppendEditModeRow(edit_mode, winelist, "year", wine_data.id);
} // for wine in wines
AppendEditModeRow(edit_mode, winelist, "wine", vineyard_data.id);
}
PopulateVineyards();
PopulateLog();
UpdateTotals();
}
function PopulateList_Sorted() {
var winelist = document.getElementById("winelist");
DropAllChildren(winelist);
var response = GetResponse(this);
for (var wine of response) {
var tr = document.createElement("tr");
tr.id = "year_" + wine.year_id;
tr.year_id = wine.year_id;
// Vineyard.
tr.appendChild(MakeVineyardTd(wine.vineyard_name, wine.vineyard_id,
wine.region));
// Wine.
tr.appendChild(MakeWineTd(wine.wine_name, wine.wine_id, wine.grape));
// Year.
tr.appendChild(MakeYearTd(wine.year));
// Count, price, comment.
let count = IsStockMode() ? wine.stock : wine.count;
tr.appendChild(MakeCountTd(count, wine.count));
AppendTextTd(tr, FormatPrice(wine.price));
AppendTextTd(tr, wine.comment);
// Buttons.
tr.appendChild(MakeButtonsTd());
// Ratings.
tr.appendChild(MakeRatingTd(wine.year_id, wine.rating));
tr.appendChild(MakeValueTd(wine.year_id, wine.value));
tr.appendChild(MakeSweetnessTd(wine.year_id, wine.sweetness));
tr.appendChild(MakeAgeTd(wine.age));
winelist.append(tr);
}
}
function UpdateVineyard() {
var update = GetResponse(this);
var td = document.getElementById("vineyard_" + update.id);
SetInnerText(td, update.name);
td.setAttribute("title", update.region);
}
function UpdateWine() {
var update = GetResponse(this);
var td = document.getElementById("wine_" + update.id);
SetInnerText(td, update.name);
td.setAttribute("title", update.grape);
}
function AddWine() {
var vineyard_input = document.getElementById("add_vineyard");
var wine_input = document.getElementById("add_wine");
var year_input = document.getElementById("add_year");
var count_input = document.getElementById("add_count");
var price_input = document.getElementById("add_price");
var comment_input = document.getElementById("add_comment");
var data = {
vineyard: vineyard_input.value,
wine: wine_input.value,
};
if (data.vineyard === "") {
alert(kLang.invalid_vineyard);
return;
}
if (data.wine === "") {
alert(kLang.invalid_wine);
return;
}
if (!CollectInputValues(data, year_input, count_input, price_input,
comment_input)) {
return;
}
SendPost("add_all", PopulateList_Callback, data);
vineyard_input.value = "";
wine_input.value = "";
year_input.value = "";
count_input.value = "";
price_input.value = "";
comment_input.value = "";
}
function PopulateDataList(datalist, options) {
DropAllChildren(datalist);
for (var o of options) {
var option = document.createElement("option");
option.value = o;
datalist.appendChild(option);
}
}
function PopulateVineyards() {
SendGet("get_vineyards", PopulateVineyards_Callback);
}
function PopulateVineyards_Callback() {
var vineyards_list = document.getElementById("vineyards_completions");
PopulateDataList(vineyards_list, GetResponse(this));
}
function PopulateWines() {
var vineyard = document.getElementById("add_vineyard").value;
if (!vineyard) return;
DropAllChildren(document.getElementById("wines_completions"));
SendGet("get_wines", PopulateWines_Callback, {vineyard: vineyard});
}
function PopulateWines_Callback() {
var wines_list = document.getElementById("wines_completions");
PopulateDataList(wines_list, GetResponse(this));
}
function UpdateTotals() {
SendGet("get_totals", UpdateTotals_Callback);
}
function UpdateTotals_Callback() {
var data = GetResponse(this);
var count = document.getElementById("total_count");
SetInnerText(count, data.count);
var price = document.getElementById("total_price");
SetInnerText(price, FormatPrice(data.price));
}