<commit_before>angular.module('loomioApp').factory 'FormService', ($rootScope, FlashService, DraftService, AbilityService, $filter) ->
  new class FormService

    confirmDiscardChanges: (event, record) ->
      if record.isModified() && !confirm($filter('translate')('common.confirm_discard_changes'))
          return event.preventDefault()

    errorTypes =
      400: 'badRequest'
      401: 'unauthorizedRequest'
      403: 'forbiddenRequest'
      404: 'resourceNotFound'
      422: 'unprocessableEntity'
      500: 'internalServerError'

    prepare = (scope, model, options, prepareArgs) ->
      FlashService.loading(options.loadingMessage)
      options.prepareFn(prepareArgs) if typeof options.prepareFn is 'function'
      scope.isDisabled = true
      model.setErrors()

    success = (scope, model, options) ->
      (response) ->
        FlashService.dismiss()
        model.resetDraft() if options.draftFields and AbilityService.isLoggedIn()
        if options.flashSuccess?
          options.flashSuccess = options.flashSuccess() if typeof options.flashSuccess is 'function'
          FlashService.success options.flashSuccess, calculateFlashOptions(options.flashOptions)
        scope.$close()                                          if !options.skipClose? and typeof scope.$close is 'function'
        options.successCallback(response)                       if typeof options.successCallback is 'function'
        $rootScope.$broadcast options.successEvent              if options.successEvent

    failure = (scope, model, options) ->
      (response) ->
        FlashService.dismiss()
        model.setErrors response.data.errors                            if response.status == 422
        $rootScope.$broadcast errorTypes[response.status] or 'unknownError',
          model: model
          response: response

    cleanup = (scope) ->
      ->
        scope.isDisabled = false

    submit: (scope, model, options = {}) ->
      DraftService.applyDrafting(scope, model, options.draftFields) if options.draftFields and AbilityService.isLoggedIn()
      submitFn = options.submitFn or model.save
      (prepareArgs) ->
        prepare(scope, model, options, prepareArgs)
        submitFn(model).then(
          success(scope, model, options),
          failure(scope, model, options),
        ).finally(
          cleanup(scope)
        )

    upload: (scope, model, options = {}) ->
      submitFn = options.submitFn
      (files) ->
        if _.any files
          prepare(scope, model, options)
          submitFn(files[0], options.uploadKind).then(
            success(scope, model, options),
            failure(scope, model, options)
          ).finally(
            cleanup(scope)
          )

    calculateFlashOptions = (options) ->
      _.each _.keys(options), (key) ->
        options[key] = options[key]() if typeof options[key] is 'function'<commit_msg>Don't submit in FormService if scope is disabled (#3294)<commit_after>angular.module('loomioApp').factory 'FormService', ($rootScope, FlashService, DraftService, AbilityService, $filter) ->
  new class FormService

    confirmDiscardChanges: (event, record) ->
      if record.isModified() && !confirm($filter('translate')('common.confirm_discard_changes'))
          return event.preventDefault()

    errorTypes =
      400: 'badRequest'
      401: 'unauthorizedRequest'
      403: 'forbiddenRequest'
      404: 'resourceNotFound'
      422: 'unprocessableEntity'
      500: 'internalServerError'

    prepare = (scope, model, options, prepareArgs) ->
      FlashService.loading(options.loadingMessage)
      options.prepareFn(prepareArgs) if typeof options.prepareFn is 'function'
      scope.isDisabled = true
      model.setErrors()

    success = (scope, model, options) ->
      (response) ->
        FlashService.dismiss()
        model.resetDraft() if options.draftFields and AbilityService.isLoggedIn()
        if options.flashSuccess?
          options.flashSuccess = options.flashSuccess() if typeof options.flashSuccess is 'function'
          FlashService.success options.flashSuccess, calculateFlashOptions(options.flashOptions)
        scope.$close()                                          if !options.skipClose? and typeof scope.$close is 'function'
        options.successCallback(response)                       if typeof options.successCallback is 'function'
        $rootScope.$broadcast options.successEvent              if options.successEvent

    failure = (scope, model, options) ->
      (response) ->
        FlashService.dismiss()
        model.setErrors response.data.errors                            if response.status == 422
        $rootScope.$broadcast errorTypes[response.status] or 'unknownError',
          model: model
          response: response

    cleanup = (scope) ->
      ->
        scope.isDisabled = false

    submit: (scope, model, options = {}) ->
      DraftService.applyDrafting(scope, model, options.draftFields) if options.draftFields and AbilityService.isLoggedIn()
      submitFn = options.submitFn or model.save
      (prepareArgs) ->
        return if scope.isDisabled
        prepare(scope, model, options, prepareArgs)
        submitFn(model).then(
          success(scope, model, options),
          failure(scope, model, options),
        ).finally(
          cleanup(scope)
        )

    upload: (scope, model, options = {}) ->
      submitFn = options.submitFn
      (files) ->
        if _.any files
          prepare(scope, model, options)
          submitFn(files[0], options.uploadKind).then(
            success(scope, model, options),
            failure(scope, model, options)
          ).finally(
            cleanup(scope)
          )

    calculateFlashOptions = (options) ->
      _.each _.keys(options), (key) ->
        options[key] = options[key]() if typeof options[key] is 'function'
{'fraction_non_alphanumeric': 0.10703311710481393, 'fraction_numerical': 0.008193922840559918, 'mean_word_length': 2.485425342058299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887860', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1486, 'n_words': 358}