<commit_before>// Disabling autoDiscover, otherwise Dropzone will try to attach twice.
Dropzone.autoDiscover = false;

$(function() {
    $('.ni-ub').each(function() {
        var $form = $(this);
        var $dropzone = $form.find('.ni-ub-dz');

        // we go read options from HTML data-* attributes
        var options = $dropzone.data();

        // But we force some
        $.extend(options, {
            fallback: null,
            paramName: 'file'
        });

        var dropzone = new Dropzone('#'+$dropzone.attr('id'), options);

        dropzone.on('error', function(file, errorMessage) {
            // Small inconsistency within Dropzone!
            if (typeof errorMessage === "object") {
                errorMessage = errorMessage.error;
            }

            $form.find('.ni-ub-error').html(errorMessage);

            dropzone.removeFile(file);
        });

        dropzone.on('addedfile', function() {
            $form.find('.ni-ub-error').html('');
        });

        /*
            Some handling is different depending whether under we have a single file
            or a collection
         */

        var isCollection = ($form.find('[data-collection]') !== []);
        var successHandler;
        var removeHandler;

        if (isCollection) {
            successHandler = function(file, response) {
                var $collection = $form.find('[data-collection]');
                var prototype = $collection.data('prototype');
                var number = $collection.length;
                var newChild = prototype.replace(/__name__/g, number);

                var $newChild = $(newChild).appendTo($collection);

                $newChild.find('input[data-path]').val(response.path);
                $newChild.find('input[data-temporary]').val(1);

                // Store some file properties we will reuse later
                file.ub = {
                    formId: $newChild.attr('id'),
                    number: response.number,
                    path: response.path,
                    temporary: true
                }
            };

            removeHandler = function(file) {
                // Temporary files must be removed from server sandbox
                if (file.ub.temporary) {
                    $.ajax(options.removeUrl, {
                        data: {
                            path: file.ub.path
                        }
                    });
                }
                // Non temporary will be removed via traditional form workflow

                $form.find('#'+file.ub.formId).remove();
            };
        } else {
            successHandler = function(file, response) {
                $form.find('input[data-path]').val(response.path);

                // Anyway a temporary file was stored
                $form.find('input[data-temporary]').val(1);

                // Store some file properties we will reuse later
                file.ub = {
                    path: response.path,
                    temporary: true
                }
            };

            removeHandler = function(file) {
                // Temporary files must be removed from server sandbox
                if (file.ub.temporary) {
                    $.ajax(options.removeUrl, {
                        data: {
                            path: file.ub.path
                        }
                    });
                }
                // Non temporary will be removed via traditional form workflow

                $form.find('input[data-path]').val('');
                $form.find('input[data-temporary]').val('');
            };
        }

        dropzone.on('success', successHandler);
        dropzone.on('removedfile', removeHandler);
    });
});<commit_msg>Displaying already uploaded files (without thumbnail)<commit_after>// Disabling autoDiscover, otherwise Dropzone will try to attach twice.
Dropzone.autoDiscover = false;

$(function() {
    $('.ni-ub').each(function() {
        var $form = $(this);
        var $dropzone = $form.find('.ni-ub-dz');

        // we go read options from HTML data-* attributes
        var options = $dropzone.data();

        // But we force some
        $.extend(options, {
            fallback: null,
            paramName: 'file'
        });

        var dropzone = new Dropzone('#'+$dropzone.attr('id'), options);

        dropzone.on('error', function(file, errorMessage) {
            // Small inconsistency within Dropzone!
            if (typeof errorMessage === "object") {
                errorMessage = errorMessage.error;
            }

            $form.find('.ni-ub-error').html(errorMessage);

            dropzone.removeFile(file);
        });

        dropzone.on('addedfile', function() {
            $form.find('.ni-ub-error').html('');
        });

        /*
         Some handling is different depending whether under we have a single file
         or a collection
         */

        var $collection = $form.find('[data-collection]');
        var successHandler;
        var removeHandler;

        if ($collection) {
            successHandler = function(file, response) {
                var prototype = $collection.data('prototype');
                var number = $collection.length;
                var newChild = prototype.replace(/__name__/g, number);

                var $newChild = $(newChild).appendTo($collection);

                $newChild.find('input[data-path]').val(response.path);
                $newChild.find('input[data-temporary]').val(1);

                // Store some file properties we will reuse later
                file.ub = {
                    formId: $newChild.attr('id'),
                    number: response.number,
                    path: response.path,
                    temporary: true
                }
            };

            removeHandler = function(file) {
                // Temporary files must be removed from server sandbox
                if (file.ub.temporary) {
                    $.ajax(options.removeUrl, {
                        data: {
                            path: file.ub.path
                        }
                    });
                }
                // Non temporary will be removed via traditional form workflow

                $form.find('#'+file.ub.formId).remove();
            };
        } else {
            successHandler = function(file, response) {
                $form.find('input[data-path]').val(response.path);

                // Anyway a temporary file was stored
                $form.find('input[data-temporary]').val(1);

                // Store some file properties we will reuse later
                file.ub = {
                    path: response.path,
                    temporary: true
                }
            };

            removeHandler = function(file) {
                // Temporary files must be removed from server sandbox
                if (file.ub.temporary) {
                    $.ajax(options.removeUrl, {
                        data: {
                            path: file.ub.path
                        }
                    });
                }
                // Non temporary will be removed via traditional form workflow

                $form.find('input[data-path]').val('');
                $form.find('input[data-temporary]').val('');
            };
        }

        dropzone.on('success', successHandler);
        dropzone.on('removedfile', removeHandler);

        /*
         Dropzone.js provides no way to handle already uploaded files
         but manually!
         */

        var addExistingFile = function(path, thumbnailUrl) {
            var mockFile = {
                name: 'file',
                size: 0,
                ub: {
                    path: path,
                    temporary: false // they were already persisted
                }
            };

            dropzone.emit('addedfile', mockFile);

            dropzone.emit('thumbnail', mockFile, thumbnailUrl);

            dropzone.emit('complete', mockFile);

            // And we even have to update that one
            dropzone.options.maxFiles = dropzone.options.maxFiles--;
        };

        $form.find('.ni-ub-dz-image').each(function() {
            addExistingFile($(this).find('input[data-path]').val(), '');
        });
    });
});
{'fraction_non_alphanumeric': 0.11927045836333093, 'fraction_numerical': 0.0005999520038396928, 'mean_word_length': 1.3465653153153154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6703010', 'n_tokens_mistral': 2106, 'n_tokens_neox': 1957, 'n_words': 465}