-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.element.ts
104 lines (87 loc) · 2.46 KB
/
app.element.ts
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
import { css, Element, html, debounce, listen } from './elements/core'
import { HttpDataService } from './data-access/http-data.service'
import { Gallery, Photo } from './elements/gallery'
import { Tree, Content } from './entities/tree'
@Element({
selector: 'devpr-root',
useShadow: true,
})
export class AppElement extends HTMLElement {
public static observedAttributes = []
dataService = new HttpDataService()
gallery: Gallery<Photo>
images: Photo[] = []
loadEvery = 99
loaded = 0
@debounce(600)
@listen('input', 'input', true)
onChange({ value }: HTMLInputElement) {
console.log(value)
}
styles = css`
:host {
display: flex;
flex-direction: column;
}
header,
footer {
padding: 25px;
display: flex;
justify-content: space-between;
}
`
template = html`
<header>
<img height="86px" src="assets/images/logo.svg" alt="DevParaná" />
<devpr-icon name="add-r"></devpr-icon>
<text-field>
<input type="text" disabled />
<label>Pesquisar</label>
</text-field>
</header>
<main>
<devpr-gallery></devpr-gallery>
</main>
<footer>
<p>Copyright 2021 Katerina Limpitsouni</p>
<button>Carregar mais</button>
</footer>
`
find<T extends HTMLElement>(selector: string) {
return this.shadowRoot.querySelector<T>(selector)
}
async findImages() {
const res = await fetch('assets/images.json')
return await res.json()
}
connectedCallback() {
this.gallery = this.find<Gallery<Photo>>('devpr-gallery')
this.find('button').onclick = () => {
this.loadMore(this.loaded, this.loaded + this.loadEvery)
}
this.findImages().then(([images, report]: Tree[]) => {
images.contents.map((content) => this.handleImages(content, content.name))
queueMicrotask(() => {
this.loadMore(this.loaded, this.loaded + this.loadEvery)
})
// const label = this.find('label')
// label.textContent = `Pesquisar em ${report.files} imagens`
})
}
loadMore(start: number, end: number) {
this.images.slice(start, end).map((image) => {
this.gallery.addItem(image)
})
this.loaded = end
}
handleImages({ contents }: Tree, name: string) {
console.log(contents)
contents.map((content) => {
if (content.type === 'file') {
const title = content.name
const image = `assets/images/${name}/${title}`
this.images.push({ title, image })
}
})
}
}