-
Notifications
You must be signed in to change notification settings - Fork 1
/
html-widget-progress.js
67 lines (64 loc) · 1.84 KB
/
html-widget-progress.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
HTML Widget progress 1.4
https://github.com/Normal-Tangerine8609/Scriptable-HTML-Widget/blob/main/add-ons/html-widget-progress.js
- Compatible with HTML Widget 6.10
*/
module.exports = async (
validate,
template,
update,
styles,
attrs,
innerText
) => {
const mapping = {
url: "url",
background: ["colour", "gradient"],
progressBackground: ["colour", "gradient"],
borderColor: "colour",
borderWidth: "posInt",
cornerRadius: "posInt",
progressCornerRadius: "posInt",
width: "posInt",
height: "posInt",
value: "decimal"
}
validate(attrs, styles, mapping)
let value = /\d*(?:\.\d*)?%?/.exec(attrs.value)[0]
if (value.endsWith("%")) {
value = Number(value.replace("%", ""))
value /= 100
}
if (!attrs.value) {
throw new Error("`progress` tag must have a `value` attribute")
}
if (value < 0) {
throw new Error(`\`value\` attribute must be above \`0\`: ${attrs.value}`)
}
if (value > 1) {
throw new Error(`\`value\` attribute must be below \`1\`: ${attrs.value}`)
}
let width = Number(
styles.width && styles.width !== "null" ? styles.width : 100
)
let height = Number(
styles.height && styles.height !== "null" ? styles.height : 1
)
await template(`
<stack url="${styles.url || "null"}"
background="${styles.background || "black-white"}"
borderColor="${styles.borderColor || "null"}"
borderWidth="${styles.borderWidth || "null"}"
cornerRadius="${styles.cornerRadius || "null"}"
size="${width}, ${height}"
>
<stack background="${styles.progressBackground || "gray"}"
cornerRadius="${styles.progressCornerRadius || "null"}"
size="${Math.round(width * Number(value))}, ${height}">
</stack>
<stack size="${Math.round(
width * (1 - Number(value))
)}, ${height}"></stack>
</stack>
`)
}