Skip to content
Snippets Groups Projects
Commit ee2cfded authored by Alfredo Sumaran's avatar Alfredo Sumaran Committed by Phil Hughes
Browse files

Refactor label toggling for Labels dropdown

parent 4ffe142f
No related branches found
No related tags found
1 merge request!5293Updated issuable form with GL dropdowns
Loading
@@ -486,7 +486,7 @@ class GitLabDropdown
Loading
@@ -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)
Loading
@@ -513,9 +513,6 @@ class GitLabDropdown
Loading
@@ -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)
Loading
@@ -524,6 +521,10 @@ class GitLabDropdown
Loading
@@ -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)->
Loading
@@ -616,15 +617,12 @@ class GitLabDropdown
Loading
@@ -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 ->
Loading
Loading
Loading
@@ -278,23 +278,61 @@ class @LabelsSelect
Loading
@@ -278,23 +278,61 @@ 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
defaultLabel
# when clicking on a dropdown option
else else
index = selectedLabels.indexOf selected.title # Return when clicking "No Label"
selectedLabels.splice index, 1 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 = $()
   
if selectedLabels.length > 1 $labelInputs = $dropdownParent.find "input[name='#{@fieldName}']"
"#{selectedLabels[0]} +#{selectedLabels.length - 1} more"
else if selectedLabels.length is 1 # Find the label by its attribute according the dropdown settings
selectedLabels[0] if $dropdown.hasClass 'js-issuable-form-dropdown'
else # When settings labels to a issuable we find the label for its ID
console.log selectedLabels.length, defaultLabel whereQuery = { id: parseInt $labelInputs.first().val() }
defaultLabel 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 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: $dropdown.data('field-name')
id: (label) -> id: (label) ->
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment