-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
184 lines (158 loc) · 5.55 KB
/
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
var productList = [];
var draggedElement;
function loadLocalStorage() {
if(localStorage.productList) {
productList = JSON.parse(localStorage.productList)
console.log(productList)
}
}
function printHeaderList(lp, nazwa, ilosc, cena, suma) {
let div = document.getElementById('productList');
let table = document.createElement('table');
table.id = "paragon";
let tr = document.createElement('tr');
let th1 = document.createElement('th');
let th2 = document.createElement('th');
let th3 = document.createElement('th');
let th4 = document.createElement('th');
let th5 = document.createElement('th');
th1.innerHTML = lp;
tr.appendChild(th1)
th2.innerHTML = nazwa;
tr.appendChild(th2)
th3.innerHTML = ilosc;
tr.appendChild(th3)
th4.innerHTML = cena;
tr.appendChild(th4)
th5.innerHTML = suma;
tr.appendChild(th5)
table.appendChild(tr);
div.appendChild(table);
}
function printRow(lp, nazwa, ilosc, cena, suma) {
let table = document.getElementById('paragon');
let tr = document.createElement('tr');
let td1 = document.createElement('td');
let td2 = document.createElement('td');
let td3 = document.createElement('td');
let td4 = document.createElement('td');
let td5 = document.createElement('td');
let deleteButton = document.createElement('button');
if(lp != 'totalSum') {
tr.id = lp - 1;
td1.innerHTML = lp;
tr.draggable = true;
tr.addEventListener('dragstart', (element) => {
draggedElement = element.currentTarget;
})
tr.addEventListener('dragover', element => {
element.preventDefault();
})
tr.addEventListener('drop', (element) => {
let droppedElementId = element.currentTarget.id;
let tmp = productList[draggedElement.id];
productList[draggedElement.id] = productList[droppedElementId];
productList[droppedElementId] = tmp;
printList();
})
}
else {
tr.id = 'totalSum';
td1.innerHTML = '';
}
td2.innerHTML = nazwa;
td2.contentEditable = lp != 'totalSum' ? true : false;
td2.addEventListener("keyup", (element) => {
// onchange not working xD
if(element.currentTarget.innerHTML == '') {
alert("Niepoprawna wartość: nazwa")
}
else {
productList[element.currentTarget.parentElement.id].name = element.currentTarget.innerHTML;
}
});
td3.innerHTML = ilosc;
td3.contentEditable = lp != 'totalSum' ? true : false;
if(lp != 'totalSum') {
td3.addEventListener('mouseout', (element) => {
if(Number(element.currentTarget.innerHTML) <= 0 || (Number(element.currentTarget.innerHTML) * 100) %1 != 0) {
alert('Niepoprawna wartość: ilość')
}
else {
console.log(element.currentTarget.innerHTML)
productList[element.currentTarget.parentElement.id].amount = element.currentTarget.innerHTML;
printList()
}
})
}
td4.innerHTML = cena;
td4.contentEditable = lp != 'totalSum' ? true : false;
if(lp != 'totalSum') {
td4.addEventListener('mouseout', (element) => {
if(Number(element.currentTarget.innerHTML) <= 0 || (Number(element.currentTarget.innerHTML) * 100) %1 != 0) {
console.log(element.currentTarget.innerHTML)
alert("Niepoprawna wartość: cena")
}
else {
console.log(element.currentTarget.innerHTML)
productList[element.currentTarget.parentElement.id].price = element.currentTarget.innerHTML;
printList()
}
})
}
td5.innerHTML = suma;
deleteButton.innerHTML = "Usuń"
deleteButton.addEventListener('click', (element) => {
id = element.currentTarget.parentElement.id;
productList.splice(id, 1);
printList();
})
tr.appendChild(td1)
tr.appendChild(td2)
tr.appendChild(td3)
tr.appendChild(td4)
tr.appendChild(td5)
if(lp != 'totalSum') {
tr.appendChild(deleteButton)
}
table.appendChild(tr);
}
function printList() {
let table = document.getElementById('paragon')
let totalSum = 0;
while(table.childNodes.length > 1){
table.removeChild(table.lastChild);
}
productList.forEach((element, index) => {
printRow(index + 1, element.name, element.amount, element.price, Number((element.amount * element.price).toFixed(2)))
totalSum += element.amount * element.price;
});
localStorage.productList = JSON.stringify(productList)
printRow('totalSum', '', '', 'RAZEM', totalSum.toFixed(2))
}
function addProduct() {
let name = document.getElementById('productNameInput').value
let amount = document.getElementById('productAmountInput').value
let price = document.getElementById('productPriceInput').value
if(name === '') {
alert("Niepoprawna wartość: nazwa")
return
}
if(amount <= 0 || (amount * 100) %1 != 0) {
alert("Niepoprawna wartość: ilość")
return
}
if(price <= 0 || (price * 100) %1 != 0 ) {
alert("Niepoprawna wartość: cena")
return
}
productList.push({
name: name,
amount: amount,
price: Number(price).toFixed(2),
})
printList();
}
document.body.onload = loadLocalStorage();
printHeaderList('LP', 'NAZWA', 'ILOŚĆ', 'CENA', 'SUMA');
printList();