<commit_before>/* global $, _ */

var PhotoUpload = (function (){
  'use strict';

  var PhotoUpload = {

    defaultImage:   "<%= asset_path('medium_no_photo.png') %>",
    imageSizeLimit: 10.0,

    enhance: function ( el ){
      var els = PhotoUpload.findElements(el);

      els.$input.change(PhotoUpload.photoSelected);
      els.$uploadButton.click(PhotoUpload.uploadPhoto);
      els.$cropButton.click(PhotoUpload.cropDone);
      els.$cropAgainButton.click(PhotoUpload.cropAgain);
      els.$resetButton.click(PhotoUpload.resetImage);
    },

    photoSelected: function ( event ){
      var els = PhotoUpload.findElements(event.target);
      PhotoUpload.clearError(els);
      PhotoUpload.showUploadButton(els, false);
      PhotoUpload.checkFileSize(this, PhotoUpload.imageSizeLimit, els);

      $('.save-cancel-actions input').hide();
    },

    checkFileSize: function ( fileInput, limit, els ) {
      // let backend validator handle files between 3 and 10MB for more consistent error handling.
      var el = fileInput;
      var fileSize = PhotoUpload.fileSize(el)
      if (fileSize > limit) {
        PhotoUpload.showError(els, 'File ' + el.files[0].name + ' is too large for upload (' + fileSize.toFixed(2) + 'MB)');
        els.$input.val('');
      } else {
        PhotoUpload.showUploadButton(els, true);
      }
    },

    fileSize: function ( el ) {
      // return 0 if File API (IE 11+) not available
      // NOTE: server will throw 413 for POST exceeding 20MB
      var fileSize = 0;
      if (window.FileReader) {
        if (el.files.length > 0) {
          fileSize = el.files[0].size / 1048576;
        }
      }
      return fileSize;
    },

    showUploadButton: function (els, show){
      var show = typeof show !== 'undefined' ? show : true;
      if (show) {
        els.$uploadButton.removeClass('hidden');
      } else {
        els.$uploadButton.addClass('hidden');
      }
    },

    uploadPhoto: function ( event ){
      event.preventDefault();
      var els = PhotoUpload.findElements(event.target);

      var iframeId = 'photo-upload-iframe-' + ~new Date();
      var iframe = $('<iframe id="' + iframeId + '" name="' + iframeId + '"></iframe>');
      iframe.hide();
      $(els.$el).append(iframe);
      iframe.load(PhotoUpload.photoUploaded);

      var form = $('<form target="' +
        iframeId +
        '" method="POST" action="/profile_photos" ' +
        'enctype="multipart/form-data"></form>');

      form.append(els.$input);
      form.append($('input[name="authenticity_token"]').clone());
      $(els.$el).append(form);
      PhotoUpload.setState(els, 'uploading');
      form.submit();
      els.$label.after(els.$input);
      form.remove();

      if( els.$preview.data('jcrop') ) {
        els.$preview.data('jcrop').destroy();
      }
    },

    photoUploaded: function ( event ){
      var $iframe = $(event.target);
      var jsonText = $iframe.contents().text();
      if(jsonText.length>1){
        var uploadData = $.parseJSON(jsonText);
        var els = PhotoUpload.findElements($iframe);

        if (uploadData.error) {
          // e.g. carrierwave whitelist errors
          PhotoUpload.showError(els, uploadData.error);
          PhotoUpload.setState(els, 'cropped');
        } else {
          els.$preview.attr('src', uploadData.image.croppable.url);
          els.$preview.css({clip: ''});
          PhotoUpload.setState(els, 'cropping');
          setTimeout(_.bind(function (){
            PhotoUpload.setupCrop(els);
          }, this), 1000);

          els.$photo_id.data('old-id', els.$photo_id.val());
          els.$photo_id.val(uploadData.id);
        }

        els.$input.val('');
        PhotoUpload.showUploadButton(els, false);
        $iframe.remove();
      }
    },

    showError: function ( els, error_messages ){
      els.$uploadError.removeClass('hidden');
      els.$uploadError.text(error_messages);
    },

    clearError: function ( els ) {
      els.$uploadError.addClass('hidden');
    },

    preview: function ( event ){
      if( !window.FileReader ) {
        return false;
      }

      var els = PhotoUpload.findElements(event.target);
      var file = els.$input[0].files[0];
      var reader = new FileReader();

      reader.onloadend = _.bind(function (){
        els.$preview[0].src = reader.result;
      }, this);

      reader.readAsDataURL(file);
    },

    togglePreviewUploading: function ( $preview, on ){
      var $parent = $preview.closest('.maginot');

      on = on || !$parent.hasClass('upload-in-progress');
      $parent[on ? 'addClass' : 'removeClass']('upload-in-progress');
    },

    setupCrop: function ( els ){
      var imgw = els.$preview.width();
      var trueSize = [
        els.$preview.naturalWidth(),
        els.$preview.naturalHeight()
      ];

      console.log(imgw, trueSize);

      els.$preview.Jcrop({
        setSelect: [0,0,trueSize[0],trueSize[0]],
        trueSize: trueSize,
        onSelect: function ( cropData ){
          els.$preview.data('new-crop-data', cropData);
        },
        aspectRatio: 1
      }, function (){
        els.$preview.data('jcrop', this);
      });
    },
<commit_msg>removed debug<commit_after>/* global $, _ */

var PhotoUpload = (function (){
  'use strict';

  var PhotoUpload = {

    defaultImage:   "<%= asset_path('medium_no_photo.png') %>",
    imageSizeLimit: 10.0,

    enhance: function ( el ){
      var els = PhotoUpload.findElements(el);

      els.$input.change(PhotoUpload.photoSelected);
      els.$uploadButton.click(PhotoUpload.uploadPhoto);
      els.$cropButton.click(PhotoUpload.cropDone);
      els.$cropAgainButton.click(PhotoUpload.cropAgain);
      els.$resetButton.click(PhotoUpload.resetImage);
    },

    photoSelected: function ( event ){
      var els = PhotoUpload.findElements(event.target);
      PhotoUpload.clearError(els);
      PhotoUpload.showUploadButton(els, false);
      PhotoUpload.checkFileSize(this, PhotoUpload.imageSizeLimit, els);

      $('.save-cancel-actions input').hide();
    },

    checkFileSize: function ( fileInput, limit, els ) {
      // let backend validator handle files between 3 and 10MB for more consistent error handling.
      var el = fileInput;
      var fileSize = PhotoUpload.fileSize(el)
      if (fileSize > limit) {
        PhotoUpload.showError(els, 'File ' + el.files[0].name + ' is too large for upload (' + fileSize.toFixed(2) + 'MB)');
        els.$input.val('');
      } else {
        PhotoUpload.showUploadButton(els, true);
      }
    },

    fileSize: function ( el ) {
      // return 0 if File API (IE 11+) not available
      // NOTE: server will throw 413 for POST exceeding 20MB
      var fileSize = 0;
      if (window.FileReader) {
        if (el.files.length > 0) {
          fileSize = el.files[0].size / 1048576;
        }
      }
      return fileSize;
    },

    showUploadButton: function (els, show){
      var show = typeof show !== 'undefined' ? show : true;
      if (show) {
        els.$uploadButton.removeClass('hidden');
      } else {
        els.$uploadButton.addClass('hidden');
      }
    },

    uploadPhoto: function ( event ){
      event.preventDefault();
      var els = PhotoUpload.findElements(event.target);

      var iframeId = 'photo-upload-iframe-' + ~new Date();
      var iframe = $('<iframe id="' + iframeId + '" name="' + iframeId + '"></iframe>');
      iframe.hide();
      $(els.$el).append(iframe);
      iframe.load(PhotoUpload.photoUploaded);

      var form = $('<form target="' +
        iframeId +
        '" method="POST" action="/profile_photos" ' +
        'enctype="multipart/form-data"></form>');

      form.append(els.$input);
      form.append($('input[name="authenticity_token"]').clone());
      $(els.$el).append(form);
      PhotoUpload.setState(els, 'uploading');
      form.submit();
      els.$label.after(els.$input);
      form.remove();

      if( els.$preview.data('jcrop') ) {
        els.$preview.data('jcrop').destroy();
      }
    },

    photoUploaded: function ( event ){
      var $iframe = $(event.target);
      var jsonText = $iframe.contents().text();
      if(jsonText.length>1){
        var uploadData = $.parseJSON(jsonText);
        var els = PhotoUpload.findElements($iframe);

        if (uploadData.error) {
          // e.g. carrierwave whitelist errors
          PhotoUpload.showError(els, uploadData.error);
          PhotoUpload.setState(els, 'cropped');
        } else {
          els.$preview.attr('src', uploadData.image.croppable.url);
          els.$preview.css({clip: ''});
          PhotoUpload.setState(els, 'cropping');
          setTimeout(_.bind(function (){
            PhotoUpload.setupCrop(els);
          }, this), 1000);

          els.$photo_id.data('old-id', els.$photo_id.val());
          els.$photo_id.val(uploadData.id);
        }

        els.$input.val('');
        PhotoUpload.showUploadButton(els, false);
        $iframe.remove();
      }
    },

    showError: function ( els, error_messages ){
      els.$uploadError.removeClass('hidden');
      els.$uploadError.text(error_messages);
    },

    clearError: function ( els ) {
      els.$uploadError.addClass('hidden');
    },

    preview: function ( event ){
      if( !window.FileReader ) {
        return false;
      }

      var els = PhotoUpload.findElements(event.target);
      var file = els.$input[0].files[0];
      var reader = new FileReader();

      reader.onloadend = _.bind(function (){
        els.$preview[0].src = reader.result;
      }, this);

      reader.readAsDataURL(file);
    },

    togglePreviewUploading: function ( $preview, on ){
      var $parent = $preview.closest('.maginot');

      on = on || !$parent.hasClass('upload-in-progress');
      $parent[on ? 'addClass' : 'removeClass']('upload-in-progress');
    },

    setupCrop: function ( els ){
      var imgw = els.$preview.width();
      var trueSize = [
        els.$preview.naturalWidth(),
        els.$preview.naturalHeight()
      ];

      els.$preview.Jcrop({
        setSelect: [0,0,trueSize[0],trueSize[0]],
        trueSize: trueSize,
        onSelect: function ( cropData ){
          els.$preview.data('new-crop-data', cropData);
        },
        aspectRatio: 1
      }, function (){
        els.$preview.data('jcrop', this);
      });
    },

{'fraction_non_alphanumeric': 0.1568798449612403, 'fraction_numerical': 0.007558139534883721, 'mean_word_length': 2.435752330226365, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '4935933', 'n_tokens_mistral': 3209, 'n_tokens_neox': 3084, 'n_words': 575}