<commit_before># Reference jQuery
$ = jQuery

$.awesomeCropper = (inputAttachTo, options) ->
  # Default settings
  settings =
    width: 100
    height: 100
    debug: true

  # Merge default settings with options.
  settings = $.extend settings, options

  # Simple logger.
  log = (msg) ->
    console?.log msg if settings.debug

  # Input
  $inputAttachTo = $(inputAttachTo)

  input = (type) ->
    return $("<input type = \"#{type}\" />")

  div = () ->
    return $("<div/>")

  a = (text) ->
    return $("<a href=\"#\">#{text}</a>")

  image = () ->
    return $('<img/>')

  # Main box
  $container = div().insertAfter($inputAttachTo).addClass('awesome-cropper')

  $cropSandbox = $('<canvas></canvas>')
  $cropSandbox.attr
    width: settings.width
    height: settings.height

  $container.append($cropSandbox)

  # File chooser
  $fileSelect = input('file')
  $container.append(
    div().addClass('control-group')
      .append($fileSelect)
  )

  if (settings.proxy_path != undefined)
    # URL input
    $urlSelect = input('text')
    $urlSelectButton = input('button')
    $urlSelectButton.val('Upload from url')

    $container.append(
      div().addClass('control-group form-inline')
        .append($urlSelect)
        .append($urlSelectButton)
    )

  # Progress bar
  $progressBar = div().addClass('progress progress-striped active hide').append(
    div().addClass('bar').css('width', '100%')
  )
  $container.append($progressBar)

  # Result Image
  $resultIm = image()
  $container.append($resultIm)

  # Modal dialog with cropping
  $sourceIm = image()
  $applyButton = a('Apply').addClass('btn btn-primary')
  $cancelButton = a('Cancel').addClass('btn').attr
    'data-dismiss': "modal"
    'aria-hidden': "true"

  $imagesContainer = div().append(
    div().addClass('modal-body row-fluid').append(
      div().addClass('span9')
        .append($sourceIm)
    ).append(
      div().addClass('span3')
      .append($cropSandbox)
    )
    div().addClass('modal-footer').append($cancelButton).append($applyButton)
  ).append(
  ).addClass('modal hide fade').attr
    role: 'dialog'
  $container.append($imagesContainer)

  # Plugin UI functions
  setLoading = () ->
    $progressBar.removeClass('hide')

  removeLoading = () ->
    $imagesContainer.modal().on('shown', () ->
      setAreaSelect($sourceIm)
    )
    $progressBar.addClass('hide')

  setOriginalSize = (img) ->
    tempImage = new Image()

    tempImage.onload = () ->
      width = tempImage.width
      img.attr
        'data-original-width': tempImage.width
        'data-original-height': tempImage.height

    tempImage.src = img.attr('src')

  setImages = (uri) ->
    $sourceIm.attr('src', uri).load ->
      removeLoading()
      setOriginalSize($sourceIm)

  cleanImages = () ->
    $sourceIm.attr('src', '')

  drawImage = (img, x, y, width, height) ->
    r = img.attr('data-original-width') / img.width()

    sourceX = Math.round(x * r)
    sourceY = Math.round(y * r)
    sourceWidth = Math.round(width * r)
    sourceHeight = Math.round(height * r)
    destX = 0
    destY = 0
    destWidth = settings.width
    destHeight = settings.height

    context = $cropSandbox.get(0).getContext('2d')
    context.drawImage(img.get(0), sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight)

  setAreaSelect = (image) ->
    if (image.width() > image.height())
      y2 = image.height()
      x2 = image.height()
    else
      x2 = image.width()
      y2 = image.width()

    drawImage($sourceIm, 0, 0, x2, y2)

    image.imgAreaSelect
      aspectRatio: '1:1'
      handles: true
      x1: 0
      y1: 0
      x2: x2
      y2: y2
      onSelectEnd: (img, selection) =>
        drawImage($sourceIm, selection.x1, selection.y1, selection.width, selection.height)

  removeAreaSelect = (image) ->
    image.imgAreaSelect
      remove: true

  # Plugin images loading function
  readFile = (file) ->
    reader = new FileReader()

    setLoading()

    reader.onload = (e) ->
      setImages(e.target.result)

    reader.readAsDataURL(file)

  handleDropFileSelect = (evt) ->
    evt.stopPropagation()
    evt.preventDefault()

    readFile(evt.originalEvent.dataTransfer.files[0])

  handleDragOver = (e) ->
    e.originalEvent.dataTransfer.dropEffect = "copy"
    e.stopPropagation()
    e.preventDefault()

  handleFileSelect = (evt) ->
    readFile(evt.target.files[0])

  saveCrop = () ->
    result = $cropSandbox.get(0).toDataURL()
    $resultIm.attr('src', result)
    $inputAttachTo.val(result)
    cleanImages()<commit_msg>Lib updated<commit_after># Reference jQuery
$ = jQuery

