awards_handler.coffee 5.05 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
class @AwardsHandler
Valery Sizov's avatar
Valery Sizov committed
2
  constructor: (@post_emoji_url, @noteable_type, @noteable_id, @aliases) ->
3 4 5 6 7 8 9 10 11
    $(".add-award").click (event)->
      event.stopPropagation()
      event.preventDefault()
      $(".emoji-menu").show()

    $("html").click ->
      if !$(event.target).closest(".emoji-menu").length
        if $(".emoji-menu").is(":visible")
          $(".emoji-menu").hide()
Valery Sizov's avatar
Valery Sizov committed
12

13
    @renderFrequentlyUsedBlock()
Valery Sizov's avatar
Valery Sizov committed
14
    @setupSearch()
15

Valery Sizov's avatar
Valery Sizov committed
16
  addAward: (emoji) ->
Valery Sizov's avatar
Valery Sizov committed
17
    emoji = @normilizeEmojiName(emoji)
Valery Sizov's avatar
Valery Sizov committed
18
    @postEmoji emoji, =>
19
      @addAwardToEmojiBar(emoji)
20 21

    $(".emoji-menu").hide()
Valery Sizov's avatar
Valery Sizov committed
22
    
23
  addAwardToEmojiBar: (emoji) ->
24 25
    @addEmojiToFrequentlyUsedList(emoji)

Valery Sizov's avatar
Valery Sizov committed
26
    emoji = @normilizeEmojiName(emoji)
27 28 29 30 31 32 33
    if @exist(emoji)
      if @isActive(emoji)
        @decrementCounter(emoji)
      else
        counter = @findEmojiIcon(emoji).siblings(".counter")
        counter.text(parseInt(counter.text()) + 1)
        counter.parent().addClass("active")
Valery Sizov's avatar
Valery Sizov committed
34
        @addMeToAuthorList(emoji)
35
    else
36
      @createEmoji(emoji)
Valery Sizov's avatar
Valery Sizov committed
37 38

  exist: (emoji) ->
39
    @findEmojiIcon(emoji).length > 0
Valery Sizov's avatar
Valery Sizov committed
40 41

  isActive: (emoji) ->
42
    @findEmojiIcon(emoji).parent().hasClass("active")
Valery Sizov's avatar
Valery Sizov committed
43 44

  decrementCounter: (emoji) ->
45
    counter = @findEmojiIcon(emoji).siblings(".counter")
46
    emojiIcon = counter.parent()
Valery Sizov's avatar
Valery Sizov committed
47 48 49

    if parseInt(counter.text()) > 1
      counter.text(parseInt(counter.text()) - 1)
50
      emojiIcon.removeClass("active")
Valery Sizov's avatar
Valery Sizov committed
51
      @removeMeFromAuthorList(emoji)
52 53 54 55
    else if emoji =="thumbsup" || emoji == "thumbsdown"
      emojiIcon.tooltip("destroy")
      counter.text(0)
      emojiIcon.removeClass("active")
Valery Sizov's avatar
Valery Sizov committed
56
    else
57 58
      emojiIcon.tooltip("destroy")
      emojiIcon.remove()
Valery Sizov's avatar
Valery Sizov committed
59

Valery Sizov's avatar
Valery Sizov committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  removeMeFromAuthorList: (emoji) ->
    award_block = @findEmojiIcon(emoji).parent()
    authors = award_block.attr("data-original-title").split(", ")
    authors = _.without(authors, "me").join(", ")
    award_block.attr("title", authors)
    @resetTooltip(award_block)

  addMeToAuthorList: (emoji) ->
    award_block = @findEmojiIcon(emoji).parent()
    authors = award_block.attr("data-original-title").split(", ")
    authors.push("me")
    award_block.attr("title", authors.join(", "))
    @resetTooltip(award_block)

  resetTooltip: (award) ->
    award.tooltip("destroy")

Valery Sizov's avatar
Valery Sizov committed
77
    # "destroy" call is asynchronous and there is no appropriate callback on it, this is why we need to set timeout.
Valery Sizov's avatar
Valery Sizov committed
78 79 80 81
    setTimeout (->
      award.tooltip()
    ), 200
    
