diff options
author | jan <jan@ruken.pw> | 2016-11-14 15:55:50 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2016-11-14 15:55:50 (UTC) |
commit | c735553214400f3af69e516ff41a6da9214449a7 (patch) | |
tree | c9a8e8fc84fdbaffb3a3fa318453b36270550ebb /assets_src/js | |
parent | 9832cd28de49421021ed7c1f3caa7fdda7bc4372 (diff) |
likes und so.
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 | }); | ||