diff options
Diffstat (limited to 'assets_src/js')
-rw-r--r-- | assets_src/js/lib/dom.js | 10 | ||||
-rw-r--r-- | assets_src/js/like.js | 22 |
2 files changed, 32 insertions, 0 deletions
diff --git a/assets_src/js/lib/dom.js b/assets_src/js/lib/dom.js index bfbeea7..3c2f4d9 100644 --- a/assets_src/js/lib/dom.js +++ b/assets_src/js/lib/dom.js | |||
@@ -17,6 +17,16 @@ export function closest(el, fn) { | |||
17 | return null; | 17 | return null; |
18 | } | 18 | } |
19 | 19 | ||
20 | export function withClass(className) { | ||
21 | const els = document.getElementsByClassName(className); | ||
22 | const arr = []; | ||
23 | |||
24 | for (let i = 0; i < els.length; i++) { | ||
25 | arr.push(els.item(i)); | ||
26 | } | ||
27 | return arr; | ||
28 | } | ||
29 | |||
20 | export function next(el, fn) { | 30 | export function next(el, fn) { |
21 | if (!el) { | 31 | if (!el) { |
22 | return null; | 32 | return null; |
diff --git a/assets_src/js/like.js b/assets_src/js/like.js new file mode 100644 index 0000000..03b15a1 --- /dev/null +++ b/assets_src/js/like.js | |||
@@ -0,0 +1,22 @@ | |||
1 | import * as ajax from './lib/ajax'; | ||
2 | import * as dom from './lib/dom'; | ||
3 | |||
4 | async function updateLikeCount(el) { | ||
5 | const type = parseInt(el.getAttribute('content-type'), 10); | ||
6 | const contentId = parseInt(el.getAttribute('content-id'), 10); | ||
7 | |||
8 | if (isNaN(type) || isNaN(contentId)) { | ||
9 | return; | ||
10 | } | ||
11 | |||
12 | const count = await ajax.get(`/api/likes/count?id=${contentId}&type=${type}`, {}); | ||
13 | |||
14 | el.textContent = count; | ||
15 | } | ||
16 | |||
17 | dom.ready(() => { | ||
18 | dom.withClass('like-count') | ||
19 | .forEach(el => { | ||
20 | updateLikeCount(el); | ||
21 | }); | ||
22 | }); | ||