2016-03-07 17:17:11 +00:00
|
|
|
class @LabelsSelect
|
|
|
|
constructor: ->
|
|
|
|
$('.js-label-select').each (i, dropdown) ->
|
|
|
|
projectId = $(dropdown).data('project-id')
|
2016-03-08 12:32:04 +00:00
|
|
|
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')
|
2016-03-08 15:18:01 +00:00
|
|
|
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) ->
|
2016-03-08 12:32:04 +00:00
|
|
|
# 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()
|
|
|
|
)
|
|
|
|
|
2016-03-08 15:18:01 +00:00
|
|
|
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"
|
|
|
|
|
2016-03-08 12:32:04 +00:00
|
|
|
callback data
|
2016-03-07 17:17:11 +00:00
|
|
|
renderRow: (label) ->
|
2016-03-08 12:32:04 +00:00
|
|
|
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}'>
|
2016-03-08 12:32:04 +00:00
|
|
|
#{label.title}
|
2016-03-07 17:17:11 +00:00
|
|
|
</a>
|
|
|
|
</li>"
|
|
|
|
filterable: true
|
|
|
|
search:
|
2016-03-08 15:18:01 +00:00
|
|
|
fields: ['title']
|
2016-03-07 17:17:11 +00:00
|
|
|
selectable: true
|
|
|
|
fieldName: $(dropdown).data('field-name')
|
|
|
|
id: (label) ->
|
2016-03-08 12:32:04 +00:00
|
|
|
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
|
|
|
)
|