gitlab-org--gitlab-foss/app/assets/javascripts/awards_handler.coffee

59 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2015-11-11 13:12:51 +00:00
class @AwardsHandler
constructor: (@post_emoji_url, @noteable_type, @noteable_id) ->
addAward: (emoji) ->
@postEmoji emoji, =>
@addAwardToEmojiBar(emoji)
2015-11-11 13:12:51 +00:00
addAwardToEmojiBar: (emoji) ->
if @exist(emoji)
if @isActive(emoji)
@decrementCounter(emoji)
else
counter = @findEmojiIcon(emoji).siblings(".counter")
counter.text(parseInt(counter.text()) + 1)
counter.parent().addClass("active")
else
@createEmoji(emoji)
2015-11-11 13:12:51 +00:00
exist: (emoji) ->
@findEmojiIcon(emoji).length > 0
2015-11-11 13:12:51 +00:00
isActive: (emoji) ->
@findEmojiIcon(emoji).parent().hasClass("active")
2015-11-11 13:12:51 +00:00
decrementCounter: (emoji) ->
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")
else
counter.parent().remove()
createEmoji: (emoji) ->
nodes = []
nodes.push("<div class='award active'>")
nodes.push("<div class='icon' data-emoji='" + emoji + "'>")
2015-11-11 13:12:51 +00:00
nodes.push(@getImage(emoji))
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
getImage: (emoji) ->
$("li[data-emoji='" + emoji + "'").html()
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
callback.call()
findEmojiIcon: (emoji) ->
$(".icon[data-emoji='" + emoji + "'")