<commit_before>#
# Copyright (C) 2015 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

define [
  'underscore'
  'Backbone'
  'jst/outcomes/outcomeCalculationMethodForm'
  'jsx/shared/helpers/numberHelper'
], (_, Backbone, template, numberHelper) ->
  class CalculationMethodFormView extends Backbone.View
    @optionProperty 'el'
    @optionProperty 'model'
    @optionProperty 'state'

    template: template

    els:
      '#calculation_int': '$calculation_int'
    events:
      'blur #calculation_int': 'blur'
      'keyup #calculation_int': 'keyup'

    afterRender: ->
      if @hadFocus
        @$calculation_int.focus().val(@$calculation_int.val())
      @hadFocus = undefined

    attach: ->
      @model.on('change:calculation_method', @render)

    blur: (e) ->
      clearTimeout(@timeout) if @timeout
      @change(e)

    change: (e) ->
      val = parseInt(numberHelper.parse($(e.target).val()))
      return if _.isNaN(val)
      @model.set({
        calculation_int: val
      })
      @render()

    keyup: (e) ->
      clearTimeout(@timeout) if @timeout
      @timeout = setTimeout(=>
        @change(e)
      , 500)

    # Three things we want to accomplish with this override:
    # 1 - capture whether or not the calculation int input field has
    #     focus (this will be true if we're rendering after a keyup
    #     event) so we can go back to it after re-render.
    # 2 - undelegateEvents so the re-render doesn't trigger blur if
    #     the calculation int input has focus.
    # 3 - delegateEvents again after render so that we are hooked up
    #     to handle the next round of events.
    render: ->
      @hadFocus = !_.isEmpty(@$calculation_int) and
        document.activeElement is @$calculation_int[0]
      @undelegateEvents()
      super
      @delegateEvents()

    toJSON: ->
      _.extend super, {
        state: @state
        writeStates: ['add', 'edit']
      }<commit_msg>fix focus issue on decaying average input box<commit_after>#
# Copyright (C) 2015 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

define [
  'underscore'
  'Backbone'
  'jst/outcomes/outcomeCalculationMethodForm'
  'jsx/shared/helpers/numberHelper'
], (_, Backbone, template, numberHelper) ->
  class CalculationMethodFormView extends Backbone.View
    @optionProperty 'el'
    @optionProperty 'model'
    @optionProperty 'state'

    template: template

    els:
      '#calculation_int': '$calculation_int'
    events:
      'blur #calculation_int': 'blur'
      'keyup #calculation_int': 'keyup'

    afterRender: ->
      if @hadFocus
        @$calculation_int.focus().val(@$calculation_int.val())
        @$calculation_int[0].selectionStart = @selectionStart
      @hadFocus = undefined

    attach: ->
      @model.on('change:calculation_method', @render)

    blur: (e) ->
      clearTimeout(@timeout) if @timeout
      @change(e)

    change: (e) ->
      val = parseInt(numberHelper.parse($(e.target).val()))
      return if _.isNaN(val)
      @model.set({
        calculation_int: val
      })
      @render()

    keyup: (e) ->
      clearTimeout(@timeout) if @timeout
      @timeout = setTimeout(=>
        @change(e)
      , 500)

    # Three things we want to accomplish with this override:
    # 1 - capture whether or not the calculation int input field has
    #     focus (this will be true if we're rendering after a keyup
    #     event) so we can go back to it after re-render.
    # 2 - undelegateEvents so the re-render doesn't trigger blur if
    #     the calculation int input has focus.
    # 3 - delegateEvents again after render so that we are hooked up
    #     to handle the next round of events.
    render: ->
      @hadFocus = !_.isEmpty(@$calculation_int) and
        document.activeElement is @$calculation_int[0]
      if @hadFocus
        @selectionStart = document.activeElement.selectionStart
      @undelegateEvents()
      super
      @delegateEvents()

    toJSON: ->
      _.extend super, {
        state: @state
        writeStates: ['add', 'edit']
      }
{'fraction_non_alphanumeric': 0.09872307985515533, 'fraction_numerical': 0.00476462740613684, 'mean_word_length': 2.878787878787879, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6540829', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1490, 'n_words': 537}