-
Notifications
You must be signed in to change notification settings - Fork 1
/
selector-mixin.js
53 lines (48 loc) · 1.45 KB
/
selector-mixin.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
import merge from '../../backed/src/utils/merge.js';
import SelectMixin from './select-mixin.js';
export default base => {
return class SelectorMixin extends SelectMixin(base) {
static get properties() {
return merge(super.properties, {
selected: {
value: 0,
observer: '__selectedObserver__'
},
multi: {
value: false,
reflect: true
}
});
}
constructor() {
super();
}
connectedCallback() {
super.connectedCallback()
this._onClick = this._onClick.bind(this);
this.addEventListener('click', this._onClick);
}
disconnectedCallback() {
this.removeEventListener('click', this._onClick);
}
_onClick(event) {
const target = event.path ? event.path[0] : event.composedPath()[0];
const attr = target.getAttribute(this.attrForSelected);
let selected;
if (target.localName !== this.localName) {
selected = attr ? attr : target;
} else {
selected = attr;
}
if (this.multi) {
if (!Array.isArray(this.selected)) this.selected = [];
const index = this.selected.indexOf(selected);
if (index === -1) this.selected.push(selected);
else this.selected.splice(index, 1)
// trigger observer
this.select(this.selected)
} else this.selected = selected;
this.dispatchEvent(new CustomEvent('selected', { detail: selected }));
}
}
}