|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/*globals $, app, utils, socket, templates, config*/ |
| 4 | + |
| 5 | +$(document).ready(function() { |
| 6 | + |
| 7 | + var env = utils.findBootstrapEnvironment(); |
| 8 | + setupPostReactions(); |
| 9 | + |
| 10 | + function setupPostReactions() { |
| 11 | + $(window).on('action:ajaxify.end', function(ev, data) { |
| 12 | + if (data.url && data.url.match('^topic/')) { |
| 13 | + setupReactionSockets(); |
| 14 | + createReactionTooltips(); |
| 15 | + $('[component="topic"]').on('click', '[component="post/reaction"]', function(e) { |
| 16 | + var tid = $('[component="topic"]').attr("data-tid"); |
| 17 | + var reactionElement = $(this); |
| 18 | + var pid = reactionElement.attr("data-pid"); |
| 19 | + var reaction = reactionElement.attr("data-reaction"); |
| 20 | + var reacted = reactionElement.hasClass("reacted"); |
| 21 | + var event = 'plugins.reactions.' + (reacted ? 'removePostReaction' : 'addPostReaction'); |
| 22 | + socket.emit(event, { |
| 23 | + tid: tid, |
| 24 | + pid: pid, |
| 25 | + reaction: reaction |
| 26 | + }, function(err) { |
| 27 | + if (err) { |
| 28 | + app.alertError(err.message); |
| 29 | + } |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + $('[component="topic"]').on('click', '[component="post/reaction/add"]', function (e) { |
| 34 | + var reactionAddEl = $(this); |
| 35 | + var tid = $('[component="topic"]').attr("data-tid"); |
| 36 | + var pid = reactionAddEl.attr("data-pid"); |
| 37 | + require(['plugin/emoji-extended/composer/modal'], function (modal) { |
| 38 | + modal.open().then(function (item) { |
| 39 | + socket.emit('plugins.reactions.addPostReaction', { |
| 40 | + tid: tid, |
| 41 | + pid: pid, |
| 42 | + reaction: item.id |
| 43 | + }, function(err) { |
| 44 | + if (err) { |
| 45 | + app.alertError(err.message); |
| 46 | + } |
| 47 | + |
| 48 | + $('[component="post/reaction"][data-pid="' + pid + '"][data-reaction="' + item.id + '"]').addClass("reacted"); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + socket.on('event:post_deleted', function(data) { |
| 57 | + $('[component="post/reactions"][data-pid="' + data.pid + '"]').addClass("hidden"); |
| 58 | + }); |
| 59 | + |
| 60 | + socket.on('event:post_restored', function(data) { |
| 61 | + $('[component="post/reactions"][data-pid="' + data.pid + '"]').removeClass("hidden"); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + function setupReactionSockets() { |
| 66 | + socket.on('event:reactions.addPostReaction', function (data) { |
| 67 | + updateReactionCount(data); |
| 68 | + }); |
| 69 | + |
| 70 | + socket.on('event:reactions.removePostReaction', function (data) { |
| 71 | + updateReactionCount(data); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + function updateReactionCount(data) { |
| 76 | + var maxReactionsReached = parseInt(data.totalReactions, 10) >= config.maximumReactions; |
| 77 | + $('[component="post/reaction/add"][data-pid="' + data.pid + '"]').toggleClass("max-reactions", maxReactionsReached); |
| 78 | + |
| 79 | + var reactionEl = $('[component="post/reaction"][data-pid="' + data.pid + '"][data-reaction="' + data.reaction + '"]'); |
| 80 | + |
| 81 | + if (parseInt(data.reactionCount, 10) === 0) { |
| 82 | + reactionEl.tooltip('destroy'); |
| 83 | + reactionEl.remove(); |
| 84 | + } |
| 85 | + |
| 86 | + if (reactionEl.length === 0) { |
| 87 | + templates.parse('partials/topic/reaction', { |
| 88 | + "posts": { |
| 89 | + "pid": data.pid, |
| 90 | + "reactions": { |
| 91 | + "pid": data.pid, |
| 92 | + "reaction": data.reaction, |
| 93 | + "memberCount": data.reactionCount, |
| 94 | + "usernames": data.usernames, |
| 95 | + "reacted": (parseInt(data.uid, 10) === app.user.uid), |
| 96 | + "reactionImage": data.reactionImage |
| 97 | + } |
| 98 | + } |
| 99 | + }, function(html) { |
| 100 | + $('[component="post/reactions"][data-pid="' + data.pid + '"]').append(html); |
| 101 | + }); |
| 102 | + } else { |
| 103 | + reactionEl.find(".reaction-emoji-count").attr("data-count", data.reactionCount); |
| 104 | + reactionEl.attr("data-original-title", data.usernames); |
| 105 | + reactionEl.toggleClass("reacted", !(parseInt(data.uid, 10) === app.user.uid)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + function createReactionTooltips() { |
| 110 | + $('.reaction, .reaction-add').each(function () { |
| 111 | + if (!utils.isTouchDevice()) { |
| 112 | + $(this).tooltip({ |
| 113 | + placement: 'top', |
| 114 | + title: $(this).attr('title') |
| 115 | + }); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | +}); |
0 commit comments