-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.js
137 lines (108 loc) · 3.79 KB
/
files.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
/**
* Maintains the file selection
*/
const input = document.getElementById("fileSelector")
const settings = require("settings-store")
const { basename } = require('path')
const {getSetting} = require('./options')
let fileNumber = 1;
function filesInit() {
input.addEventListener('change', () => {
addFile(input.files[0].path)
saveFilesList()
});
//https://stackoverflow.com/questions/1163667/how-to-rename-html-browse-button-of-an-input-type-file
$('#addFileButton').click(function (e) {
e.preventDefault(); // prevents submitting
$('#fileSelector').trigger('click');
});
$('#reloadFileButton').click(function (e) {
reloadFiles();
});
for (f of settings.value("files.list", [])) {
addFile(f)
}
if(getCurrentPaths().length == 0) {
$(`<div class="alert alert-dismissible fade show alert-warning" role="alert">
Use the Files menu to select one or more journal files.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`).prependTo('body')
}
}
function reloadFiles() {
for (path of getCurrentPaths()) {
ipc.send("parse", getSetting("options.ledger.command"), getSetting('options.hledger'), path)
}
}
function saveFilesList() {
settings.setValue("files.list", getCurrentPaths())
}
function getCurrentPaths() {
paths = [];
for (d of document.querySelectorAll('[id^="fileRow"]')) {
paths.push(d.path);
}
return paths
}
function alertCantparse(file, error) {
for (d of document.querySelectorAll('[id^="fileRow"]')) {
if (d.path === file) {
document.getElementById('enable' + d.id).checked = false
}
}
$(`<div class="alert alert-dismissible fade show alert-danger" role="alert">
can't parse ${escapeHtml(file)} <br>${escapeHtml(error)}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`).prependTo('body')
}
function addFile(path) {
if (getCurrentPaths().indexOf(path) !== -1) {
return;
}
const id = `fileRow${fileNumber++}`
const newItem = document.createElement('a')
newItem.innerHTML =
`<div style='display:flex; justify-content:space-between; align-items:center; '>
<label><input id='enable${id}' type="checkbox" value="" checked )>
${escapeHtml(basename(path))} </label>
<button class="btn btn-warning" id="remove${id}">Close</button>
</div>`
newItem.classList.add('dropdown-item');
newItem.href = '#'
newItem.id = id
newItem.path = path
document.getElementById('filesListDropDown').insertBefore(
newItem,
document.getElementById('filesDropDownDivider'))
document.getElementById('enable' + id).addEventListener("click", function () {
enableFileById(id)
});
document.getElementById('remove' + id).addEventListener("click", function () {
removeFileById(id)
});
ipc.send("parse", getSetting("options.ledger.command"), getSetting('options.hledger'), path)
}
function removeFileById(id) {
const element = document.getElementById(id)
const path = element.path
element.remove();
saveFilesList()
state.files.delete(path)
update()
}
function enableFileById(id) {
const element = document.getElementById(id)
const path = element.path
enabled = document.getElementById('enable' + id).checked
if (enabled) {
ipc.send("parse", getSetting("options.ledger.command"), getSetting('options.hledger'), path)
} else {
state.files.delete(path)
update()
}
}
module.exports = { filesInit, alertCantparse, reloadFiles }