<commit_before>/**
 * Created by jgn on 20/02/2017.
 */
$(function () {

    var switchInputFileToInputText = function (formGroup){
        var inputText = $(formGroup).find('input[type="text"]');
        if (!$(inputText).length) {
            var inputFile = $(formGroup).find('input[type="file"]');
            inputText = $(document.createElement('input')).attr('type','text')
                                                .attr('id',$(inputFile).attr('id'))
                                                .attr('name',$(inputFile).attr('name'))
                                                .attr('class','hidden');
            $(inputFile).attr('id','').attr('name','');
            $(inputText).appendTo($(formGroup));
        }
        return inputText;
    };

    var switchInputTextToInputFile = function (formGroup){
        var inputText = $(formGroup).find('input[type="text"]');
        if ($(inputText).length) {
            $(formGroup).find('input[type="file"]').attr('id',$(inputText).attr('id'))
                                                    .attr('name',$(inputText).attr('name'));
            $(inputText).remove();
        }
    };

    var createOrGetFormGroup = function(mediazone){
        var prototype = $(mediazone).find('[data-prototype]');
        if ($(prototype).length) {
            var total = $(mediazone).find('[data-provider]').length;
            var formGroup = $.parseHTML( $(prototype).data('prototype').replace(/__name__/g, ++total) );
            $(formGroup).attr('data-provider',$(mediazone).data('provider'))
                        .attr('data-thumbnail-height',$(mediazone).data('thumbnail-height'))
                        .attr('data-multi','on')
                        .appendTo($(prototype));
            return $(formGroup);
        } else {
            return $(mediazone);
        }

    };

    var media_image = function(file, reader, formGroup){
        return $(document.createElement('img')).attr('src', reader.result).attr('height', $(formGroup).data('thumbnail-height')).addClass('img-rounded visible-xs-inline-block visible-sm-inline-block visible-md-inline-block visible-lg-inline-block');
    };

    var media_file = function(file, reader, formGroup){
        return file.name;
    };

    var processFile = function (file, formGroup) {
        var reader  = new FileReader();
        reader.addEventListener('loadstart', function(){
            $(formGroup).find('input[id$="originalFilename"]').val(file.name);
            $(formGroup).find('span.media-info').html(
                    media_file(file, reader, formGroup)
                );
            $(formGroup).closest('form').submit(function (evt) {
                evt.preventDefault();
            });
        });
        reader.addEventListener("load", function () {
            if (reader.result) {
                $(formGroup).find('span.media-info').html(
                    eval('media_'+$(formGroup).data('provider')+'(file, reader, formGroup)')
                );
                var inputText = $(formGroup).find('input[type="text"]');
                if ($(inputText).length) $(inputText).val(reader.result);
            }

        }, false);
        reader.addEventListener('loadend'), function()
        {
            $(formGroup).closest('form').submit(function (evt) {
                //
            });
        }
        reader.readAsDataURL(file);
    };

    //on vire le bloc empty de easyadmin
    $('[data-mediazone="on"] > .empty').remove();

    $('[data-mediazone="on"] > span.message')
    .on(
        'dragover dragenter',
        function(e) {
            $(this).addClass('hover');
            e.preventDefault();
            e.stopPropagation();
        }
    ).on(
        'dragleave dragexit',
        function(e) {
            $(this).removeClass('hover');
            e.preventDefault();
            e.stopPropagation();
        }
    )
    .on(
        'click ontouchstart',
        function() {
            var _this = this;
            var formGroup = createOrGetFormGroup($(this).closest('[data-mediazone="on"]'));<commit_msg>better management of FileReader  prolly  one of last commit before refactoring this part<commit_after>/**
 * Created by jgn on 20/02/2017.
 */
$(function () {

    var readerLoading = false;

    var switchInputFileToInputText = function (formGroup){
        var inputText = $(formGroup).find('input[type="text"]');
        if (!$(inputText).length) {
            var inputFile = $(formGroup).find('input[type="file"]');
            inputText = $(document.createElement('input')).attr('type','text')
                                                .attr('id',$(inputFile).attr('id'))
                                                .attr('name',$(inputFile).attr('name'))
                                                .attr('class','hidden');
            $(inputFile).attr('id','').attr('name','');
            $(inputText).appendTo($(formGroup));
        }
        return inputText;
    };

    var switchInputTextToInputFile = function (formGroup){
        var inputText = $(formGroup).find('input[type="text"]');
        if ($(inputText).length) {
            $(formGroup).find('input[type="file"]').attr('id',$(inputText).attr('id'))
                                                    .attr('name',$(inputText).attr('name'));
            $(inputText).remove();
        }
    };

    var createOrGetFormGroup = function(mediazone){
        var prototype = $(mediazone).find('[data-prototype]');
        if ($(prototype).length) {
            var total = $(mediazone).find('[data-provider]').length;
            var formGroup = $.parseHTML( $(prototype).data('prototype').replace(/__name__/g, ++total) );
            $(formGroup).attr('data-provider',$(mediazone).data('provider'))
                        .attr('data-thumbnail-height',$(mediazone).data('thumbnail-height'))
                        .attr('data-multi','on')
                        .appendTo($(prototype));
            return $(formGroup);
        } else {
            return $(mediazone);
        }

    };

    var media_image = function(file, reader, formGroup){
        return $(document.createElement('img')).attr('src', reader.result).attr('height', $(formGroup).data('thumbnail-height')).addClass('img-rounded visible-xs-inline-block visible-sm-inline-block visible-md-inline-block visible-lg-inline-block');
    };

    var media_file = function(file, reader, formGroup){
        return file.name;
    };



    var processFile = function (file, formGroup) {

        var reader  = new FileReader();

        reader.addEventListener('loadstart', function(){
            $(formGroup).find('input[id$="originalFilename"]').val(file.name);
            $(formGroup).find('span.media-info').html(
                    media_file(file, reader, formGroup)
                );
            readerLoading = true;
        });
        reader.addEventListener("loadend", function () {



            if (reader.result) {
                $(formGroup).find('input[id$="originalFilename"]').val(file.name);
                $(formGroup).find('span.media-info').html(
                    eval('media_'+$(formGroup).data('provider')+'(file, reader, formGroup)')
                );
                var inputText = $(formGroup).find('input[type="text"]');
                if ($(inputText).length) {
                    $(inputText).val(reader.result);
                }

            } else {

                var mediaInfo = $(formGroup).find('span.media-info').html('error '+file.name+' too big to be dropped').addClass('text-warning');
                if ($(formGroup).data('multi')) {
                    $(formGroup).html($(mediaInfo));
                }
            }
            readerLoading =  false;

        }, false);


        reader.readAsDataURL(file);
    };

    //on vire le bloc empty de easyadmin
    $('[data-mediazone="on"] > .empty').remove();

    $('[data-mediazone="on"]').closest('form').submit(function (evt) {
            if (readerLoading) evt.preventDefault();
        });


    $('[data-mediazone="on"] > span.message')
    .on(
        'dragover dragenter',
        function(e) {
            $(this).addClass('hover');
            e.preventDefault();
            e.stopPropagation();
        }
    ).on(
        'dragleave dragexit',
        function(e) {
            $(this).removeClass('hover');
            e.preventDefault();
            e.stopPropagation();
        }
    )
    .on(
        'click ontouchstart',
        function() {
            var _this = this;
            var formGroup = createOrGetFormGroup($(this).closest('[data-mediazone="on"]'));
{'fraction_non_alphanumeric': 0.17373714021500405, 'fraction_numerical': 0.0018494971679574616, 'mean_word_length': 1.9289099526066351, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1525783', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2253, 'n_words': 348}