Commit ee2cfded authored by Alfredo Sumaran's avatar Alfredo Sumaran Committed by Phil Hughes

Refactor label toggling for Labels dropdown

parent 4ffe142f
...@@ -486,7 +486,7 @@ class GitLabDropdown ...@@ -486,7 +486,7 @@ class GitLabDropdown
# Toggle the dropdown label # Toggle the dropdown label
if @options.toggleLabel if @options.toggleLabel
@updateLabel(selectedObject, el, @, false) @updateLabel(selectedObject, el, @)
else else
selectedObject selectedObject
else if el.hasClass(INDETERMINATE_CLASS) else if el.hasClass(INDETERMINATE_CLASS)
...@@ -513,9 +513,6 @@ class GitLabDropdown ...@@ -513,9 +513,6 @@ class GitLabDropdown
# Toggle active class for the tick mark # Toggle active class for the tick mark
el.addClass ACTIVE_CLASS el.addClass ACTIVE_CLASS
# Toggle the dropdown label
if @options.toggleLabel
@updateLabel(selectedObject, el, @, true)
if value? if value?
if !field.length and fieldName if !field.length and fieldName
@addInput(fieldName, value) @addInput(fieldName, value)
...@@ -524,6 +521,10 @@ class GitLabDropdown ...@@ -524,6 +521,10 @@ class GitLabDropdown
.val value .val value
.trigger 'change' .trigger 'change'
# Toggle the dropdown label
if @options.toggleLabel
@updateLabel(selectedObject, el, @)
return selectedObject return selectedObject
addInput: (fieldName, value)-> addInput: (fieldName, value)->
...@@ -616,15 +617,12 @@ class GitLabDropdown ...@@ -616,15 +617,12 @@ class GitLabDropdown
# Scroll the dropdown content up # Scroll the dropdown content up
$dropdownContent.scrollTop(listItemTop - dropdownContentTop) $dropdownContent.scrollTop(listItemTop - dropdownContentTop)
updateLabel: (selected = null, el = null, instance = null, added = false) => updateLabel: (selected = null, el = null, instance = null) =>
$toggleText = $(@el).find(".dropdown-toggle-text") $toggleText = @getElement '.dropdown-toggle-text'
$toggleText.text @options.toggleLabel(selected, el, instance, added) $toggleText.text @options.toggleLabel(selected, el, instance)
if @options.defaultLabel if @options.defaultLabel
if $toggleText.text().trim() is @options.defaultLabel $toggleText.toggleClass('is-default', $toggleText.text().trim() is @options.defaultLabel)
$toggleText.addClass('is-default')
else
$toggleText.removeClass('is-default')
$.fn.glDropdown = (opts) -> $.fn.glDropdown = (opts) ->
return @.each -> return @.each ->
......
...@@ -278,22 +278,60 @@ class @LabelsSelect ...@@ -278,22 +278,60 @@ class @LabelsSelect
fields: ['title'] fields: ['title']
selectable: true selectable: true
filterable: true filterable: true
toggleLabel: (selected, el, e, added) -> toggleLabel: (selected, $el, glDropdownInstance) ->
if added # When comes from a triggered event handle it VERY differently
selectedLabels.push selected.title if selected instanceof jQuery.Event
$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 else
index = selectedLabels.indexOf selected.title defaultLabel
selectedLabels.splice index, 1 # 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 selected.id? and selected.id is 0 if glDropdownInstance?
selectedLabels = [] $dropdownParent = glDropdownInstance.dropdown.closest '.issuable-form-select-holder, .labels-filter'
else
$dropdownParent = $()
$labelInputs = $dropdownParent.find "input[name='#{@fieldName}']"
# Find the label by its attribute according the dropdown settings
if $dropdown.hasClass 'js-issuable-form-dropdown'
# When settings labels to a issuable we find the label for its ID
whereQuery = { id: parseInt $labelInputs.first().val() }
else
# When filtering issuables we find the label for its title
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 selectedLabels.length > 1 # If we are adding a label
"#{selectedLabels[0]} +#{selectedLabels.length - 1} more" if $el.is '.is-active'
else if selectedLabels.length is 1 if numberSelectedLabels is 1
selectedLabels[0] 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 else
console.log selectedLabels.length, defaultLabel
defaultLabel defaultLabel
defaultLabel: defaultLabel defaultLabel: defaultLabel
fieldName: $dropdown.data('field-name') fieldName: $dropdown.data('field-name')
......
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