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
|
const path = window.location.pathname
// Setting up interactivity according to the current page
if (path == '/login') {
trim_inputs_on_blur()
} else if (path == '/') { // Payment table
allow_select_reset()
allow_date_reset()
} else if (path == '/payment') { // Payment creation
trim_inputs_on_blur()
auto_fill_category()
} else if (path.startsWith('/payment/')) { // Payment modification
trim_inputs_on_blur()
auto_fill_category()
control_remove_button()
} else if (path == '/income') { // Income creation
trim_inputs_on_blur()
} else if (path.startsWith('/income/')) { // Income modification
trim_inputs_on_blur()
control_remove_button()
} else if (path == '/category') { // Category creation
trim_inputs_on_blur()
} else if (path.startsWith('/category/')) { // Category modification
trim_inputs_on_blur()
control_remove_button()
} else if (path == '/balance') {
} else if (path == '/statistics') {
show_statistics()
}
// Functions
function trim_inputs_on_blur() {
document.querySelectorAll('input').forEach(input => {
input.addEventListener('blur', () => {
input.value = input.value.trim()
})
})
}
function allow_select_reset() {
document.querySelectorAll('select').forEach(select => {
const canBeReset = Array.from(select.options).find(option => option.disabled)
if (canBeReset) {
const button = document.createElement('input')
button.type = 'button'
button.value = 'Effacer'
button.className = 'g-Form__ResetSelect'
button.onclick = function() {
select.selectedIndex = 0
button.style = 'visibility: hidden'
}
if (select.selectedIndex === 0) {
button.style = 'visibility: hidden'
}
select.onchange = function() {
button.style = 'visibility: visible'
}
select.parentNode.appendChild(button)
}
})
}
function allow_date_reset() {
document.querySelectorAll('input[type="date"]').forEach(input => {
const button = document.createElement('input')
button.type = 'button'
button.value = 'Effacer'
button.className = 'g-Form__ResetSelect'
button.onclick = function() {
input.value = ''
button.style = 'visibility: hidden'
}
if (input.value === '') {
button.style = 'visibility: hidden'
}
input.onchange = function() {
button.style = 'visibility: visible'
}
input.parentNode.appendChild(button)
})
}
function control_remove_button() {
const removeInput = document.getElementsByName('remove-input')[0]
const removeButton = document.getElementById('remove-button')
console.log(removeInput, removeButton)
if (removeInput && removeButton) {
removeInput.addEventListener('input', () => {
if (removeInput.value.trim() == removeInput.getAttribute('data-name')) {
removeButton.removeAttribute('disabled')
} else {
removeButton.setAttribute('disabled', true)
}
})
}
}
function auto_fill_category() {
const name = document.getElementsByName('name')[0]
const category = document.getElementsByName('category_id')[0]
function onNameChange() {
const query = name.value.trim()
if (query) {
const xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = () => {
if (xhttp.readyState == 4 && xhttp.status == 200) {
category.value = xhttp.responseText
}
}
xhttp.open('GET', `/payment/category?payment_name=${query}`, true)
xhttp.send()
}
}
name.addEventListener('input', debounce(onNameChange, 500))
}
function show_statistics() {
const categories = JSON.parse(document.getElementById('categories').textContent)
const incomes = JSON.parse(document.getElementById('incomes').textContent)
const payments = JSON.parse(document.getElementById('payments').textContent)
const dates = incomes.map(i => i.date)
const datasets = [
{
label: 'Revenus',
data: incomes.map(i => i.amount),
fill: false,
backgroundColor: '#222222',
borderColor: '#222222',
hidden: true
}
]
const total_payments = {}
const categories_payments = {}
payments.forEach(p => {
if (categories_payments[p.category_id] === undefined) {
categories_payments[p.category_id] = {}
}
categories_payments[p.category_id][p.start_date] = p.cost
if (total_payments[p.start_date] === undefined) {
total_payments[p.start_date] = 0
}
total_payments[p.start_date] += p.cost
})
datasets.push({
label: 'Total des paiements',
data: dates.map(d => total_payments[d] || 0),
fill: false,
backgroundColor: '#555555',
borderColor: '#555555'
})
Object.keys(categories_payments).forEach(category_id => {
const category_payments = categories_payments[category_id]
const category = categories.find(c => c.id == category_id)
datasets.push({
label: category.name,
data: dates.map(d => category_payments[d] || 0),
fill: false,
backgroundColor: category.color,
borderColor: category.color,
})
})
const chart = new Chart(document.getElementById('g-Chart__Canvas').getContext('2d'), {
type: 'line',
data: {
labels: dates,
datasets
},
options: {
responsive: true,
tooltips: {
mode: 'nearest',
intersect: false,
callbacks: {
title: (tooltipItem, data) => {
return capitalize(prettyPrintMonth(tooltipItem[0].xLabel))
},
label: (tooltipItem, data) => {
let label = data.datasets[tooltipItem.datasetIndex].label || ''
if (label) {
label += ': '
}
label += `${tooltipItem.yLabel} €`
return label
}
}
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [
{
ticks: {
callback: prettyPrintMonth
}
}
],
yAxes: [
{
ticks: {
beginAtZero: true,
callback: value => `${value} €`
}
}
]
}
}
})
}
function debounce(callback, delay) {
let timeout
return function() {
clearTimeout(timeout)
timeout = setTimeout(callback, delay)
}
}
function prettyPrintMonth(isoDate) {
xs = isoDate.split('-')
months = [
'janvier',
'février',
'mars',
'avril',
'mai',
'juin',
'juillet',
'août',
'septembre',
'octobre',
'novembre',
'décembre'
]
return `${months[parseInt(xs[1]) - 1]} ${xs[0]}`
}
function capitalize(str) {
return str.replace(/^\w/, function (c) { return c.toUpperCase() })
}
|