Commit a7edaa33 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Cleanup methods and simplify overrides

Reduce the number of methods to reduce the levels
of indirection. Also, made the CE methods easier
to extend so the EE overrides are simpler.
parent d3d6d043
...@@ -46,7 +46,7 @@ module LabelsHelper ...@@ -46,7 +46,7 @@ module LabelsHelper
end end
end end
def render_label(label, tooltip: true, link: nil, dataset: nil, small: false) def render_label(label, tooltip: true, link: nil, dataset: nil, small: false, wrapper_class: nil, wrapper_style: nil)
# if scoped label is used then EE wraps label tag with scoped label # if scoped label is used then EE wraps label tag with scoped label
# doc link # doc link
html = render_colored_label(label) html = render_colored_label(label)
...@@ -60,10 +60,11 @@ module LabelsHelper ...@@ -60,10 +60,11 @@ module LabelsHelper
) )
end end
wrapper_class = "gl-label" wrapper_classes = Array.wrap(wrapper_class)
wrapper_class += " gl-label-sm" if small wrapper_classes << 'gl-label'
wrapper_classes << 'gl-label-sm' if small
content_tag(:span, html.html_safe, class: wrapper_class) 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, label_suffix: '')
...@@ -290,9 +291,6 @@ module LabelsHelper ...@@ -290,9 +291,6 @@ module LabelsHelper
css = 'gl-link gl-label-link' css = 'gl-link gl-label-link'
tooltip ? "#{css} has-tooltip" : css tooltip ? "#{css} has-tooltip" : css
end end
# Required for Banzai::Filter::LabelReferenceFilter
module_function :render_colored_label, :text_color_for_bg, :escape_once, :label_tooltip_title
end end
LabelsHelper.prepend_if_ee('EE::LabelsHelper') LabelsHelper.prepend_if_ee('EE::LabelsHelper')
...@@ -9,62 +9,45 @@ module EE ...@@ -9,62 +9,45 @@ module EE
end end
def render_label(label, tooltip: true, link: nil, dataset: nil, small: false) def render_label(label, tooltip: true, link: nil, dataset: nil, small: false)
if label.scoped_label? return super unless label.scoped_label?
render_scoped_label(
scoped_label_doc_wrapper(
super(
label, label,
tooltip: tooltip,
link: link, link: link,
css: label_css_classes(tooltip), dataset: dataset,
dataset: label_dataset(label, dataset, tooltip), small: small,
small: small wrapper_class: 'gl-label-scoped',
) wrapper_style: "color: #{label.color}"
else ),
super label
end )
end end
def render_scoped_label(label, link: nil, css: nil, dataset: nil, small: false) def render_colored_label(label, label_suffix: '')
# if scoped label is used then EE wraps label tag with scoped label return super unless label.scoped_label?
# doc link
size_class = small ? "gl-label-sm" : ""
html = render_colored_scoped_label(label)
html = link_to(html, link, class: css, data: dataset) if link
html = %(<span class="gl-label gl-label-scoped #{size_class}" style="color: #{label.color}">#{html}</span>)
wrapped_html = scoped_label_wrapper(html, label)
wrapped_html.html_safe
end
def render_colored_scoped_label(label, label_suffix: '') text_color_class = text_color_class_for_bg(label.color)
text_color_class = ::LabelsHelper.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)
# Intentionally not using content_tag here so that this method can be called
# by LabelReferenceFilter
# Tooltip is omitted as it's attached to the link containing label title on scoped labels # Tooltip is omitted as it's attached to the link containing label title on scoped labels
::LabelsHelper.render_partial_label( render_partial_label(
label, label_suffix: label_suffix, label, label_suffix: label_suffix,
label_name: scope_name, label_name: scope_name,
css_class: text_color_class, css_class: text_color_class,
bg_color: label.color bg_color: label.color
) + ::LabelsHelper.render_partial_label( ) + render_partial_label(
label, label_suffix: label_suffix, label, label_suffix: label_suffix,
label_name: label_name, label_name: label_name,
css_class: 'gl-label-scoped-text' css_class: 'gl-label-scoped-text'
) )
end end
def scoped_label_wrapper(link, label) def scoped_label_doc_wrapper(link, label)
%(<span class="d-inline-block position-relative scoped-label-wrapper">#{link}#{scoped_labels_doc_link(label)}</span>).html_safe %(<span class="d-inline-block position-relative scoped-label-wrapper">#{link}#{scoped_labels_doc_link(label)}</span>).html_safe
end end
def scoped_labels_doc_link(label)
content = %(<i class="fa fa-question-circle"></i>)
help_url = ::Gitlab::Routing.url_helpers.help_page_url('user/project/labels.md', anchor: 'scoped-labels')
%(<a href="#{help_url}" class="label scoped-label gl-link gl-label-icon" target="_blank" rel="noopener">#{content}</a>)
end
def label_tooltip_title(label) def label_tooltip_title(label)
tooltip = super tooltip = super
tooltip = %(<span class='font-weight-bold scoped-label-tooltip-title'>Scoped label</span><br />#{tooltip}) if label.scoped_label? tooltip = %(<span class='font-weight-bold scoped-label-tooltip-title'>Scoped label</span><br />#{tooltip}) if label.scoped_label?
...@@ -101,14 +84,13 @@ module EE ...@@ -101,14 +84,13 @@ module EE
super + ['epics'] super + ['epics']
end end
private
def scoped_labels_doc_link(label) def scoped_labels_doc_link(label)
text_color = text_color_for_bg(label.color) content = %(<i class="fa fa-question-circle"></i>)
content = %(<i class="fa fa-question-circle" style="background-color: #{label.color}; color: #{text_color}"></i>)
help_url = ::Gitlab::Routing.url_helpers.help_page_url('user/project/labels.md', anchor: 'scoped-labels') help_url = ::Gitlab::Routing.url_helpers.help_page_url('user/project/labels.md', anchor: 'scoped-labels')
%(<a href="#{help_url}" class="label scoped-label" target="_blank" rel="noopener">#{content}</a>) %(<a href="#{help_url}" class="label scoped-label gl-link gl-label-icon" target="_blank" rel="noopener">#{content}</a>)
end end
module_function :render_colored_scoped_label, :scoped_label_wrapper, :scoped_labels_doc_link, :label_tooltip_title
end end
end end
...@@ -6,20 +6,6 @@ module EE ...@@ -6,20 +6,6 @@ module EE
module LabelReferenceFilter module LabelReferenceFilter
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
override :object_link_text
def object_link_text(object, matches)
presenter = label_link_text(object, matches)
label_suffix = label_link_suffix(object, matches)
content = if object.scoped_label?
::EE::LabelsHelper.render_colored_scoped_label(presenter, label_suffix: label_suffix)
else
::LabelsHelper.render_colored_label(presenter, label_suffix: label_suffix)
end
content
end
override :wrap_link override :wrap_link
def wrap_link(link, label) def wrap_link(link, label)
content = super content = super
...@@ -27,8 +13,7 @@ module EE ...@@ -27,8 +13,7 @@ module EE
if label.scoped_label? && parent && parent.feature_available?(:scoped_labels) if label.scoped_label? && parent && parent.feature_available?(:scoped_labels)
presenter = label.present(issuable_parent: parent) presenter = label.present(issuable_parent: parent)
content = %(<span class="gl-label gl-label-scoped gl-label-sm" style="color: #{label.color}">#{link}</span>) content = ::LabelsHelper.scoped_label_wrapper(content, presenter)
content = ::EE::LabelsHelper.scoped_label_wrapper(content, presenter)
end end
content content
...@@ -37,10 +22,6 @@ module EE ...@@ -37,10 +22,6 @@ module EE
def tooltip_title(label) def tooltip_title(label)
::LabelsHelper.label_tooltip_title(label) ::LabelsHelper.label_tooltip_title(label)
end end
def object_link_title(object, matches)
::EE::LabelsHelper.label_tooltip_title(object)
end
end end
end end
end end
......
...@@ -33,11 +33,10 @@ describe Banzai::Filter::LabelReferenceFilter do ...@@ -33,11 +33,10 @@ describe Banzai::Filter::LabelReferenceFilter do
stub_licensed_features(scoped_labels: false) stub_licensed_features(scoped_labels: false)
end end
it 'does not include link to scoped labels documentation' do it 'renders label as a common label' do
doc = reference_filter("See #{scoped_label.to_reference}") doc = reference_filter("See #{scoped_label.to_reference}")
scoped = scoped_label.name.split('::')
expect(doc.to_html).to match(%r(<span.+><a.+><span.+>#{scoped.first}</span><span.+>#{scoped.last}</span></a></span>$)) expect(doc.to_html).to match(%r(<span.+><a.+><span.+>#{scoped_label.name}</span></a></span>$))
end end
end end
end end
...@@ -80,15 +80,7 @@ module Banzai ...@@ -80,15 +80,7 @@ module Banzai
end end
end end
def label_link_text(object, matches) def object_link_text(object, matches)
parent = project || group
presenter = object.present(issuable_subject: parent)
presenter
end
def label_link_suffix(object, matches)
label_suffix = '' label_suffix = ''
parent = project || group parent = project || group
...@@ -100,12 +92,7 @@ module Banzai ...@@ -100,12 +92,7 @@ module Banzai
label_suffix = " <i>in #{ERB::Util.html_escape(reference)}</i>" if reference.present? label_suffix = " <i>in #{ERB::Util.html_escape(reference)}</i>" if reference.present?
end end
label_suffix presenter = object.present(issuable_subject: parent)
end
def object_link_text(object, matches)
presenter = label_link_text(object, matches)
label_suffix = label_link_suffix(object, matches)
LabelsHelper.render_colored_label(presenter, label_suffix: label_suffix) LabelsHelper.render_colored_label(presenter, label_suffix: label_suffix)
end end
...@@ -116,10 +103,6 @@ module Banzai ...@@ -116,10 +103,6 @@ module Banzai
content content
end end
def tooltip_title(label)
nil
end
def full_path_ref?(matches) def full_path_ref?(matches)
matches[:namespace] && matches[:project] matches[:namespace] && matches[:project]
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