due_date_select.js.coffee 2.6 KB
Newer Older
P
Phil Hughes 已提交
1 2
class @DueDateSelect
  constructor: ->
3 4 5
    $loading = $('.js-issuable-update .due_date')
      .find('.block-loading')
      .hide()
6

P
Phil Hughes 已提交
7 8 9 10 11 12 13
    $('.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')
P
Phil Hughes 已提交
14
      $valueContent = $block.find('.value-content')
P
Phil Hughes 已提交
15
      $sidebarValue = $('.js-due-date-sidebar-value', $block)
P
Phil Hughes 已提交
16 17 18 19 20

      fieldName = $dropdown.data('field-name')
      abilityName = $dropdown.data('ability-name')
      issueUpdateURL = $dropdown.data('issue-update')

21 22 23
      $dropdown.glDropdown(
        hidden: ->
          $selectbox.hide()
P
Phil Hughes 已提交
24
          $value.css('display', '')
25 26
      )

27
      addDueDate = (isDropdown) ->
P
Phil Hughes 已提交
28 29
        # Create the post date
        value = $("input[name='#{fieldName}']").val()
P
Phil Hughes 已提交
30 31 32 33 34

        if value isnt ''
          date = new Date value.replace(new RegExp('-', 'g'), ',')
          mediumDate = $.datepicker.formatDate 'M d, yy', date
        else
35
          mediumDate = 'No due date'
P
Phil Hughes 已提交
36 37 38 39 40 41 42 43 44

        data = {}
        data[abilityName] = {}
        data[abilityName].due_date = value

        $.ajax(
          type: 'PUT'
          url: issueUpdateURL
          data: data
P
Phil Hughes 已提交
45
          dataType: 'json'
P
Phil Hughes 已提交
46 47
          beforeSend: ->
            $loading.fadeIn()
48 49 50
            if isDropdown
              $dropdown.trigger('loading.gl.dropdown')
              $selectbox.hide()
P
Phil Hughes 已提交
51
            $value.css('display', '')
P
Phil Hughes 已提交
52

53
            cssClass = if Date.parse(mediumDate) then 'bold' else 'no-value'
54
            $valueContent.html("<span class='#{cssClass}'>#{mediumDate}</span>")
P
Phil Hughes 已提交
55
            $sidebarValue.html(mediumDate)
P
Phil Hughes 已提交
56 57 58 59 60

            if value isnt ''
              $('.js-remove-due-date-holder').removeClass 'hidden'
            else
              $('.js-remove-due-date-holder').addClass 'hidden'
P
Phil Hughes 已提交
61
        ).done (data) ->
62 63 64
          if isDropdown
            $dropdown.trigger('loaded.gl.dropdown')
            $dropdown.dropdown('toggle')
P
Phil Hughes 已提交
65 66
          $loading.fadeOut()

P
Phil Hughes 已提交
67 68 69
      $block.on 'click', '.js-remove-due-date', (e) ->
        e.preventDefault()
        $("input[name='#{fieldName}']").val ''
70
        addDueDate(false)
P
Phil Hughes 已提交
71

P
Phil Hughes 已提交
72
      $datePicker.datepicker(
73
        dateFormat: 'yy-mm-dd',
P
Phil Hughes 已提交
74 75
        defaultDate: $("input[name='#{fieldName}']").val()
        altField: "input[name='#{fieldName}']"
76
        onSelect: ->
77
          addDueDate(true)
P
Phil Hughes 已提交
78
      )
79 80 81 82 83

    $(document)
      .off 'click', '.ui-datepicker-header a'
      .on 'click', '.ui-datepicker-header a', (e) ->
        e.stopImmediatePropagation()