2015-11-11 13:12:51 +00:00
|
|
|
class @AwardsHandler
|
|
|
|
constructor: (@post_emoji_url, @noteable_type, @noteable_id) ->
|
|
|
|
|
|
|
|
addAward: (emoji) ->
|
|
|
|
@postEmoji emoji, =>
|
2015-11-17 14:44:58 +00:00
|
|
|
@addAwardToEmojiBar(emoji)
|
2015-11-11 13:12:51 +00:00
|
|
|
|
2015-11-18 21:59:58 +00:00
|
|
|
addAwardToEmojiBar: (emoji, custom_path = '') ->
|
2015-11-17 14:44:58 +00:00
|
|
|
if @exist(emoji)
|
|
|
|
if @isActive(emoji)
|
|
|
|
@decrementCounter(emoji)
|
|
|
|
else
|
|
|
|
counter = @findEmojiIcon(emoji).siblings(".counter")
|
|
|
|
counter.text(parseInt(counter.text()) + 1)
|
|
|
|
counter.parent().addClass("active")
|
2015-11-18 13:43:53 +00:00
|
|
|
@addMeToAuthorList(emoji)
|
2015-11-17 14:44:58 +00:00
|
|
|
else
|
2015-11-18 21:59:58 +00:00
|
|
|
@createEmoji(emoji, custom_path)
|
2015-11-11 13:12:51 +00:00
|
|
|
|
|
|
|
exist: (emoji) ->
|
2015-11-17 14:44:58 +00:00
|
|
|
@findEmojiIcon(emoji).length > 0
|
2015-11-11 13:12:51 +00:00
|
|
|
|
|
|
|
isActive: (emoji) ->
|
2015-11-17 14:44:58 +00:00
|
|
|
@findEmojiIcon(emoji).parent().hasClass("active")
|
2015-11-11 13:12:51 +00:00
|
|
|
|
|
|
|
decrementCounter: (emoji) ->
|
2015-11-17 14:44:58 +00:00
|
|
|
counter = @findEmojiIcon(emoji).siblings(".counter")
|
2015-11-11 13:12:51 +00:00
|
|
|
|
|
|
|
if parseInt(counter.text()) > 1
|
|
|
|
counter.text(parseInt(counter.text()) - 1)
|
|
|
|
counter.parent().removeClass("active")
|
2015-11-18 13:43:53 +00:00
|
|
|
@removeMeFromAuthorList(emoji)
|
2015-11-11 13:12:51 +00:00
|
|
|
else
|
2015-11-18 13:43:53 +00:00
|
|
|
award = counter.parent()
|
|
|
|
award.tooltip("destroy")
|
|
|
|
award.remove()
|
2015-11-11 13:12:51 +00:00
|
|
|
|
2015-11-18 13:43:53 +00:00
|
|
|
removeMeFromAuthorList: (emoji) ->
|
|
|
|
award_block = @findEmojiIcon(emoji).parent()
|
|
|
|
authors = award_block.attr("data-original-title").split(", ")
|
|
|
|
authors = _.without(authors, "me").join(", ")
|
|
|
|
award_block.attr("title", authors)
|
|
|
|
@resetTooltip(award_block)
|
|
|
|
|
|
|
|
addMeToAuthorList: (emoji) ->
|
|
|
|
award_block = @findEmojiIcon(emoji).parent()
|
|
|
|
authors = award_block.attr("data-original-title").split(", ")
|
|
|
|
authors.push("me")
|
|
|
|
award_block.attr("title", authors.join(", "))
|
|
|
|
@resetTooltip(award_block)
|
|
|
|
|
|
|
|
resetTooltip: (award) ->
|
|
|
|
award.tooltip("destroy")
|
|
|
|
|
|
|
|
# "destroy" call is asynchronous, this is why we need to set timeout.
|
|
|
|
setTimeout (->
|
|
|
|
award.tooltip()
|
|
|
|
), 200
|
|
|
|
|
2015-11-11 13:12:51 +00:00
|
|
|
|
2015-11-18 21:59:58 +00:00
|
|
|
createEmoji: (emoji, custom_path) ->
|
2015-11-11 13:12:51 +00:00
|
|
|
nodes = []
|
2015-11-18 13:43:53 +00:00
|
|
|
nodes.push("<div class='award active' title='me'>")
|
2015-11-17 14:44:58 +00:00
|
|
|
nodes.push("<div class='icon' data-emoji='" + emoji + "'>")
|
2015-11-18 21:59:58 +00:00
|
|
|
nodes.push(@getImage(emoji, custom_path))
|
2015-11-11 13:12:51 +00:00
|
|
|
nodes.push("</div>")
|
|
|
|
nodes.push("<div class='counter'>1")
|
|
|
|
nodes.push("</div></div>")
|
|
|
|
|
2015-11-17 11:16:16 +00:00
|
|
|
$(".awards-controls").before(nodes.join("\n"))
|
2015-11-11 13:12:51 +00:00
|
|
|
|
2015-11-18 13:43:53 +00:00
|
|
|
$(".award").tooltip()
|
|
|
|
|
2015-11-18 21:59:58 +00:00
|
|
|
getImage: (emoji, custom_path) ->
|
|
|
|
if custom_path
|
|
|
|
$(".awards-menu li").first().html().replace(/emoji\/.*\.png/, custom_path)
|
|
|
|
else
|
2015-11-19 11:20:09 +00:00
|
|
|
$("li[data-emoji='" + emoji + "']").html()
|
2015-11-18 21:59:58 +00:00
|
|
|
|
2015-11-11 13:12:51 +00:00
|
|
|
|
|
|
|
postEmoji: (emoji, callback) ->
|
|
|
|
$.post @post_emoji_url, {
|
|
|
|
emoji: emoji
|
|
|
|
noteable_type: @noteable_type
|
|
|
|
noteable_id: @noteable_id
|
|
|
|
},(data) ->
|
|
|
|
if data.ok
|
2015-11-17 14:44:58 +00:00
|
|
|
callback.call()
|
|
|
|
|
|
|
|
findEmojiIcon: (emoji) ->
|
2015-11-19 11:20:09 +00:00
|
|
|
$(".icon[data-emoji='" + emoji + "']")
|