-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.html
More file actions
181 lines (166 loc) · 4.82 KB
/
index.html
File metadata and controls
181 lines (166 loc) · 4.82 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Demo</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
body {
font-family: 'Roboto';
background-color: #f2f2f2;
}
</style>
</head>
<body>
<demo-app></demo-app>
<script type="module">
/* eslint-disable */
import { LitElement, html, css } from 'https://unpkg.com/lit-element@2.0.1?module';
import 'https://unpkg.com/@polymer/paper-card/paper-card.js?module';
import 'https://unpkg.com/@polymer/paper-fab@3.0.1/paper-fab.js?module';
import 'https://unpkg.com/@polymer/paper-button/paper-button.js?module';
import 'https://unpkg.com/@polymer/iron-icons@3.0.1/iron-icons?module';
class DemoApp extends LitElement {
static get properties() {
return {
stories: { type: Array },
filter: { type: String },
};
}
static get styles() {
return [
css`
:host {
display: flex;
flex-direction: column;
}
.stories {
display: flex;
flex-direction: column;
}
my-article {
margin: 10px;
}
header {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-bottom: 20px;
}
`,
];
}
constructor() {
super();
this.stories = [];
this.filter = 'all';
}
connectedCallback() {
super.connectedCallback();
fetch('https://newsapi.org/v2/everything?q=weather&from=2019-05-09&sortBy=publishedAt&apiKey=0067878edf7d4084a6557032dbd0cab1')
.then(r => r.json())
.then((r) => {
this.stories = r.articles.map((article, i) => ({ ...article, id: i, read: false }));
});
}
_toggleReadStatus(e) {
this.stories[e.detail].read = !this.stories[e.detail].read;
this.requestUpdate();
}
getFilteredStories(filter) {
return this.stories.filter((article) => {
if (filter === 'read') return article.read;
if (filter === 'unread') return !article.read;
if (filter === 'all') return true;
});
}
render() {
return html`
<header>
<h1>The News</h1>
<div>
<paper-button raised @click=${() => this.filter = 'all'}>all</paper-button>
<paper-button raised @click=${() => this.filter = 'read'}>read (${this.getFilteredStories('read').length})</paper-button>
<paper-button raised @click=${() => this.filter = 'unread'}>unread (${this.getFilteredStories('unread').length})</paper-button>
</div>
</header>
<div class="stories">
${this.stories.filter((article) => {
if (this.filter === 'read') return article.read;
if (this.filter === 'unread') return !article.read;
if (this.filter === 'all') return true;
}).map(article => html`
<my-article
@toggled=${this._toggleReadStatus}
.read=${article.read}
.title=${article.title}
.description=${article.description}
.id=${article.id}>
</my-article>`)}
</div>
`;
}
}
customElements.define('demo-app', DemoApp);
class Article extends LitElement {
static get properties() {
return {
title: { type: String },
description: { type: String },
read: { type: Boolean },
id: { type: Number },
};
}
static get styles() {
return [
css`
:host {
display: flex;
flex: 1;
}
paper-card {
flex: 1;
padding: 15px;
border-radius: 10px;
}
.header {
display: flex;
}
.header h2 {
flex: 1;
margin-right: 25px;
}
.buttons {
display: flex;
align-items: center;
justify-content: center;
}
`,
];
}
_toggleRead() {
this.dispatchEvent(new CustomEvent('toggled', { detail: this.id }));
}
render() {
return html`
<paper-card>
<div class="header">
<h2>${this.title}</h2>
<div class="buttons">
${this.read
? html`<paper-fab @click=${this._toggleRead} mini icon="icons:visibility"></paper-fab>`
: html`<paper-fab @click=${this._toggleRead} mini icon="icons:visibility-off"></paper-fab>`
}
</div>
</div>
<p>${this.description}</p>
</paper-card>
`;
}
}
customElements.define('my-article', Article);
</script>
</body>
</html>