gitlab-org--gitlab-foss/app/assets/javascripts/issues-bulk-assignment.js.c...

128 lines
3.3 KiB
CoffeeScript
Raw Normal View History

2016-05-05 22:10:54 +00:00
class @IssuableBulkActions
constructor: (opts = {}) ->
# Set defaults
{
@container = $('.content')
@form = @getElement('.bulk-update')
@issues = @getElement('.issues-list .issue')
} = opts
# Save instance
@form.data 'bulkActions', @
@willUpdateLabels = false
2016-05-05 22:10:54 +00:00
@bindEvents()
# Fixes bulk-assign not working when navigating through pages
Issuable.initChecks();
2016-05-05 22:10:54 +00:00
getElement: (selector) ->
@container.find selector
bindEvents: ->
2016-05-31 21:11:46 +00:00
@form.off('submit').on('submit', @onFormSubmit.bind(@))
2016-05-05 22:10:54 +00:00
onFormSubmit: (e) ->
e.preventDefault()
@submit()
submit: ->
_this = @
xhr = $.ajax
url: @form.attr 'action'
method: @form.attr 'method'
dataType: 'JSON',
data: @getFormDataAsObject()
xhr.done (response, status, xhr) ->
2016-05-09 20:54:20 +00:00
location.reload()
2016-05-05 22:10:54 +00:00
xhr.fail ->
2016-05-13 05:56:37 +00:00
new Flash("Issue update failed")
2016-05-05 22:10:54 +00:00
2016-05-13 05:56:37 +00:00
xhr.always @onFormSubmitAlways.bind(@)
2016-05-05 22:10:54 +00:00
onFormSubmitAlways: ->
@form.find('[type="submit"]').enable()
getSelectedIssues: ->
@issues.has('.selected_issue:checked')
getLabelsFromSelection: ->
labels = []
@getSelectedIssues().map ->
_labels = $(@).data('labels')
if _labels
2016-05-13 05:56:37 +00:00
_labels.map (labelId) ->
2016-05-05 22:10:54 +00:00
labels.push(labelId) if labels.indexOf(labelId) is -1
labels
###*
* Will return only labels that were marked previously and the user has unmarked
* @return {Array} Label IDs
###
getUnmarkedIndeterminedLabels: ->
result = []
labelsToKeep = []
for el in @getElement('.labels-filter .is-indeterminate')
labelsToKeep.push $(el).data('labelId')
for id in @getLabelsFromSelection()
# Only the ones that we are not going to keep
result.push(id) if labelsToKeep.indexOf(id) is -1
result
###*
* Simple form serialization, it will return just what we need
* Returns key/value pairs from form data
###
getFormDataAsObject: ->
formData =
update:
2016-05-09 20:54:12 +00:00
state_event : @form.find('input[name="update[state_event]"]').val()
assignee_id : @form.find('input[name="update[assignee_id]"]').val()
milestone_id : @form.find('input[name="update[milestone_id]"]').val()
issues_ids : @form.find('input[name="update[issues_ids]"]').val()
add_label_ids : []
remove_label_ids : []
2016-05-05 22:10:54 +00:00
if @willUpdateLabels
@getLabelsToApply().map (id) ->
formData.update.add_label_ids.push id
2016-05-05 22:10:54 +00:00
@getLabelsToRemove().map (id) ->
formData.update.remove_label_ids.push id
2016-05-05 22:10:54 +00:00
formData
getLabelsToApply: ->
labelIds = []
$labels = @form.find('.labels-filter input[name="update[label_ids][]"]')
2016-05-13 05:56:37 +00:00
$labels.each (k, label) ->
labelIds.push parseInt($(label).val()) if label
2016-05-05 22:10:54 +00:00
labelIds
###*
* Returns Label IDs that will be removed from issue selection
* @return {Array} Array of labels IDs
2016-05-05 22:10:54 +00:00
###
getLabelsToRemove: ->
result = []
indeterminatedLabels = @getUnmarkedIndeterminedLabels()
labelsToApply = @getLabelsToApply()
indeterminatedLabels.map (id) ->
# We need to exclude label IDs that will be applied
# By not doing this will cause issues from selection to not add labels at all
result.push(id) if labelsToApply.indexOf(id) is -1
result