$.awesomeCropper = (inputAttachTo, options) ->
  # Default settings
  settings =
    width: 100
    height: 100
    debug: true

  # Merge default settings with options.
  settings = $.extend settings, options

  # Simple logger.
  log = (msg) ->
    console?.log msg if settings.debug

  # Input
  $inputAttachTo = $(inputAttachTo)

  input = (type) ->
    return $("<input type = \"#{type}\" />")

  div = () ->
    return $("<div/>")

  a = (text) ->
    return $("<a href=\"#\">#{text}</a>")

  image = () ->
    return $('<img/>')

  # Main box
  $container = div().insertAfter($inputAttachTo).addClass('awesome-cropper')

  $cropSandbox = $('<canvas></canvas>')
  $cropSandbox.attr
    width: settings.width
    height: settings.height

  $container.append($cropSandbox)

  # File chooser
  $fileSelect = input('file')
  $container.append(
    div().addClass('control-group')
      .append($fileSelect)
  )

  if (settings.proxy_path != undefined)
    # URL input
    $urlSelect = input('text')
    $urlSelectButton = input('button')
    $urlSelectButton.val('Upload from url')

    $container.append(
      div().addClass('control-group form-inline')
        .append($urlSelect)
        .append($urlSelectButton)
    )

  # Progress bar
  $progressBar = div().addClass('progress progress-striped active hide').append(
    div().addClass('bar').css('width', '100%')
  )
  $container.append($progressBar)

  # Result Image
  $resultIm = image()
  $container.append($resultIm)

  # Modal dialog with cropping
  $sourceIm = image()
  $applyButton = a('Apply').addClass('btn btn-primary')
  $cancelButton = a('Cancel').addClass('btn').attr
    'data-dismiss': "modal"
    'aria-hidden': "true"

  $imagesContainer = div().append(
    div().addClass('modal-body row-fluid').append(
      div().addClass('span9')
        .append($sourceIm)
    ).append(
      div().addClass('span3')
      .append($cropSandbox)
    )
    div().addClass('modal-footer').append($cancelButton).append($applyButton)
  ).append(
  ).addClass('modal hide fade').attr
    role: 'dialog'
  $container.append($imagesContainer)

  # Plugin UI functions
  removeAreaSelect = (image) ->
    image.imgAreaSelect
      remove: true

  cleanImages = () ->
    $sourceIm.attr('src', '')

  setLoading = () ->
    $progressBar.removeClass('hide')

  removeLoading = () ->
    $imagesContainer.modal().on('shown', () ->
      setAreaSelect($sourceIm)
    ).on('hidden', () ->
      cleanImages()
      removeAreaSelect($sourceIm)
    )
    $progressBar.addClass('hide')

  setOriginalSize = (img) ->
    tempImage = new Image()

    tempImage.onload = () ->
      width = tempImage.width
      img.attr
        'data-original-width': tempImage.width
        'data-original-height': tempImage.height

    tempImage.src = img.attr('src')

  setImages = (uri) ->
    $sourceIm.attr('src', uri).load ->
      removeLoading()
      setOriginalSize($sourceIm)

  drawImage = (img, x, y, width, height) ->
    oWidth = img.attr('data-original-width')
    oHeight = img.attr('data-original-height')

    console.log(width, height)
    if oWidth > oHeight
      r = img.attr('data-original-height') / img.height()
    else
      r = img.attr('data-original-width') / img.width()

    sourceX = Math.round(x * r)
    sourceY = Math.round(y * r)
    sourceWidth = Math.round(width  * r)
    sourceHeight = Math.round(height * r)
    destX = 0
    destY = 0
    destWidth = settings.width
    destHeight = settings.height

    console.log(sourceWidth, sourceHeight)
    console.log(img.attr('data-original-height'), img.attr('data-original-width'))

    context = $cropSandbox.get(0).getContext('2d')
    context.drawImage(img.get(0), sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight)

  setAreaSelect = (image) ->
    if (image.width() > image.height())
      y2 = image.height()
      x2 = image.height()
    else
      x2 = image.width()
      y2 = image.width()

    drawImage($sourceIm, 0, 0, x2, y2)

    image.imgAreaSelect
      aspectRatio: '1:1'
      handles: true
      x1: 0
      y1: 0
      x2: x2
      y2: y2
      onSelectEnd: (img, selection) =>
        drawImage($sourceIm, selection.x1, selection.y1, selection.width, selection.height)

  # Plugin images loading function
  readFile = (file) ->
    reader = new FileReader()

    setLoading()

    reader.onload = (e) ->
      setImages(e.target.result)

    reader.readAsDataURL(file)

  handleDropFileSelect = (evt) ->
    evt.stopPropagation()
    evt.preventDefault()

    readFile(evt.originalEvent.dataTransfer.files[0])

  handleDragOver = (e) ->
    e.originalEvent.dataTransfer.dropEffect = "copy"
    e.stopPropagation()
    e.preventDefault()

  handleFileSelect = (evt) ->
    readFile(evt.target.files[0])

  saveCrop = () ->
    result = $cropSandbox.get(0).toDataURL()
    $resultIm.attr('src', result)
    $inputAttachTo.val(result)
    cleanImages()
{'fraction_non_alphanumeric': 0.16023489932885907, 'fraction_numerical': 0.008179530201342282, 'mean_word_length': 2.97375, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 69, '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': '3398459', 'n_tokens_mistral': 3178, 'n_tokens_neox': 3077, 'n_words': 596}