awards_handler.coffee 4.91 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")
Valery Sizov's avatar
Valery Sizov committed
46 47 48 49

    if parseInt(counter.text()) > 1
      counter.text(parseInt(counter.text()) - 1)
      counter.parent().removeClass("active")
Valery Sizov's avatar
Valery Sizov committed
50
      @removeMeFromAuthorList(emoji)
Valery Sizov's avatar
Valery Sizov committed
51
    else
Valery Sizov's avatar
Valery Sizov committed
52 53 54
      award = counter.parent()
      award.tooltip("destroy")
      award.remove()
Valery Sizov's avatar
Valery Sizov committed
55

Valery Sizov's avatar
Valery Sizov committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  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
73
    # "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
74 75 76 77
    setTimeout (->
      award.tooltip()
    ), 200
    
Valery Sizov's avatar
Valery Sizov committed
78

79 80 81
  createEmoji: (emoji) ->
    emojiCssClass = @resolveNameToCssClass(emoji)

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

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

Valery Sizov's avatar
Valery Sizov committed
90 91
    $(".award").tooltip()

92
  resolveNameToCssClass: (emoji) ->
93
    emoji_icon = $(".emoji-menu-content [data-emoji='#{emoji}']")
94

95 96 97 98 99 100 101
    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
102 103

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

  findEmojiIcon: (emoji) ->
113
    $(".award [data-emoji='#{emoji}']")
114 115 116 117 118

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

  normilizeEmojiName: (emoji) ->
    @aliases[emoji] || emoji
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

  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)

      $(".emoji-menu-content").prepend(ul).prepend($("<h4>").text("Frequently used"))

Valery Sizov's avatar
Valery Sizov committed
144
  setupSearch: ->
Valery Sizov's avatar
Valery Sizov committed
145
    $("input.emoji-search").keyup (ev) =>
Valery Sizov's avatar
Valery Sizov committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      term = $(ev.target).val()

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

      if term
        # Generate search result block
        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
162
    $(".emoji-menu-content [data-emoji*='#{term}']").closest("li").clone()