Commit 49bc1cd6 authored by Phil Hughes's avatar Phil Hughes

Updated how the label toggle gets the text

Fixed some issues based on self-review
parent 357ad44a
...@@ -25,11 +25,9 @@ class @LabelsSelect ...@@ -25,11 +25,9 @@ class @LabelsSelect
$newLabelError = $('.js-label-error') $newLabelError = $('.js-label-error')
$colorPreview = $('.js-dropdown-label-color-preview') $colorPreview = $('.js-dropdown-label-color-preview')
$newLabelCreateButton = $('.js-new-label-btn') $newLabelCreateButton = $('.js-new-label-btn')
selectedLabels = [] fieldName = $dropdown.data('field-name')
useId = $dropdown.hasClass('js-issuable-form-dropdown') or $dropdown.hasClass('js-filter-bulk-update')
$("input[name='#{$dropdown.data('field-name')}']").each -> propertyName = if useId then "id" else "title"
title = $(this).data('title')
selectedLabels.push($(this).data('title')) if title
$newLabelError.hide() $newLabelError.hide()
$loading = $block.find('.block-loading').fadeOut() $loading = $block.find('.block-loading').fadeOut()
...@@ -125,7 +123,7 @@ class @LabelsSelect ...@@ -125,7 +123,7 @@ class @LabelsSelect
saveLabelData = -> saveLabelData = ->
selected = $dropdown selected = $dropdown
.closest('.selectbox') .closest('.selectbox')
.find("input[name='#{$dropdown.data('field-name')}']") .find("input[name='#{fieldName}']")
.map(-> .map(->
@value @value
).get() ).get()
...@@ -278,63 +276,21 @@ class @LabelsSelect ...@@ -278,63 +276,21 @@ class @LabelsSelect
fields: ['title'] fields: ['title']
selectable: true selectable: true
filterable: true filterable: true
toggleLabel: (selected, $el, glDropdownInstance) -> toggleLabel: (selected, el, glDropdown) ->
# When comes from a triggered event handle it VERY differently if glDropdown?
if selected instanceof jQuery.Event selectedIds = $("input[name='#{fieldName}']").map(-> $(this).val()).get()
$dropdownParent = $dropdown.closest '.labels-filter'
$labelInputs = $dropdownParent.find "input[name='#{@fieldName}']"
numberSelectedLabels = $labelInputs.length
firstLabel = _.pluck($labelInputs, 'value')[0]
if numberSelectedLabels is 1
firstLabel
else if numberSelectedLabels > 1
"#{firstLabel} +#{numberSelectedLabels - 1} more"
else
defaultLabel
# when clicking on a dropdown option
else
# Return when clicking "No Label"
return if selected.id is 0
return 'Any Label' if selected.isAny is true
if glDropdownInstance?
$dropdownParent = glDropdownInstance.dropdown.closest '.issuable-form-select-holder, .labels-filter'
else
$dropdownParent = $()
$labelInputs = $dropdownParent.find "input[name='#{@fieldName}']" selected = _.filter glDropdown.fullData, (label) ->
selectedIds.indexOf("#{label[propertyName]}") >= 0 if label[propertyName]?
# Find the label by its attribute according the dropdown settings if selected.length is 1
if $dropdown.hasClass('js-issuable-form-dropdown') or $dropdown.hasClass('js-filter-bulk-update') selected[0].title
# When settings labels to a issuable we find the label for its ID else if selected.length > 1
whereQuery = { id: parseInt $labelInputs.first().val() } "#{selected[0].title} +#{selected.length - 1} more"
else else
# When filtering issuables we find the label for its title defaultLabel
whereQuery = { title: $labelInputs.first().val() }
firstLabel = _.findWhere glDropdownInstance.fullData, whereQuery
# Better rely on inputs since filtering may returns invalid number of active labels
numberSelectedLabels = $labelInputs.length
# If we are adding a label
if $el.is '.is-active'
if numberSelectedLabels is 1
selected.title
else
"#{selected.title} +#{numberSelectedLabels - 1} more"
# otherwise we are removing a label
else
if numberSelectedLabels is 1
firstLabel.title
else if numberSelectedLabels > 1
"#{firstLabel.title} +#{numberSelectedLabels - 1} more"
else
defaultLabel
defaultLabel: defaultLabel defaultLabel: defaultLabel
fieldName: $dropdown.data('field-name') fieldName: fieldName
id: (label) -> id: (label) ->
if $dropdown.hasClass('js-issuable-form-dropdown') if $dropdown.hasClass('js-issuable-form-dropdown')
if label.id is 0 if label.id is 0
......
...@@ -70,8 +70,8 @@ class @MilestoneSelect ...@@ -70,8 +70,8 @@ class @MilestoneSelect
search: search:
fields: ['title'] fields: ['title']
selectable: true selectable: true
toggleLabel: (selected, el, e, added) -> toggleLabel: (selected, el, e) ->
if selected and 'id' of selected and added if selected and 'id' of selected and $(el).hasClass('is-active')
selected.title selected.title
else else
defaultLabel defaultLabel
...@@ -80,7 +80,7 @@ class @MilestoneSelect ...@@ -80,7 +80,7 @@ class @MilestoneSelect
text: (milestone) -> text: (milestone) ->
_.escape(milestone.title) _.escape(milestone.title)
id: (milestone) -> id: (milestone) ->
if !useId and not $dropdown.is('.js-issuable-form-dropdown') if not useId and not $dropdown.is('.js-issuable-form-dropdown')
milestone.name milestone.name
else else
milestone.id milestone.id
......
...@@ -116,7 +116,7 @@ module LabelsHelper ...@@ -116,7 +116,7 @@ module LabelsHelper
def labels_filter_path def labels_filter_path
project = @target_project || @project project = @target_project || @project
if @project if project
namespace_project_labels_path(project.namespace, project, :json) namespace_project_labels_path(project.namespace, project, :json)
else else
dashboard_labels_path(:json) dashboard_labels_path(:json)
......
...@@ -48,7 +48,7 @@ module MilestonesHelper ...@@ -48,7 +48,7 @@ module MilestonesHelper
def milestones_filter_dropdown_path def milestones_filter_dropdown_path
project = @target_project || @project project = @target_project || @project
if @project if project
namespace_project_milestones_path(project.namespace, project, :json) namespace_project_milestones_path(project.namespace, project, :json)
else else
dashboard_milestones_path(:json) dashboard_milestones_path(:json)
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
- selected.each do |label| - selected.each do |label|
- id = label.try(:id) || label - id = label.try(:id) || label
- title = label.try(:title) || label - title = label.try(:title) || label
= hidden_field_tag data_options[:field_name], useId ? id : title, id: nil, data: { title: title } = hidden_field_tag data_options[:field_name], useId ? id : title, id: nil
.dropdown .dropdown
%button.dropdown-menu-toggle.js-label-select.js-multiselect{class: classes.join(' '), type: "button", data: dropdown_data} %button.dropdown-menu-toggle.js-label-select.js-multiselect{class: classes.join(' '), type: "button", data: dropdown_data}
%span.dropdown-toggle-text{ class: ("is-default" if selected.nil? || selected.to_a.size == 0) } %span.dropdown-toggle-text{ class: ("is-default" if selected.nil? || selected.to_a.size == 0) }
......
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