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

216 lines
6.6 KiB
CoffeeScript
Raw Normal View History

2015-11-11 08:12:51 -05:00
class @AwardsHandler
constructor: (@getEmojisUrl, @postEmojiUrl, @noteableType, @noteableId, @unicodes) ->
2016-04-25 15:37:25 -04:00
$('.js-add-award').on 'click', (event) =>
event.stopPropagation()
event.preventDefault()
@showEmojiMenu()
2016-04-25 15:37:25 -04:00
$('html').on 'click', (event) ->
if !$(event.target).closest('.emoji-menu').length
if $('.emoji-menu').is(':visible')
$('.emoji-menu').removeClass 'is-visible'
2015-11-11 08:12:51 -05:00
2016-04-25 15:37:25 -04: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)
2016-04-25 15:37:25 -04:00
.find('.icon')
.data 'emoji'
2016-04-25 15:37:25 -04:00
if emoji is 'thumbsup' and awardsHandler.didUserClickEmoji $(this), 'thumbsdown'
awardsHandler.addAward 'thumbsdown'
2016-04-25 15:37:25 -04:00
else if emoji is 'thumbsdown' and awardsHandler.didUserClickEmoji $(this), 'thumbsup'
awardsHandler.addAward 'thumbsup'
awardsHandler.addAward emoji
$(this).trigger 'blur'
didUserClickEmoji: (that, emoji) ->
2016-04-25 15:37:25 -04:00
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: ->
2016-04-25 15:37:25 -04:00
if $('.emoji-menu').length
if $('.emoji-menu').is '.is-visible'
$('.emoji-menu').removeClass 'is-visible'
$('#emoji_search').blur()
2016-03-11 04:47:18 -05:00
else
2016-04-25 15:37:25 -04:00
$('.emoji-menu').addClass 'is-visible'
$('#emoji_search').focus()
else
2016-04-25 15:37:25 -04:00
$('.js-add-award').addClass 'is-loading'
$.get @getEmojisUrl, (response) =>
2016-04-25 15:37:25 -04:00
$('.js-add-award').removeClass 'is-loading'
$('.js-award-holder').append response
2016-03-11 04:47:18 -05:00
setTimeout =>
2016-04-25 15:37:25 -04:00
$('.emoji-menu').addClass 'is-visible'
$('#emoji_search').focus()
2016-03-11 04:47:18 -05:00
@setupSearch()
, 200
2015-11-11 08:12:51 -05:00
addAward: (emoji) ->
@postEmoji emoji, =>
@addAwardToEmojiBar(emoji)
2016-04-25 15:37:25 -04:00
$('.emoji-menu').removeClass 'is-visible'
addAwardToEmojiBar: (emoji) ->
@addEmojiToFrequentlyUsedList(emoji)
if @exist(emoji)
if @isActive(emoji)
@decrementCounter(emoji)
else
2016-04-25 15:37:25 -04:00
counter = @findEmojiIcon(emoji).siblings('.js-counter')
counter.text(parseInt(counter.text()) + 1)
2016-04-25 15:37:25 -04:00
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) ->
2016-04-25 15:37:25 -04:00
@findEmojiIcon(emoji).parent().hasClass('active')
2015-11-11 08:12:51 -05:00
decrementCounter: (emoji) ->
2016-04-25 15:37:25 -04:00
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)
2016-04-25 15:37:25 -04:00
emojiIcon.removeClass('active')
2015-11-18 08:43:53 -05:00
@removeMeFromAuthorList(emoji)
2016-04-25 15:37:25 -04:00
else if emoji == 'thumbsup' || emoji == 'thumbsdown'
emojiIcon.tooltip('destroy')
counter.text(0)
2016-04-25 15:37:25 -04:00
emojiIcon.removeClass('active')
@removeMeFromAuthorList(emoji)
2015-11-11 08:12:51 -05:00
else
2016-04-25 15:37:25 -04:00
emojiIcon.tooltip('destroy')
emojiIcon.remove()
2015-11-11 08:12:51 -05:00
2015-11-18 08:43:53 -05:00
removeMeFromAuthorList: (emoji) ->
awardBlock = @findEmojiIcon(emoji).parent()
authors = awardBlock
2016-04-25 15:37:25 -04:00
.attr('data-original-title')
.split(', ')
authors.splice(authors.indexOf('me'),1)
awardBlock
2016-04-25 15:37:25 -04:00
.closest('.js-emoji-btn')
.attr('data-original-title', authors.join(', '))
@resetTooltip(awardBlock)
2015-11-18 08:43:53 -05:00
addMeToAuthorList: (emoji) ->
awardBlock = @findEmojiIcon(emoji).parent()
2016-04-25 15:37:25 -04:00
origTitle = awardBlock.attr('data-original-title').trim()
authors = []
if origTitle
authors = origTitle.split(', ')
2016-04-25 15:37:25 -04:00
authors.push('me')
awardBlock.attr('data-original-title', authors.join(', '))
@resetTooltip(awardBlock)
2015-11-18 08:43:53 -05:00
resetTooltip: (award) ->
2016-04-25 15:37:25 -04:00
award.tooltip('destroy')
2015-11-18 08:43:53 -05:00
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>"
)
$(nodes.join("\n"))
2016-04-25 15:37:25 -04:00
.insertBefore('.js-award-holder')
.find('.emoji-icon')
.data('emoji', emoji)
$('.award-control').tooltip()
2015-11-18 08:43:53 -05:00
resolveNameToCssClass: (emoji) ->
2016-04-25 15:37:25 -04:00
emojiIcon = $(".emoji-menu-content [data-emoji='#{emoji}']")
2016-04-25 15:37:25 -04:00
if emojiIcon.length > 0
unicodeName = emojiIcon.data('unicode-name')
else
# Find by alias
unicodeName = $(".emoji-menu-content [data-aliases*=':#{emoji}:']").data('unicode-name')
"emoji-#{unicodeName}"
2015-11-11 08:12:51 -05:00
postEmoji: (emoji, callback) ->
$.post @postEmojiUrl, { note: {
2015-12-24 04:28:51 -05:00
note: ":#{emoji}:"
noteable_type: @noteableType
noteable_id: @noteableId
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) ->
frequentlyUsedEmojis = @getFrequentlyUsedEmojis()
frequentlyUsedEmojis.push(emoji)
2016-04-25 15:37:25 -04:00
$.cookie('frequently_used_emojis', frequentlyUsedEmojis.join(','), { expires: 365 })
getFrequentlyUsedEmojis: ->
2016-04-25 15:37:25 -04:00
frequentlyUsedEmojis = ($.cookie('frequently_used_emojis') || '').split(',')
_.compact(_.uniq(frequentlyUsedEmojis))
renderFrequentlyUsedBlock: ->
if $.cookie('frequently_used_emojis')
frequentlyUsedEmojis = @getFrequentlyUsedEmojis()
2016-04-25 15:37:25 -04:00
ul = $('<ul>')
for emoji in frequentlyUsedEmojis
do (emoji) ->
2016-04-25 15:37:25 -04:00
$(".emoji-menu-content [data-emoji='#{emoji}']").closest('li').clone().appendTo(ul)
2016-04-25 15:37:25 -04:00
$('input.emoji-search').after(ul).after($('<h5>').text('Frequently used'))
2015-12-23 05:01:31 -05:00
setupSearch: ->
2016-04-25 15:37:25 -04:00
$('input.emoji-search').keyup (ev) =>
2015-12-23 05:01:31 -05:00
term = $(ev.target).val()
# Clean previous search results
2016-04-25 15:37:25 -04: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
2016-04-25 15:37:25 -04:00
h5 = $('<h5>').text('Search results').addClass('emoji-search')
foundEmojis = @searchEmojis(term).show()
2016-04-25 15:37:25 -04:00
ul = $('<ul>').addClass('emoji-menu-list emoji-menu-search').append(foundEmojis)
$('.emoji-menu-content ul, .emoji-menu-content h5').hide()
$('.emoji-menu-content').append(h5).append(ul)
2015-12-23 05:01:31 -05:00
else
2016-04-25 15:37:25 -04:00
$('.emoji-menu-content').children().show()
2015-12-23 05:01:31 -05:00
searchEmojis: (term)->
2015-12-24 04:57:46 -05:00
$(".emoji-menu-content [data-emoji*='#{term}']").closest("li").clone()