<commit_before>class window.Neat.ModelEditor extends Backbone.View
  tagName: 'li'
  className: 'neat-row neat-interactive neat-editable'
  
  initialize: (options)->
    options = options ? {}
    @templateOptions = options.templateOptions ? {}
    @viewPath = @viewPath ? options.viewPath
    @resource = @resource ? window.inflect.singularize(options.resource)
    @$el.addClass(@resource)
    
    @model.bind 'change', @render, @
    
    # Renders the 'show' template normally,
    # renders 'edit' when in edit mode.
    @showTemplate = Neat.template["#{@viewPath}/show"]
    @editTemplate = Neat.template["#{@viewPath}/edit"]
    
    # Wire up events.
    # Don't use Backbone's events hash because if subclasses
    # use that more familiar syntax, they'll blow away events
    # defined in this class.
    @$el.delegate('.save-button',   'click', _.bind(@save, @))
    @$el.delegate('.delete-button', 'click', _.bind(@delete, @))
    @$el.delegate('.cancel-button', 'click', _.bind(@cancelEdit, @))
    
    # Begin editing when this resource is clicked
    # unless the user clicked a link or button.
    @$el.click (e)=>
      @edit() if @canEdit() and !$(e.target).isIn('input, button, a, label')
  
  render: ->
    json = _.extend(@model.toJSON(), {options: @templateOptions})
    @$el.html @template()(json)
    @$el.attr('id', "#{@resource}_#{@model.get('id')}") # e.g. "calendar_5"
    
    @afterRender()
    @
  
  afterRender: ->
    @
  
  inEdit: -> @$el.hasClass('editor')
  canEdit: -> @$el.hasClass('neat-editable') and !@inEdit()
  template: -> if @inEdit() then @editTemplate else @showTemplate
  
  cancelEdit: (e)->
    e?.preventDefault()
    e?.stopImmediatePropagation()
    if @inEdit()
      @$el.removeClass('editor').addClass('neat-editable')
      @render()
      @trigger('edit:end')
    @
  
  edit: ->
    unless @inEdit()
      @$el.addClass('editor').removeClass('neat-editable hovered')
      @trigger('edit:begin')
      @render()
      @autofocus()
    @
  
  autofocus: ->
    @$el.find(':input:visible').first().focus()
  
  save: (e)->
    e?.preventDefault()
    newAttributes = @attributesFromForm(@$el)
    @debug 'saving: ', newAttributes
    attributes =  @model.changedAttributes(newAttributes)
    
    return unless @okToSave(attributes)
    
    if attributes
      previousAttributes = @model.toJSON()
      
      @model.save attributes,
        wait: true
        success: =>
          for attribute, newValue of attributes<commit_msg>[skip] Blurred any focused input element before replacing the edit mode with the view mode (3m)<commit_after>class window.Neat.ModelEditor extends Backbone.View
  tagName: 'li'
  className: 'neat-row neat-interactive neat-editable'
  
  initialize: (options)->
    options = options ? {}
    @templateOptions = options.templateOptions ? {}
    @viewPath = @viewPath ? options.viewPath
    @resource = @resource ? window.inflect.singularize(options.resource)
    @$el.addClass(@resource)
    
    @model.bind 'change', @render, @
    
    # Renders the 'show' template normally,
    # renders 'edit' when in edit mode.
    @showTemplate = Neat.template["#{@viewPath}/show"]
    @editTemplate = Neat.template["#{@viewPath}/edit"]
    
    # Wire up events.
    # Don't use Backbone's events hash because if subclasses
    # use that more familiar syntax, they'll blow away events
    # defined in this class.
    @$el.delegate('.save-button',   'click', _.bind(@save, @))
    @$el.delegate('.delete-button', 'click', _.bind(@delete, @))
    @$el.delegate('.cancel-button', 'click', _.bind(@cancelEdit, @))
    
    # Begin editing when this resource is clicked
    # unless the user clicked a link or button.
    @$el.click (e)=>
      @edit() if @canEdit() and !$(e.target).isIn('input, button, a, label')
  
  render: ->
    json = _.extend(@model.toJSON(), {options: @templateOptions})
    @$el.html @template()(json)
    @$el.attr('id', "#{@resource}_#{@model.get('id')}") # e.g. "calendar_5"
    
    @afterRender()
    @
  
  afterRender: ->
    @
  
  inEdit: -> @$el.hasClass('editor')
  canEdit: -> @$el.hasClass('neat-editable') and !@inEdit()
  template: -> if @inEdit() then @editTemplate else @showTemplate
  
  cancelEdit: (e)->
    e?.preventDefault()
    e?.stopImmediatePropagation()
    if @inEdit()
      @$el.find(':focus').blur()
      @$el.removeClass('editor').addClass('neat-editable')
      @render()
      @trigger('edit:end')
    @
  
  edit: ->
    unless @inEdit()
      @$el.addClass('editor').removeClass('neat-editable hovered')
      @trigger('edit:begin')
      @render()
      @autofocus()
    @
  
  autofocus: ->
    @$el.find(':input:visible').first().focus()
  
  save: (e)->
    e?.preventDefault()
    newAttributes = @attributesFromForm(@$el)
    @debug 'saving: ', newAttributes
    attributes =  @model.changedAttributes(newAttributes)
    
    return unless @okToSave(attributes)
    
    if attributes
      previousAttributes = @model.toJSON()
      
      @model.save attributes,
        wait: true
        success: =>
          for attribute, newValue of attributes
{'fraction_non_alphanumeric': 0.1630456057936974, 'fraction_numerical': 0.0005871990604815032, 'mean_word_length': 3.0523394131641552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '249325', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1632, 'n_words': 357}