Valery Sizov's avatar
Valery Sizov committed
82

83 84 85
  createEmoji: (emoji) ->
    emojiCssClass = @resolveNameToCssClass(emoji)

Valery Sizov's avatar
Valery Sizov committed
86
    nodes = []
Valery Sizov's avatar
Valery Sizov committed
87
    nodes.push("<div class='award active' title='me'>")
88
    nodes.push("<div class='icon emoji-icon #{emojiCssClass}' data-emoji='#{emoji}'></div>")
89
    nodes.push("<div class='counter'>1</div>")
Valery Sizov's avatar
Valery Sizov committed
90 91
    nodes.push("</div>")

92
    emoji_node = $(nodes.join("\n")).insertBefore(".awards-controls").find(".emoji-icon").data("emoji", emoji)
Valery Sizov's avatar
Valery Sizov committed
93

Valery Sizov's avatar
Valery Sizov committed
94 95
    $(".award").tooltip()

96
  resolveNameToCssClass: (emoji) ->
97
    emoji_icon = $(".emoji-menu-content [data-emoji='#{emoji}']")
98

99 100 101 102 103 104 105
    if emoji_icon.length > 0
      unicodeName = emoji_icon.data("unicode-name")
    else
      # Find by alias
      unicodeName = $(".emoji-menu-content [data-aliases*=':#{emoji}:']").data("unicode-name")

    "emoji-#{unicodeName}"
Valery Sizov's avatar
Valery Sizov committed
106 107

  postEmoji: (emoji, callback) ->
Valery Sizov's avatar
Valery Sizov committed
108
    $.post @post_emoji_url, { note: {
109
      note: ":#{emoji}:"
Valery Sizov's avatar
Valery Sizov committed
110 111
      noteable_type: @noteable_type
      noteable_id: @noteable_id
Valery Sizov's avatar
Valery Sizov committed
112
    }},(data) ->
Valery Sizov's avatar
Valery Sizov committed
113
      if data.ok
114 115 116
        callback.call()

  findEmojiIcon: (emoji) ->
117
    $(".award [data-emoji='#{emoji}']")
118 119 120 121 122

  scrollToAwards: ->
    $('body, html').animate({
      scrollTop: $('.awards').offset().top - 80
    }, 200)
Valery Sizov's avatar
Valery Sizov committed
123 124 125

  normilizeEmojiName: (emoji) ->
    @aliases[emoji] || emoji
126 127 128 129 130 131 132 133 134 135 136

  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: ->
137 138
    if $.cookie('frequently_used_emojis')
      frequently_used_emojis = @getFrequentlyUsedEmojis()
139

140
      ul = $("<ul>")
141

142 143 144
      for emoji in frequently_used_emojis
        do (emoji) ->
          $(".emoji-menu-content [data-emoji='#{emoji}']").closest("li").clone().appendTo(ul)
145

146
      $("input.emoji-search").after(ul).after($("<h5>").text("Frequently used"))
147

Valery Sizov's avatar
Valery Sizov committed
148
  setupSearch: ->
Valery Sizov's avatar
Valery Sizov committed
149
    $("input.emoji-search").keyup (ev) =>
Valery Sizov's avatar
Valery Sizov committed
150 151 152 153 154 155
      term = $(ev.target).val()

      # Clean previous search results
      $("ul.emoji-search,h5.emoji-search").remove()

      if term
156
        # Generate a search result block
Valery Sizov's avatar
Valery Sizov committed
157 158 159 160 161 162 163 164 165
        h5 = $("<h5>").text("Search results").addClass("emoji-search")
        found_emojis = @searchEmojis(term).show()
        ul = $("<ul>").addClass("emoji-search").append(found_emojis)
        $(".emoji-menu-content ul, .emoji-menu-content h5").hide()
        $(".emoji-menu-content").append(h5).append(ul)
      else
        $(".emoji-menu-content").children().show()

  searchEmojis: (term)->
Valery Sizov's avatar
Valery Sizov committed
166
    $(".emoji-menu-content [data-emoji*='#{term}']").closest("li").clone()