diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2013-04-16 05:21:21 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2013-04-16 05:21:31 -0400 |
commit | d850e2ab84144240db8eb122dc04679280c599d8 (patch) | |
tree | 419499f8aff5b0500ccaa5a55d79e0e29c83cc1c | |
parent | c913ec44fb92711712df93c3ca1f2c9d8aab628c (diff) | |
download | askbot-d850e2ab84144240db8eb122dc04679280c599d8.tar.gz askbot-d850e2ab84144240db8eb122dc04679280c599d8.tar.bz2 askbot-d850e2ab84144240db8eb122dc04679280c599d8.zip |
created a new js voting control for posts
-rw-r--r-- | askbot/media/js/post.js | 130 | ||||
-rw-r--r-- | askbot/templates/macros.html | 4 |
2 files changed, 131 insertions, 3 deletions
diff --git a/askbot/media/js/post.js b/askbot/media/js/post.js index 755ce158..fdb21290 100644 --- a/askbot/media/js/post.js +++ b/askbot/media/js/post.js @@ -1219,7 +1219,7 @@ var questionRetagger = function(){ } }, error: function(xhr, textStatus, errorThrown) { - showMessage(tagsDiv, 'sorry, something is not right here'); + showMessage(tagsDiv, gettext('sorry, something is not right here')); cancelRetag(); } }); @@ -1343,6 +1343,134 @@ var questionRetagger = function(){ }; }(); +/** + * @constructor + * Controls vor voting for a post + */ +var VoteControls = function() { + WrappedElement.call(this); + this._postAuthorId = undefined; + this._postId = undefined; +}; +inherits(VoteControls, WrappedElement); + +VoteControls.prototype.setPostId = function(postId) { + this._postId = postId; +}; + +VoteControls.prototype.getPostId = function() { + return this._postId; +}; + +VoteControls.prototype.setPostAuthorId = function(userId) { + this._postAuthorId = userId; +}; + +VoteControls.prototype.setSlug = function(slug) { + this._slug = slug; +}; + +VoteControls.prototype.setPostType = function(postType) { + this._postType = postType; +}; + +VoteControls.prototype.getPostType = function() { + return this._postType; +}; + +VoteControls.prototype.clearVotes = function() { + this._upvoteButton.removeClass('on'); + this._downvoteButton.removeClass('on'); +}; + +VoteControls.prototype.toggleButton = function(button) { + if (button.hasClass('on')) { + button.removeClass('on'); + } else { + button.addClass('on'); + } +}; + +VoteControls.prototype.toggleVote = function(voteType) { + if (voteType === 'upvote') { + this.toggleButton(this._upvoteButton); + } else { + this.toggleButton(this._downvoteButton); + } +}; + +VoteControls.prototype.setVoteCount = function(count) { + this._voteCount.html(count); +}; + +VoteControls.prototype.updateDisplay = function(voteType, data) { + if (data['status'] == '1'){ + this.clearVotes(); + } else { + this.toggleVote(voteType); + } + this.setVoteCount(data['count']); + if (data['message'] && data['message'].length > 0){ + showMessage(this._element, data.message); + } +}; + +VoteControls.prototype.getAnonymousMessage = function(message) { + var pleaseLogin = " <a href='" + askbot['urls']['user_signin'] + ">" + + gettext('please login') + "</a>"; + message += pleaseLogin; + message = message.replace("{{QuestionID}}", me._postId); + return message.replace('{{questionSlug}}', me._slug); +}; + +VoteControls.prototype.getVoteHandler = function(voteType) { + var me = this; + return function() { + if (askbot['data']['userIsAuthenticated'] === false) { + var message = me.getAnonymousMessage(gettext('anonymous users cannot vote')); + showMessage(me.getElement(), message); + } else { + //this function submits votes + var voteMap = { + 'question': { 'upvote': 1, 'downvote': 2 }, + 'answer': { 'upvote': 5, 'downvote': 6 } + }; + var legacyVoteType = voteMap[me.getPostType()][voteType]; + $.ajax({ + type: "POST", + cache: false, + dataType: "json", + url: askbot['urls']['vote_url'], + data: { + "type": legacyVoteType, + "postId": me.getPostId() + }, + error: function() { + showMessage(me.getElement(), gettext('sorry, something is not right here')); + }, + success: function(data) { + if (data['success']) { + me.updateDisplay(voteType, data); + } else { + showMessage(me.getElement(), data['message']); + } + } + }); + } + }; +}; + +VoteControls.prototype.decorate = function(element) { + this._element = element; + var upvoteButton = element.find('.upvote'); + this._upvoteButton = upvoteButton; + setupButtonEventHandlers(upvoteButton, this.getVoteHandler('upvote')); + var downvoteButton = element.find('.downvote'); + this._downvoteButton = downvoteButton; + setupButtonEventHandlers(downvoteButton, this.getVoteHandler('downvote')); + this._voteCount = element.find('.vote-number'); +}; + var DeletePostLink = function(){ SimpleControl.call(this); this._post_id = null; diff --git a/askbot/templates/macros.html b/askbot/templates/macros.html index 74fc37ce..57f19b00 100644 --- a/askbot/templates/macros.html +++ b/askbot/templates/macros.html @@ -36,7 +36,7 @@ {%- macro post_vote_buttons(post = None) -%} <div id="{{post.post_type}}-img-upvote-{{ post.id }}" - class="{{post.post_type}}-img-upvote post-vote"> + class="{{post.post_type}}-img-upvote post-vote upvote"> </div> <div id="{{post.post_type}}-vote-number-{{ post.id }}" @@ -45,7 +45,7 @@ >{{ post.score }}</div> <div id="{{post.post_type}}-img-downvote-{{ post.id }}" - class="{{post.post_type}}-img-downvote post-vote"> + class="{{post.post_type}}-img-downvote post-vote downvote"> </div> <script type="text/javascript"> askbot['functions']['renderPostVoteButtons']('{{post.post_type}}', '{{post.id}}'); |