-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
137 lines (114 loc) · 5.3 KB
/
scripts.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
function resizeCanvas() {
const canvas = document.getElementById('silverBarCanvas');
const container = document.querySelector('.silver-bar-container');
// Set canvas dimensions to match the container
canvas.width = container.clientWidth * 0.9; // 90% of the canvas width
canvas.height = container.clientHeight * 0.1; // 10% of the canvas height
}
function drawSilverBar() {
resizeCanvas(); // Ensure the canvas is resized before drawing
const canvas = document.getElementById('silverBarCanvas');
const context = canvas.getContext('2d');
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw the silver bar
const barX = (canvas.width - canvas.width) / 2;
const barY = (canvas.height - canvas.height) / 2;
context.fillStyle = 'silver';
context.fillRect(barX, barY, canvas.width, canvas.height);
// Add a shadow effect
context.shadowColor = 'rgba(0, 0, 0, 0.5)';
context.shadowBlur = 10;
context.shadowOffsetX = 5;
context.shadowOffsetY = 5;
}
// Draw the silver bar when the window is resized
window.addEventListener('resize', drawSilverBar);
// Initial draw
window.addEventListener('load', drawSilverBar);
function appendToSummary(item, quantity) {
const list = document.querySelector("#summary-list");
const selectedOptions = Array.from(document.querySelectorAll('input[name="pack"]:checked')).map(option => ({
value: option.value,
type: option.dataset.type
}));
// Find the main item entry
let mainItem = Array.from(list.children).find((li) => li.dataset.item === item);
if (!mainItem) {
// Create main item entry if it doesn't exist
mainItem = document.createElement("li");
mainItem.dataset.item = item;
mainItem.textContent = `${item}`;
list.appendChild(mainItem);
}
// Remove main item's remove button if it has sub-items
if (mainItem.querySelector('button')) {
mainItem.removeChild(mainItem.querySelector('button'));
}
// Add or update sub-entries for "and" options
selectedOptions.forEach(option => {
let subItem = Array.from(mainItem.children).find((li) => li.dataset.option === option.value);
if (subItem) {
// Update quantity of existing sub-item
const existingQuantity = parseInt(subItem.dataset.quantity, 10);
const newQuantity = existingQuantity + parseInt(quantity, 10);
subItem.dataset.quantity = newQuantity;
subItem.textContent = `${option.value} (Quantity: ${newQuantity})`;
} else {
// Create new sub-item
subItem = document.createElement("li");
subItem.dataset.option = option.value;
subItem.dataset.quantity = quantity;
subItem.textContent = `${option.value} (Quantity: ${quantity})`;
// Add remove button to sub-item
const subRemoveButton = document.createElement("button");
subRemoveButton.textContent = "remove from order";
subRemoveButton.onclick = function() {
let currentQuantity = parseInt(subItem.dataset.quantity, 10);
if (currentQuantity > 1) {
subItem.dataset.quantity = --currentQuantity;
subItem.textContent = `${option.value} (Quantity: ${currentQuantity})`;
} else {
mainItem.removeChild(subItem);
if (mainItem.children.length === 0) {
list.removeChild(mainItem);
}
}
};
subItem.appendChild(subRemoveButton);
mainItem.appendChild(subItem);
}
});
// If no sub-items are selected, update or create the main item
if (selectedOptions.length === 0) {
if (mainItem.querySelector('.quantity')) {
// Update quantity of existing main item
const existingQuantity = parseInt(mainItem.dataset.quantity, 10);
const newQuantity = existingQuantity + parseInt(quantity, 10);
mainItem.dataset.quantity = newQuantity;
mainItem.querySelector('.quantity').textContent = `Quantity: ${newQuantity}`;
} else {
// Create new main item with remove button
mainItem.dataset.quantity = quantity;
const textSpan = document.createElement("span");
textSpan.textContent = `${item} `;
const quantitySpan = document.createElement("span");
quantitySpan.className = 'quantity';
quantitySpan.textContent = `Quantity: ${quantity}`;
const removeButton = document.createElement("button");
removeButton.textContent = "remove from order";
removeButton.onclick = function() {
let currentQuantity = parseInt(mainItem.dataset.quantity, 10);
if (currentQuantity > 1) {
mainItem.dataset.quantity = --currentQuantity;
quantitySpan.textContent = `Quantity: ${currentQuantity}`;
} else {
list.removeChild(mainItem);
}
};
mainItem.appendChild(textSpan);
mainItem.appendChild(quantitySpan);
mainItem.appendChild(removeButton);
}
}
}