Skip to content

Commit

Permalink
Désactiver select2 pour les champs avec une courte liste
Browse files Browse the repository at this point in the history
  • Loading branch information
Yalaeddin committed Apr 3, 2024
1 parent 118d9f0 commit 8c1b9fe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
13 changes: 8 additions & 5 deletions app/views/issue_templates/_section_groups_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
toggleFieldAndLinkIconVisibilityOnAddPossibleValue($(this));
})

$('form#issue-template-form').on('change', 'select[id*="select_type"][class="select2-hidden-accessible"]', function (event) {
$('form#issue-template-form').on('change', 'select[id*="select_type"][class="classic-select select-type"]', function (event) {
toggleIconSpanOnChangeType($(this));
});

Expand All @@ -97,8 +97,9 @@

function toggleFieldAndLinkIconVisibilityOnAddPossibleValue(elem){
let divParent = elem.parent();
// Select the neighboring select element of the parent div.which corresponds to the selected type
let selectType = divParent.prev('p').find('select');
// Select the neighboring select element of the parent div.which corresponds to the selected type.
// This allows for handling multiple sections of select type.
let selectType = divParent.prev('p').find('select.classic-select.select-type');
toggleFieldAndLinkIconVisibility(divParent, selectType);
}

Expand All @@ -117,8 +118,10 @@
function updateFieldAndLinkIconVisibilityBasedOnSelectedType(){
let elements = document.querySelectorAll('div.sections');
elements.forEach(function( ele) {
let selectType = $(ele).find('select[id*="select_type"]');
toggleIconSpanOnChangeType(selectType );
let selectTypeList = $(ele).find('select.classic-select.select-type');
selectTypeList.each(function() {
toggleIconSpanOnChangeType($(this));
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<%= check_box_tag :checked, '1', default_values.include?(value) %>
<span><%= text_field_tag 'value', value, size: 45, class: 'value' %></span>

<span class="span-icons">
<%= link_to 'Liste des icones compatibles', 'https://octicons.github.com', target: '_blank' %>
<%= text_field_tag 'icon_name', icon_name, size: 45, class: 'buttons-icons' %>
<span class="span-icons">
<span style="font-weight: bold;"><%= l(:label_icon) %></span>
<%= text_field_tag 'icon_name', icon_name, size: 25, class: 'buttons-icons' , :placeholder => l(:label_icon) %>
</span>

<%= check_box_tag :read_only, '1', empty_value.include?(value) %><span style="cursor: default;"> <%= l(:read_only) %></span>
Expand Down
69 changes: 36 additions & 33 deletions app/views/issues/sections/_select_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
<% if section.required %>
<span class="required"> *</span>
<% end %>
</div>
<% if section.description.present? %>
<em class="info" style="padding-top: 3px; line-height: 1.5em;">
<%= section.description %>
</em>
<% end %>
</div>
<% end %>
<% case section.select_type
Expand Down Expand Up @@ -48,41 +49,43 @@
<% end %>
</div>
<% when 'buttons_icons' %>
<% defaultValue = values.present? ? values["text"] : default_values.first %>
<%# This variable tracks whether an option has already been selected,
so that another one doesn't need to be selected,
particularly when they have the same value %>
<% isOptionSelected = false %>
<% possible_values.each_with_index do |select_value, value_index| %>
<%
if !values.present?
if defaultValue == select_value && !isOptionSelected
selected = "selected-button-icon"
isOptionSelected = true
else
selected = ""
end
else
if defaultValue == select_value && !isOptionSelected
selected = "selected-button-icon"
isOptionSelected = true
<div style="display:inline-flex">
<% defaultValue = values.present? ? values["text"] : default_values.first %>
<%# This variable tracks whether an option has already been selected,
so that another one doesn't need to be selected,
particularly when they have the same value %>
<% isOptionSelected = false %>
<% possible_values.each_with_index do |select_value, value_index| %>
<%
if !values.present?
if defaultValue == select_value && !isOptionSelected
selected = "selected-button-icon"
isOptionSelected = true
else
selected = ""
end
else
selected = ""
if defaultValue == select_value && !isOptionSelected
selected = "selected-button-icon"
isOptionSelected = true
else
selected = ""
end
end
end
%>
<div class="template_card <%= selected %>" style="display: inline-block; text-align: center;" id="<%= "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]"%>" value="<%= select_value%>">
<% begin %>
<%= octicon(possible_icons[value_index], :height => 30, :class => "right left", :"aria-label" => "hi") %>
<% rescue => e %>
<%= octicon("alert-fill", :height => 20, :class => "right left", :"aria-label" => "hi") %>
<% end %>
%>
<div class="template_card <%= selected %>" style="display: inline-block; text-align: center;flex:0;min-width:150px" id="<%= "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]"%>" value="<%= select_value%>">
<% begin %>
<%= octicon(possible_icons[value_index], :height => 30, :class => "right left", :"aria-label" => "hi") %>
<% rescue => e %>
<%= octicon("alert-fill", :height => 20, :class => "right left", :"aria-label" => "hi") %>
<% end %>
<%= hidden_field_tag "", select_value, :id => "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]" %>
<span style="display: block;"><%= select_value %></span>
</div>
<% end %>
<%= hidden_field_tag "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]", defaultValue %>
<%= hidden_field_tag "", select_value, :id => "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]" %>
<span style="display: block;"><%= select_value %></span>
</div>
<% end %>
<%= hidden_field_tag "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]", defaultValue %>
</div>
<% when 'monovalue_select', 'multivalue_select' %>
<%= select_tag "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][text]",
Expand Down
5 changes: 4 additions & 1 deletion assets/javascripts/issue_templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ $(document).ready(function ($) {

function applySelect2ToSelects() {
if ((typeof $().select2) === 'function') {
$('.split_description.select select:visible').select2({
$('.split_description.select select:visible').filter(function() {
// Exclude elements with the class 'classic-select', because they have a short list
return !$(this).hasClass('classic-select');
}).select2({
tags: true,
tokenSeparators: [';'],
containerCss: {
Expand Down

0 comments on commit 8c1b9fe

Please sign in to comment.