BigW Consortium Gitlab

issuable_form.js.coffee 2.84 KB
Newer Older
1
class @IssuableForm
2
  issueMoveConfirmMsg: 'Are you sure you want to move this issue to another project?'
3
  wipRegex: /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i
4

5
  constructor: (@form) ->
6 7 8 9
    GitLab.GfmAutoComplete.setup()
    new UsersSelect()
    new ZenMode()

10 11
    @titleField       = @form.find("input[name*='[title]']")
    @descriptionField = @form.find("textarea[name*='[description]']")
12
    @issueMoveField   = @form.find("#move_to_project_id")
13 14 15 16 17

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

    @initAutosave()

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

21
    @initWip()
22
    @initMoveDropdown()
23

24 25 26 27 28 29 30 31 32
    $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())

33 34 35 36 37 38 39 40 41 42 43 44 45
  initAutosave: ->
    new Autosave @titleField, [
      document.location.pathname,
      document.location.search,
      "title"
    ]

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

46
  handleSubmit: =>
47
    if (parseInt(@issueMoveField?.val()) ? 0) > 0
48
      return false unless confirm(@issueMoveConfirmMsg)
49 50

    @resetAutosave()
51

52 53 54
  resetAutosave: =>
    @titleField.data("autosave").reset()
    @descriptionField.data("autosave").reset()
55 56

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

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

63
    @titleField.on "keyup blur", @renderWipExplanation
64 65 66 67

    @renderWipExplanation()

  workInProgress: ->
68
    @wipRegex.test @titleField.val()
69 70 71

  renderWipExplanation: =>
    if @workInProgress()
72 73
      @$wipExplanation.show()
      @$noWipExplanation.hide()
74
    else
75 76
      @$wipExplanation.hide()
      @$noWipExplanation.show()
77

78
  toggleWip: (event) =>
79 80
    event.preventDefault()

81 82 83 84
    if @workInProgress()
      @removeWip()
    else
      @addWip()
85 86 87

    @renderWipExplanation()

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

91
  addWip: ->
92
    @titleField.val "WIP: #{@titleField.val()}"
93 94 95 96 97 98 99 100 101 102 103 104

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

    if $moveDropdown.length
      $('.js-move-dropdown').select2
        ajax:
          url: $moveDropdown.data('projects-url')
          results: (data) ->
            return {
              results: data
            }
105 106 107 108
          data: (query) ->
            {
              search: query
            }
109 110 111 112
        formatResult: (project) ->
          project.name_with_namespace
        formatSelection: (project) ->
          project.name_with_namespace