-
Notifications
You must be signed in to change notification settings - Fork 23
/
rss-widget.js
131 lines (121 loc) · 3.31 KB
/
rss-widget.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
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
import {html, css, LitElement} from 'lit';
export class RSSWidget extends LitElement {
static get styles() {
return css`
.rss-widget {
display: inline-flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-width: 100px;
height: 60px;
overflow: hidden;
border: 1px solid #ccc;
background-color: var(--background-color, white);
color: var(--text-color, black);
--link-color: blue;
--link-color-dark: lightblue;
}
.title {
flex: 1;
margin: 0 10px;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block;
animation: scroll 10s linear infinite;
}
.title-container {
overflow: hidden;
}
.feed-title {
text-align: center;
}
.feed-items {
display: flex;
}
button {
margin: 0 5px;
}
@keyframes scroll {
0% { transform: translateX(100%); }
100% { transform: translateX(-100%); }
}
/* Dark mode styles */
.rss-widget.dark {
background-color: black;
color: white;
--link-color: var(--link-color-dark);
}
a {
color: var(--link-color);
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
`;
}
static get properties() {
return {
rss: { attribute: "rss", type: String},
currentItemIndex: { type: Number },
items: { type: Array },
dark: { attribute: "dark", type: Boolean }
}
};
constructor() {
super();
this.dark = false;
this.rss = 'https://developer.webex.com/api/content/blog/feed';
this.items = [];
this.currentItemIndex = 0;
this.feed = {};
}
// This method will be called whenever the rssFeed property changes.
async updated(changedProperties) {
if (changedProperties.has('rss')) {
const response = await fetch(`https://api.rss2json.com/v1/api.json?rss_url=${encodeURIComponent(this.rss)}`);
const data = await response.json();
this.items = data.items;
this.feed = data.feed;
}
}
nextItem() {
this.currentItemIndex = (this.currentItemIndex + 1) % this.items.length;
}
previousItem() {
this.currentItemIndex = (this.currentItemIndex - 1 + this.items.length) % this.items.length;
}
render() {
const currentItem = this.items[this.currentItemIndex] || {};
return html`
<div class="rss-widget ${this.dark && "dark"}">
<div class="feed-title">${this.feed.title}: ${this.items.length} items</div>
<div class="feed-items">
<div>
<button
@click="${this.previousItem}"
?disabled="${this.currentItemIndex === 0}"
>
<
</button>
</div>
<div>
<div class="title-container">
<a class="title" href="${currentItem.link}">${currentItem.title}</a>
</div>
</div>
<div>
<button
@click="${this.nextItem}"
?disabled="${this.currentItemIndex === this.items.length - 1}"
>
>
</button>
</div>
</div>
</div>
`;
}
}
customElements.define('rss-widget', RSSWidget);