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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/**
@license
Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import { LitElement, html } from '@polymer/lit-element';
import { setPassiveTouchGestures } from '@polymer/polymer/lib/utils/settings.js';
import { connect } from 'pwa-helpers/connect-mixin.js';
import { installMediaQueryWatcher } from 'pwa-helpers/media-query.js';
import { installOfflineWatcher } from 'pwa-helpers/network.js';
import { installRouter } from 'pwa-helpers/router.js';
import { updateMetadata } from 'pwa-helpers/metadata.js';
// This element is connected to the Redux store.
import { store } from '../store.js';
// These are the actions needed by this element.
import {
navigate,
updateOffline,
updateDrawerState,
updateLayout
} from '../actions/app.js';
// We are lazy loading its reducer.
import firebase from '../reducers/firebase.js';
store.addReducers({
firebase
});
// These are the elements needed by this element.
import '@polymer/app-layout/app-drawer/app-drawer.js';
import '@polymer/app-layout/app-header/app-header.js';
import '@polymer/app-layout/app-scroll-effects/effects/waterfall.js';
import '@polymer/app-layout/app-toolbar/app-toolbar.js';
import { menuIcon } from './my-icons.js';
import './snack-bar.js';
class MaoApp extends connect(store)(LitElement) {
_render({appTitle, _page, _drawerOpened, _snackbarOpened, _offline, _isAdmin}) {
// Anything that's related to rendering should be done in here.
return html`
<style>
:host {
--app-drawer-width: 256px;
display: block;
--app-primary-color: #E91E63;
--app-secondary-color: #293237;
--app-dark-text-color: var(--app-secondary-color);
--app-light-text-color: white;
--app-section-even-color: #f7f7f7;
--app-section-odd-color: white;
--app-header-background-color: white;
--app-header-text-color: var(--app-dark-text-color);
--app-header-selected-color: var(--app-primary-color);
--app-drawer-background-color: var(--app-secondary-color);
--app-drawer-text-color: var(--app-light-text-color);
--app-drawer-selected-color: #78909C;
}
app-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
text-align: center;
background-color: var(--app-header-background-color);
color: var(--app-header-text-color);
border-bottom: 1px solid #eee;
z-index: 101;
}
app-drawer {
z-index: 101;
}
.toolbar-top {
background-color: var(--app-header-background-color);
}
[main-title] {
font-family: 'Pacifico';
text-transform: lowercase;
font-size: 30px;
/* In the narrow layout, the toolbar is offset by the width of the
drawer button, and the text looks not centered. Add a padding to
match that button */
padding-right: 44px;
}
.toolbar-list {
display: none;
}
.toolbar-list > a {
display: inline-block;
color: var(--app-header-text-color);
text-decoration: none;
line-height: 30px;
padding: 4px 24px;
}
.toolbar-list > a[selected] {
color: var(--app-header-selected-color);
border-bottom: 4px solid var(--app-header-selected-color);
}
.menu-btn {
background: none;
border: none;
fill: var(--app-header-text-color);
cursor: pointer;
height: 44px;
width: 44px;
}
.drawer-list {
box-sizing: border-box;
width: 100%;
height: 100%;
padding: 24px;
background: var(--app-drawer-background-color);
position: relative;
}
.drawer-list > a {
display: block;
text-decoration: none;
color: var(--app-drawer-text-color);
line-height: 40px;
padding: 0 24px;
}
.drawer-list > a[selected] {
color: var(--app-drawer-selected-color);
}
/* Workaround for IE11 displaying <main> as inline */
main {
display: block;
}
.main-content {
padding-top: 64px;
min-height: 100vh;
}
.page {
display: none;
}
.page[active] {
display: block;
}
footer {
padding: 24px;
background: var(--app-drawer-background-color);
color: var(--app-drawer-text-color);
text-align: center;
}
/* Wide layout: when the viewport width is bigger than 460px, layout
changes to a wide layout. */
@media (min-width: 460px) {
.toolbar-list {
display: block;
}
.menu-btn {
display: none;
}
.main-content {
padding-top: 107px;
}
/* The drawer button isn't shown in the wide layout, so we don't
need to offset the title */
[main-title] {
padding-right: 0px;
}
}
</style>
<!-- Header -->
<app-header condenses reveals effects="waterfall">
<app-toolbar class="toolbar-top">
<button class="menu-btn" title="Menu" on-click="${_ => store.dispatch(updateDrawerState(true))}">${menuIcon}</button>
<div main-title>${appTitle}</div>
</app-toolbar>
<!-- This gets hidden on a small screen-->
<nav class="toolbar-list">
<a selected?="${_page === 'home'}" href="/home">Home</a>
<a selected?="${_page === 'tutor'}" href="/tutor">Tutoring</a>
<a selected?="${_page === 'compete'}" href="/compete">Compete</a>
<a selected?="${_page === 'fourms'}" href="/fourms">Fourms</a>
<a selected?="${_page === 'account'}" href="/account">Account</a>
<a style="display: ${_isAdmin ? 'inline-block' : 'none'};"
selected?="${_page === 'admin'}" href="/admin">Admin</a>
</nav>
</app-header>
<!-- Drawer content -->
<app-drawer opened="${_drawerOpened}"
on-opened-changed="${e => store.dispatch(updateDrawerState(e.target.opened))}">
<nav class="drawer-list">
<a selected?="${_page === 'home'}" href="/home">Home</a>
<a selected?="${_page === 'tutor'}" href="/tutor">Tutoring</a>
<a selected?="${_page === 'compete'}" href="/compete">Compete</a>
<a selected?="${_page === 'fourms'}" href="/fourms">Fourms</a>
<a selected?="${_page === 'account'}" href="/account">Account</a>
<a style="display: ${_isAdmin ? 'inline-block' : 'none'};"
selected?="${_page === 'admin'}" href="/admin">Admin</a>
</nav>
</app-drawer>
<!-- Main content -->
<main role="main" class="main-content">
<mao-home class="page" active?="${_page === 'home'}"></mao-home>
<mao-tutoring class="page" active?="${_page === 'tutor'}"></mao-tutoring>
<mao-competitions class="page" active?="${_page === 'compete'}"></mao-competitions>
<mao-account class="page" active?="${_page === 'account'}"></mao-account>
<mao-fourms class="page" active?="${_page === 'fourms'}"></mao-fourms>
<mao-admin class="page" active?="${_page === 'admin'}"></mao-admin>
<my-view404 class="page" active?="${_page === 'view404'}"></my-view404>
</main>
<footer>
<p>Made with ♥ by the Polymer team.</p>
</footer>
<snack-bar active?="${_snackbarOpened}">
You are now ${_offline ? 'offline' : 'online'}.</snack-bar>
`;
}
static get properties() {
return {
appTitle: String,
_page: String,
_drawerOpened: Boolean,
_snackbarOpened: Boolean,
_offline: Boolean,
_isAdmin: Boolean
}
}
constructor() {
super();
// To force all event listeners for gestures to be passive.
// See https://www.polymer-project.org/3.0/docs/devguide/settings#setting-passive-touch-gestures
setPassiveTouchGestures(true);
}
_firstRendered() {
installRouter((location) => store.dispatch(navigate(window.decodeURIComponent(location.pathname))));
installOfflineWatcher((offline) => store.dispatch(updateOffline(offline)));
installMediaQueryWatcher(`(min-width: 460px)`,
(matches) => store.dispatch(updateLayout(matches)));
}
_didRender(properties, changeList) {
if ('_page' in changeList) {
const pageTitle = properties.appTitle + ' - ' + changeList._page;
updateMetadata({
title: pageTitle,
description: pageTitle
// This object also takes an image property, that points to an img src.
});
}
}
_stateChanged(state) {
this._page = state.app.page;
this._offline = state.app.offline;
this._snackbarOpened = state.app.snackbarOpened;
this._drawerOpened = state.app.drawerOpened;
this._isAdmin = state.firebase.isAdmin;
}
}
window.customElements.define('mao-app', MaoApp);
|