<commit_before>AddModelToCollectionMixin =
  addModel: (model, options) ->
    @options.addToCollection.unshift(model, options)
    model.save {},
      success: =>
        @addModelSuccess(model) if @addModelSuccess?
      error: =>
        @options.addToCollection.remove(model)
        @addModelError(model) if @addModelError?

  addDefaultModel: (options) -> @addModel @model, options

  addWrappedModel: -> @addModel @wrapNewModel(@model)

class window.AddCommentView extends Backbone.Marionette.Layout
  _.extend @prototype, AddModelToCollectionMixin

  className: 'add-comment'
  events:
    'click .js-post': 'addComment'

  template: 'comments/add_comment'

  ui:
    submit:  '.js-post'

  regions:
    inputRegion: '.js-input-region'
    shareFactSelectionRegion: '.js-share-fact-selection-region'

  initialize: ->
    @_textAreaComponent = ReactTextArea
      onChange: (text) =>
        @_textModel().set text: text
      onSubmit: =>
        @addComment()
      defaultValue: @_textModel().get('text')
      onFocus: =>
        @$('.add-comment-posting-controls').show()
        @options.onFocus?()
    @_textAreaView = new ReactView
      component: @_textAreaComponent

  focus: -> @_textAreaComponent.focusInput()

  insert: (text) ->
    @_textAreaComponent.insert text

  onRender: ->
    @inputRegion.show @_textAreaView

    if Factlink.Global.signed_in
      @shareFactSelectionRegion.show @_shareFactSelectionView()

  addComment: ->
    return if @submitting

    @model = new Comment
      content: $.trim(@_textModel().get('text'))
      created_by: currentUser.toJSON()
      type: @options.argumentTypeModel.get 'argument_type'

    return @addModelError() unless @model.isValid()

    @disableSubmit()
    @addDefaultModel()
    @_shareFactlink(@model)

  setFormContent: (content) ->
    @_textModel().set 'text', content
    @_textAreaComponent.updateText('')

  addModelSuccess: (model) ->
    @enableSubmit()
    @setFormContent ''

    mp_track "Factlink: Added comment",
      factlink_id: @options.addToCollection.fact.id
      type: @options.argumentTypeModel.get 'argument_type'

  _shareFactlink: (model) ->
    return unless Factlink.Global.signed_in

    @_shareFactSelectionView().submit model.get('content')

  addModelError: ->
    @enableSubmit()
    FactlinkApp.NotificationCenter.error 'Your comment could not be posted, please try again.'

  enableSubmit: ->
    @submitting = false
    @ui.submit.prop('disabled',false).text(Factlink.Global.t.post_argument)

  disableSubmit: ->
    @submitting = true
    @ui.submit.prop('disabled',true ).text('Posting...')

  _textModel: -> @__textModel ?= new Backbone.Factlink.SemiPersistentTextModel {},
    key: "add_comment_to_fact_#{@options.addToCollection.fact.id}"

  _shareFactSelectionView: ->
    @___shareFactSelectionView ?= new ShareFactSelectionView
      model: @options.addToCollection.fact
<commit_msg>Use loading in submit of comment<commit_after>AddModelToCollectionMixin =
  addModel: (model, options) ->
    @options.addToCollection.unshift(model, options)
    model.save {},
      success: =>
        @addModelSuccess(model) if @addModelSuccess?
      error: =>
        @options.addToCollection.remove(model)
        @addModelError(model) if @addModelError?

  addDefaultModel: (options) -> @addModel @model, options

  addWrappedModel: -> @addModel @wrapNewModel(@model)

class window.AddCommentView extends Backbone.Marionette.Layout
  _.extend @prototype, AddModelToCollectionMixin

  className: 'add-comment'
  events:
    'click .js-post': 'addComment'

  template: 'comments/add_comment'

  ui:
    submit:  '.js-post'

  regions:
    inputRegion: '.js-input-region'
    shareFactSelectionRegion: '.js-share-fact-selection-region'

  initialize: ->
    @_textAreaComponent = ReactTextArea
      onChange: (text) =>
        @_textModel().set text: text
      onSubmit: =>
        @addComment()
      defaultValue: @_textModel().get('text')
      onFocus: =>
        @$('.add-comment-posting-controls').show()
        @options.onFocus?()
    @_textAreaView = new ReactView
      component: @_textAreaComponent

  focus: -> @_textAreaComponent.focusInput()

  insert: (text) ->
    @_textAreaComponent.insert text

  onRender: ->
    @inputRegion.show @_textAreaView

    if Factlink.Global.signed_in
      @shareFactSelectionRegion.show @_shareFactSelectionView()

  addComment: ->
    return if @submitting

    @model = new Comment
      content: $.trim(@_textModel().get('text'))
      created_by: currentUser.toJSON()
      type: @options.argumentTypeModel.get 'argument_type'

    return @addModelError() unless @model.isValid()

    @disableSubmit()
    @addDefaultModel()
    @_shareFactlink(@model)

  setFormContent: (content) ->
    @_textModel().set 'text', content
    @_textAreaComponent.updateText('')

  addModelSuccess: (model) ->
    @enableSubmit()
    @setFormContent ''

    mp_track "Factlink: Added comment",
      factlink_id: @options.addToCollection.fact.id
      type: @options.argumentTypeModel.get 'argument_type'

  _shareFactlink: (model) ->
    return unless Factlink.Global.signed_in

    @_shareFactSelectionView().submit model.get('content')

  addModelError: ->
    @enableSubmit()
    FactlinkApp.NotificationCenter.error 'Your comment could not be posted, please try again.'

  enableSubmit: ->
    @submitting = false
    @ui.submit.removeClass 'button-loading'
    @ui.submit.prop('disabled',false).text(Factlink.Global.t.post_argument)

  disableSubmit: ->
    @submitting = true
    @ui.submit.addClass 'button-loading'
    @ui.submit.prop('disabled',true ).text('Posting...')

  _textModel: -> @__textModel ?= new Backbone.Factlink.SemiPersistentTextModel {},
    key: "add_comment_to_fact_#{@options.addToCollection.fact.id}"

  _shareFactSelectionView: ->
    @___shareFactSelectionView ?= new ShareFactSelectionView
      model: @options.addToCollection.fact

{'fraction_non_alphanumeric': 0.12740790807705304, 'fraction_numerical': 0.0, 'mean_word_length': 3.6606299212598423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2272120', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1794, 'n_words': 329}