summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issuable_form.js.coffee
blob: 5b7a4831dfc8fe1449b6649f9b8aedd6ea928dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
class @IssuableForm
  issueMoveConfirmMsg: 'Are you sure you want to move this issue to another project?'
  wipRegex: /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i

  constructor: (@form) ->
    GitLab.GfmAutoComplete.setup()
    new UsersSelect()
    new ZenMode()

    @titleField       = @form.find("input[name*='[title]']")
    @descriptionField = @form.find("textarea[name*='[description]']")
    @issueMoveField   = @form.find("#move_to_project_id")

    return unless @titleField.length && @descriptionField.length

    @initAutosave()

    @form.on "submit", @handleSubmit
    @form.on "click", ".btn-cancel", @resetAutosave

    @initWip()
    @initMoveDropdown()

    $issuableDueDate = $('#issuable-due-date')

    if $issuableDueDate.length
      $('.datepicker').datepicker(
        dateFormat: 'yy-mm-dd',
        onSelect: (dateText, inst) ->
          $issuableDueDate.val dateText
      ).datepicker 'setDate', $.datepicker.parseDate('yy-mm-dd', $issuableDueDate.val())

  initAutosave: ->
    new Autosave @titleField, [
      document.location.pathname,
      document.location.search,
      "title"
    ]

    new Autosave @descriptionField, [
      document.location.pathname,
      document.location.search,
      "description"
    ]

  handleSubmit: =>
    if (parseInt(@issueMoveField?.val()) ? 0) > 0
      return false unless confirm(@issueMoveConfirmMsg)

    @resetAutosave()

  resetAutosave: =>
    @titleField.data("autosave").reset()
    @descriptionField.data("autosave").reset()

  initWip: ->
    @$wipExplanation = @form.find(".js-wip-explanation")
    @$noWipExplanation = @form.find(".js-no-wip-explanation")
    return unless @$wipExplanation.length and @$noWipExplanation.length

    @form.on "click", ".js-toggle-wip", @toggleWip

    @titleField.on "keyup blur", @renderWipExplanation

    @renderWipExplanation()

  workInProgress: ->
    @wipRegex.test @titleField.val()

  renderWipExplanation: =>
    if @workInProgress()
      @$wipExplanation.show()
      @$noWipExplanation.hide()
    else
      @$wipExplanation.hide()
      @$noWipExplanation.show()

  toggleWip: (event) =>
    event.preventDefault()

    if @workInProgress()
      @removeWip()
    else
      @addWip()

    @renderWipExplanation()

  removeWip: ->
    @titleField.val @titleField.val().replace(@wipRegex, "")

  addWip: ->
    @titleField.val "WIP: #{@titleField.val()}"

  initMoveDropdown: ->
    $moveDropdown = $('.js-move-dropdown')

    if $moveDropdown.length
      $('.js-move-dropdown').select2
        ajax:
          url: $moveDropdown.data('projects-url')
          results: (data) ->
            return {
              results: data
            }
          data: (query) ->
            {
              search: query
            }
        formatResult: (project) ->
          project.name_with_namespace
        formatSelection: (project) ->
          project.name_with_namespace