-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
176 lines (158 loc) · 5.03 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
import { setColor, solidColor, alphaColor } from "./randomColor.js";
// DATABASE
//
const db = firebase.firestore();
const tradesRef = db.collection('gutentrades');
// Cloud listener: onSnapshot as realtime listener
let trades
tradesRef.where("id", "!=", true)
.onSnapshot(function (querySnapshot) {
trades = []; // resets array preventing append the updated collection
querySnapshot.forEach(function (doc) {
trades.push(doc.data());
});
render();
newChart();
});
const tradeAddBtn = document.getElementById('trade-add-btn').addEventListener('click', () => updateTrade(true));
const tradeSubBtn = document.getElementById('trade-sub-btn').addEventListener('click', () => updateTrade(false));
function updateTrade(condition) {
if (selection) {
let tradeSelection = trades.find(i => i.id == selection);
condition == true ? tradeSelection.quant += 1 : tradeSelection.quant -= 1;
return tradesRef.doc(selection).update({
quant: tradeSelection.quant
})
.then(function () {
console.log("Document successfully updated!");
})
.catch(function (error) {
console.error("Error updating document: ", error);
});
}
}
// RENDERING
//
let selection;
function selectItem(id) {
selection = id;
focusItem();
}
function focusItem() {
let items = document.getElementsByClassName('item');
for (let e = 0; e < items.length; e++) {
items[e].style.borderColor = 'transparent'
}
if (selection) {
document.getElementById(selection).style.borderColor = 'rgba(255, 255, 255, 1)';
}
}
function addQuantUpdateStyle() {
if (selection) document.getElementById(selection).lastElementChild.classList.add('itemQuantUpdate');
}
let itemContainer = document.getElementById('itemContainer');
let chartLabel;
let chartQuant;
function render() {
itemContainer.innerHTML = '';
chartLabel = [];
chartQuant = [];
trades.sort((a,b) => b.quant - a.quant)
console.log(trades)
for (let e = 0; e < (trades.length); e++) {
// COLORS FOR THE CHART
setColor()
let item = document.createElement('div'); item.classList.add('item');
item.setAttribute('id', trades[e].id);
item.style.backgroundColor = alphaColor[e];
// item.style.borderColor = solidColor[e];
let itemName = document.createElement('div');
itemName.classList.add('itemName');
itemName.innerHTML = `${trades[e].name}`;
let itemQuant = document.createElement('div');
itemQuant.classList.add('itemQuant');
itemQuant.innerHTML = `${trades[e].quant}`;
item.appendChild(itemName);
item.appendChild(itemQuant);
itemContainer.appendChild(item);
chartLabel.push(trades[e].name);
chartQuant.push(trades[e].quant);
item.addEventListener('click', function () {
selectItem(this.getAttribute('id'));
});
};
focusItem();
addQuantUpdateStyle();
}
// CHART
//
let ctx = document.getElementById('chart').getContext('2d');
let myChart;
function newChart() {
if (myChart === undefined) {
myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: chartLabel,
datasets: [{
label: ' ',
data: chartQuant,
backgroundColor: alphaColor,
hoverBackgroundColor: solidColor,
borderColor: solidColor,
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: true,
scales: {
yAxes: [{
ticks: {
// fontColor: 'rgba(0, 250, 250, .7)'
},
gridLines: {
color: "rgba(255, 255, 255,.15)",
borderDash: [2]
}
}],
xAxes: [{
ticks: {
fontColor: 'white'
}
}]
},
layout: {
padding: {
left: 0,
right: 0,
top: 20,
bottom: 0
}
},
animation: {
duration: 0
},
legend: {
display: false,
}
}
});
} else {
updateConfigByMutating();
}
}
function updateConfigByMutating() {
myChart.data = {
labels: chartLabel,
datasets: [{
label: ' ',
data: chartQuant,
backgroundColor: alphaColor,
hoverBackgroundColor: solidColor,
borderColor: solidColor,
borderWidth: 1,
}]
}
myChart.update();
}