-
Notifications
You must be signed in to change notification settings - Fork 64
/
dfwp.js
92 lines (72 loc) · 1.72 KB
/
dfwp.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
const DFWP = {};
try {
DFWP.browser = browser;
} catch {
DFWP.browser = chrome;
}
if (DFWP.browser.storage.sync) {
DFWP.storage = DFWP.browser.storage.sync;
} else {
DFWP.storage = DFWP.browser.storage.local;
}
export default DFWP;
export class Rule {
constructor(value) {
this.value = value || '';
}
get pattern() {
return new RegExp(this.value || '(?=a)b');
}
test(string) {
return this.pattern.test(string);
}
}
export class Rules extends Set {
static get [Symbol.species]() { return Set; }
static deserialize(values) {
const rules = values.map((v) => new Rule(v));
return new Rules(rules);
}
get array() {
return Array.from(this);
}
filter(cb) {
return this.array.filter(cb);
}
find(cb) {
return this.array.find(cb);
}
serialize() {
return this.array.map(r => r.value).filter(v => v.length);
}
some(cb) {
return this.array.some(cb);
}
}
export class RuleView {
constructor(rule, rules) {
this.rule = rule;
this.rules = rules;
}
onclick(event) {
this.rules.delete(this.rule);
event.target.parentNode.remove();
}
oninput(event) {
this.rule.value = event.target.value;
}
render(container, templateSelector = '#template') {
const clone = document.importNode(this.template(templateSelector).content, true);
const input = clone.querySelector('.input');
input.value = this.rule.value;
input.addEventListener('input', this.oninput.bind(this));
const button = clone.querySelector('.delete');
if (button) {
button.addEventListener('click', this.onclick.bind(this));
}
container.appendChild(clone);
}
template(templateSelector) {
return document.querySelector(templateSelector);
}
}