2021-02-08 13:09:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module IssuablesDescriptionTemplatesHelper
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include GitlabRoutingHelper
|
|
|
|
|
|
|
|
def template_dropdown_tag(issuable, &block)
|
2022-07-22 05:11:01 -04:00
|
|
|
template_names = template_names(issuable)
|
|
|
|
|
|
|
|
selected_template = selected_template_name(template_names)
|
|
|
|
default_template = default_template_name(template_names, issuable)
|
|
|
|
title = _('Choose a template')
|
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
options = {
|
|
|
|
toggle_class: 'js-issuable-selector',
|
|
|
|
title: title,
|
|
|
|
filter: true,
|
|
|
|
placeholder: 'Filter',
|
|
|
|
footer_content: true,
|
|
|
|
data: {
|
|
|
|
data: issuable_templates(ref_project, issuable.to_ability_name),
|
|
|
|
field_name: 'issuable_template',
|
2021-02-18 07:09:34 -05:00
|
|
|
selected: selected_template,
|
2022-07-22 05:11:01 -04:00
|
|
|
default: default_template,
|
2021-02-11 13:09:10 -05:00
|
|
|
project_id: ref_project.id
|
2021-02-08 13:09:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dropdown_tag(title, options: options) do
|
|
|
|
capture(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def issuable_templates(project, issuable_type)
|
|
|
|
@template_types ||= {}
|
|
|
|
@template_types[project.id] ||= {}
|
2021-05-28 02:10:48 -04:00
|
|
|
@template_types[project.id][issuable_type] ||= TemplateFinder.all_template_names(project, issuable_type.pluralize)
|
2021-02-08 13:09:49 -05:00
|
|
|
end
|
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
def selected_template_name(template_names)
|
|
|
|
template_names.find { |tmpl_name| tmpl_name == params[:issuable_template] }
|
|
|
|
end
|
2021-11-09 07:12:15 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
def default_template_name(template_names, issuable)
|
|
|
|
return if issuable.description.present? || issuable.persisted?
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
template_names.find { |tmpl_name| tmpl_name.casecmp?('default') }
|
|
|
|
end
|
2022-01-27 22:15:57 -05:00
|
|
|
|
2022-07-22 05:11:01 -04:00
|
|
|
def template_names(issuable)
|
|
|
|
# Only local templates will be listed if licenses for inherited templates are not present
|
|
|
|
issuable_templates(ref_project, issuable.to_ability_name).values.flatten.map { |tpl| tpl[:name] }.compact.uniq
|
2021-02-08 13:09:49 -05:00
|
|
|
end
|
|
|
|
|
2021-11-09 07:12:15 -05:00
|
|
|
def available_service_desk_templates_for(project)
|
|
|
|
issuable_templates(project, 'issue').flatten.to_json
|
2021-02-08 13:09:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def template_names_path(parent, issuable)
|
|
|
|
return '' unless parent.is_a?(Project)
|
|
|
|
|
|
|
|
project_template_names_path(parent, template_type: issuable.to_ability_name)
|
|
|
|
end
|
|
|
|
end
|
2021-08-18 08:11:19 -04:00
|
|
|
|
|
|
|
IssuablesDescriptionTemplatesHelper.prepend_mod
|