|
| 1 | +<template> |
| 2 | +<div class="rss-wrapper"> |
| 3 | + <!-- Feed Meta Info --> |
| 4 | + <a class="meta-container" v-if="meta" :href="meta.link" :title="meta.description"> |
| 5 | + <img class="feed-icon" :src="meta.image" v-if="meta.image" /> |
| 6 | + <div class="feed-text"> |
| 7 | + <p class="feed-title">{{ meta.title }}</p> |
| 8 | + <p class="feed-author" v-if="meta.author">By {{ meta.author }}</p> |
| 9 | + </div> |
| 10 | + </a> |
| 11 | + <!-- Feed Content --> |
| 12 | + <div class="post-wrapper" v-if="posts"> |
| 13 | + <div class="post-row" v-for="(post, indx) in posts" :key="indx"> |
| 14 | + <a class="post-top" :href="post.link"> |
| 15 | + <img class="post-img" :src="post.image" v-if="post.image"> |
| 16 | + <div class="post-title-wrap"> |
| 17 | + <p class="post-title">{{ post.title }}</p> |
| 18 | + <p class="post-date"> |
| 19 | + {{ post.date | formatDate }} {{ post.author | formatAuthor }} |
| 20 | + </p> |
| 21 | + </div> |
| 22 | + </a> |
| 23 | + <div class="post-body" v-html="post.description"></div> |
| 24 | + <a class="continue-reading-btn" :href="post.link">Continue Reading</a> |
| 25 | + </div> |
| 26 | + </div> |
| 27 | + <!-- End Feed Content --> |
| 28 | +</div> |
| 29 | +</template> |
| 30 | + |
| 31 | +<script> |
| 32 | +import axios from 'axios'; |
| 33 | +import WidgetMixin from '@/mixins/WidgetMixin'; |
| 34 | +import { widgetApiEndpoints } from '@/utils/defaults'; |
| 35 | +
|
| 36 | +export default { |
| 37 | + mixins: [WidgetMixin], |
| 38 | + components: {}, |
| 39 | + data() { |
| 40 | + return { |
| 41 | + meta: null, |
| 42 | + posts: null, |
| 43 | + }; |
| 44 | + }, |
| 45 | + mounted() { |
| 46 | + this.fetchData(); |
| 47 | + }, |
| 48 | + computed: { |
| 49 | + /* The URL to users atom-format RSS feed */ |
| 50 | + rssUrl() { |
| 51 | + if (!this.options.rssUrl) this.error('Missing feed URL'); |
| 52 | + return encodeURIComponent(this.options.rssUrl || ''); |
| 53 | + }, |
| 54 | + apiKey() { |
| 55 | + return this.options.apiKey; |
| 56 | + }, |
| 57 | + limit() { |
| 58 | + const usersChoice = this.options.limit; |
| 59 | + if (usersChoice && !Number.isNaN(usersChoice)) return usersChoice; |
| 60 | + return 10; |
| 61 | + }, |
| 62 | + orderBy() { |
| 63 | + const usersChoice = this.options.orderBy; |
| 64 | + const options = ['title', 'pubDate', 'author']; |
| 65 | + if (usersChoice && options.includes(usersChoice)) return usersChoice; |
| 66 | + return 'pubDate'; |
| 67 | + }, |
| 68 | + orderDirection() { |
| 69 | + const usersChoice = this.options.orderBy; |
| 70 | + if (usersChoice && (usersChoice === 'desc' || usersChoice === 'asc')) return usersChoice; |
| 71 | + return 'desc'; |
| 72 | + }, |
| 73 | + endpoint() { |
| 74 | + const apiKey = this.apiKey ? `&api_key=${this.apiKey}` : ''; |
| 75 | + const limit = this.limit && this.apiKey ? `&count=${this.limit}` : ''; |
| 76 | + const orderBy = this.orderBy && this.apiKey ? `&order_by=${this.orderBy}` : ''; |
| 77 | + const direction = this.orderDirection ? `&order_dir=${this.orderDirection}` : ''; |
| 78 | + return `${widgetApiEndpoints.rssToJson}?rss_url=${this.rssUrl}` |
| 79 | + + `${apiKey}${limit}${orderBy}${direction}`; |
| 80 | + }, |
| 81 | + }, |
| 82 | + filters: { |
| 83 | + formatDate(timestamp) { |
| 84 | + const localFormat = navigator.language; |
| 85 | + const dateFormat = { weekday: 'short', day: 'numeric', month: 'short' }; |
| 86 | + const date = new Date(timestamp).toLocaleDateString(localFormat, dateFormat); |
| 87 | + return date; |
| 88 | + }, |
| 89 | + formatAuthor(author) { |
| 90 | + return author ? `by ${author}` : ''; |
| 91 | + }, |
| 92 | + }, |
| 93 | + methods: { |
| 94 | + /* Extends mixin, and updates data. Called by parent component */ |
| 95 | + update() { |
| 96 | + this.startLoading(); |
| 97 | + this.fetchData(); |
| 98 | + }, |
| 99 | + /* Make GET request to Rss2Json */ |
| 100 | + fetchData() { |
| 101 | + axios.get(this.endpoint) |
| 102 | + .then((response) => { |
| 103 | + this.processData(response.data); |
| 104 | + }) |
| 105 | + .catch((error) => { |
| 106 | + this.error('Unable to RSS feed', error); |
| 107 | + }) |
| 108 | + .finally(() => { |
| 109 | + this.finishLoading(); |
| 110 | + }); |
| 111 | + }, |
| 112 | + /* Assign data variables to the returned data */ |
| 113 | + processData(data) { |
| 114 | + const { feed, items } = data; |
| 115 | + this.meta = { |
| 116 | + title: feed.title, |
| 117 | + link: feed.link, |
| 118 | + author: feed.author, |
| 119 | + description: feed.description, |
| 120 | + image: feed.image, |
| 121 | + }; |
| 122 | + const posts = []; |
| 123 | + items.forEach((post) => { |
| 124 | + posts.push({ |
| 125 | + title: post.title, |
| 126 | + description: post.description, |
| 127 | + image: post.thumbnail, |
| 128 | + author: post.author, |
| 129 | + date: post.pubDate, |
| 130 | + link: post.link, |
| 131 | + }); |
| 132 | + }); |
| 133 | + this.posts = posts; |
| 134 | + }, |
| 135 | + }, |
| 136 | +}; |
| 137 | +</script> |
| 138 | + |
| 139 | +<style scoped lang="scss"> |
| 140 | +.rss-wrapper { |
| 141 | + .meta-container { |
| 142 | + display: flex; |
| 143 | + align-items: center; |
| 144 | + text-decoration: none; |
| 145 | + margin: 0.25rem 0 0.5rem 0; |
| 146 | + p.feed-title { |
| 147 | + margin: 0; |
| 148 | + font-size: 1.2rem; |
| 149 | + font-weight: bold; |
| 150 | + color: var(--widget-text-color); |
| 151 | + } |
| 152 | + p.feed-author { |
| 153 | + margin: 0; |
| 154 | + font-size: 0.8rem; |
| 155 | + opacity: var(--dimming-factor); |
| 156 | + color: var(--widget-text-color); |
| 157 | + } |
| 158 | + img.feed-icon { |
| 159 | + border-radius: var(--curve-factor); |
| 160 | + width: 2rem; |
| 161 | + height: 2rem; |
| 162 | + margin-right: 0.5rem; |
| 163 | + } |
| 164 | + } |
| 165 | +
|
| 166 | + .post-row { |
| 167 | + border-top: 1px dashed var(--widget-text-color); |
| 168 | + padding: 0.5rem 0 0.25rem 0; |
| 169 | + .post-top { |
| 170 | + display: flex; |
| 171 | + align-items: center; |
| 172 | + text-decoration: none; |
| 173 | + .post-title-wrap {} |
| 174 | + p.post-title { |
| 175 | + margin: 0; |
| 176 | + font-size: 1rem; |
| 177 | + font-weight: bold; |
| 178 | + color: var(--widget-text-color); |
| 179 | + } |
| 180 | + p.post-date { |
| 181 | + font-size: 0.8rem; |
| 182 | + margin: 0; |
| 183 | + opacity: var(--dimming-factor); |
| 184 | + color: var(--widget-text-color); |
| 185 | + } |
| 186 | + img.post-img { |
| 187 | + border-radius: var(--curve-factor); |
| 188 | + width: 2rem; |
| 189 | + height: 2rem; |
| 190 | + margin-right: 0.5rem; |
| 191 | + } |
| 192 | + } |
| 193 | + .post-body { |
| 194 | + font-size: 0.85rem; |
| 195 | + color: var(--widget-text-color); |
| 196 | + max-height: 400px; |
| 197 | + overflow: hidden; |
| 198 | + ::v-deep p { |
| 199 | + margin: 0.5rem 0; |
| 200 | + } |
| 201 | + ::v-deep img { |
| 202 | + max-width: 80%; |
| 203 | + display: flex; |
| 204 | + margin: 0 auto; |
| 205 | + border-radius: var(--curve-factor); |
| 206 | + } |
| 207 | + ::v-deep a { |
| 208 | + color: var(--widget-text-color); |
| 209 | + } |
| 210 | + ::v-deep svg path { |
| 211 | + fill: var(--widget-text-color); |
| 212 | + } |
| 213 | + ::v-deep blockquote { |
| 214 | + margin-left: 0.5rem; |
| 215 | + padding-left: 0.5rem; |
| 216 | + border-left: 4px solid var(--widget-text-color); |
| 217 | + } |
| 218 | + ::v-deep .avatar.avatar-user { display: none; } |
| 219 | + } |
| 220 | + a.continue-reading-btn { |
| 221 | + width: 100%; |
| 222 | + display: block; |
| 223 | + font-size: 0.9rem; |
| 224 | + text-align: right; |
| 225 | + margin: 0 0 0.25rem; |
| 226 | + padding: 0.1rem 0.25rem; |
| 227 | + text-decoration: none; |
| 228 | + opacity: var(--dimming-factor); |
| 229 | + color: var(--widget-text-color); |
| 230 | + &:hover, &:focus { |
| 231 | + opacity: 1; |
| 232 | + text-decoration: underline; |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | +</style> |
0 commit comments