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

208 lines
6.4 KiB
CoffeeScript
Raw Normal View History

2015-11-11 08:12:51 -05:00
class @AwardsHandler
constructor: (@get_emojis_url, @post_emoji_url, @noteable_type, @noteable_id, @unicodes) ->
2016-03-02 10:40:00 -05:00
$(".js-add-award").on "click", (event) =>
event.stopPropagation()
event.preventDefault()
@showEmojiMenu()
$("html").on 'click', (event) ->
if !$(event.target).closest(".emoji-menu").length
if $(".emoji-menu").is(":visible")
2016-03-11 04:47:18 -05:00
$(".emoji-menu").removeClass "is-visible"
2015-11-11 08:12:51 -05:00
2016-03-04 04:13:06 -05:00
$(".awards")
.off "click"
.on "click", ".js-emoji-btn", @handleClick
@renderFrequentlyUsedBlock()
handleClick: (e) ->
e.preventDefault()
2016-03-02 10:40:00 -05:00
emoji = $(this)
.find(".icon")
.data "emoji"
if emoji is "thumbsup" and awards_handler.didUserClickEmoji $(this), "thumbsdown"
awards_handler.addAward "thumbsdown"
else if emoji is "thumbsdown" and awards_handler.didUserClickEmoji $(this), "thumbsup"
awards_handler.addAward "thumbsup"
awards_handler.addAward emoji
$(this).trigger 'blur'
didUserClickEmoji: (that, emoji) ->
if $(that).siblings("button:has([data-emoji=#{emoji}])").attr("data-original-title")
$(that).siblings("button:has([data-emoji=#{emoji}])").attr("data-original-title").indexOf('me') > -1
showEmojiMenu: ->
if $(".emoji-menu").length
2016-03-11 04:47:18 -05:00
if $(".emoji-menu").is ".is-visible"
$(".emoji-menu").removeClass "is-visible"
$("#emoji_search").blur()
else
$(".emoji-menu").addClass "is-visible"
$("#emoji_search").focus()
else
$('.js-add-award').addClass "is-loading"
$.get @get_emojis_url, (response) =>
$('.js-add-award').removeClass "is-loading"
$(".js-award-holder").append response
2016-03-11 04:47:18 -05:00
setTimeout =>
$(".emoji-menu").addClass "is-visible"
$("#emoji_search").focus()
@setupSearch()
, 200
2015-11-11 08:12:51 -05:00
addAward: (emoji) ->
@postEmoji emoji, =>
@addAwardToEmojiBar(emoji)
2016-03-11 04:47:18 -05:00
$(".emoji-menu").removeClass "is-visible"
addAwardToEmojiBar: (emoji) ->
@addEmojiToFrequentlyUsedList(emoji)
if @exist(emoji)
if @isActive(emoji)
@decrementCounter(emoji)
else
counter = @findEmojiIcon(emoji).siblings(".js-counter")
counter.text(parseInt(counter.text()) + 1)
counter.parent().addClass("active")
2015-11-18 08:43:53 -05:00
@addMeToAuthorList(emoji)
else
@createEmoji(emoji)
2015-11-11 08:12:51 -05:00
exist: (emoji) ->
@findEmojiIcon(emoji).length > 0
2015-11-11 08:12:51 -05:00
isActive: (emoji) ->
@findEmojiIcon(emoji).parent().hasClass("active")
2015-11-11 08:12:51 -05:00
decrementCounter: (emoji) ->
counter = @findEmojiIcon(emoji).siblings(".js-counter")
emojiIcon = counter.parent()
if parseInt(counter.text()) > 1
2015-11-11 08:12:51 -05:00
counter.text(parseInt(counter.text()) - 1)
emojiIcon.removeClass("active")
2015-11-18 08:43:53 -05:00
@removeMeFromAuthorList(emoji)
else if emoji == "thumbsup" || emoji == "thumbsdown"
emojiIcon.tooltip("destroy")
counter.text(0)
emojiIcon.removeClass("active")
@removeMeFromAuthorList(emoji)
2015-11-11 08:12:51 -05:00
else
emojiIcon.tooltip("destroy")
emojiIcon.remove()
2015-11-11 08:12:51 -05:00
2015-11-18 08:43:53 -05:00
removeMeFromAuthorList: (emoji) ->
award_block = @findEmojiIcon(emoji).parent()
2016-03-02 10:40:00 -05:00
authors = award_block
.attr("data-original-title")
.split(", ")
authors.splice(authors.indexOf("me"),1)
2016-03-02 10:40:00 -05:00
award_block
.closest(".js-emoji-btn")
.attr("data-original-title", authors.join(", "))
2015-11-18 08:43:53 -05:00
@resetTooltip(award_block)
addMeToAuthorList: (emoji) ->
award_block = @findEmojiIcon(emoji).parent()
origTitle = award_block.attr("data-original-title").trim()
authors = []
if origTitle
authors = origTitle.split(', ')
authors.push("me")
award_block.attr("data-original-title", authors.join(", "))
2015-11-18 08:43:53 -05:00
@resetTooltip(award_block)
resetTooltip: (award) ->
award.tooltip("destroy")
2015-12-22 13:02:22 -05:00
# "destroy" call is asynchronous and there is no appropriate callback on it, this is why we need to set timeout.
2015-11-18 08:43:53 -05:00
setTimeout (->
award.tooltip()
), 200
2015-11-11 08:12:51 -05:00
createEmoji: (emoji) ->
emojiCssClass = @resolveNameToCssClass(emoji)
2015-11-11 08:12:51 -05:00
nodes = []
2016-03-02 10:43:01 -05:00
nodes.push(
"<button class='btn award-control js-emoji-btn has-tooltip active' data-original-title='me'>",
2016-03-02 10:43:01 -05:00
"<div class='icon emoji-icon #{emojiCssClass}' data-emoji='#{emoji}'></div>",
"<span class='award-control-text js-counter'>1</span>",
"</button>"
)
emoji_node = $(nodes.join("\n"))
.insertBefore(".js-award-holder")
.find(".emoji-icon")
.data("emoji", emoji)
$('.award-control').tooltip()
2015-11-18 08:43:53 -05:00
resolveNameToCssClass: (emoji) ->
"emoji-#{@unicodes[emoji]}"
2015-11-11 08:12:51 -05:00
postEmoji: (emoji, callback) ->
2015-11-19 11:12:17 -05:00
$.post @post_emoji_url, { note: {
2015-12-24 04:28:51 -05:00
note: ":#{emoji}:"
2015-11-11 08:12:51 -05:00
noteable_type: @noteable_type
noteable_id: @noteable_id
2015-11-19 11:12:17 -05:00
}},(data) ->
2015-11-11 08:12:51 -05:00
if data.ok
callback.call()
findEmojiIcon: (emoji) ->
$(".awards > .js-emoji-btn [data-emoji='#{emoji}']")
scrollToAwards: ->
$('body, html').animate({
scrollTop: $('.awards').offset().top - 80
}, 200)
2015-12-11 06:10:00 -05:00
addEmojiToFrequentlyUsedList: (emoji) ->
frequently_used_emojis = @getFrequentlyUsedEmojis()
frequently_used_emojis.push(emoji)
$.cookie('frequently_used_emojis', frequently_used_emojis.join(","), { expires: 365 })
getFrequentlyUsedEmojis: ->
frequently_used_emojis = ($.cookie('frequently_used_emojis') || "").split(",")
_.compact(_.uniq(frequently_used_emojis))
renderFrequentlyUsedBlock: ->
if $.cookie('frequently_used_emojis')
frequently_used_emojis = @getFrequentlyUsedEmojis()
ul = $("<ul>")
for emoji in frequently_used_emojis
do (emoji) ->
$(".emoji-menu-content [data-emoji='#{emoji}']").closest("li").clone().appendTo(ul)
$("input.emoji-search").after(ul).after($("<h5>").text("Frequently used"))
2015-12-23 05:01:31 -05:00
setupSearch: ->
2015-12-24 04:57:46 -05:00
$("input.emoji-search").keyup (ev) =>
2015-12-23 05:01:31 -05:00
term = $(ev.target).val()
# Clean previous search results
2016-03-11 04:11:36 -05:00
$("ul.emoji-menu-search, h5.emoji-search").remove()
2015-12-23 05:01:31 -05:00
if term
2015-12-24 08:44:43 -05:00
# Generate a search result block
2015-12-23 05:01:31 -05:00
h5 = $("<h5>").text("Search results").addClass("emoji-search")
found_emojis = @searchEmojis(term).show()
2016-03-11 04:11:36 -05:00
ul = $("<ul>").addClass("emoji-menu-list emoji-menu-search").append(found_emojis)
2015-12-23 05:01:31 -05:00
$(".emoji-menu-content ul, .emoji-menu-content h5").hide()
$(".emoji-menu-content").append(h5).append(ul)
else
$(".emoji-menu-content").children().show()
searchEmojis: (term)->
2015-12-24 04:57:46 -05:00
$(".emoji-menu-content [data-emoji*='#{term}']").closest("li").clone()