forked from jswanner/DontF-WithPaste
-
Notifications
You must be signed in to change notification settings - Fork 21
/
dfwp.js
92 lines (73 loc) · 1.91 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
(function() {
window.DFWP = window.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;
}
DFWP.Rule = class Rule {
constructor(value) {
this.value = value || '';
}
get pattern() {
return new RegExp(this.value || '(?=a)b');
}
test(string) {
return this.pattern.test(string);
}
}
DFWP.Rules = class Rules extends Set {
static get [Symbol.species]() { return Set; }
static deserialize(values) {
const rules = values.map((v) => new DFWP.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);
}
}
DFWP.RuleView = 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));
}
const child = container.appendChild(clone);
}
template(templateSelector) {
return document.querySelector(templateSelector);
}
}
})();