-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
130 lines (113 loc) · 3.9 KB
/
Copy pathfilter.js
File metadata and controls
130 lines (113 loc) · 3.9 KB
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
export class Filter extends HTMLElement {
static observedAttributes = ['form', 'target', 'include', 'exclude', 'index']
static debounceDelay = 50
static listenedEvents = ['input', 'change']
get form() {
return document.forms[this.getAttribute('form')]
|| this.closest('form')
|| document.forms[0]
|| console.warn('No form found for', this)
}
get targets() {
const value = this.getAttribute('target')
const targets = value && value
.split(' ')
.map(str => document.getElementById(str))
.filter(node => node !== null)
return targets && targets.length && Array.from(targets) || [this]
}
get #include() {
const value = this.getAttribute('include')
return value && value.split(' ')
}
get #exclude() {
const value = this.getAttribute('exclude')
return value && value.split(' ')
}
constructor() {
super()
this.#filterDebounced = debounce(this.#filterSync, Filter.debounceDelay)
}
connectedCallback() {this.#listen()}
disconnectedCallback() {this.#unlisten()}
adoptedCallback() {this.#relisten()}
#listening = []
#listen() {
if (this.#listening.length) return
this.#listening = Filter.listenedEvents
this.#listening.forEach(name => document.addEventListener(name, this.#handleEvent))
}
#unlisten() {
if (!this.#listening.length) return
this.#listening.forEach(name => document.removeEventListener(name, this.#handleEvent))
this.#listening = []
}
#relisten() {
this.#unlisten()
this.#listen()
}
#handleEvent = (e) => {
if (e.target.form === this.form || e.target === this.form) this.#filterDebounced()
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'index') {
if (oldValue !== null) this.#autoIndex(oldValue, 'remove')
if (newValue !== null) this.#autoIndex(newValue, 'add')
}
this.#filterDebounced()
}
#autoIndex(name, method) {
const nodes = this.targets.flatMap(t => Array.from(t.children))
nodes.map(node => {
const attributeName = this.localName + '-' + name
const attributeValue = node.textContent.trim().replaceAll(/\s+/g, ' ')
if (method === 'add') {
node.setAttribute(attributeName, attributeValue)
}
if (method === 'remove') {
node.removeAttribute(attributeName)
}
})
}
#dispatch(target, found, hidden) {
const event = new CustomEvent(this.localName, {
cancelable: true,
bubbles: true,
detail: {found, hidden},
})
return target.dispatchEvent(event)
}
#filterDebounced
#filterSync = () => {
const includedNames = this.#include
const excludedNames = this.#exclude
const data = Array.from(new FormData(this.form))
.filter(([name, value]) => value)
.filter(([name, value]) => !includedNames || includedNames.includes(name))
.filter(([name, value]) => !excludedNames || !excludedNames.includes(name))
const filterSelector = data.map(this.#getFilterSelector).join('')
this.targets.map(target => {
const items = Array.from(target.children)
const found = Array.from(target.querySelectorAll(':scope > ' + (filterSelector || '*')))
const hidden = items.map(item => !found.includes(item))
if (!this.#dispatch(target, found, hidden)) return
items.map(item => item.hidden = !found.includes(item))
})
}
#getFilterSelector = ([name, value], flags) => {
[name, ...flags] = name.split(':')
name = CSS.escape(name)
value = CSS.escape(value)
const attrSelector = `[${this.localName}-${name}*="${value}" i]`
const positiveSelector = `:is(${attrSelector},:has(${attrSelector}))`
const negativeSelector = `:not(${attrSelector}):not(:has(${attrSelector}))`
return flags.includes('not') ? negativeSelector : positiveSelector
}
}
function debounce (fn, delay) {
let id
return function (...args) {
if (id) clearTimeout(id)
id = setTimeout(() => fn(...args), delay)
}
}