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

131 lines
4.4 KiB
CoffeeScript
Raw Normal View History

2016-03-08 03:38:16 -05:00
class @MilestoneSelect
constructor: (currentProject) ->
if currentProject?
_this = @
@currentProject = JSON.parse(currentProject)
2016-03-08 03:38:16 -05:00
$('.js-milestone-select').each (i, dropdown) ->
$dropdown = $(dropdown)
projectId = $dropdown.data('project-id')
milestonesUrl = $dropdown.data('milestones')
2016-03-11 14:20:44 -05:00
issueUpdateURL = $dropdown.data('issueUpdate')
selectedMilestone = $dropdown.data('selected')
showNo = $dropdown.data('show-no')
showAny = $dropdown.data('show-any')
showUpcoming = $dropdown.data('show-upcoming')
useId = $dropdown.data('use-id')
defaultLabel = $dropdown.data('default-label')
issuableId = $dropdown.data('issuable-id')
abilityName = $dropdown.data('ability-name')
$selectbox = $dropdown.closest('.selectbox')
$block = $selectbox.closest('.block')
2016-03-27 11:04:51 -04:00
$sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon')
$value = $block.find('.value')
2016-03-12 11:34:26 -05:00
$loading = $block.find('.block-loading').fadeOut()
2016-03-08 03:38:16 -05:00
if issueUpdateURL
milestoneLinkTemplate = _.template(
'<a href="/<%= namespace %>/<%= path %>/milestones/<%= iid %>"><%= _.escape(title) %></a>'
)
milestoneLinkNoneTemplate = '<div class="light">None</div>'
$dropdown.glDropdown(
2016-03-08 04:09:39 -05:00
data: (term, callback) ->
$.ajax(
url: milestonesUrl
).done (data) ->
extraOptions = []
if showAny
extraOptions.push(
id: 0
name: ''
title: 'Any Milestone'
)
if showNo
extraOptions.push(
id: -1
name: 'No Milestone'
title: 'No Milestone'
)
if showUpcoming
extraOptions.push(
id: -2
name: '#upcoming'
title: 'Upcoming'
)
2016-03-29 07:17:22 -04:00
if extraOptions.length > 2
extraOptions.push 'divider'
callback(extraOptions.concat(data))
2016-03-08 03:38:16 -05:00
filterable: true
search:
2016-03-08 04:09:39 -05:00
fields: ['title']
2016-03-08 03:38:16 -05:00
selectable: true
toggleLabel: (selected) ->
if selected && 'id' of selected
selected.title
else
defaultLabel
fieldName: $dropdown.data('field-name')
2016-03-08 03:38:16 -05:00
text: (milestone) ->
_.escape(milestone.title)
2016-03-08 03:38:16 -05:00
id: (milestone) ->
if !useId
milestone.name
else
milestone.id
2016-03-08 03:38:16 -05:00
isSelected: (milestone) ->
milestone.name is selectedMilestone
2016-03-12 15:37:02 -05:00
hidden: ->
$selectbox.hide()
# display:block overrides the hide-collapse rule
2016-06-01 05:11:30 -04:00
$value.css('display', '')
clicked: (selected) ->
page = $('body').data 'page'
isIssueIndex = page is 'projects:issues:index'
isMRIndex = page is page is 'projects:merge_requests:index'
if $dropdown.hasClass 'js-filter-bulk-update'
return
if $dropdown.hasClass('js-filter-submit') and (isIssueIndex or isMRIndex)
if selected.name?
selectedMilestone = selected.name
else
selectedMilestone = ''
Issuable.filterResults $dropdown.closest('form')
else if $dropdown.hasClass('js-filter-submit')
$dropdown.closest('form').submit()
else
selected = $selectbox
.find('input[type="hidden"]')
.val()
data = {}
data[abilityName] = {}
data[abilityName].milestone_id = selected
2016-03-12 11:34:26 -05:00
$loading
.fadeIn()
2016-03-27 11:04:51 -04:00
$dropdown.trigger('loading.gl.dropdown')
2016-03-11 14:20:44 -05:00
$.ajax(
type: 'PUT'
url: issueUpdateURL
data: data
2016-03-11 14:20:44 -05:00
).done (data) ->
2016-03-27 11:04:51 -04:00
$dropdown.trigger('loaded.gl.dropdown')
2016-03-12 11:34:26 -05:00
$loading.fadeOut()
$selectbox.hide()
2016-06-01 05:11:30 -04:00
$value.css('display', '')
if data.milestone?
data.milestone.namespace = _this.currentProject.namespace
data.milestone.path = _this.currentProject.path
$value.html(milestoneLinkTemplate(data.milestone))
2016-03-27 11:04:51 -04:00
$sidebarCollapsedValue.find('span').text(data.milestone.title)
else
$value.html(milestoneLinkNoneTemplate)
2016-03-27 11:04:51 -04:00
$sidebarCollapsedValue.find('span').text('No')
)