diff options
Diffstat (limited to 'src/mixins')
-rw-r--r-- | src/mixins/post.pug | 61 | ||||
-rw-r--r-- | src/mixins/postUtils.pug | 39 |
2 files changed, 60 insertions, 40 deletions
diff --git a/src/mixins/post.pug b/src/mixins/post.pug index 172da32..487f366 100644 --- a/src/mixins/post.pug +++ b/src/mixins/post.pug | |||
@@ -1,4 +1,5 @@ | |||
1 | include ../utils | 1 | include ../utils |
2 | include postUtils | ||
2 | mixin post(p) | 3 | mixin post(p) |
3 | article.post | 4 | article.post |
4 | div.post-container | 5 | div.post-container |
@@ -20,62 +21,42 @@ mixin post(p) | |||
20 | | · | 21 | | · |
21 | a(href=`/comments/${p.id}`) #{fmtnum (p.num_comments)} ↩ | 22 | a(href=`/comments/${p.id}`) #{fmtnum (p.num_comments)} ↩ |
22 | div.media-preview | 23 | div.media-preview |
23 | if p.is_gallery && p.is_gallery == true | 24 | if isPostGallery(p) |
24 | if p.gallery_data | 25 | - var item = postGalleryItems(p)[0] |
25 | if p.gallery_data.items | 26 | img(src=item.url onclick=`toggleDetails('${p.id}')`) |
26 | - var item = p.gallery_data.items[0] | 27 | else if isPostImage(p) |
27 | - var url = `https://i.redd.it/${item.media_id}.jpg` | ||
28 | img(src=url onclick=`toggleDetails('${p.id}')`) | ||
29 | else if p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default" | ||
30 | img(src=p.thumbnail onclick=`toggleDetails('${p.id}')`) | 28 | img(src=p.thumbnail onclick=`toggleDetails('${p.id}')`) |
31 | else if p.post_hint == "hosted:video" | 29 | else if isPostVideo(p) |
32 | - var url = p.secure_media.reddit_video.scrubber_media_url | 30 | - var url = p.secure_media.reddit_video.scrubber_media_url |
33 | video(src=url data-dashjs-player width='100px' height='100px' onclick=`toggleDetails('${p.id}')`) | 31 | video(src=url data-dashjs-player width='100px' height='100px' onclick=`toggleDetails('${p.id}')`) |
34 | else if p.post_hint == "link" | 32 | else if isPostLink(p) |
35 | a(href=p.url) | 33 | a(href=p.url) |
36 | | ↗ | 34 | | ↗ |
37 | 35 | ||
38 | if p.is_gallery && p.is_gallery == true | 36 | if isPostGallery(p) |
39 | if p.gallery_data | 37 | details(id=`${p.id}`) |
40 | if p.gallery_data.items | 38 | summary.expand-post expand gallery |
41 | details(id=`${p.id}`) | 39 | div.gallery |
42 | summary.expand-post expand gallery | 40 | each item in postGalleryItems(p) |
43 | div.gallery | 41 | div.gallery-item |
44 | - var total = p.gallery_data.items.length | 42 | div.gallery-item-idx |
45 | - var idx = 0 | 43 | | #{`${item.idx}/${item.total}`} |
46 | - var metadata = p.media_metadata | 44 | a(href=`/media/${item.url}`) |
47 | - | 45 | img(src=item.url loading="lazy") |
48 | var img_ext = (id) => { | 46 | button(onclick=`toggleDetails('${p.id}')`) close |
49 | if (metadata[id].status == 'valid') { | 47 | else if isPostImage(p) |
50 | return stripPrefix(metadata[id].m, "image/"); | ||
51 | } else { | ||
52 | // dosent matter | ||
53 | return 'jpg'; | ||
54 | } | ||
55 | } | ||
56 | each item in p.gallery_data.items | ||
57 | - var id = item.media_id | ||
58 | - var ext = img_ext(item.media_id) | ||
59 | - var url = `https://i.redd.it/${id}.${ext}` | ||
60 | div.gallery-item | ||
61 | a(href=`/media/${url}`) | ||
62 | img(src=url loading="lazy") | ||
63 | div.gallery-item-idx | ||
64 | | #{`${++idx}/${total}`} | ||
65 | button(onclick=`toggleDetails('${p.id}')`) close | ||
66 | if p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default" | ||
67 | details(id=`${p.id}`) | 48 | details(id=`${p.id}`) |
68 | summary.expand-post expand image | 49 | summary.expand-post expand image |
69 | a(href=`/media/${p.url}`) | 50 | a(href=`/media/${p.url}`) |
70 | img(src=p.url loading="lazy").post-media | 51 | img(src=p.url loading="lazy").post-media |
71 | button(onclick=`toggleDetails('${p.id}')`) close | 52 | button(onclick=`toggleDetails('${p.id}')`) close |
72 | else if p.post_hint == "hosted:video" | 53 | else if isPostVideo(p) |
73 | details(id=`${p.id}`) | 54 | details(id=`${p.id}`) |
74 | summary.expand-post expand video | 55 | summary.expand-post expand video |
75 | - var url = p.secure_media.reddit_video.dash_url | 56 | - var url = p.secure_media.reddit_video.dash_url |
76 | video(src=url controls data-dashjs-player loading="lazy").post-media | 57 | video(src=url controls data-dashjs-player loading="lazy").post-media |
77 | button(onclick=`toggleDetails('${p.id}')`) close | 58 | button(onclick=`toggleDetails('${p.id}')`) close |
78 | else if p.post_hint == "link" | 59 | else if isPostLink(p) |
79 | details(id=`${p.id}`) | 60 | details(id=`${p.id}`) |
80 | summary.expand-post expand link | 61 | summary.expand-post expand link |
81 | a(href=`${p.url}`) | 62 | a(href=`${p.url}`) |
diff --git a/src/mixins/postUtils.pug b/src/mixins/postUtils.pug new file mode 100644 index 0000000..4f480b6 --- /dev/null +++ b/src/mixins/postUtils.pug | |||
@@ -0,0 +1,39 @@ | |||
1 | - | ||
2 | function isPostGallery(p) { | ||
3 | return (p.is_gallery && p.is_gallery == true); | ||
4 | } | ||
5 | - | ||
6 | function isPostImage(p) { | ||
7 | return (p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default"); | ||
8 | } | ||
9 | - | ||
10 | function isPostVideo(p) { | ||
11 | return (p.post_hint == "hosted:video"); | ||
12 | } | ||
13 | - | ||
14 | function isPostLink(p) { | ||
15 | return (p.post_hint == "link"); | ||
16 | } | ||
17 | - | ||
18 | function imgExt(p, id) { | ||
19 | var metadata = p.media_metadata; | ||
20 | if (metadata[id].status == 'valid') { | ||
21 | return stripPrefix(metadata[id].m, "image/"); | ||
22 | } else { | ||
23 | // dosent matter | ||
24 | return 'jpg'; | ||
25 | } | ||
26 | } | ||
27 | - | ||
28 | function postGalleryItems(p) { | ||
29 | if (p.gallery_data && p.gallery_data.items) { | ||
30 | return p.gallery_data.items.map((item, idx) => ({ | ||
31 | id: item.media_id, | ||
32 | url: `https://i.redd.it/${item.media_id}.${imgExt(p, item.media_id)}`, | ||
33 | total: p.gallery_data.items.length, | ||
34 | idx: idx+1, | ||
35 | })); | ||
36 | } else { | ||
37 | return null; | ||
38 | } | ||
39 | } | ||