2015-11-11 08:12:51 -05:00
|
|
|
class @AwardsHandler
|
2016-04-22 12:01:53 -04:00
|
|
|
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) =>
|
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-01-15 14:04:07 -05:00
|
|
|
$("html").on 'click', (event) ->
|
2015-12-17 07:29:55 -05:00
|
|
|
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
|
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)
|
|
|
|
.find(".icon")
|
|
|
|
.data "emoji"
|
2016-04-01 12:27:28 -04:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2016-03-01 06:25:59 -05:00
|
|
|
awards_handler.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) ->
|
|
|
|
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-02-18 23:02:22 -05:00
|
|
|
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()
|
2016-02-18 23:02:22 -05:00
|
|
|
else
|
2016-03-01 06:25:59 -05:00
|
|
|
$('.js-add-award').addClass "is-loading"
|
2016-04-01 10:20:29 -04:00
|
|
|
$.get @get_emojis_url, (response) =>
|
2016-03-01 06:25:59 -05:00
|
|
|
$('.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
|
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-03-11 04:47:18 -05: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-03-01 06:25:59 -05:00
|
|
|
counter = @findEmojiIcon(emoji).siblings(".js-counter")
|
2015-11-17 09:44:58 -05:00
|
|
|
counter.text(parseInt(counter.text()) + 1)
|
|
|
|
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) ->
|
2015-11-17 09:44:58 -05:00
|
|
|
@findEmojiIcon(emoji).parent().hasClass("active")
|
2015-11-11 08:12:51 -05:00
|
|
|
|
|
|
|
decrementCounter: (emoji) ->
|
2016-03-01 06:25:59 -05: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)
|
2015-12-24 12:46:35 -05:00
|
|
|
emojiIcon.removeClass("active")
|
2015-11-18 08:43:53 -05:00
|
|
|
@removeMeFromAuthorList(emoji)
|
2016-02-12 20:41:52 -05:00
|
|
|
else if emoji == "thumbsup" || emoji == "thumbsdown"
|
2015-12-24 12:46:35 -05:00
|
|
|
emojiIcon.tooltip("destroy")
|
|
|
|
counter.text(0)
|
|
|
|
emojiIcon.removeClass("active")
|
2016-02-12 20:41:52 -05:00
|
|
|
@removeMeFromAuthorList(emoji)
|
2015-11-11 08:12:51 -05:00
|
|
|
else
|
2015-12-24 12:46:35 -05:00
|
|
|
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(", ")
|
2016-01-18 19:27:59 -05:00
|
|
|
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()
|
2016-01-18 15:40:48 -05:00
|
|
|
origTitle = award_block.attr("data-original-title").trim()
|
|
|
|
authors = []
|
|
|
|
if origTitle
|
|
|
|
authors = origTitle.split(', ')
|
2016-01-18 19:27:59 -05:00
|
|
|
authors.push("me")
|
2016-04-01 12:27:28 -04:00
|
|
|
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
|
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>"
|
|
|
|
)
|
|
|
|
|
|
|
|
emoji_node = $(nodes.join("\n"))
|
|
|
|
.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-22 12:01:53 -04:00
|
|
|
"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
|
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) ->
|
|
|
|
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: ->
|
2015-12-28 03:11:01 -05:00
|
|
|
if $.cookie('frequently_used_emojis')
|
|
|
|
frequently_used_emojis = @getFrequentlyUsedEmojis()
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2015-12-28 03:11:01 -05:00
|
|
|
ul = $("<ul>")
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2015-12-28 03:11:01 -05:00
|
|
|
for emoji in frequently_used_emojis
|
|
|
|
do (emoji) ->
|
|
|
|
$(".emoji-menu-content [data-emoji='#{emoji}']").closest("li").clone().appendTo(ul)
|
2015-12-22 16:04:19 -05:00
|
|
|
|
2015-12-28 03:11:01 -05: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: ->
|
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()
|