openvibers/openvibe.finance publicfinance.js

finance.js

main 3.7 KB · 65 lines · javascript

1// OpenVibe.Finance: a chosen CSV, OFX or QFX file goes into the paste box; a PDF statement is read as data for the server
2// to read (the form posts it to /accounts/:id/statement, which shows what it found before anything is imported).
3(function () {
4 document.addEventListener('change', function (e) {
5 var input = e.target;
6 if (!input || !input.matches || !input.matches('input[data-fin-file="csv"]') || !input.files || !input.files[0]) return;
7 var file = input.files[0];
8 var form = input.closest('form');
9 input.setCustomValidity('');
10 var isPdf = /\.pdf$/i.test(file.name) || file.type === 'application/pdf';
11 var limit = isPdf ? 6 : 4;
12 if (file.size > limit * 1024 * 1024) { input.setCustomValidity('That file is over ' + limit + ' MB. Export fewer months at a time.'); input.reportValidity(); input.value = ''; return; }
13 var box = form && form.querySelector('textarea[data-fin-csv]');
14 var pdf = form && form.querySelector('[data-fin-pdf]');
15 var named = input.closest('label') && input.closest('label').querySelector('[data-fin-file-name]');
16 if (named) named.textContent = file.name;
17 var reader = new FileReader();
18 if (isPdf && pdf && form.getAttribute('data-fin-statement')) {
19 if (!form.getAttribute('data-fin-import')) form.setAttribute('data-fin-import', form.getAttribute('action'));
20 form.setAttribute('action', form.getAttribute('data-fin-statement'));
21 if (box) { box.required = false; box.value = ''; }
22 var nm = form.querySelector('[data-fin-pdf-name]'); if (nm) nm.value = file.name;
23 reader.onload = function () { pdf.value = String(reader.result || ''); };
24 reader.readAsDataURL(file);
25 return;
26 }
27 if (form && form.getAttribute('data-fin-import')) form.setAttribute('action', form.getAttribute('data-fin-import'));
28 if (pdf) pdf.value = '';
29 if (box) box.required = true;
30 if (!box) return;
31 reader.onload = function () { box.value = String(reader.result || ''); };
32 reader.readAsText(file);
33 });
34})();
35
36// A receipt photo (v0.12): redrawn at most 2000 px (enough to read, and the camera's own data is not carried over), then
37// sent once for this server to read; nothing is added before the person checks it.
38(function () {
39 document.addEventListener('change', function (e) {
40 var input = e.target;
41 if (!input || !input.matches || !input.matches('input[data-fin-receipt]') || !input.files || !input.files[0]) return;
42 var form = input.closest('form');
43 var out = form && form.querySelector('[data-fin-image]');
44 var said = form && form.querySelector('[data-fin-receipt-said]');
45 var file = input.files[0];
46 if (!out || !/^image\//.test(file.type)) { if (said) said.textContent = 'That is not a photo.'; return; }
47 if (said) said.textContent = 'Reading the receipt…';
48 form.classList.add('is-busy');
49 var url = URL.createObjectURL(file);
50 var img = new Image();
51 img.onload = function () {
52 var scale = Math.min(1, 2000 / Math.max(img.naturalWidth, img.naturalHeight));
53 var c = document.createElement('canvas');
54 c.width = Math.max(1, Math.round(img.naturalWidth * scale));
55 c.height = Math.max(1, Math.round(img.naturalHeight * scale));
56 c.getContext('2d').drawImage(img, 0, 0, c.width, c.height);
57 URL.revokeObjectURL(url);
58 out.value = c.toDataURL('image/jpeg', 0.92);
59 form.submit();
60 };
61 img.onerror = function () { URL.revokeObjectURL(url); form.classList.remove('is-busy'); if (said) said.textContent = 'That photo could not be opened.'; };
62 img.src = url;
63 });
64})();
65