-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
230 lines (203 loc) · 6.08 KB
/
main.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
// declaring & assigning inputs/variables
let title = document.getElementById('title');
let price = document.getElementById('price');
let taxes = document.getElementById('taxes');
let ads = document.getElementById('ads');
let discount = document.getElementById('discount');
let total = document.getElementById('total');
let count = document.getElementById('count');
let category = document.getElementById('category');
let submit = document.getElementById('submit');
let warningMsg = document.getElementById('warning');
let mode = 'create'; //intializing mode to be create
let tmp;
let dataPro; //array of data
let searchMode = 'title'; //intialize search mode for title
var newPro = new Object();
var table = '';
// --- keeping data if exist in local storage
if ( localStorage.product != null ) {
dataPro = JSON.parse(localStorage.product);
}
else {
dataPro=[];
}
// --- function get total
function getTotal()
{
if ( price.value != '' ) {
let result = +price.value + +taxes.value +
+ads.value - +discount.value;
total.innerHTML = result;
total.style.background= '#1abc9c';
}
else {
total.innerHTML = '';
total.style.background='#bdc3c7';
}
}
// --- function write data in object newPro
function writeData() {
newPro.title=title.value.toLowerCase();
newPro.price=price.value;
newPro.taxes=taxes.value;
newPro.ads=ads.value;
newPro.discount=discount.value;
newPro.total=total.innerHTML;
newPro.count=count.value;
newPro.category=category.value.toLowerCase();
}
// --- function create button
function createButton() {
writeData();
if ( title.value != '' && price.value !='' && category.value !='' && newPro.count < 101 ) {
if ( mode === 'create' ) { // create mode
createProduct();
}
else { // update mode
updateProduct();
}
warningMsg.innerHTML='';
clearData();
}
else {
warningMsg.innerHTML= `<h4 style="text-align: center; margin-top:20px; text-transform: uppercase; color:#e74c3c;">Please Enter Valid Data</h4>`
}
localStorage.setItem('product', JSON.stringify(dataPro) );
showData();
}
// --- function create product
function createProduct() {
if ( newPro.count > 1 ) {
for(let i = 0; i< newPro.count; i++)
{dataPro.push(newPro);}
}
else {
{dataPro.push(newPro);}
}
}
// --- function update product
function updateProduct() {
dataPro[tmp] = newPro;
mode = 'create';
submit.innerHTML = 'Create';
count.style.display = 'block';
}
// --- function clear inputs
function clearData() {
title.value = '';
price.value = '';
taxes.value = '';
ads.value = '';
discount.value = '';
total.innerHTML = '';
count.value = '';
category.value = '';
}
// --- function write table
function writeTable( ) {
for ( let i = 0; i < dataPro.length; i++ ) {
table += `
<tr>
<td>${i+1}</td>
<td>${dataPro[i].title}</td>
<td>${dataPro[i].price}</td>
<td>${dataPro[i].taxes}</td>
<td>${dataPro[i].ads}</td>
<td>${dataPro[i].discount}</td>
<td>${dataPro[i].total}</td>
<td>${dataPro[i].category}</td>
<td><button onclick="updateData(${i})" id="update">update</button></td>
<td><button onclick="deleteData(${i})" id="delete">delete</button></td>
</tr>`;
}
}
// --- function show data
function showData()
{ getTotal()
table = '';
writeTable()
document.getElementById('tbody').innerHTML = table;
let btnDelete = document.getElementById('deleteAll')
if ( dataPro.length > 0 ) {
btnDelete.innerHTML = `
<button onclick="deleteAll()">Delete All (${dataPro.length})</button>`
}
else {
btnDelete.innerHTML='';
}
}
showData()
// --- function delete button
function deleteData(i) {
dataPro.splice(i,1);
localStorage.product = JSON.stringify(dataPro);
showData()
}
// --- function delete all button
function deleteAll() {
localStorage.clear()
dataPro.splice(0)
showData()
}
// --- function update button
function updateData(para) {
title.value = dataPro[para].title;
price.value = dataPro[para].price;
taxes.value = dataPro[para].taxes;
ads.value = dataPro[para].ads;
discount.value = dataPro[para].discount;
category.value = dataPro[para].category;
getTotal()
count.style.display = 'none';
submit.innerHTML = 'Update';
mode = 'update';
tmp = para;
scroll({top:0,behavior:'smooth'});
}
// --- function get search mode
function getSearchMode(id) {
let search = document.getElementById('search');
if ( id == 'searchTitle' ) {
searchMode = 'title';
}
else {
searchMode = 'category';
}
search.placeholder = 'Search by '+ searchMode;
search.focus()
search.value = '';
showData()
}
// --- function search for data
function searchData(value) {
table='';
for ( let i = 0; i < dataPro.length; i++ ) {
if ( searchMode == 'title' ) {
if ( dataPro[i].title.includes( value.toLowerCase() ) ) {
searchResult(i)
}
}
else { if ( dataPro[i].category.includes( value.toLowerCase() ) ) {
searchResult(i)
}
}
}
document.getElementById('tbody').innerHTML = table;
}
// --- function search result table
function searchResult(i) {
table += `
<tr>
<td>${i+1}</td>
<td>${dataPro[i].title}</td>
<td>${dataPro[i].price}</td>
<td>${dataPro[i].taxes}</td>
<td>${dataPro[i].ads}</td>
<td>${dataPro[i].discount}</td>
<td>${dataPro[i].total}</td>
<td>${dataPro[i].category}</td>
<td><button onclick="updateData(${i})" id="update">update</button></td>
<td><button onclick="deleteData(${i})" id="delete">delete</button></td>
</tr>`;
}