define('home/views/PostActionsView',[ 'jquery', 'backbone-marionette', 'home/templates/postActions', 'home/ReplyViewManager', 'home/models/Session', 'core/views/VotersCard', 'core/views/ClickTooltip', 'home/templates/anonUpvoteCard', 'home/templates/anonDownvoteCard', 'core/bus', 'core/constants/voteConstants', 'core/utils', ], function ( $, Marionette, postActionsTemplate, ReplyViewManager, Session, VotersCard, ClickTooltip, anonUpvoteCardTemplate, anonDownvoteCardTemplate, bus, voteConstants, utils ) { 'use strict'; var PostActionView = Marionette.ItemView.extend({ template: postActionsTemplate, templateHelpers: function () { return { discussionRoute: this.model.thread.permalink(), sessionUserHasUpvoted: this.model.get('userScore') > 0, sessionUserHasDownvoted: this.model.get('userScore') < 0, wasAuthoredBySessionUser: this.model.author.isSessionUser(), isLoggedIn: Session.get().isLoggedIn(), isWithinEditPeriod: (new Date()).toISOString() < this.model.get('editableUntil'), showDownvoteCount: this.model.get('dislikes') && this.model.thread.forum.get('votingType') !== voteConstants.VOTING_TYPES.DOWNVOTE_LIMITED, showDownvote: this.model.thread.forum.get('votingType') !== voteConstants.VOTING_TYPES.DOWNVOTE_DISABLED && this.model.thread.forum.get('votingType') !== voteConstants.VOTING_TYPES.DISABLED, showUpvote: this.model.thread.forum.get('votingType') !== voteConstants.VOTING_TYPES.DISABLED, }; }, events: { 'click [data-action=vote]': 'vote', 'click [data-action=reply]': 'toggleReplyView', 'click [data-action=edit]': 'trackEdit', }, initialize: function (options) { this.session = Session.get(); this.initializeReplyViewManager(options); }, initializeReplyViewManager: function (options) { this.replyViewManager = new ReplyViewManager(options.parentPostView); }, onRender: function () { this.initVotersCard(); this.initAnonVoteCards(); }, initAnonVoteCards: function () { // Don't show anon vote tooltip if user is not anon, or // if forum allows anon voting if (!Session.isKnownToBeLoggedOut() || this.model.thread.forum.get('settings').allowAnonVotes) return; if (this.anonUpvoteCard) this.anonUpvoteCard.remove(); var $target = this.$('[data-action=vote][data-vote=1]'); if (!$target.length) return; this.anonUpvoteCard = ClickTooltip.create({ targetElement: $target, template: anonUpvoteCardTemplate, id: 'anonUpvote', }); this.listenTo(this.anonUpvoteCard, 'show', this.closeUpvotersCard); if (this.anonDownvoteCard) this.anonDownvoteCard.remove(); $target = this.$('[data-action=vote][data-vote=-1]'); if (!$target.length) return; this.anonDownvoteCard = ClickTooltip.create({ targetElement: $target, template: anonDownvoteCardTemplate, id: 'anonDownvote', }); this.listenTo(this.anonDownvoteCard, 'show', this.closeDownvotersCard); }, closeUpvotersCard: function () { if (!this.upvoteHoverCard) return; this.upvoteHoverCard.hide(); }, closeDownvotersCard: function () { if (!this.downvoteHoverCard) return; this.downvoteHoverCard.hide(); }, initVotersCard: function () { // We don't use voter cards on devices with mobile-like UA strings. if (utils.isMobileUserAgent()) return; if (this.upvoteHoverCard) this.upvoteHoverCard.remove(); if (this.downvoteHoverCard) this.downvoteHoverCard.remove(); var upvoteTarget = this.$('[data-action=vote][data-vote=1]'); var downvoteTarget = this.$('[data-action=vote][data-vote=-1]'); var forumVotingType = this.model.thread.forum.get('votingType'); if (upvoteTarget.length && forumVotingType !== voteConstants.VOTING_TYPES.DISABLED) { this.upvoteHoverCard = VotersCard.create({ targetElement: upvoteTarget, model: this.model, session: this.session, voteType: 1, }); } if (downvoteTarget.length && (forumVotingType === null || forumVotingType === undefined || forumVotingType === voteConstants.VOTING_TYPES.DETAILED)) { this.downvoteHoverCard = VotersCard.create({ targetElement: downvoteTarget, model: this.model, session: this.session, voteType: -1, }); } }, vote: function (evt) { // Don't allow voting if user is anon and forum does not // allow anon voting if (!evt || (!this.model.thread.forum.get('settings').allowAnonVotes && Session.isKnownToBeLoggedOut())) return; evt.preventDefault(); evt.stopPropagation(); var $anchor = $(evt.currentTarget); var vote = parseInt($anchor.data('vote'), 10); // Is the user undoing a vote? var isUndo = this.model.get('userScore') === vote; if (isUndo) bus.trigger('uiAction:postUnvote', this.model, evt); else if (vote === 1) bus.trigger('uiAction:postUpvote', this.model, evt); else if (vote === -1) bus.trigger('uiAction:postDownvote', this.model, evt); this.model.vote(isUndo ? 0 : vote); this.$('[data-vote=1]').toggleClass('active', this.model.get('userScore') === 1); this.$('[data-vote=-1]').toggleClass('active', this.model.get('userScore') === -1); var forumVotingType = this.model.thread.forum.get('votingType'); if (forumVotingType !== voteConstants.VOTING_TYPES.DISABLED) this.$('[data-role=upvotes]').text(this.model.get('likes') || ''); if (forumVotingType === null || forumVotingType === undefined || forumVotingType === voteConstants.VOTING_TYPES.DETAILED) this.$('[data-role=downvotes]').text(this.model.get('dislikes') || ''); }, toggleReplyView: function (evt) { if (!evt) return; evt.preventDefault(); evt.stopPropagation(); this.replyViewManager.toggleReplyView(evt.currentTarget); }, trackEdit: function () { bus.trigger('uiAction:postStartUpdate', this.model, { 'forum_id': this.model.thread.forum.attributes.pk, 'thread_id': this.model.thread.id, }); }, }); return PostActionView; }); // https://c.disquscdn.com/next/current/home/js/views/PostActionsView.js