gl_form.js.coffee 1.4 KB
Newer Older
P
Phil Hughes 已提交
1 2
class @GLForm
  constructor: (@form) ->
3
    @textarea = @form.find('textarea.js-gfm-input')
P
Phil Hughes 已提交
4

5 6 7 8
    # Before we start, we should clean up any previous data for this form
    @destroy()

    # Setup the form
P
Phil Hughes 已提交
9 10
    @setupForm()

P
Phil Hughes 已提交
11 12 13 14 15 16 17
    @form.data 'gl-form', @

  destroy: ->
    # Clean form listeners
    @clearEventListeners()
    @form.data 'gl-form', null

P
Phil Hughes 已提交
18 19 20
  setupForm: ->
    isNewForm = @form.is(':not(.gfm-form)')

P
Phil Hughes 已提交
21
    @form.removeClass 'js-new-note-form'
P
Phil Hughes 已提交
22 23 24 25

    if isNewForm
      @form.find('.div-dropzone').remove()
      @form.addClass('gfm-form')
P
Phil Hughes 已提交
26
      disableButtonIfEmptyField @form.find('.js-note-text'), @form.find('.js-comment-button')
27
      disableButtonIfEmptyField @form.find('.js-note-text'), @form.find('.js-comment-resolve-button')
P
Phil Hughes 已提交
28 29 30 31 32 33 34 35 36 37

      # remove notify commit author checkbox for non-commit notes
      GitLab.GfmAutoComplete.setup()
      new DropzoneInput(@form)

      autosize(@textarea)

      # form and textarea event listeners
      @addEventListeners()

38 39
      gl.text.init(@form)

P
Phil Hughes 已提交
40 41 42 43 44
    # hide discard button
    @form.find('.js-note-discard').hide()

    @form.show()

P
Phil Hughes 已提交
45 46 47
  clearEventListeners: ->
    @textarea.off 'focus'
    @textarea.off 'blur'
48
    gl.text.removeListeners(@form)
P
Phil Hughes 已提交
49

P
Phil Hughes 已提交
50 51 52 53 54 55
  addEventListeners: ->
    @textarea.on 'focus', ->
      $(@).closest('.md-area').addClass 'is-focused'

    @textarea.on 'blur', ->
      $(@).closest('.md-area').removeClass 'is-focused'