<commit_before>_ = require('lodash')
Decimal = require('decimal')
debug = require('debug')("bucket-item")

### @ngInject ###
module.exports = ($timeout) ->
  (scope, element, attrs) ->

    round = scope.round
    bucket = scope.bucket

    scope.model = { }

    Object.defineProperties scope.model,
      myContributionAmount:
        get: ->
          Number(bucket.myContribution.amount.toFixed(2))
        set: (amount) ->
          # if amount is Not a Number (NaN)
          if amount != 0 and not amount
            # yeah nah
            return

          # if amount is negative
          if amount < 0
            # set to zero
            amount = 0

          # save amount to bucket contribution
          myContribution = bucket.myContribution
          myContribution.amount = new Decimal(amount)

          if not myContribution.amount.eq(0) and bucket.myContributionIndex == -1
            bucket.contributions.add(myContribution)

          # if over contributed, cap at allocation
          if round.myAllocationLeft.lt(0)
            myContribution.amount = myContribution.amount.plus(round.myAllocationLeft)

      myContributionPercentageMaxed:
        get: ->
          Math.min(
            Number(bucket.myContributionPercentage.times(100).toString()),
            Number(new Decimal(100).minus(bucket.groupContributionPercentage.times(100)).toString())
          )

      startContributing:
        value: ($event) ->
          debug("startContributing", $event)

          scope.model.initStatus = scope.model.inputStatus
          scope.model.initValue = scope.model.myContributionAmount

          scope.model.inputStatus = "unsaved"
          debug("unsaved")

          # focus onto input after rendering unsaved state
          $timeout ->
            input = element.find('input[type="number"]')

            # HACK for firefox micro-blurring on focus
            scope.model.focusHack = true
            debug("focusing input", input)
            input.focus() if not input.is(':focus')
            scope.model.focusHack = false

            # select input on next tick
            $timeout -> input.select()

      saveContribution:
        value: (myContribution) ->
          debug("saveContribution", myContribution)

          # hack for firefox micro-blurring on startContributing focus
          return if scope.model.focusHack

          # if contribution is empty and new, go back to ready
          if not myContribution.amount.gt(0) and not myContribution.id
            debug("ready")
            scope.model.inputStatus = "ready"
            return

          scope.model.inputStatus = "saving"
          debug("saving")

          scope.saveContribution(myContribution).then ->
            if myContribution.amount.eq(0)
              scope.model.inputStatus = "ready"
              debug("ready")
            else
              scope.model.inputStatus = "saved"
              debug("saved")
          , (err) ->
            scope.model.inputStatus = "error"
            debug("error", err)
            # TODO handle error

      cancelContribution:
        value: ->
          debug("cancelContribution")

          scope.model.inputStatus = scope.model.initStatus
          scope.model.myContributionAmount = scope.model.initValue

      handleKeyDown:
        value: ($event) ->
          debug("handleKeyPress", $event)

          # enter
          if $event.keyCode == 13
            scope.model.saveContribution(bucket.myContribution)
          # escape
          else if $event.keyCode == 27
            scope.model.cancelContribution()
          # backspace
          else if $event.keyCode == 8
            if scope.model.myContributionAmount.toString().length == 1
              scope.model.myContributionAmount = 0

    if bucket.myContribution.amount > 0
      scope.model.inputStatus = "saved"
    else if round.status.name != "open"
      scope.model.inputStatus = "done"
    else
      scope.model.inputStatus = "ready"
<commit_msg>remove contribution from collection when you delete it<commit_after>_ = require('lodash')
Decimal = require('decimal')
debug = require('debug')("bucket-item")

### @ngInject ###
module.exports = ($timeout) ->
  (scope, element, attrs) ->

    round = scope.round
    bucket = scope.bucket

    scope.model = { }

    Object.defineProperties scope.model,
      myContributionAmount:
        get: ->
          Number(bucket.myContribution.amount.toFixed(2))
        set: (amount) ->
          # if amount is Not a Number (NaN)
          if amount != 0 and not amount
            # yeah nah
            return

          # if amount is negative
          if amount < 0
            # set to zero
            amount = 0

          # save amount to bucket contribution
          myContribution = bucket.myContribution
          myContribution.amount = new Decimal(amount)

          if not myContribution.amount.eq(0) and bucket.myContributionIndex == -1
            bucket.contributions.add(myContribution)

          # if over contributed, cap at allocation
          if round.myAllocationLeft.lt(0)
            myContribution.amount = myContribution.amount.plus(round.myAllocationLeft)

      myContributionPercentageMaxed:
        get: ->
          Math.min(
            Number(bucket.myContributionPercentage.times(100).toString()),
            Number(new Decimal(100).minus(bucket.groupContributionPercentage.times(100)).toString())
          )

      startContributing:
        value: ($event) ->
          debug("startContributing", $event)

          scope.model.initStatus = scope.model.inputStatus
          scope.model.initValue = scope.model.myContributionAmount

          scope.model.inputStatus = "unsaved"
          debug("unsaved")

          # focus onto input after rendering unsaved state
          $timeout ->
            input = element.find('input[type="number"]')

            # HACK for firefox micro-blurring on focus
            scope.model.focusHack = true
            debug("focusing input", input)
            input.focus() if not input.is(':focus')
            scope.model.focusHack = false

            # select input on next tick
            $timeout -> input.select()

      saveContribution:
        value: (myContribution) ->
          debug("saveContribution", myContribution)

          # hack for firefox micro-blurring on startContributing focus
          return if scope.model.focusHack

          # if contribution is empty and new, go back to ready
          if not myContribution.amount.gt(0) and not myContribution.id
            debug("ready")
            scope.model.inputStatus = "ready"
            return

          scope.model.inputStatus = "saving"
          debug("saving")

          scope.saveContribution(myContribution).then ->
            if myContribution.amount.eq(0)
              bucket.contributions.remove(myContribution)
              scope.model.inputStatus = "ready"
              debug("ready")
            else
              scope.model.inputStatus = "saved"
              debug("saved")
          , (err) ->
            scope.model.inputStatus = "error"
            debug("error", err)
            # TODO handle error

      cancelContribution:
        value: ->
          debug("cancelContribution")

          scope.model.inputStatus = scope.model.initStatus
          scope.model.myContributionAmount = scope.model.initValue

      handleKeyDown:
        value: ($event) ->
          debug("handleKeyPress", $event)

          # enter
          if $event.keyCode == 13
            scope.model.saveContribution(bucket.myContribution)
          # escape
          else if $event.keyCode == 27
            scope.model.cancelContribution()
          # backspace
          else if $event.keyCode == 8
            if scope.model.myContributionAmount.toString().length == 1
              scope.model.myContributionAmount = 0

    if bucket.myContribution.amount > 0
      scope.model.inputStatus = "saved"
    else if round.status.name != "open"
      scope.model.inputStatus = "done"
    else
      scope.model.inputStatus = "ready"

{'fraction_non_alphanumeric': 0.09763935236682734, 'fraction_numerical': 0.0064268940798418, 'mean_word_length': 1.9265822784810127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '2763155', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2076, 'n_words': 476}