gitlab-org--gitlab-foss/app/assets/javascripts/labels_select.js.coffee

84 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2016-03-07 17:17:11 +00:00
class @LabelsSelect
constructor: ->
$('.js-label-select').each (i, dropdown) ->
projectId = $(dropdown).data('project-id')
labelUrl = $(dropdown).data("labels")
2016-03-08 08:38:16 +00:00
selectedLabel = $(dropdown).data('selected')
2016-03-08 11:23:54 +00:00
newLabelField = $('#new_label_name')
newColorField = $('#new_label_color')
showNo = $(dropdown).data('show-no')
showAny = $(dropdown).data('show-any')
2016-03-08 11:23:54 +00:00
if newLabelField.length
$('.suggest-colors-dropdown a').on "click", (e) ->
e.preventDefault()
e.stopPropagation()
newColorField.val $(this).data("color")
$('.js-dropdown-label-color-preview')
.css 'background-color', $(this).data("color")
.addClass 'is-active'
$('.js-new-label-btn').on "click", (e) ->
e.preventDefault()
e.stopPropagation()
if newLabelField.val() isnt "" && newColorField.val() isnt ""
$('.js-new-label-btn').disable()
# Create new label with API
Api.newLabel projectId, {
name: newLabelField.val()
color: newColorField.val()
}, (label) ->
$('.js-new-label-btn').enable()
$('.dropdown-menu-back', $(dropdown).parent()).trigger "click"
2016-03-07 17:17:11 +00:00
$(dropdown).glDropdown(
2016-03-08 09:09:39 +00:00
data: (term, callback) ->
# We have to fetch the JS version of the labels list because there is no
# public facing JSON url for labels
$.ajax(
url: labelUrl
).done (data) ->
html = $(data)
data = []
html.find('.label-row a').each ->
data.push(
title: $(@).text().trim()
)
if showNo
data.unshift(
id: "0"
title: 'No label'
)
if showAny
data.unshift(
title: 'Any label'
)
if data.length > 2
data.splice 2, 0, "divider"
callback data
2016-03-07 17:17:11 +00:00
renderRow: (label) ->
selected = if label.title is selectedLabel then "is-active" else ""
2016-03-08 08:38:16 +00:00
2016-03-07 17:17:11 +00:00
"<li>
2016-03-08 08:38:16 +00:00
<a href='#' class='#{selected}'>
#{label.title}
2016-03-07 17:17:11 +00:00
</a>
</li>"
filterable: true
search:
fields: ['title']
2016-03-07 17:17:11 +00:00
selectable: true
fieldName: $(dropdown).data('field-name')
id: (label) ->
label.title
2016-03-07 17:17:11 +00:00
clicked: ->
2016-03-08 11:58:14 +00:00
if $(dropdown).hasClass "js-filter-submit"
$(dropdown).parents('form').submit()
2016-03-07 17:17:11 +00:00
)