/
dot-card-view.tsx
163 lines (138 loc) · 5.01 KB
/
dot-card-view.tsx
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
import {
Component,
h,
Host,
Prop,
Event,
EventEmitter,
Method,
Watch,
Element
} from '@stencil/core';
import {
DotCardContentletItem,
DotCardContentletEvent
} from '../../models/dot-card-contentlet.model';
import { DotContentletItem } from '../../models/dot-contentlet-item.model';
const getValueAsArray = (value: string): string[] => {
return value && typeof value === 'string' ? value.split(',') : [];
};
const getSelecttion = (items: DotCardContentletItem[], value: string): DotContentletItem[] => {
if (items && items.length && value && typeof value === 'string') {
return items
.filter(({ data: { inode } }: DotCardContentletItem) =>
value.split(',').includes(inode)
)
.map(({ data }: DotCardContentletItem) => data);
}
return [];
};
@Component({
tag: 'dot-card-view',
styleUrl: 'dot-card-view.scss',
shadow: true
})
export class DotCardView {
@Element() el: HTMLElement;
@Prop() items: DotCardContentletItem[] = [];
@Prop({
reflect: true,
mutable: true
})
value: string;
@Prop() showVideoThumbnail = true;
@Event() selected: EventEmitter;
@Event() cardClick: EventEmitter;
private selection: DotContentletItem[] = [];
private cards: NodeListOf<HTMLDotCardContentletElement>;
private lastChecked;
@Watch('items')
watchItems(newValue: DotCardContentletItem[]) {
this.selection = getSelecttion(newValue, this.value);
}
@Watch('value')
watchValue(newValue: string) {
this.selection = getSelecttion(this.items, newValue);
}
@Method()
async getValue(): Promise<DotContentletItem[]> {
return this.selection;
}
@Method()
async clearValue(): Promise<void> {
this.value = '';
const cards = this.getCards();
cards.forEach((card: HTMLDotCardContentletElement) => {
card.checked = false;
});
}
componentDidLoad() {
this.selection = getSelecttion(this.items, this.value);
this.cards = this.getCards();
}
private clearMenu() {
this.cards.forEach((card: HTMLDotCardContentletElement) => {
card.hideMenu();
});
}
render() {
const value = getValueAsArray(this.value);
return (
<Host>
{this.items.map((item: DotCardContentletItem) => (
<dot-card-contentlet
onContextMenu={async (e: MouseEvent) => {
e.preventDefault();
const target = e.target as HTMLDotCardContentletElement;
this.clearMenu();
target.showMenu(e.x, e.y);
}}
onContextMenuClick={() => {
this.clearMenu();
}}
onClick={() => {
this.clearMenu();
this.cardClick.emit(item.data);
}}
key={item.data.inode}
checked={value.includes(item.data.inode)}
onCheckboxChange={({
detail: { originalTarget, shiftKey }
}: CustomEvent<DotCardContentletEvent>) => {
let inBetween = false;
if (shiftKey && originalTarget.checked) {
this.cards.forEach((card: HTMLDotCardContentletElement) => {
if (card === originalTarget || card === this.lastChecked) {
inBetween = !inBetween;
}
if (inBetween) {
card.checked = true;
this.setValue(originalTarget, card.item.data);
}
});
}
this.lastChecked = originalTarget;
this.setValue(originalTarget, item.data);
}}
item={item}
showVideoThumbnail={this.showVideoThumbnail}
/>
))}
</Host>
);
}
private setValue(originalTarget: HTMLDotCardContentletElement, data: DotContentletItem): void {
if (originalTarget.checked) {
this.selection.push(data);
} else {
this.selection = this.selection.filter(
({ identifier }: DotContentletItem) => identifier !== data.identifier
);
}
this.value = this.selection.map(({ inode }: DotContentletItem) => inode).join(',');
this.selected.emit(this.selection);
}
private getCards(): NodeListOf<HTMLDotCardContentletElement> {
return this.el.shadowRoot.querySelectorAll('dot-card-contentlet');
}
}