forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTag.ts
95 lines (76 loc) · 2.24 KB
/
Tag.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
import { TagStatus } from './TagStatus'
type TagClasses = 'Cors' | 'Flag' | 'Ipns' | 'Node' | 'Origin' | 'Status' | 'Trustless'
type TagContent = TagStatus
class Tag {
element: HTMLElement
constructor (tagName: keyof HTMLElementTagNameMap = 'div', className: TagClasses | undefined = undefined, textContent: TagContent = TagStatus.pending) {
const element = document.createElement(tagName)
this.element = element
if (className != null) {
this.className = className
}
this.textContent = textContent
}
public static fromElement (element: HTMLElement): Tag {
const tag = new Tag('div')
tag.element = element
return tag
}
/**
* Use the below functions to keep displays consistent
*/
asterisk (): void {
this.textContent = TagStatus.asterisk
}
lose (): void {
this.textContent = TagStatus.failed
}
win (url?: string): void {
if (url != null) {
this.textContent = TagStatus.empty
const linkToImageSubdomain = document.createElement('a')
linkToImageSubdomain.href = url
linkToImageSubdomain.target = '_blank'
linkToImageSubdomain.textContent = TagStatus.successful
this.element.title = url
this.element.appendChild(linkToImageSubdomain)
} else {
this.textContent = TagStatus.successful
}
}
global (): void {
this.textContent = TagStatus.global
}
err (): void {
this.textContent = TagStatus.caution
}
empty (): void {
this.textContent = TagStatus.empty
}
get style (): CSSStyleDeclaration {
return this.element.style
}
append (child: string | Node | Tag): void {
if (child instanceof Tag) {
child = child.element
}
return this.element.append(child)
}
get classList (): DOMTokenList {
return this.element.classList
}
// eslint-disable-next-line accessor-pairs
set title (newTitle: string) {
this.element.title = newTitle
}
// eslint-disable-next-line accessor-pairs
private set className (className: TagClasses) {
this.element.className = className
}
// eslint-disable-next-line accessor-pairs
private set textContent (content: typeof this.element.textContent) {
this.element.textContent = content
}
}
export type { TagClasses, TagContent }
export { Tag }