-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedview.html
More file actions
633 lines (524 loc) · 26.1 KB
/
Copy pathfeedview.html
File metadata and controls
633 lines (524 loc) · 26.1 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<link href='./listviewstyles.css' rel='stylesheet'>
<link href='./darkmodestyles.css' rel='stylesheet'>
<script src='./Autolinker.js'></script>
<script src='./Block.js'></script>
<script src='./BlockManager.js'></script>
<script src='./humanized_time_span.js'></script>
<script src='./Chart.bundle.min.js'></script>
<script>
/*!
* Determine if an element is in the viewport
* (c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Node} elem The element
* @return {Boolean} Returns true if element is in the viewport
*/
var isInViewport = function (elem) {
var distance = elem.getBoundingClientRect();
return (
(distance.top >= 0 && distance.top <= (window.innerHeight || document.documentElement.clientHeight)) ||
(distance.bottom <= (window.innerHeight || document.documentElement.clientHeight) && distance.bottom >= 0) &&
(distance.left >= 0 ||
distance.right <= (window.innerWidth || document.documentElement.clientWidth) )
);
};
</script>
<script>
function postMessageToHost(name, payload) {
if (window.webkit && window.webkit.messageHandlers) {
window.webkit.messageHandlers[name].postMessage(payload)
}
else if (window.Android) {
//Refer the message to Android
Android[name](payload);
}
else {
/*Show premium screen on web version*/
}
}
function darkMode() {
document.body.classList.add('dark')
document.getElementsByTagName('html')[0].classList.add('dark')
}
function lightMode() {
document.body.classList.remove('dark')
document.getElementsByTagName('html')[0].classList.remove('dark')
}
function randomString(len, charSet) {
charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var randomString = '';
for (var i = 0; i < len; i++) {
var randomPoz = Math.floor(Math.random() * charSet.length);
randomString += charSet.substring(randomPoz,randomPoz+1);
}
return randomString;
}
function musicPlayer(audioUrl, title, activityId) {
const audioId = randomString(24)
const player = document.createElement('div')
player.classList.add('audio-player')
player.innerHTML = `
<h3>${title}</h3>
`
const button = document.createElement('button')
button.className = 'tap-to-listen'
button.innerHTML = "<i class='material-icons'>play_arrow</i>"
button.addEventListener('click', e => {
postMessageToHost('activitySelected', activityId)
})
player.appendChild(button)
return player
}
function meditationDisplay(timeLength) {
return `<h1>${Math.round(timeLength/60) + ' min'}</h1>`
}
function addComment(user, comment, row) {
}
class User {
constructor(data) {
this.firstname = data['firstname']
this.lastname = data['lastname']
this.uid = data['uid']
this.email = data['email']
this.profileimage = data['profileimage']
this.streak = data['streak']
this.bio = data['bio']
}
fullName() {
return this.firstname + ' ' + this.lastname
}
getProfileImage() {
if (this.profileimage.startsWith('http')) {
return this.profileimage
}
return 'https://storage.googleapis.com/highlowfiles/' + this.profileimage
}
}
class Activity {
constructor(data) {
console.log(data)
this.activityId = data['activity_id']
this.uid = data['uid']
this.type = data['type']
this.title = data['title']
this.timestamp = data['timestamp']
this.data = data['data']
this.date = data['date']
this.flagged = data['flagged']
this.comments = data['comments']
this.liked = data['liked']
this.likes = data['likes']
}
update(data) {
this.activityId = data['activityId'] || this.activityId
this.uid = data['uid'] || this.uid
this.type = data['type'] || this.type
this.title = data['title'] || this.title
this.timestamp = data['timestamp'] || this.timestamp
this.data = data['data'] || this.data
this.date = data['date'] || this.date
if (data['flagged'] != null && data['flagged'] != undefined) { this.flagged = data['flagged'] }
if (data['liked'] != null && data['liked'] != undefined) { this.liked = data['liked'] }
this.comments = data['comments'] || this.comments
const likes = data['likes'].map(user => new User(user))
console.log(likes)
this.likes = likes || this.likes
}
diff(data) {
let keys = []
if (data['activityId'] != this.activityId) { keys.push('activityId') }
if (data['uid'] != this.activityId) { keys.push('uid') }
if (data['type'] != this.type) { keys.push('type') }
if (data['title'] != this.title) { keys.push('title') }
if (data['timestamp'] != this.timestamp) { keys.push('timestamp') }
for (let item in data['data']) {
if (data['data'][item] !== this.data[item]) { keys.push('data'); break }
}
if (data['date'] != this.date) { keys.push('date') }
if (data['flagged'] != this.flagged) { keys.push('flagged') }
if (data['liked'] != this.liked) { keys.push('liked') }
if (data['comments'].length != this.comments.length) { keys.push('comments') }
else {
for (let i = 0; i < data['comments'].length; i++) {
for (let item in data['comments'][i]) {
if (data['comments'][i][item] !== this.comments[i][item]) { keys.push('comments'); break }
}
}
}
if (data['likes'].length != this.likes.length) { keys.push('likes') }
return keys
}
}
class ActivityCard {
constructor(activity, user, row) {
this.activity = activity
this.el = document.createElement('div')
this.el.classList.add('activity')
this.row = row
this.user = user
this.userArea = document.createElement('div')
let userAction = 'posted'
switch(activity.type) {
case 'diary':
userAction = 'created a <b>diary entry</b>'
break
case 'highlow':
userAction = 'created a <b>High/Low</b>'
break
case 'audio':
userAction = 'created an <b>audio entry</b>'
break
case 'meditation':
userAction = 'did a <b>meditation session</b>'
break
}
const timestamp = activity.timestamp
userAction += ' ' + humanized_time_span(timestamp) + ':'
this.userArea.innerHTML = `
<div class='user'><img src='${user.getProfileImage()}' class='profileimage'><span class='name-and-action'><b>${user.fullName()}</b> ${userAction}</span></div>
`
this.flagButton = document.createElement('div')
this.flagButton.className = 'flag-button'
this.flagButton.onclick = (e) => {
e.preventDefault()
postMessageToHost("flag", this.activity.activityId)
return false
}
this.flagButton.style.color = this.activity.flagged ? '#FB2A57':'rgb(200, 200, 200)'
this.flagButton.innerHTML = `<i class='material-icons'>${!this.activity.flagged ? 'outlined_flag':'flag'}</i><p>${this.activity.flagged ? 'Reported': 'Report'}</p>`;
this.userArea.appendChild(this.flagButton)
this.userArea.classList.add('user')
this.el.appendChild(this.userArea)
this.userArea.getElementsByClassName('user')[0].addEventListener('click', e => {
postMessageToHost('userSelected', this.user.uid)
})
if (activity.type == 'diary' || activity.type == 'highlow') {
this.blockArea = document.createElement('div')
this.el.appendChild(this.blockArea)
this.blockManager = new BlockManager(this.blockArea)
this.blockManager.loadBlocks(activity.data['blocks'], true)
this.blockArea.addEventListener('click', e => {
postMessageToHost('activitySelected', this.row)
})
} else if (activity.type == 'audio') {
this.audioVisualizer = musicPlayer(activity.data['audio_file'], activity.title, activity.activityId)
this.el.appendChild(this.audioVisualizer)
} else if (activity.type == 'meditation') {
this.meditationDisplay = document.createElement('div')
this.meditationDisplay.classList.add('meditation')
this.meditationDisplay.innerHTML = meditationDisplay(activity.data['length'])
this.el.appendChild(this.meditationDisplay)
}
this.likesArea = document.createElement('div')
this.likesArea.classList.add('likes')
this.likeButton = document.createElement('div')
this.likeButton.classList.add('like-button')
this.likeButton.innerHTML = `${this.activity.likes.length}<i class='material-icons'>${!this.activity.liked ? 'favorite_border':'favorite'}</i>`
this.likeButton.addEventListener('click', (e) => {
postMessageToHost('like', this.activity.activityId)
})
this.el.appendChild(this.likesArea)
this.reloadLikes()
this.commentsArea = document.createElement('div')
this.commentsArea.classList.add('comments')
this.addComment = document.createElement('div')
this.addComment.classList.add('add-comment')
this.addComment.innerHTML = `
<img src='${viewingUser.getProfileImage()}' class='profileimage'>
`
this.addCommentInput = document.createElement('input')
this.addCommentInput.classList.add('comment-textarea')
this.addCommentInput.placeholder = 'Leave a comment'
this.addComment.appendChild(this.addCommentInput)
const addCommentButton = document.createElement('button')
addCommentButton.innerHTML = 'Submit'
addCommentButton.addEventListener('click', (e) => {
let message = this.addCommentInput.value
postMessageToHost('submitComment', {
'message': message,
'activityId': this.activity.activityId
})
this.addCommentInput.value = ''
})
this.addComment.appendChild(addCommentButton)
this.el.appendChild(this.commentsArea)
this.reloadComments()
}
reloadComments() {
this.commentsArea.innerHTML = ''
for (let i = 0; i < this.activity.comments.length; i++) {
const commentCard = new CommentCard(this.activity.comments[i])
this.commentsArea.appendChild(commentCard.getElement())
}
this.commentsArea.appendChild(this.addComment)
}
reloadLikes() {
this.likesArea.innerHTML = ''
this.likersArea = document.createElement('div')
this.likersArea.classList.add('likers')
this.likeButton.innerHTML = `${this.activity.likes.length}<i class='material-icons'>${!this.activity.liked ? 'favorite_border':'favorite'}</i>`
this.likesArea.appendChild(this.likeButton)
this.likesArea.appendChild(this.likersArea)
for (let i = 0; i < this.activity.likes.length; i++) {
const liker = document.createElement('span')
liker.classList.add('liker')
liker.innerHTML = `<img src='${this.activity.likes[i].getProfileImage()}'>`
this.likersArea.appendChild(liker)
}
}
getElement() {
return this.el
}
update(data) {
let diff = this.activity.diff(data)
this.activity.update(data)
for (let key in diff) {
this.updateKey(diff[key])
}
}
updateKey(key) {
switch (key) {
case 'comments':
this.reloadComments()
break
case 'likes':
this.reloadLikes()
break
case 'data':
if (this.activity.type == "highlow" || this.activity.type == "diary") {
this.blockManager.loadBlocks(this.activity.data['blocks'], true)
} else if (this.activity.type == "meditation") {
this.meditationDisplay.innerHTML = meditationDisplay(this.activity.data['length'])
}
break
case 'flagged':
this.flagButton.style.color = this.activity.flagged ? '#FB2A57':'rgb(200, 200, 200)'
this.flagButton.innerHTML = `<i class='material-icons'>${!this.activity.flagged ? 'outlined_flag':'flag'}</i><p>${this.activity.flagged ? 'Reported': 'Report'}</p>`
break
case 'liked':
this.likeButton.innerHTML = `<i class='material-icons'>${!this.activity.liked ? 'favorite_border':'favorite'}</i>`
break
default:
break
}
}
}
class Announcement {
constructor(data) {
this.message = data['message']
this.severity = data['severity']
this.link = data['link']
}
}
class ActivityChartCard {
constructor(data) {
this.chartData = []
this.labels = []
for (let i = 0; i < data['chart'].length; i++) {
this.chartData.push(data['chart'][i]['activities'])
this.labels.push(data['chart'][i]['date'])
}
this.el = document.createElement('div')
this.el.classList.add('activity-chart')
const content = `
<h2>Your Activity</h2>
`
this.el.innerHTML = content
this.canvas = document.createElement('canvas')
this.canvas.classList.add('chart')
this.el.appendChild(this.canvas)
this.ctx = this.canvas.getContext('2d')
this.canvas.height = 100
console.log(this.chartData)
this.chart = new Chart(this.ctx, {
type: 'line',
data: {
labels: this.labels,
datasets: [
{
label: 'Activities',
backgroundColor: 'rgba(69, 246, 76, 0.3)',
borderColor: 'rgb(69, 246, 76)',
data: this.chartData,
pointRadius: 0
}
]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: false
}],
yAxes: [{
display: false,
ticks: {
suggestedMin: 0
}
}]
}
}
})
}
getElement() {
return this.el
}
}
class AnnouncementCard {
constructor(announcementData) {
this.severityToColor = ['green', 'orange', 'red']
this.announcement = new Announcement(announcementData)
this.el = document.createElement('div')
this.el.classList.add('announcement')
this.el.style.background = this.severityToColor[this.announcement.severity]
const content = `
<a href='${this.announcement.link}'><span>${this.announcement.message}</span><i class='material-icons'>navigate_next</i></a>
`
this.el.innerHTML = content
}
getElement() {
return this.el
}
}
class Comment {
constructor(commentData) {
this.uid = commentData['uid']
this.commentId = commentData['commentid']
this.timestamp = commentData['_timestamp']
this.message = commentData['message']
}
asJson() {
return {
uid: this.uid,
commentid: this.commentId,
_timestamp: this.timestamp,
message: this.message
}
}
}
class CommentCard {
constructor(commentData) {
this.user = new User(commentData)
this.comment = new Comment(commentData)
this.el = document.createElement('div')
this.el.classList.add('comment')
const content = `
<img src='${this.user.getProfileImage()}' class='profileimage'><span class='comment-content'><p><strong>${this.user.fullName()}</strong> ${humanized_time_span(this.comment.timestamp)}:</p><p class='comment-message'>${this.comment.message}</p></span>
`
this.el.innerHTML = content
if (this.user.uid == viewingUser.uid) {
const moreOptions = document.createElement('i')
moreOptions.className = 'material-icons'
moreOptions.classList.add('comment-more-button')
moreOptions.onclick = (e) => {
postMessageToHost('moreOptions', this.comment.asJson())
}
moreOptions.innerHTML = 'more_vert'
this.el.appendChild(moreOptions)
}
}
getElement() {
return this.el
}
}
</script>
</head>
<body>
<div id='list'>
</div>
<div class='end-message'>
You've reached the end 🎉
</div>
<script>
let feedItems = []
let feedItemsList = document.getElementById('list')
let globalUser = undefined
let viewingUser = undefined
function setViewingUser(userData) {
viewingUser = new User(userData)
}
function setFeedData(feedJson, reset = false) {
if (reset) {
feedItems = []
feedItemsList.innerHTML = ''
}
const len = feedItems.length
for (let i = 0; i < feedJson.length; i++) {
const feedItem = feedJson[i]
if (feedItem['type'] == 'activity') {
for (let j = 0; j < feedItem['activity']['likes'].length; j++) {
feedItem['activity']['likes'][j] = new User(feedItem['activity']['likes'][j])
}
const activity = new ActivityCard( new Activity(feedItem['activity']), new User(feedItem['user']), reset ? i:i + len )
feedItems.push(activity)
} else if (feedItem['type'] == 'announcement') {
const announcement = new AnnouncementCard(feedItem)
feedItems.push(announcement)
} else if (feedItem['type'] == 'activity_chart') {
const activityChart = new ActivityChartCard(feedItem)
feedItems.push(activityChart)
}
}
loadFeedItems()
}
function loadFeedItems() {
//For every feed item...
for (let i = 0; i < feedItems.length; i++) {
//Add it to the list
feedItemsList.appendChild( feedItems[i].getElement() )
}
//Finish loading
finishedLoadingContent()
}
function updateActivity(activityData) {
//Get the activityId
const activityId = activityData['activity_id']
//Start the row at 0
let row = 0
//Go through all the feed items
for (let i = 0; i < feedItems.length; i++) {
//Get the current item
const feedItem = feedItems[i]
//If the feedItem has the type "activity"...
if (feedItem instanceof ActivityCard) {
//Get the activity
const activity = feedItem.activity
//If the activity has the correct activityId...
if (activity.activityId == activityId) {
//Update the activity
feedItem.update(activityData)
//Stop the loop
break
}
}
}
}
let isLoadingNewContent = false
window.addEventListener('scroll', (event) => {
//Get all the feed items
const allFeedItems = feedItemsList.children
//Get the last feed item
const lastElement = allFeedItems[ allFeedItems.length - 1 ]
//If we're not loading new content, and the last element is in the viewport...
if ( !isLoadingNewContent && isInViewport(lastElement) ) {
//We are loading new content
isLoadingNewContent = true
//Tell the host to load the next page
postMessageToHost('loadNextPage', 0)
}
})
function finishedLoadingContent() {
//Stop loading new content
isLoadingNewContent = false
}
</script>
</body>
</html>