Skip to content

Commit 2808ffa

Browse files
committed
feat: Add #stylesheet to MyOpenBadgeElement class
1 parent a9c12dc commit 2808ffa

1 file changed

Lines changed: 34 additions & 19 deletions

File tree

src/components/MyOpenBadgeElement.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class MyOpenBadgeElement extends HTMLElement {
1616

1717
get imageSize () { return parseInt(this.getAttribute('image-size') || 10); }
1818

19-
get hasBorder () { return this.hasAttribute('border'); }
19+
get addStyle () { return this.hasAttribute('add-style'); }
2020

2121
get #anchorSelector () { return 'a[ href *= "credly.com" ]'; }
2222

@@ -37,7 +37,7 @@ export class MyOpenBadgeElement extends HTMLElement {
3737
// return `https://api.allorigins.win/get?url=${encodeURIComponent(this.#apiUrl)}`;
3838
}
3939

40-
#createRequest () {
40+
#createHttpRequest () {
4141
return new Request(this.#corsProxyUrl, {
4242
// credentials: 'omit',
4343
mode: 'cors',
@@ -49,14 +49,14 @@ export class MyOpenBadgeElement extends HTMLElement {
4949
}
5050

5151
async #fetchBadgeData () {
52-
const response = await fetch(this.#createRequest());
52+
const response = await fetch(this.#createHttpRequest());
5353
console.assert(response.ok, `HTTP error fetching Credly badge data: ${response.status}`);
5454
if (response.ok) {
55-
const data = await response.json();
55+
const proxyData = await response.json();
5656
/* eslint-disable camelcase */
57-
const { http_code, content_type } = data.status;
57+
const { http_code, content_type } = proxyData.status;
5858
console.assert(/application\/json.*/.test(content_type), 'Expecting JSON');
59-
const badgeData = JSON.parse(data.contents);
59+
const badgeData = JSON.parse(proxyData.contents);
6060
this.#badgeData = badgeData.data;
6161
if (http_code !== 200) {
6262
const { message } = badgeData.data;
@@ -76,18 +76,31 @@ export class MyOpenBadgeElement extends HTMLElement {
7676
this.dataset.error = ex;
7777
}
7878
attachTemplate(this.#ok ? this.#htmlTemplate : this.#errorTemplate).to.shadowDOM(this);
79-
this.#applyBorderStyle();
8079

8180
console.debug('my-open-badge:', this.#ok, this.#badgeData);
8281
}
8382

84-
#applyBorderStyle () {
85-
if (this.hasBorder) {
86-
this.style.border = '1px solid silver';
87-
this.style.borderRadius = '.5rem';
88-
this.style.display = 'block';
89-
this.style.padding = '.5rem';
90-
}
83+
get #stylesheet () {
84+
if (!this.addStyle) { return ''; }
85+
return `
86+
:host {
87+
border: 1px solid silver;
88+
border-radius: .5rem;
89+
display: block;
90+
padding: .5rem;
91+
}
92+
img {
93+
display: block;
94+
margin: auto;
95+
}
96+
summary::marker {
97+
font-size: larger;
98+
}
99+
sep::before {
100+
content: '·';
101+
display: inline-block;
102+
margin: 0 0.6rem;
103+
}`;
91104
}
92105

93106
get #htmlTemplate () {
@@ -100,21 +113,23 @@ export class MyOpenBadgeElement extends HTMLElement {
100113
this.dataset.ready = true;
101114
return `
102115
<template>
116+
<style>${this.#stylesheet}</style>
103117
<details part="details">
104118
<summary part="summary">
105119
<img part="img" alt="${name}" title="${name}" src="${image_url}" style="max-height:${this.imageSize}rem;">
106120
</summary>
107121
<div part="div">
108122
<h2 part="h2 name">${name}</h2>
109123
<p part="p issuer">${issuer.summary.replace('issued by', '')}</p>
110-
<p part="p date at">Issued on: <time>${issued_at_date}</time>
111-
<span part="span date exp">· Expires on: <time>${expires_at_date}</time></span>
124+
<p part="p date at">Issued on: <time>${issued_at_date}</time><span
125+
part="span date exp"><sep part="sep"
126+
></sep>Expires on: <time>${expires_at_date}</time></span>
112127
</p>
113128
<p part="p to" hidden>Issued to: ${issued_to}</p>
114129
<p part="p desc" X_hidden>${description}</p>
115-
<a part="a learn" href="${global_activity_url}">Learn more</a>
116-
·
117-
<a part="a badge" href="https://www.credly.com/badges/${id}">View badge on Credly</a>
130+
<a part="a learn" href="${global_activity_url}"
131+
>Learn more</a><sep part="sep"
132+
></sep><a part="a badge" href="https://www.credly.com/badges/${id}">View badge on Credly</a>
118133
</div>
119134
</details>
120135
</template>`;

0 commit comments

Comments
 (0)