<commit_before>/* This file is part of Indico.
 * Copyright (C) 2002 - 2018 European Organization for Nuclear Research (CERN).
 *
 * Indico is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Indico is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Indico; if not, see <http://www.gnu.org/licenses/>.
 */

/* eslint no-shadow: ['warn', {allow: ['file']}] */

import Dropzone from 'dropzone';
import 'dropzone/dist/dropzone.css';

(function(global) {
    global.Dropzone = Dropzone;

    $(document).ready(function() {
        Dropzone.autoDiscover = false;
    });

    global.setupDropzone = function(element) {
        var $dz = $(element);
        var $form = $dz.closest('form');
        var $flashArea = $form.find('.flashed-messages');
        var options = {
            clickable: element + ' .dropzone-inner',
            previewsContainer: element + ' .dropzone-previews',
            autoProcessQueue: false,

            init: function() {
                var $button = $form.find('input[type="submit"], .js-dropzone-upload');
                var self = this;
                var files = $dz.data('value');

                function addFile(file) {
                    var existingFile = {
                        name: file.filename,
                        id: file.id,
                        size: file.size,
                        accepted: true
                    };

                    self.emit('addedfile', existingFile);
                    $dz.find('.dz-progress').hide();
                    self.files.push(existingFile);

                    /* Disable opening the upload dialog when clicking on the file's preview element */
                    $dz.find('.dz-preview').on('click', function(e) {
                        e.stopPropagation();
                    });

                    // Check if the existing file is an image and add the image preview.
                    if (file.content_type.match(/image\/.*/) && file.url) {
                        $dz.find('.dz-image > img').attr('src', file.url);
                        $dz.find('.dz-image > img').attr('width', '120px');
                        $dz.find('.dz-preview').removeClass('dz-file-preview').addClass('dz-image-preview');
                    }
                }

                if (files) {
                    if (options.uploadMultiple) {
                        files.forEach(addFile);
                    } else {
                        addFile(files);
                    }
                }

                // We'll first remove the default 'submit' handler
                $form.off('submit').on('submit', function(e) {
                    var evt = $.Event('ajaxForm:validateBeforeSubmit');
                    $(this).trigger(evt);
                    if (!evt.isDefaultPrevented()) {
                        $button.prop('disabled', true);
                        $.each(self.getRejectedFiles(), function(index, file) {
                            self.removeFile(file);
                        });
                        if (self.getQueuedFiles().length) {
                            e.preventDefault();
                            e.stopPropagation();
                            $dz.find('.dz-progress').show();
                            self.processQueue();
                        }
                    }
                });

                self.on('addedfile', function(file) {
                    if (!options.uploadMultiple && self.files.length > 1) {
                        self.removeFile(self.files[0]);
                    }

                    // force change in form, so that we can process
                    // the 'change' event
                    $button.prop('disabled', false);
                    $form.find('.change-trigger').val('added-file');
                    $form.trigger('change');

                    $dz.find('.dz-message').hide();
                    $dz.find('.dz-progress').hide();
                    $(file.previewElement).on('click', function(e) {
                        e.stopPropagation();
                    });
                });

                self.on('removedfile', function(file) {
                    $button.prop('disabled', false);
                    if (self.files.length === 0) {
                        // force change in form, so that we can process the 'change' event
                        $form.find('.change-trigger').val('no-file');
                        $form.trigger('change');
                        $dz.find('.dz-message').show();
                    }
<commit_msg>Fix abstract submission when attachments enabled<commit_after>/* This file is part of Indico.
 * Copyright (C) 2002 - 2018 European Organization for Nuclear Research (CERN).
 *
 * Indico is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Indico is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Indico; if not, see <http://www.gnu.org/licenses/>.
 */

/* eslint no-shadow: ['warn', {allow: ['file']}] */

import Dropzone from 'dropzone';
import 'dropzone/dist/dropzone.css';

(function(global) {
    global.Dropzone = Dropzone;

    $(document).ready(function() {
        Dropzone.autoDiscover = false;
    });

    // Indicates whether default 'submit' handle was removed
    let handlerRemoved = false;

    global.setupDropzone = function(element) {
        var $dz = $(element);
        var $form = $dz.closest('form');
        var $flashArea = $form.find('.flashed-messages');
        var options = {
            clickable: element + ' .dropzone-inner',
            previewsContainer: element + ' .dropzone-previews',
            autoProcessQueue: false,

            init: function() {
                var $button = $form.find('input[type="submit"], .js-dropzone-upload');
                var self = this;
                var files = $dz.data('value');

                function addFile(file) {
                    var existingFile = {
                        name: file.filename,
                        id: file.id,
                        size: file.size,
                        accepted: true
                    };

                    self.emit('addedfile', existingFile);
                    $dz.find('.dz-progress').hide();
                    self.files.push(existingFile);

                    /* Disable opening the upload dialog when clicking on the file's preview element */
                    $dz.find('.dz-preview').on('click', function(e) {
                        e.stopPropagation();
                    });

                    // Check if the existing file is an image and add the image preview.
                    if (file.content_type.match(/image\/.*/) && file.url) {
                        $dz.find('.dz-image > img').attr('src', file.url);
                        $dz.find('.dz-image > img').attr('width', '120px');
                        $dz.find('.dz-preview').removeClass('dz-file-preview').addClass('dz-image-preview');
                    }
                }

                if (files) {
                    if (options.uploadMultiple) {
                        files.forEach(addFile);
                    } else {
                        addFile(files);
                    }
                }

                $form.on('ajaxForm:validateBeforeSubmit', (e) => {
                    if (self.files.length && !handlerRemoved) {
                         e.preventDefault();
                         handlerRemoved = true;
                    }
                });

                $form.on('submit', function(e) {
                    var evt = $.Event('ajaxForm:validateBeforeSubmit');
                    $(this).trigger(evt);
                    if (!evt.isDefaultPrevented()) {
                        $button.prop('disabled', true);
                        $.each(self.getRejectedFiles(), function(index, file) {
                            self.removeFile(file);
                        });
                        if (self.getQueuedFiles().length) {
                            e.preventDefault();
                            e.stopPropagation();
                            $dz.find('.dz-progress').show();
                            self.processQueue();
                        }
                    } else {
                        handlerRemoved = false;
                    }
                });

                self.on('addedfile', function(file) {
                    if (!options.uploadMultiple && self.files.length > 1) {
                        self.removeFile(self.files[0]);
                    }

                    // force change in form, so that we can process
                    // the 'change' event
                    $button.prop('disabled', false);
                    $form.find('.change-trigger').val('added-file');
                    $form.trigger('change');

                    $dz.find('.dz-message').hide();
                    $dz.find('.dz-progress').hide();
                    $(file.previewElement).on('click', function(e) {
                        e.stopPropagation();
                    });
                });

                self.on('removedfile', function(file) {
                    $button.prop('disabled', false);
                    if (self.files.length === 0) {
                        // force change in form, so that we can process the 'change' event
                        $form.find('.change-trigger').val('no-file');
                        $form.trigger('change');
                        $dz.find('.dz-message').show();
                    }

{'fraction_non_alphanumeric': 0.1224803225187176, 'fraction_numerical': 0.002879631407179881, 'mean_word_length': 1.3040689960194605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2970405', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2389, 'n_words': 621}