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

84 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2016-04-08 08:04:20 -04:00
class @DueDateSelect
constructor: ->
$loading = $('.js-issuable-update .due_date')
.find('.block-loading')
.hide()
2016-04-08 11:23:59 -04:00
2016-04-08 08:04:20 -04:00
$('.js-due-date-select').each (i, dropdown) ->
$dropdown = $(dropdown)
$dropdownParent = $dropdown.closest('.dropdown')
$datePicker = $dropdownParent.find('.js-due-date-calendar')
$block = $dropdown.closest('.block')
$selectbox = $dropdown.closest('.selectbox')
$value = $block.find('.value')
$valueContent = $block.find('.value-content')
2016-04-11 06:07:02 -04:00
$sidebarValue = $('.js-due-date-sidebar-value', $block)
2016-04-08 08:04:20 -04:00
fieldName = $dropdown.data('field-name')
abilityName = $dropdown.data('ability-name')
issueUpdateURL = $dropdown.data('issue-update')
2016-04-08 11:23:59 -04:00
$dropdown.glDropdown(
hidden: ->
$selectbox.hide()
2016-06-01 05:11:30 -04:00
$value.css('display', '')
2016-04-08 11:23:59 -04:00
)
addDueDate = (isDropdown) ->
2016-04-08 08:04:20 -04:00
# Create the post date
value = $("input[name='#{fieldName}']").val()
if value isnt ''
date = new Date value.replace(new RegExp('-', 'g'), ',')
mediumDate = $.datepicker.formatDate 'M d, yy', date
else
mediumDate = 'No due date'
2016-04-08 08:04:20 -04:00
data = {}
data[abilityName] = {}
data[abilityName].due_date = value
$.ajax(
type: 'PUT'
url: issueUpdateURL
data: data
2016-06-01 05:11:30 -04:00
dataType: 'json'
2016-04-08 08:04:20 -04:00
beforeSend: ->
$loading.fadeIn()
if isDropdown
$dropdown.trigger('loading.gl.dropdown')
$selectbox.hide()
2016-06-01 05:11:30 -04:00
$value.css('display', '')
2016-04-08 08:04:20 -04:00
cssClass = if mediumDate is "No due date" then 'no-value' else 'bold'
$valueContent.html("<span class='#{cssClass}'>#{mediumDate}</span>")
2016-04-11 06:07:02 -04:00
$sidebarValue.html(mediumDate)
if value isnt ''
$('.js-remove-due-date-holder').removeClass 'hidden'
else
$('.js-remove-due-date-holder').addClass 'hidden'
2016-04-08 08:04:20 -04:00
).done (data) ->
if isDropdown
$dropdown.trigger('loaded.gl.dropdown')
$dropdown.dropdown('toggle')
2016-04-08 08:04:20 -04:00
$loading.fadeOut()
$block.on 'click', '.js-remove-due-date', (e) ->
e.preventDefault()
$("input[name='#{fieldName}']").val ''
addDueDate(false)
2016-04-08 08:04:20 -04:00
$datePicker.datepicker(
dateFormat: 'yy-mm-dd',
2016-04-08 08:04:20 -04:00
defaultDate: $("input[name='#{fieldName}']").val()
altField: "input[name='#{fieldName}']"
onSelect: ->
addDueDate(true)
2016-04-08 08:04:20 -04:00
)
$(document)
.off 'click', '.ui-datepicker-header a'
.on 'click', '.ui-datepicker-header a', (e) ->
e.stopImmediatePropagation()