-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
349 lines (318 loc) · 8.87 KB
/
options.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* global browser, Tabulator */
const extId = "PER";
const temporary = browser.runtime.id.endsWith("@temporary-addon");
const log = (level, msg) => {
level = level.trim().toLowerCase();
if (
["error", "warn"].includes(level) ||
(temporary && ["debug", "info", "log"].includes(level))
) {
console[level]("[" + extId + "] [" + level.toUpperCase() + "] " + msg);
return;
}
};
let table = null;
// button refs
const impbtnWrp = document.getElementById("impbtn_wrapper");
const impbtn = document.getElementById("impbtn");
const savbtn = document.getElementById("savbtn");
const discbtn = document.getElementById("discbtn");
const expbtn = document.getElementById("expbtn");
const delbtn = document.getElementById("delbtn");
const ablebtn = document.getElementById("ablebtn");
const addbtn = document.getElementById("addbtn");
const tgladv = document.getElementById("tgladv");
function hightlightChange() {
savbtn.style.borderColor = "red";
}
function unhightlightChange() {
savbtn.style.borderColor = "";
}
tgladv.addEventListener("click", async function () {
table.toggleColumn("tags");
table.toggleColumn("annotation");
});
addbtn.addEventListener("click", async () => {
table.deselectRow();
table.addRow(
{
enabled: true,
group: "",
annotation: "",
tags: "",
code: "",
},
true
); // add at the top
hightlightChange();
});
ablebtn.addEventListener("click", () => {
let changed = false;
table.getSelectedRows().forEach((row) => {
const cell = row.getCell("enabled");
if (cell.setValue(!cell.getValue())) {
changed = true;
}
});
if (changed) {
hightlightChange();
}
});
delbtn.addEventListener("click", () => {
let changed = false;
table.getSelectedRows().forEach((row) => {
row.delete();
changed = true;
});
if (changed) {
hightlightChange();
}
});
discbtn.addEventListener("click", () => {
window.location.reload();
});
savbtn.addEventListener("click", () => {
let data = table.getData();
let i = 0;
for (i = 0; i < data.length; i++) {
// numbers need parsing ... for whatever reason
data[i].idx = i;
}
browser.storage.local.set({ selectors: data });
unhightlightChange();
});
expbtn.addEventListener("click", () => {
let selectedRows = table.getSelectedRows();
// order the selected by position
selectedRows.sort((a, b) => {
return b.getPosition() - a.getPosition();
});
let idx_count = 0;
// fixup the export data
const expData = [];
selectedRows.forEach((row) => {
const rowData = row.getData();
rowData.idx = idx_count;
expData.push(rowData);
});
const content = JSON.stringify(expData, null, 4);
console.log(content);
let dl = document.createElement("a");
const href =
"data:application/json;charset=utf-8," + encodeURIComponent(content);
dl.setAttribute("href", href);
dl.setAttribute("download", extId + "-rules.json");
dl.setAttribute("visibility", "hidden");
dl.setAttribute("display", "none");
document.body.appendChild(dl);
dl.click();
document.body.removeChild(dl);
});
// delegate to real import Button which is a file selector
impbtnWrp.addEventListener("click", function () {
impbtn.click();
});
// read data from file into current table
impbtn.addEventListener("input", function () {
var file = this.files[0];
var reader = new FileReader();
reader.onload = async function () {
try {
var config = JSON.parse(reader.result);
let imported_something = false;
config.forEach((selector) => {
table.addRow(
{
enabled: selector.activ || selector.enabled || false,
group: selector.group || "",
annotation: selector.annotation || "",
tags: selector.tags || "",
code: selector.code || "",
},
false
);
imported_something = true;
});
if (imported_something) {
hightlightChange();
}
} catch (e) {
log("ERROR", "error loading file " + e);
}
};
reader.readAsText(file);
});
function tagValuesLookup() {
const rows = table.getRows();
const tags = [];
for (const row of rows) {
const cell = row.getCell("tags");
const vals = cell.getValue().split(/[\s,]+/);
for (const val of vals) {
if (val !== "" && !tags.includes(val)) {
tags.push(val);
}
}
}
return tags;
}
async function onDOMContentLoaded() {
table = new Tabulator("#mainTable", {
//height: "100%",
virtualDom: false, //disable virtual DOM rendering
layout: "fitDataStretch", //fit columns to width of table
responsiveLayout: "hide", //hide columns that dont fit on the table
pagination: false, //paginate the data
movableRows: true,
groupBy: ["group"],
groupUpdateOnCellEdit: true,
groupStartOpen: false,
initialSort: [{ column: "group", dir: "asc" }],
columns: [
{
rowHandle: true,
formatter: "handle",
headerSort: false,
frozen: true,
width: 30,
minWidth: 30,
},
{
formatter: "rowSelection",
titleFormatter: "rowSelection",
width: 30,
minWidth: 30,
hozAlign: "left",
headerSort: false,
cellClick: function (e, cell) {
cell.getRow().toggleSelect();
},
},
{
title: "Enabled",
width: 100,
field: "enabled",
headerSort: false,
sorter: "boolean",
formatter: "tickCross",
headerHozAlign: "center",
hozAlign: "center",
editor: true,
editorParams: { tristate: false },
},
{
title: "Group",
width: 120,
field: "group",
sorter: "string",
headerFilter: "input",
headerFilterPlaceholder: "Text filter",
editor: "input",
sorterParams: { locale: true, alignEmptyValues: "top" },
}, // add new rows to the top
{
title: "Tags",
width: 120,
field: "tags",
sorter: "string",
headerFilter: "select",
headerFilterPlaceholder: "Multiselect",
editor: "input",
headerFilterParams: {
values: tagValuesLookup, // get values
verticalNavigation: "hybrid", //navigate to new row when at the top or bottom of the selection list
multiselect: true, //allow multiple entries to be selected
},
visible: false,
},
{
title: "Annotation",
field: "annotation",
maxWidth: 240,
headerFilter: "input",
headerFilterPlaceholder: "Text filter",
editor: "input",
sorter: "string",
sorterParams: { locale: true, alignEmptyValues: "top" },
visible: false,
},
{
title: "Boolean User Function (*)",
field: "code",
headerFilter: "input",
headerFilterPlaceholder: "Text filter",
editor: "textarea",
editorParams: { verticalNavigation: "editor" },
formatter: "plaintext",
},
],
});
// Load data
const data = await getTblData();
data.forEach((e) => {
table.addRow(e, true);
});
/**
* Register Table Events
*/
// hlchange if values change
table.on("cellEdited", function (cell) {
if (cell.getValue() !== cell.getOldValue()) {
hightlightChange();
}
});
// todo: determine if the row actually moved
table.on("rowMoved", function () {
hightlightChange();
});
// invert the selected state of each row
table.on("groupClick", function (e, group) {
group.getRows().forEach((row) => {
row.toggleSelect();
});
});
// after adding a row, open the group it is in and highlight/select it
table.on("rowAdded", function (row) {
var group = row.getGroup();
group.show();
row.select();
});
}
async function getTblData() {
const data = [];
var res = await browser.storage.local.get("selectors");
if (Array.isArray(res.selectors)) {
res.selectors.sort(function (b, a) {
// > 0 => b before a
// < 0 => a before b
// === 0 => keep original order of a and b
if (typeof a.idx === "undefined" && typeof b.idx === "number") {
return 1; // b before a
}
if (typeof a.idx === "number" && typeof b.idx === "undefined") {
return -1; // a before b
}
if (typeof a.idx === "number" && typeof b.idx === "number") {
if (a.idx > b.idx) {
return 1;
}
if (a.idx < b.idx) {
return -1;
}
}
// if in doubt, do nothing :) , also covers double undefined and a === b
return 0;
});
res.selectors.forEach((selector) => {
data.push({
enabled: selector.activ || selector.enabled || false,
annotation: selector.annotation || "",
tags: selector.tags || "",
group: selector.group || "",
code: selector.code || "",
});
});
}
return data;
}
document.addEventListener("DOMContentLoaded", onDOMContentLoaded);