<commit_before># = require ../files
# = require ./dragdrop
# = require ./template
# = require ./dialog
# = require_self
# = require ./widget
# = require ./multiple-widget

{
  namespace,
  utils,
  settings: s,
  jQuery: $,
  dragdrop,
  locale: {t}
} = uploadcare

namespace 'uploadcare.widget', (ns) ->
  class ns.BaseWidget

    constructor: (element) ->
      @element = $(element)
      @settings = s.build @element.data()

      @__onChange = $.Callbacks()
      @onChange = utils.publicCallbacks @__onChange
      @__initOnUploadComplete()

      @__setupWidget()
      @template.reset()

      @element.on 'change.uploadcare', => @reloadInfo()
      @reloadInfo()

    __setupWidget: ->
      @template = new ns.Template(@settings, @element)

      @template.addButton('cancel', t('buttons.cancel')).on('click', @__reset)
      @template.addButton('remove', t('buttons.remove')).on('click', @__reset)

      # Create the dialog and widget buttons
      if @settings.tabs.length > 0
        if 'file' in @settings.tabs
          fileButton = @template.addButton('file')
          fileButton.on 'click', =>
            @openDialog('file')

        dialogButton = @template.addButton('dialog')
        dialogButton.on 'click', => @openDialog()

      # Enable drag and drop
      dragdrop.receiveDrop(@template.dropArea, @__handleDirectSelection)
      @template.dropArea.on 'dragstatechange.uploadcare', (e, active) =>
        unless active && uploadcare.isDialogOpened()
          @template.dropArea.toggleClass('uploadcare-dragging', active)

      @template.content.on 'click', '@uploadcare-widget-file-name', =>
        @openDialog()

    __infoToValue: (info) ->
      if info.cdnUrlModifiers || @settings.pathValue
        info.cdnUrl
      else
        info.uuid

    __setValue: (value) ->
      if @element.val() != value
        @element.val(value)
        @__onChange.fire @__currentObject()

    __reset: =>
      @__clearCurrentObj()
      @template.reset()
      @__setValue ''

    __watchCurrentObject: ->
      object = @__currentFile()
      if object
        @template.listen object
        object
          .fail (error) =>
            @__onUploadingFailed(error) if object == @__currentFile()
          .done (info) =>
            @__onUploadingDone(info) if object == @__currentFile()

    __onUploadingDone: (info) ->
      @__setValue @__infoToValue(info)
      @template.setFileInfo(info)
      @template.loaded()

    __onUploadingFailed: (error) ->
      @template.error error

    api: ->
      unless @__api
        @__api = utils.bindAll this, [
          'openDialog'
          'reloadInfo'
          'value'
        ]
        @__api.onChange = @onChange
        @__api.onUploadComplete = @onUploadComplete
        @__api.inputElement = @element.get(0)
      @__api
<commit_msg>reloadInfo is fat-arrow function (should works)<commit_after># = require ../files
# = require ./dragdrop
# = require ./template
# = require ./dialog
# = require_self
# = require ./widget
# = require ./multiple-widget

{
  namespace,
  utils,
  settings: s,
  jQuery: $,
  dragdrop,
  locale: {t}
} = uploadcare

namespace 'uploadcare.widget', (ns) ->
  class ns.BaseWidget

    constructor: (element) ->
      @element = $(element)
      @settings = s.build @element.data()

      @__onChange = $.Callbacks()
      @onChange = utils.publicCallbacks @__onChange
      @__initOnUploadComplete()

      @__setupWidget()
      @template.reset()

      @element.on 'change.uploadcare', @reloadInfo
      @reloadInfo()

    __setupWidget: ->
      @template = new ns.Template(@settings, @element)

      @template.addButton('cancel', t('buttons.cancel')).on('click', @__reset)
      @template.addButton('remove', t('buttons.remove')).on('click', @__reset)

      # Create the dialog and widget buttons
      if @settings.tabs.length > 0
        if 'file' in @settings.tabs
          fileButton = @template.addButton('file')
          fileButton.on 'click', =>
            @openDialog('file')

        dialogButton = @template.addButton('dialog')
        dialogButton.on 'click', => @openDialog()

      # Enable drag and drop
      dragdrop.receiveDrop(@template.dropArea, @__handleDirectSelection)
      @template.dropArea.on 'dragstatechange.uploadcare', (e, active) =>
        unless active && uploadcare.isDialogOpened()
          @template.dropArea.toggleClass('uploadcare-dragging', active)

      @template.content.on 'click', '@uploadcare-widget-file-name', =>
        @openDialog()

    __infoToValue: (info) ->
      if info.cdnUrlModifiers || @settings.pathValue
        info.cdnUrl
      else
        info.uuid

    __setValue: (value) ->
      if @element.val() != value
        @element.val(value)
        @__onChange.fire @__currentObject()

    __reset: =>
      @__clearCurrentObj()
      @template.reset()
      @__setValue ''

    __watchCurrentObject: ->
      object = @__currentFile()
      if object
        @template.listen object
        object
          .fail (error) =>
            @__onUploadingFailed(error) if object == @__currentFile()
          .done (info) =>
            @__onUploadingDone(info) if object == @__currentFile()

    __onUploadingDone: (info) ->
      @__setValue @__infoToValue(info)
      @template.setFileInfo(info)
      @template.loaded()

    __onUploadingFailed: (error) ->
      @template.error error

    api: ->
      unless @__api
        @__api = utils.bindAll this, [
          'openDialog'
          'reloadInfo'
          'value'
        ]
        @__api.onChange = @onChange
        @__api.onUploadComplete = @onUploadComplete
        @__api.inputElement = @element.get(0)
      @__api

{'fraction_non_alphanumeric': 0.1358897331684043, 'fraction_numerical': 0.0007068386640749249, 'mean_word_length': 2.493827160493827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, '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': '4739189', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1675, 'n_words': 337}