due_date_select.js.coffee 3.0 KB
Newer Older
P
Phil Hughes 已提交
1 2
class @DueDateSelect
  constructor: ->
3 4
    # Milestone edit/new form
    $datePicker = $('.datepicker')
P
Phil Hughes 已提交
5 6 7 8 9 10 11 12

    if $datePicker.length
      $dueDate = $('#milestone_due_date')
      $datePicker.datepicker
        dateFormat: 'yy-mm-dd'
        onSelect: (dateText, inst) ->
          $dueDate.val(dateText)
      .datepicker('setDate', $.datepicker.parseDate('yy-mm-dd', $dueDate.val()))
13 14 15 16 17 18 19

    $('.js-clear-due-date').on 'click', (e) ->
      e.preventDefault()
      $dueDate.val('')
      $datePicker.datepicker('setDate', '')

    # Issuable sidebar
20 21 22
    $loading = $('.js-issuable-update .due_date')
      .find('.block-loading')
      .hide()
23

P
Phil Hughes 已提交
24 25 26 27 28 29 30
    $('.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 已提交
31
      $valueContent = $block.find('.value-content')
P
Phil Hughes 已提交
32
      $sidebarValue = $('.js-due-date-sidebar-value', $block)
P
Phil Hughes 已提交
33 34 35 36 37

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

38 39 40
      $dropdown.glDropdown(
        hidden: ->
          $selectbox.hide()
P
Phil Hughes 已提交
41
          $value.css('display', '')
42 43
      )

44
      addDueDate = (isDropdown) ->
P
Phil Hughes 已提交
45 46
        # Create the post date
        value = $("input[name='#{fieldName}']").val()
P
Phil Hughes 已提交
47 48 49 50 51 52

        if value isnt ''
          date = new Date value.replace(new RegExp('-', 'g'), ',')
          mediumDate = $.datepicker.formatDate 'M d, yy', date
        else
          mediumDate = 'None'
P
Phil Hughes 已提交
53 54 55 56 57 58 59 60 61

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

        $.ajax(
          type: 'PUT'
          url: issueUpdateURL
          data: data
P
Phil Hughes 已提交
62
          dataType: 'json'
P
Phil Hughes 已提交
63 64
          beforeSend: ->
            $loading.fadeIn()
65 66 67
            if isDropdown
              $dropdown.trigger('loading.gl.dropdown')
              $selectbox.hide()
P
Phil Hughes 已提交
68
            $value.css('display', '')
P
Phil Hughes 已提交
69

P
Phil Hughes 已提交
70
            $valueContent.html(mediumDate)
P
Phil Hughes 已提交
71
            $sidebarValue.html(mediumDate)
P
Phil Hughes 已提交
72 73 74 75 76

            if value isnt ''
              $('.js-remove-due-date-holder').removeClass 'hidden'
            else
              $('.js-remove-due-date-holder').addClass 'hidden'
P
Phil Hughes 已提交
77
        ).done (data) ->
78 79 80
          if isDropdown
            $dropdown.trigger('loaded.gl.dropdown')
            $dropdown.dropdown('toggle')
P
Phil Hughes 已提交
81 82
          $loading.fadeOut()

P
Phil Hughes 已提交
83 84 85
      $block.on 'click', '.js-remove-due-date', (e) ->
        e.preventDefault()
        $("input[name='#{fieldName}']").val ''
86
        addDueDate(false)
P
Phil Hughes 已提交
87

P
Phil Hughes 已提交
88
      $datePicker.datepicker(
89
        dateFormat: 'yy-mm-dd',
P
Phil Hughes 已提交
90 91
        defaultDate: $("input[name='#{fieldName}']").val()
        altField: "input[name='#{fieldName}']"
92
        onSelect: ->
93
          addDueDate(true)
P
Phil Hughes 已提交
94
      )
95 96 97 98 99

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