Commit 4eb16290 authored by Arinde Eniola's avatar Arinde Eniola Committed by Zeger-Jan van de Weg

move frontend logic from previous MR to new MR

parent 3bdc57f0
class @AwardsHandler class @AwardsHandler
constructor: (@get_emojis_url, @post_emoji_url, @noteable_type, @noteable_id, @aliases) -> constructor: ->
$(".js-add-award").on "click", (event) => @aliases = gl.emoji.emojiAliases()
$(document)
.off "click", ".js-add-award"
.on "click", ".js-add-award", (event) =>
event.stopPropagation() event.stopPropagation()
event.preventDefault() event.preventDefault()
@showEmojiMenu() @showEmojiMenu $(event.currentTarget)
$("html").on 'click', (event) -> $("html").on 'click', (event) ->
if !$(event.target).closest(".emoji-menu").length if !$(event.target).closest(".emoji-menu").length
if $(".emoji-menu").is(":visible") if $(".emoji-menu").is(":visible")
$('.js-add-award.is-active').removeClass 'is-active'
$(".emoji-menu").removeClass "is-visible" $(".emoji-menu").removeClass "is-visible"
$(".awards") $(document)
.off "click" .off "click", ".js-emoji-btn"
.on "click", ".js-emoji-btn", @handleClick .on "click", ".js-emoji-btn", (e) => @handleClick(e)
@renderFrequentlyUsedBlock()
handleClick: (e) -> handleClick: (e) ->
e.preventDefault() e.preventDefault()
emoji = $(this) $emojiBtn = $(e.currentTarget)
$addAwardBtn = $('.js-add-award.is-active')
$votesBlock = $($addAwardBtn.closest('.js-award-holder').data('target'))
if $addAwardBtn.length is 0
$votesBlock = $emojiBtn.closest('.js-awards-block')
else if $votesBlock.length is 0
$votesBlock = $addAwardBtn.closest('.js-awards-block')
$votesBlock.addClass 'js-awards-block-current'
awardUrl = $votesBlock.data 'award-url'
emoji = $emojiBtn
.find(".icon") .find(".icon")
.data "emoji" .data "emoji"
if emoji is "thumbsup" and awards_handler.didUserClickEmoji $(this), "thumbsdown" if emoji is "thumbsup" and @didUserClickEmoji $emojiBtn, "thumbsdown"
awards_handler.addAward "thumbsdown" @addAward awardUrl, "thumbsdown"
else if emoji is "thumbsdown" and awards_handler.didUserClickEmoji $(this), "thumbsup" else if emoji is "thumbsdown" and @didUserClickEmoji $emojiBtn, "thumbsup"
awards_handler.addAward "thumbsup" @addAward awardUrl, "thumbsup"
awards_handler.addAward emoji @addAward awardUrl, emoji
didUserClickEmoji: (that, emoji) -> didUserClickEmoji: (emojiBtn, emoji) ->
if $(that).siblings("button:has([data-emoji=#{emoji}])").attr("data-original-title") if emojiBtn.siblings("button:has([data-emoji=#{emoji}])").attr("data-original-title")
$(that).siblings("button:has([data-emoji=#{emoji}])").attr("data-original-title").indexOf('me') > -1 emojiBtn.siblings("button:has([data-emoji=#{emoji}])").attr("data-original-title").indexOf('me') > -1
showEmojiMenu: -> showEmojiMenu: ($addBtn) ->
if $(".emoji-menu").length $menu = $('.emoji-menu')
if $(".emoji-menu").is ".is-visible" if $menu.length
$(".emoji-menu").removeClass "is-visible" $holder = $addBtn.closest('.js-award-holder')
if $menu.is ".is-visible"
$addBtn.removeClass "is-active"
$menu.removeClass "is-visible"
$("#emoji_search").blur() $("#emoji_search").blur()
else else
$(".emoji-menu").addClass "is-visible" $(".emoji-menu").addClass "is-visible"
$addBtn.addClass "is-active"
@positionMenu($menu, $addBtn)
$menu.addClass "is-visible"
$("#emoji_search").focus() $("#emoji_search").focus()
else else
$('.js-add-award').addClass "is-loading" $addBtn.addClass "is-loading is-active"
$.get @get_emojis_url, (response) => $.get $addBtn.data('award-menu-url'), (response) =>
$('.js-add-award').removeClass "is-loading" $addBtn.removeClass "is-loading"
$(".js-award-holder").append response $('body').append response
$menu = $(".emoji-menu")
@positionMenu($menu, $addBtn)
@renderFrequentlyUsedBlock()
setTimeout => setTimeout =>
$(".emoji-menu").addClass "is-visible" $menu.addClass "is-visible"
$("#emoji_search").focus() $("#emoji_search").focus()
@setupSearch() @setupSearch()
, 200 , 200
addAward: (emoji) -> positionMenu: ($menu, $addBtn) ->
position = $addBtn.data('position')
# The menu could potentially be off-screen or in a hidden overflow element
# So we position the element absolute in the body
css =
top: "#{$addBtn.offset().top + $addBtn.outerHeight()}px"
if position? and position is 'right'
css.left = "#{($addBtn.offset().left - $menu.outerWidth()) + 20}px"
$menu.addClass "is-aligned-right"
else
css.left = "#{$addBtn.offset().left}px"
$menu.removeClass "is-aligned-right"
$menu.css(css)
addAward: (awardUrl, emoji) ->
emoji = @normilizeEmojiName(emoji) emoji = @normilizeEmojiName(emoji)
@postEmoji emoji, => @postEmoji awardUrl, emoji, =>
@addAwardToEmojiBar(emoji) @addAwardToEmojiBar(emoji)
$('.js-awards-block').removeClass 'js-awards-block-current'
$(".emoji-menu").removeClass "is-visible" $(".emoji-menu").removeClass "is-visible"
...@@ -65,58 +111,60 @@ class @AwardsHandler ...@@ -65,58 +111,60 @@ class @AwardsHandler
@addEmojiToFrequentlyUsedList(emoji) @addEmojiToFrequentlyUsedList(emoji)
emoji = @normilizeEmojiName(emoji) emoji = @normilizeEmojiName(emoji)
if @exist(emoji) $emojiBtn = @findEmojiIcon(emoji).parent()
if @isActive(emoji)
@decrementCounter(emoji) if $emojiBtn.length > 0
if @isActive($emojiBtn)
@decrementCounter($emojiBtn, emoji)
else else
counter = @findEmojiIcon(emoji).siblings(".js-counter") $counter = $emojiBtn.find('.js-counter')
counter.text(parseInt(counter.text()) + 1) $counter.text(parseInt($counter.text()) + 1)
counter.parent().addClass("active") $emojiBtn.addClass("active")
@addMeToAuthorList(emoji) @addMeToUserList(emoji)
else else
@createEmoji(emoji) @createEmoji(emoji)
exist: (emoji) -> isActive: ($emojiBtn) ->
@findEmojiIcon(emoji).length > 0 $emojiBtn.hasClass("active")
isActive: (emoji) -> decrementCounter: ($emojiBtn, emoji) ->
@findEmojiIcon(emoji).parent().hasClass("active") $awardsBlock = $emojiBtn.closest('.js-awards-block')
isntNoteBody = $emojiBtn.closest('.note-body').length is 0
decrementCounter: (emoji) -> counter = $('.js-counter', $emojiBtn)
counter = @findEmojiIcon(emoji).siblings(".js-counter") counterNumber = parseInt(counter.text())
emojiIcon = counter.parent()
if parseInt(counter.text()) > 1 if counterNumber > 1
counter.text(parseInt(counter.text()) - 1) counter.text(counterNumber - 1)
emojiIcon.removeClass("active") @removeMeFromUserList($emojiBtn, emoji)
@removeMeFromAuthorList(emoji) else if (emoji == "thumbsup" || emoji == "thumbsdown") && isntNoteBody
else if emoji == "thumbsup" || emoji == "thumbsdown" $emojiBtn.tooltip("destroy")
emojiIcon.tooltip("destroy") counter.text('0')
counter.text(0) @removeMeFromUserList($emojiBtn, emoji)
emojiIcon.removeClass("active")
@removeMeFromAuthorList(emoji)
else else
emojiIcon.tooltip("destroy") $emojiBtn.tooltip("destroy")
emojiIcon.remove() $emojiBtn.remove()
removeMeFromAuthorList: (emoji) -> $emojiBtn.removeClass("active")
award_block = @findEmojiIcon(emoji).parent()
removeMeFromUserList: ($emojiBtn, emoji) ->
award_block = $emojiBtn
authors = award_block authors = award_block
.attr("data-original-title") .attr("data-original-title")
.split(", ") .split(", ")
authors.splice(authors.indexOf("me"),1) authors.splice(authors.indexOf("me"), 1)
award_block award_block
.closest(".js-emoji-btn") .closest(".js-emoji-btn")
.attr("data-original-title", authors.join(", ")) .attr("data-original-title", authors.join(", "))
@resetTooltip(award_block) @resetTooltip(award_block)
addMeToAuthorList: (emoji) -> addMeToUserList: (emoji) ->
award_block = @findEmojiIcon(emoji).parent() award_block = @findEmojiIcon(emoji).parent()
origTitle = award_block.attr("data-original-title").trim() origTitle = award_block.attr("data-original-title").trim()
authors = [] users = []
if origTitle if origTitle
authors = origTitle.split(', ') users = origTitle.split(', ')
authors.push("me") users.push("me")
award_block.attr("data-original-title", authors.join(", ")) award_block.attr("data-original-title", users.join(", "))
@resetTooltip(award_block) @resetTooltip(award_block)
resetTooltip: (award) -> resetTooltip: (award) ->
...@@ -127,24 +175,24 @@ class @AwardsHandler ...@@ -127,24 +175,24 @@ class @AwardsHandler
award.tooltip() award.tooltip()
), 200 ), 200
createEmoji: (emoji) -> createEmoji: (emoji) ->
emojiCssClass = @resolveNameToCssClass(emoji) emojiCssClass = @resolveNameToCssClass(emoji)
nodes = [] buttonHtml = "<button class='btn award-control js-emoji-btn has-tooltip active' title='me' data-placement='bottom'>
nodes.push( <div class='icon emoji-icon #{emojiCssClass}' data-emoji='#{emoji}'></div>
"<button class='btn award-control js-emoji-btn has-tooltip active' data-original-title='me'>", <span class='award-control-text js-counter'>1</span>
"<div class='icon emoji-icon #{emojiCssClass}' data-emoji='#{emoji}'></div>", </button>"
"<span class='award-control-text js-counter'>1</span>",
"</button>"
)
emoji_node = $(nodes.join("\n")) emoji_node = $(buttonHtml)
.insertBefore(".js-award-holder") .insertBefore(".js-awards-block-current .js-award-holder:not(.js-award-action-btn)")
.find(".emoji-icon") .find(".emoji-icon")
.data("emoji", emoji) .data("emoji", emoji)
$('.award-control').tooltip() $('.award-control').tooltip()
$currentBlock = $('.js-awards-block-current')
if $currentBlock.is('.hidden')
$currentBlock.removeClass 'hidden'
resolveNameToCssClass: (emoji) -> resolveNameToCssClass: (emoji) ->
emoji_icon = $(".emoji-menu-content [data-emoji='#{emoji}']") emoji_icon = $(".emoji-menu-content [data-emoji='#{emoji}']")
...@@ -156,17 +204,13 @@ class @AwardsHandler ...@@ -156,17 +204,13 @@ class @AwardsHandler
"emoji-#{unicodeName}" "emoji-#{unicodeName}"
postEmoji: (emoji, callback) -> postEmoji: (awardUrl, emoji, callback) ->
$.post @post_emoji_url, { note: { $.post awardUrl, { name: emoji }, (data) ->
note: ":#{emoji}:"
noteable_type: @noteable_type
noteable_id: @noteable_id
}},(data) ->
if data.ok if data.ok
callback.call() callback.call()
findEmojiIcon: (emoji) -> findEmojiIcon: (emoji) ->
$(".awards > .js-emoji-btn [data-emoji='#{emoji}']") $(".js-awards-block-current.awards > .js-emoji-btn [data-emoji='#{emoji}']")
scrollToAwards: -> scrollToAwards: ->
$('body, html').animate({ $('body, html').animate({
...@@ -189,16 +233,15 @@ class @AwardsHandler ...@@ -189,16 +233,15 @@ class @AwardsHandler
if $.cookie('frequently_used_emojis') if $.cookie('frequently_used_emojis')
frequently_used_emojis = @getFrequentlyUsedEmojis() frequently_used_emojis = @getFrequentlyUsedEmojis()
ul = $("<ul>") ul = $("<ul class='clearfix emoji-menu-list'>")
for emoji in frequently_used_emojis for emoji in frequently_used_emojis
do (emoji) ->
$(".emoji-menu-content [data-emoji='#{emoji}']").closest("li").clone().appendTo(ul) $(".emoji-menu-content [data-emoji='#{emoji}']").closest("li").clone().appendTo(ul)
$("input.emoji-search").after(ul).after($("<h5>").text("Frequently used")) $("input.emoji-search").after(ul).after($("<h5>").text("Frequently used"))
setupSearch: -> setupSearch: ->
$("input.emoji-search").keyup (ev) => $("input.emoji-search").on 'keyup', (ev) =>
term = $(ev.target).val() term = $(ev.target).val()
# Clean previous search results # Clean previous search results
......
...@@ -22,6 +22,7 @@ class Dispatcher ...@@ -22,6 +22,7 @@ class Dispatcher
new Issue() new Issue()
shortcut_handler = new ShortcutsIssuable() shortcut_handler = new ShortcutsIssuable()
new ZenMode() new ZenMode()
awards_handler = new AwardsHandler()
when 'projects:milestones:show', 'groups:milestones:show', 'dashboard:milestones:show' when 'projects:milestones:show', 'groups:milestones:show', 'dashboard:milestones:show'
new Milestone() new Milestone()
when 'dashboard:todos:index' when 'dashboard:todos:index'
...@@ -52,6 +53,7 @@ class Dispatcher ...@@ -52,6 +53,7 @@ class Dispatcher
new Diff() new Diff()
shortcut_handler = new ShortcutsIssuable(true) shortcut_handler = new ShortcutsIssuable(true)
new ZenMode() new ZenMode()
awards_handler = new AwardsHandler()
when "projects:merge_requests:diffs" when "projects:merge_requests:diffs"
new Diff() new Diff()
new ZenMode() new ZenMode()
......
((w) ->
w.gl ?= {}
w.gl.emoji ?= {}
w.gl.emoji.emojiAliases = ->
JSON.parse('<%= Gitlab::AwardEmoji.aliases.to_json %>')
) window
\ No newline at end of file
...@@ -150,12 +150,12 @@ class @Notes ...@@ -150,12 +150,12 @@ class @Notes
renderNote: (note) -> renderNote: (note) ->
unless note.valid unless note.valid
if note.award if note.award
flash = new Flash('You have already used this award emoji!', 'alert') flash = new Flash('You have already awarded this emoji, and it we\'ve removed it', 'alert')
flash.pinTo('.header-content') flash.pinTo('.header-content')
return return
if note.award if note.award
awards_handler.addAwardToEmojiBar(note.note) awards_handler.addAwardToEmojiBar(note.name)
awards_handler.scrollToAwards() awards_handler.scrollToAwards()
# render note if it not present in loaded list # render note if it not present in loaded list
......
.awards { .awards {
line-height: 34px;
.emoji-icon { .emoji-icon {
width: 20px; width: 20px;
height: 20px; height: 20px;
...@@ -9,8 +7,6 @@ ...@@ -9,8 +7,6 @@
.emoji-menu { .emoji-menu {
position: absolute; position: absolute;
top: 100%;
left: 0;
margin-top: 3px; margin-top: 3px;
z-index: 1000; z-index: 1000;
min-width: 160px; min-width: 160px;
...@@ -23,7 +19,12 @@ ...@@ -23,7 +19,12 @@
opacity: 0; opacity: 0;
transform: scale(.2); transform: scale(.2);
transform-origin: 0 -45px; transform-origin: 0 -45px;
transition: all .3s cubic-bezier(.87,-.41,.19,1.44); transition: .3s cubic-bezier(.87,-.41,.19,1.44);
transition-property: transform, opacity;
&.is-aligned-right {
transform-origin: 100% -45px;
}
&.is-visible { &.is-visible {
pointer-events: all; pointer-events: all;
...@@ -107,7 +108,7 @@ ...@@ -107,7 +108,7 @@
} }
&.is-loading { &.is-loading {
.award-control-icon { .award-control-icon-normal {
display: none; display: none;
} }
......
...@@ -63,7 +63,8 @@ ul.notes { ...@@ -63,7 +63,8 @@ ul.notes {
&.is-editting { &.is-editting {
.note-header, .note-header,
.note-text, .note-text,
.edited-text { .edited-text,
.note-awards {
display: none; display: none;
} }
...@@ -73,8 +74,6 @@ ul.notes { ...@@ -73,8 +74,6 @@ ul.notes {
} }
.note-body { .note-body {
overflow: auto;
.note-text { .note-text {
overflow: auto; overflow: auto;
word-wrap: break-word; word-wrap: break-word;
...@@ -307,6 +306,42 @@ ul.notes { ...@@ -307,6 +306,42 @@ ul.notes {
} }
} }
.note-award-control {
display: block;
&:hover,
&:focus {
text-decoration: none;
}
.award-control-icon-loading {
display: none;
}
&.is-loading {
.award-control-icon-normal {
display: none;
}
.award-control-icon-loading {
display: block;
}
}
}
.note-awards {
.awards {
padding-top: 10px;
}
.award-control {
padding-top: 2px;
padding-bottom: 2px;
color: #8f8f8f;
font-size: 13px;
}
}
.disabled-comment { .disabled-comment {
margin-left: -$gl-padding-top; margin-left: -$gl-padding-top;
margin-right: -$gl-padding-top; margin-right: -$gl-padding-top;
......
...@@ -12,9 +12,9 @@ class NotesFinder ...@@ -12,9 +12,9 @@ class NotesFinder
when "commit" when "commit"
project.notes.for_commit_id(target_id).not_inline project.notes.for_commit_id(target_id).not_inline
when "issue" when "issue"
project.issues.find(target_id).notes.nonawards.inc_author project.issues.find(target_id).notes.inc_author
when "merge_request" when "merge_request"
project.merge_requests.find(target_id).mr_and_commit_notes.nonawards.inc_author project.merge_requests.find(target_id).mr_and_commit_notes.inc_author
when "snippet", "project_snippet" when "snippet", "project_snippet"
project.snippets.find(target_id).notes project.snippets.find(target_id).notes
else else
......
- grouped_awards = awardable.grouped_awards(inline) - grouped_emojis = awardable.grouped_awards(inline)
.awards.js-awards-block{ class: ("hidden" if !inline && grouped_emojis.size == 0), data: { award_url: url_for([:toggle_award_emoji, @project.namespace.becomes(Namespace), @project, awardable]) } } .awards.js-awards-block{ class: ("hidden" if !inline && grouped_emojis.size == 0), data: { award_url: url_for([:toggle_award_emoji, @project.namespace.becomes(Namespace), @project, awardable]) } }
- awards_sort(grouped_awards).each do |emoji, awards| - awards_sort(grouped_emojis).each do |emoji, awards|
%button.btn.award-control.js-emoji-btn.has-tooltip{ type: "button", class: (award_active_class(awards, current_user)), title: award_user_list(awards, current_user), data: { placement: "bottom" } } %button.btn.award-control.js-emoji-btn.has-tooltip{ type: "button", class: (award_active_class(awards, current_user)),data: { placement: "bottom", original_title: award_user_list(awards, current_user)} }
= emoji_icon(emoji) = emoji_icon(emoji)
%span.award-control-text.js-counter %span.award-control-text.js-counter
= awards.count = awards.count
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment