-
Notifications
You must be signed in to change notification settings - Fork 1
/
topic-preview.js
63 lines (57 loc) · 2.23 KB
/
topic-preview.js
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
(async function() {
/* globals $, socket, app */
'use strict';
const { translate } = await app.require('translator')
function getTidFirstPost(tid) {
return socket.emit('topics.loadMore', {
tid,
after: 0,
count: 1,
direction: 1
})
}
async function onhover() {
var self = $(this);
if (!self.data('preview-loaded')) {
let content
try {
const data = await getTidFirstPost(self.data('my-tid'))
content = await translate(data.mainPost?.content || data.posts.find(p => p.pid === data.mainPid)?.content)
} catch (err) {
console.error(err)
content = await translate(`<span style="color:red;">שגיאה: </span>${err.message}`)
} finally {
self.data('preview-loaded', 1);
}
$('#preview-' + self.data('my-tid')).children().html(content);
}
$('#preview-' + self.data('my-tid')).stop(true).delay(500).fadeIn();
}
function onunhover() {
$('#preview-' + $(this).data('my-tid')).stop(true).fadeOut();
}
$(window).on('action:topics.loaded', (event, data) => {
for (let topic of data.topics) {
let topicElem = $('[data-tid="' + topic.tid + '"]');
createPreview(topicElem);
}
});
function addTopicTools() {
let topicelems = $('[component="category/topic"]');
topicelems.each((i, elem) => {
createPreview($(elem));
});
}
function createPreview(topicElem) {
let tid = parseInt(topicElem.attr('data-tid'));
if (!$('#preview-' + tid).length) {
topicElem.find('[component="topic/header"]').parent().append('<div class="post-preview" id="preview-' + tid + '"><div class="wrap-post-preview">טוען...</div></div>');
topicElem.find('.post-preview').hover(function () { $(this).stop(true).fadeIn(); }, function () { $(this).delay(600).fadeOut(); });
topicElem.find('[component="topic/header"] a').data('my-tid', tid).hover(
onhover,
onunhover
);
}
}
$(window).on('action:ajaxify.end', addTopicTools);
})();