Commit 5012bcde authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Cleanup backend code

Removed EE::Banzai::Filter::LabelReferenceFilter and
handled all the EE-specific logic in the EE overrides
of LabelHelper
parent 8fed318a
...@@ -42,52 +42,36 @@ module LabelsHelper ...@@ -42,52 +42,36 @@ module LabelsHelper
if block_given? if block_given?
link_to link, class: 'gl-link gl-label-link', &block link_to link, class: 'gl-link gl-label-link', &block
else else
render_label(label, tooltip: tooltip, link: link, small: small) render_label(label, link: link, tooltip: tooltip, small: small)
end end
end end
def render_label(label, tooltip: true, link: nil, dataset: nil, small: false, wrapper_class: nil, wrapper_style: nil, extra: nil) def render_label(label, link: nil, tooltip: true, dataset: nil, small: false)
# if scoped label is used then EE wraps label tag with scoped label
# doc link
html = render_colored_label(label) html = render_colored_label(label)
if link if link
html = link_to( title = label_tooltip_title(label) if tooltip
html, html = render_label_link(html, link: link, title: title, dataset: dataset)
link,
class: label_css_classes(tooltip),
data: label_dataset(label, dataset, tooltip)
)
end end
html += extra.html_safe if extra wrap_label_html(html, small: small, label: label)
wrapper_classes = Array.wrap(wrapper_class)
wrapper_classes << 'gl-label'
wrapper_classes << 'gl-label-sm' if small
content_tag(:span, html.html_safe, class: wrapper_classes.join(' '), style: wrapper_style)
end end
def render_colored_label(label, label_suffix: '') def render_colored_label(label, suffix: '')
render_partial_label( render_label_text(
label, label.name,
label_suffix: label_suffix, suffix: suffix,
label_name: label.name,
css_class: text_color_class_for_bg(label.color), css_class: text_color_class_for_bg(label.color),
bg_color: label.color bg_color: label.color
) )
end end
def render_partial_label(label, label_suffix: '', label_name: nil, css_class: nil, bg_color: nil) # We need the `label` argument here for EE
# Intentionally not using content_tag here so that this method can be called def wrap_label_html(label_html, small:, label:)
# by LabelReferenceFilter wrapper_classes = %w(gl-label)
span = %(<span class="gl-label-text #{css_class}" ) + wrapper_classes << 'gl-label-sm' if small
%(data-html="true" #{"style=\"background-color: #{bg_color}\"" if bg_color} ) +
%(data-container="body">) +
%(#{ERB::Util.html_escape_once(label_name)}#{label_suffix}</span>)
span.html_safe %(<span class="#{wrapper_classes.join(' ')}">#{label_html}</span>).html_safe
end end
def label_tooltip_title(label) def label_tooltip_title(label)
...@@ -284,14 +268,27 @@ module LabelsHelper ...@@ -284,14 +268,27 @@ module LabelsHelper
private private
def label_dataset(label, dataset, tooltip) def render_label_link(label_html, link:, title:, dataset:)
classes = %w(gl-link gl-label-link)
dataset ||= {} dataset ||= {}
dataset.merge!(html: true, title: label_tooltip_title(label)) if tooltip
if title.present?
classes << 'has-tooltip'
dataset.merge!(html: true, title: title)
end
link_to(label_html, link, class: classes.join(' '), data: dataset)
end end
def label_css_classes(tooltip) def render_label_text(name, suffix: '', css_class: nil, bg_color: nil)
css = 'gl-link gl-label-link' <<~HTML.chomp.html_safe
tooltip ? "#{css} has-tooltip" : css <span
class="gl-label-text #{css_class}"
data-container="body"
data-html="true"
#{"style=\"background-color: #{bg_color}\"" if bg_color}
>#{ERB::Util.html_escape_once(name)}#{suffix}</span>
HTML
end end
end end
......
...@@ -8,44 +8,32 @@ module EE ...@@ -8,44 +8,32 @@ module EE
singleton_class.prepend self singleton_class.prepend self
end end
def render_label(label, tooltip: true, link: nil, dataset: nil, small: false) def render_colored_label(label, suffix: '')
return super unless label.scoped_label? return super unless label.scoped_label?
scoped_label_wrapper(
super(
label,
tooltip: tooltip,
link: link,
dataset: dataset,
small: small,
wrapper_class: 'gl-label-scoped',
wrapper_style: "color: #{label.color}",
extra: scoped_labels_doc_link(label)
),
label
)
end
def render_colored_label(label, label_suffix: '')
return super unless label.scoped_label?
text_color_class = text_color_class_for_bg(label.color)
scope_name, label_name = label.name.split(Label::SCOPED_LABEL_SEPARATOR) scope_name, label_name = label.name.split(Label::SCOPED_LABEL_SEPARATOR)
# Tooltip is omitted as it's attached to the link containing label title on scoped labels render_label_text(
render_partial_label( scope_name,
label, label_suffix: label_suffix, css_class: text_color_class_for_bg(label.color),
label_name: scope_name,
css_class: text_color_class,
bg_color: label.color bg_color: label.color
) + render_partial_label( ) + render_label_text(
label, label_suffix: label_suffix, label_name,
label_name: label_name suffix: suffix
) )
end end
def scoped_label_wrapper(link, label) def wrap_label_html(label_html, small:, label:)
%(<span class="d-inline-block position-relative scoped-label-wrapper">#{link}</span>).html_safe return super unless label.scoped_label?
wrapper_classes = %w(gl-label gl-label-scoped)
wrapper_classes << 'gl-label-sm' if small
<<~HTML.chomp.html_safe
<span class="d-inline-block position-relative scoped-label-wrapper">
<span class="#{wrapper_classes.join(' ')}" style="color: #{label.color}">#{label_html + scoped_labels_doc_link}</span>
</span>
HTML
end end
def label_tooltip_title(label) def label_tooltip_title(label)
...@@ -84,11 +72,12 @@ module EE ...@@ -84,11 +72,12 @@ module EE
super + ['epics'] super + ['epics']
end end
def scoped_labels_doc_link(label) private
content = %(<i class="fa fa-question-circle"></i>)
help_url = ::Gitlab::Routing.url_helpers.help_page_url('user/project/labels.md', anchor: 'scoped-labels') def scoped_labels_doc_link
help_url = ::Gitlab::Routing.url_helpers.help_page_path('user/project/labels.md', anchor: 'scoped-labels')
%(<a href="#{help_url}" class="gl-link gl-label-icon" target="_blank" rel="noopener">#{content}</a>) %(<a href="#{help_url}" class="gl-link gl-label-icon" target="_blank" rel="noopener"><i class="fa fa-question-circle"></i></a>).html_safe
end end
end end
end end
# frozen_string_literal: true
module EE
module Banzai
module Filter
module LabelReferenceFilter
extend ::Gitlab::Utils::Override
override :wrap_link
def wrap_link(link, label)
content = super
parent = project || group
if label.scoped_label? && parent && parent.feature_available?(:scoped_labels)
presenter = label.present(issuable_parent: parent)
doc_link = ::LabelsHelper.scoped_labels_doc_link(label)
content = %(<span class="gl-label gl-label-scoped gl-label-sm" style="color: #{label.color}">#{link}#{doc_link}</span>)
content = ::LabelsHelper.scoped_label_wrapper(content, presenter)
end
content
end
def tooltip_title(label)
::LabelsHelper.label_tooltip_title(label)
end
end
end
end
end
...@@ -263,8 +263,8 @@ describe 'Issue Boards', :js do ...@@ -263,8 +263,8 @@ describe 'Issue Boards', :js do
end end
it 'removes existing scoped label' do it 'removes existing scoped label' do
scoped1 = scoped_label_1.title.split('::') scoped1 = scoped_label_1.title.split(Label::SCOPED_LABEL_SEPARATOR)
scoped2 = scoped_label_2.title.split('::') scoped2 = scoped_label_2.title.split(Label::SCOPED_LABEL_SEPARATOR)
click_card(card1) click_card(card1)
......
...@@ -14,12 +14,13 @@ describe LabelsHelper do ...@@ -14,12 +14,13 @@ describe LabelsHelper do
end end
it 'includes link to scoped labels documentation' do it 'includes link to scoped labels documentation' do
scoped = scoped_label.title.split('::') scope, name = scoped_label.title.split(Label::SCOPED_LABEL_SEPARATOR)
expect(render_label(scoped_label)).to match(%r(<span.+><span.+><span.+>#{scoped.first}</span><span.+>#{scoped.last}</span><a.+>.*question-circle.*</a></span></span>$))
expect(render_label(scoped_label)).to match(%r(<span.+>#{scope}</span><span.+>#{name}</span><a.+>.*question-circle.*</a>)m)
end end
it 'does not include link to scoped label documentation for common labels' do it 'does not include link to scoped label documentation for common labels' do
expect(render_label(label)).to match(%r(<span.+><span.+>#{label.name}</span></span>$)) expect(render_label(label)).to match(%r(<span.+><span.+>#{label.name}</span></span>$)m)
end end
end end
...@@ -29,7 +30,7 @@ describe LabelsHelper do ...@@ -29,7 +30,7 @@ describe LabelsHelper do
end end
it 'does not include link to scoped documentation' do it 'does not include link to scoped documentation' do
expect(render_label(scoped_label)).to match(%r(<span.+><span.+>#{scoped_label.name}</span></span>$)) expect(render_label(scoped_label)).to match(%r(<span.+><span.+>#{scoped_label.name}</span></span>$)m)
end end
end end
end end
......
...@@ -16,9 +16,9 @@ describe Banzai::Filter::LabelReferenceFilter do ...@@ -16,9 +16,9 @@ describe Banzai::Filter::LabelReferenceFilter do
it 'includes link to scoped documentation' do it 'includes link to scoped documentation' do
doc = reference_filter("See #{scoped_label.to_reference}") doc = reference_filter("See #{scoped_label.to_reference}")
scoped = scoped_label.name.split('::') scope, name = scoped_label.name.split(Label::SCOPED_LABEL_SEPARATOR)
expect(doc.to_html).to match(%r(<span.+><a.+><span.+>#{scoped.first}</span><span.+>#{scoped.last}</span></a><a.+>.*question-circle.*</a></span>)) expect(doc.to_html).to match(%r(<span.+><a.+><span.+>#{scope}</span><span.+>#{name}</span></a><a.+>.*question-circle.*</a></span>))
end end
it 'does not include link to scoped documentation for common labels' do it 'does not include link to scoped documentation for common labels' do
......
...@@ -93,14 +93,12 @@ module Banzai ...@@ -93,14 +93,12 @@ module Banzai
end end
presenter = object.present(issuable_subject: parent) presenter = object.present(issuable_subject: parent)
LabelsHelper.render_colored_label(presenter, label_suffix: label_suffix) LabelsHelper.render_colored_label(presenter, suffix: label_suffix)
end end
def wrap_link(link, label) def wrap_link(link, label)
content = super presenter = label.present(issuable_subject: project || group)
content = %(<span class="gl-label gl-label-sm">#{content}</span>) LabelsHelper.wrap_label_html(link, small: true, label: presenter)
content
end end
def full_path_ref?(matches) def full_path_ref?(matches)
...@@ -112,10 +110,9 @@ module Banzai ...@@ -112,10 +110,9 @@ module Banzai
end end
def object_link_title(object, matches) def object_link_title(object, matches)
LabelsHelper.label_tooltip_title(object) presenter = object.present(issuable_subject: project || group)
LabelsHelper.label_tooltip_title(presenter)
end end
end end
end end
end end
Banzai::Filter::LabelReferenceFilter.prepend_if_ee('EE::Banzai::Filter::LabelReferenceFilter')
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Gitlab module Gitlab
module MarkdownCache module MarkdownCache
# Increment this number every time the renderer changes its output # Increment this number every time the renderer changes its output
CACHE_COMMONMARK_VERSION = 18 CACHE_COMMONMARK_VERSION = 19
CACHE_COMMONMARK_VERSION_START = 10 CACHE_COMMONMARK_VERSION_START = 10
BaseError = Class.new(StandardError) BaseError = Class.new(StandardError)
......
...@@ -56,7 +56,7 @@ describe LabelsHelper do ...@@ -56,7 +56,7 @@ describe LabelsHelper do
context 'without subject' do context 'without subject' do
it "uses the label's project" do it "uses the label's project" do
expect(link_to_label(label_presenter)).to match %r{<a.*href="/#{label.project.full_path}/issues\?label_name%5B%5D=#{label.name}".*>.*</a>} expect(link_to_label(label_presenter)).to match %r{<a.*href="/#{label.project.full_path}/issues\?label_name%5B%5D=#{label.name}".*>.*</a>}m
end end
end end
...@@ -65,7 +65,7 @@ describe LabelsHelper do ...@@ -65,7 +65,7 @@ describe LabelsHelper do
let(:subject) { build(:project, namespace: namespace, name: 'bar3') } let(:subject) { build(:project, namespace: namespace, name: 'bar3') }
it 'links to project issues page' do it 'links to project issues page' do
expect(link_to_label(label_presenter)).to match %r{<a.*href="/foo3/bar3/issues\?label_name%5B%5D=#{label.name}".*>.*</a>} expect(link_to_label(label_presenter)).to match %r{<a.*href="/foo3/bar3/issues\?label_name%5B%5D=#{label.name}".*>.*</a>}m
end end
end end
...@@ -73,7 +73,7 @@ describe LabelsHelper do ...@@ -73,7 +73,7 @@ describe LabelsHelper do
let(:subject) { build(:group, name: 'bar') } let(:subject) { build(:group, name: 'bar') }
it 'links to group issues page' do it 'links to group issues page' do
expect(link_to_label(label_presenter)).to match %r{<a.*href="/groups/bar/-/issues\?label_name%5B%5D=#{label.name}".*>.*</a>} expect(link_to_label(label_presenter)).to match %r{<a.*href="/groups/bar/-/issues\?label_name%5B%5D=#{label.name}".*>.*</a>}m
end end
end end
...@@ -81,7 +81,7 @@ describe LabelsHelper do ...@@ -81,7 +81,7 @@ describe LabelsHelper do
['issue', :issue].each do |type| ['issue', :issue].each do |type|
context "set to #{type}" do context "set to #{type}" do
it 'links to correct page' do it 'links to correct page' do
expect(link_to_label(label_presenter, type: type)).to match %r{<a.*href="/#{label.project.full_path}/#{type.to_s.pluralize}\?label_name%5B%5D=#{label.name}".*>.*</a>} expect(link_to_label(label_presenter, type: type)).to match %r{<a.*href="/#{label.project.full_path}/#{type.to_s.pluralize}\?label_name%5B%5D=#{label.name}".*>.*</a>}m
end end
end end
end end
...@@ -89,7 +89,7 @@ describe LabelsHelper do ...@@ -89,7 +89,7 @@ describe LabelsHelper do
['merge_request', :merge_request].each do |type| ['merge_request', :merge_request].each do |type|
context "set to #{type}" do context "set to #{type}" do
it 'links to correct page' do it 'links to correct page' do
expect(link_to_label(label_presenter, type: type)).to match %r{<a.*href="/#{label.project.full_path}/-/#{type.to_s.pluralize}\?label_name%5B%5D=#{label.name}".*>.*</a>} expect(link_to_label(label_presenter, type: type)).to match %r{<a.*href="/#{label.project.full_path}/-/#{type.to_s.pluralize}\?label_name%5B%5D=#{label.name}".*>.*</a>}m
end end
end end
end end
......
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