/
ShieldIODynamicBadge.ts
149 lines (136 loc) · 3.86 KB
/
ShieldIODynamicBadge.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
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
import { ShieldIOBadge } from './ShieldIOBadge.js'
/**
* The types of data the {@linkcode ShieldIODynamicBadge} can use.
*/
export enum DynamicDataType {
JSON = "json",
XML = "xml",
YAML = "yaml"
}
/**
* Shield IO Dynamic Badge
*
* @see {@link https://shields.io/}
*/
export class ShieldIODynamicBadge extends ShieldIOBadge {
/**
* Create a new instance of the {@linkcode ShieldIODynamicBadge} class.
*/
constructor() {
super();
}
/** @inheritdoc */
protected setSrc(): void {
if (this.dataQuery && this.dataType && this.dataUrl) {
const srcPath: string[] = [...this.srcParameters];
if (this.suffix) {
srcPath.push("suffic=" + encodeURIComponent(this.suffix));
}
if (this.prefix) {
srcPath.push("prefix=" + encodeURIComponent(this.prefix));
}
srcPath.push("url=" + encodeURIComponent(this.dataUrl), "query=" + encodeURIComponent(this.dataQuery));
this.imgSrc = "https://img.shields.io/badge/dynamic/" + this.dataType + "?" + srcPath.join("&");
} else {
this.imgSrc = "";
}
}
/**
* Gets the attributes that should trigger a call to {@linkcode attributeChangedCallback}
*/
public static get observedAttributes(): string[] {
return ["dataurl", "dataquery", "prefix", "suffix", "datatype", ...ShieldIOBadge.baseObservedAttributes];
}
/**
* Gets the URL of the data to base the badge on.
*/
public get dataUrl() : string | null {
return this.getAttribute("dataurl");
}
/**
* Sets the URL of the data to base the badge on.
*/
public set dataUrl(v: string | null) {
if (v) {
this.setAttribute("dataurl", v);
} else {
this.removeAttribute("dataurl");
}
}
/**
* Gets the query for the dynamic content.
*/
public get dataQuery() : string | null {
return this.getAttribute("dataquery");
}
/**
* Sets the query for the dynamic content.
*/
public set dataQuery(v: string | null) {
if (v) {
this.setAttribute("dataquery", v);
} else {
this.removeAttribute("dataquery");
}
}
/**
* Gets the text to prefix the dynamic content.
*/
public get prefix(): string | null {
return this.getAttribute("prefix");
}
/**
* Sets the text to prefix the dynamic content.
*/
public set prefix(v: string | null) {
if (v) {
this.setAttribute("prefix", v);
} else {
this.removeAttribute("prefix");
}
}
/**
* Gets the text to suffix the dynamic content.
*/
public get suffix(): string | null {
return this.getAttribute("suffix");
}
/**
* Sets the text to suffix the dynamic content.
*/
public set suffix(v: string | null) {
if (v) {
this.setAttribute("suffix", v);
} else {
this.removeAttribute("suffix");
}
}
/**
* Gets the type of data to process.
*/
public get dataType(): DynamicDataType | null {
return this.getAttribute("datatype") as DynamicDataType;
}
/**
* Sets the type of data to process.
*/
public set dataType(v: DynamicDataType | null) {
if (v) {
this.setAttribute("datatype", v);
} else {
this.removeAttribute("datatype");
}
}
public get tagName() : string {
return ShieldIODynamicBadge.tagName;
}
public static get tagName(): string {
return "SHIELDIO-BADGE-DYNAMIC";
}
}
customElements.define(ShieldIODynamicBadge.tagName.toLowerCase(), ShieldIODynamicBadge);
declare global {
interface HTMLElementTagNameMap {
"shieldio-badge-dynamic": ShieldIODynamicBadge;
}
}