2015-11-11 08:12:51 -05:00
|
|
|
class @AwardsHandler
|
2016-04-22 06:25:32 -04:00
|
|
|
constructor: (@getEmojisUrl, @postEmojiUrl, @noteableType, @noteableId, @unicodes) ->
|
2016-04-25 15:37:25 -04:00
|
|
|
$('.js-add-award').on 'click', (event) =>
|
2015-12-17 07:29:55 -05:00
|
|
|
event.stopPropagation()
|
|
|
|
event.preventDefault()
|
2016-02-18 23:02:22 -05:00
|
|
|
|
|
|
|
@showEmojiMenu()
|
2015-12-17 07:29:55 -05:00
|
|
|
|
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
|
2016-03-01 06:25:59 -05:00
|
|
|
|
2015-12-22 16:04:19 -05:00
|
|
|
@renderFrequentlyUsedBlock()
|
2016-03-01 06:25:59 -05:00
|
|
|
|
|
|
|
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-01 12:27:28 -04:00
|
|
|
|
2016-04-25 15:37:25 -04:00
|
|
|
if emoji is 'thumbsup' and awardsHandler.didUserClickEmoji $(this), 'thumbsdown'
|
|
|
|
awardsHandler.addAward 'thumbsdown'
|
2016-04-01 12:27:28 -04:00
|
|
|
|
2016-04-25 15:37:25 -04:00
|
|
|
else if emoji is 'thumbsdown' and awardsHandler.didUserClickEmoji $(this), 'thumbsup'
|
|
|
|
awardsHandler.addAward 'thumbsup'
|
2016-04-01 12:27:28 -04:00
|
|
|
|
2016-04-22 06:25:32 -04:00
|
|
|
awardsHandler.addAward emoji
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2016-04-22 09:38:29 -04:00
|
|
|
$(this).trigger 'blur'
|
|
|
|
|
2016-04-01 12:27:28 -04:00
|
|
|
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
|
2016-04-01 12:27:28 -04:00
|
|
|
|
2016-02-18 23:02:22 -05:00
|
|
|
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()
|
2016-02-18 23:02:22 -05:00
|
|
|
else
|
2016-04-25 15:37:25 -04:00
|
|
|
$('.js-add-award').addClass 'is-loading'
|
2016-04-22 06:25:32 -04:00
|
|
|
$.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
|
2016-02-18 23:02:22 -05:00
|
|
|
|
2015-11-11 08:12:51 -05:00
|
|
|
addAward: (emoji) ->
|
|
|
|
@postEmoji emoji, =>
|
2015-11-17 09:44:58 -05:00
|
|
|
@addAwardToEmojiBar(emoji)
|
2015-12-17 07:29:55 -05:00
|
|
|
|
2016-04-25 15:37:25 -04:00
|
|
|
$('.emoji-menu').removeClass 'is-visible'
|
2016-01-15 10:04:48 -05:00
|
|
|
|
2015-12-17 07:29:55 -05:00
|
|
|
addAwardToEmojiBar: (emoji) ->
|
2015-12-22 16:04:19 -05:00
|
|
|
@addEmojiToFrequentlyUsedList(emoji)
|
|
|
|
|
2015-11-17 09:44:58 -05:00
|
|
|
if @exist(emoji)
|
|
|
|
if @isActive(emoji)
|
|
|
|
@decrementCounter(emoji)
|
|
|
|
else
|
2016-04-25 15:37:25 -04:00
|
|
|
counter = @findEmojiIcon(emoji).siblings('.js-counter')
|
2015-11-17 09:44:58 -05:00
|
|
|
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)
|
2015-11-17 09:44:58 -05:00
|
|
|
else
|
2015-12-17 07:29:55 -05:00
|
|
|
@createEmoji(emoji)
|
2015-11-11 08:12:51 -05:00
|
|
|
|
|
|
|
exist: (emoji) ->
|
2015-11-17 09:44:58 -05:00
|
|
|
@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')
|
2015-12-24 12:46:35 -05:00
|
|
|
emojiIcon = counter.parent()
|
2016-01-19 15:43:04 -05:00
|
|
|
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')
|
2015-12-24 12:46:35 -05:00
|
|
|
counter.text(0)
|
2016-04-25 15:37:25 -04:00
|
|
|
emojiIcon.removeClass('active')
|
2016-02-12 20:41:52 -05:00
|
|
|
@removeMeFromAuthorList(emoji)
|
2015-11-11 08:12:51 -05:00
|
|
|
else
|
2016-04-25 15:37:25 -04:00
|
|
|
emojiIcon.tooltip('destroy')
|
2015-12-24 12:46:35 -05:00
|
|
|
emojiIcon.remove()
|
2015-11-11 08:12:51 -05:00
|
|
|
|
2015-11-18 08:43:53 -05:00
|
|
|
removeMeFromAuthorList: (emoji) ->
|
2016-04-22 06:25:32 -04:00
|
|
|
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)
|
2016-04-22 06:25:32 -04:00
|
|
|
awardBlock
|
2016-04-25 15:37:25 -04:00
|
|
|
.closest('.js-emoji-btn')
|
|
|
|
.attr('data-original-title', authors.join(', '))
|
2016-04-22 06:25:32 -04:00
|
|
|
@resetTooltip(awardBlock)
|
2015-11-18 08:43:53 -05:00
|
|
|
|
|
|
|
addMeToAuthorList: (emoji) ->
|
2016-04-22 06:25:32 -04:00
|
|
|
awardBlock = @findEmojiIcon(emoji).parent()
|
2016-04-25 15:37:25 -04:00
|
|
|
origTitle = awardBlock.attr('data-original-title').trim()
|
2016-01-18 15:40:48 -05:00
|
|
|
authors = []
|
|
|
|
if origTitle
|
|
|
|
authors = origTitle.split(', ')
|
2016-04-25 15:37:25 -04:00
|
|
|
authors.push('me')
|
|
|
|
awardBlock.attr('data-original-title', authors.join(', '))
|
2016-04-22 06:25:32 -04:00
|
|
|
@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
|
2016-01-15 10:04:48 -05:00
|
|
|
|
2015-11-11 08:12:51 -05:00
|
|
|
|
2015-12-17 07:29:55 -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(
|
2016-04-01 12:27:28 -04:00
|
|
|
"<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>"
|
|
|
|
)
|
|
|
|
|
2016-04-22 06:25:32 -04:00
|
|
|
$(nodes.join("\n"))
|
2016-04-25 15:37:25 -04:00
|
|
|
.insertBefore('.js-award-holder')
|
|
|
|
.find('.emoji-icon')
|
|
|
|
.data('emoji', emoji)
|
2016-03-01 06:25:59 -05:00
|
|
|
$('.award-control').tooltip()
|
2015-11-18 08:43:53 -05:00
|
|
|
|
2015-12-17 07:29:55 -05:00
|
|
|
resolveNameToCssClass: (emoji) ->
|
2016-04-25 15:37:25 -04:00
|
|
|
emojiIcon = $(".emoji-menu-content [data-emoji='#{emoji}']")
|
2016-04-22 06:25:32 -04:00
|
|
|
|
2016-04-25 15:37:25 -04:00
|
|
|
if emojiIcon.length > 0
|
|
|
|
unicodeName = emojiIcon.data('unicode-name')
|
2016-04-22 06:25:32 -04:00
|
|
|
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) ->
|
2016-04-22 06:25:32 -04:00
|
|
|
$.post @postEmojiUrl, { note: {
|
2015-12-24 04:28:51 -05:00
|
|
|
note: ":#{emoji}:"
|
2016-04-22 06:25:32 -04:00
|
|
|
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
|
2015-11-17 09:44:58 -05:00
|
|
|
callback.call()
|
|
|
|
|
|
|
|
findEmojiIcon: (emoji) ->
|
2016-03-01 06:25:59 -05:00
|
|
|
$(".awards > .js-emoji-btn [data-emoji='#{emoji}']")
|
2015-12-02 03:36:11 -05:00
|
|
|
|
|
|
|
scrollToAwards: ->
|
|
|
|
$('body, html').animate({
|
|
|
|
scrollTop: $('.awards').offset().top - 80
|
|
|
|
}, 200)
|
2015-12-11 06:10:00 -05:00
|
|
|
|
2015-12-22 16:04:19 -05:00
|
|
|
addEmojiToFrequentlyUsedList: (emoji) ->
|
2016-04-22 06:25:32 -04:00
|
|
|
frequentlyUsedEmojis = @getFrequentlyUsedEmojis()
|
|
|
|
frequentlyUsedEmojis.push(emoji)
|
2016-04-25 15:37:25 -04:00
|
|
|
$.cookie('frequently_used_emojis', frequentlyUsedEmojis.join(','), { expires: 365 })
|
2015-12-22 16:04:19 -05:00
|
|
|
|
|
|
|
getFrequentlyUsedEmojis: ->
|
2016-04-25 15:37:25 -04:00
|
|
|
frequentlyUsedEmojis = ($.cookie('frequently_used_emojis') || '').split(',')
|
2016-04-22 06:25:32 -04:00
|
|
|
_.compact(_.uniq(frequentlyUsedEmojis))
|
2015-12-22 16:04:19 -05:00
|
|
|
|
|
|
|
renderFrequentlyUsedBlock: ->
|
2015-12-28 03:11:01 -05:00
|
|
|
if $.cookie('frequently_used_emojis')
|
2016-04-22 06:25:32 -04:00
|
|
|
frequentlyUsedEmojis = @getFrequentlyUsedEmojis()
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2016-04-25 15:37:25 -04:00
|
|
|
ul = $('<ul>')
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2016-04-22 06:25:32 -04:00
|
|
|
for emoji in frequentlyUsedEmojis
|
2015-12-28 03:11:01 -05:00
|
|
|
do (emoji) ->
|
2016-04-25 15:37:25 -04:00
|
|
|
$(".emoji-menu-content [data-emoji='#{emoji}']").closest('li').clone().appendTo(ul)
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2016-04-25 15:37:25 -04:00
|
|
|
$('input.emoji-search').after(ul).after($('<h5>').text('Frequently used'))
|
2015-12-22 16:04:19 -05:00
|
|
|
|
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')
|
2016-04-22 06:25:32 -04:00
|
|
|
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()
|