<commit_before>/**
* Django formset helper
*/
(function($) {
    "use strict";

    var pluginName = 'formset';

    /**
    * Wraps up a formset, allowing adding, and removing forms
    */
    var Formset = function(el, options) {
        var _this = this;

        //Defaults:
        this.opts = $.extend({}, Formset.defaults, options);

        this.$formset = $(el);
        this.$emptyForm = this.$formset.find(this.opts.emptyForm);
        this.$body = this.$formset.find(this.opts.body);
        this.$add = this.$formset.find(this.opts.add);

        this.formsetPrefix = $(el).data('formset-prefix');

        // Bind to the `Add form` button
        this.addForm = $.proxy(this, 'addForm');
        this.$add.click(this.addForm);

        // Set up the existing forms
        this.$body.find(this.opts.form).each(function(i, form) {
            var $form = $(form);
            _this.bindForm($(this), i);
        });

        // Store a reference to this in the formset element
        this.$formset.data(pluginName, this);
    };

    Formset.defaults = {
        form: '[data-formset-form]',
        emptyForm: 'script[type=form-template][data-formset-empty-form]',
        body: '[data-formset-body]',
        add: '[data-formset-add]',
        deleteButton: '[data-formset-delete-button]'

    };

    Formset.prototype.addForm = function() {
        var newIndex = this.formCount();
        this.formCount(newIndex + 1);

        var newFormHtml = this.$emptyForm.html()
            .replace(new RegExp('__prefix__', 'g'), newIndex)
            .replace(new RegExp('<\\\\/script>', 'g'), '</script>');

        var $newForm = $(newFormHtml);
        this.$body.append($newForm);
        this.bindForm($newForm, newIndex);
        $newForm.slideUp(0);
        $newForm.slideDown();

        return $newForm;
    };

    /**
    * Attach any events needed to a new form
    */
    Formset.prototype.bindForm = function($form, index) {
        var prefix = this.formsetPrefix + '-' + index;
        $form.data(pluginName + '__formPrefix', prefix);

        var $delete = $form.find('[name=' + prefix + '-DELETE]');
        var $deleteButton = $form.find(this.opts.deleteButton);

        if ($deleteButton.length) {
            $deleteButton.bind('click', function() {
                $form.slideUp();
                $delete.attr('checked', true);
            });

            if ($delete.is(':checked')) {
                $form.slideUp(0);
            }
        }
    };

    Formset.prototype.managementForm = function(name) {
        return this.$formset.find('[name=' + this.formsetPrefix + '-' + name + ']');
    };

    Formset.prototype.formCount = function() {
        var $totalForms = this.managementForm('TOTAL_FORMS');

        if (arguments.length) {
            $totalForms.val(arguments[0]);
            return this;
        } else {
            return parseInt($totalForms.val(), 10) || 0;
        }
    };

    Formset.getOrCreate = function(el, options) {
        var rev = $(el).data(pluginName);
        if (!rev) {
            rev = new Formset(el, options);
        }<commit_msg>Updated/renamed 'formCount' method to 'totalFormCount'. This returns real count of form elements, rather than the TOTAL_FORMS value.<commit_after>/**
* Django formset helper
*/
(function($) {
    "use strict";

    var pluginName = 'formset';

    /**
    * Wraps up a formset, allowing adding, and removing forms
    */
    var Formset = function(el, options) {
        var _this = this;

        //Defaults:
        this.opts = $.extend({}, Formset.defaults, options);

        this.$formset = $(el);
        this.$emptyForm = this.$formset.find(this.opts.emptyForm);
        this.$body = this.$formset.find(this.opts.body);
        this.$add = this.$formset.find(this.opts.add);

        this.formsetPrefix = $(el).data('formset-prefix');

        // Bind to the `Add form` button
        this.addForm = $.proxy(this, 'addForm');
        this.$add.click(this.addForm);

        // Set up the existing forms
        this.$body.find(this.opts.form).each(function(i, form) {
            var $form = $(form);
            _this.bindForm($(this), i);
        });

        // Store a reference to this in the formset element
        this.$formset.data(pluginName, this);
    };

    Formset.defaults = {
        form: '[data-formset-form]',
        emptyForm: 'script[type=form-template][data-formset-empty-form]',
        body: '[data-formset-body]',
        add: '[data-formset-add]',
        deleteButton: '[data-formset-delete-button]'

    };

    Formset.prototype.addForm = function() {
        var newIndex = this.totalFormCount();
        this.managementForm('TOTAL_FORMS').val(newIndex + 1);

        var newFormHtml = this.$emptyForm.html()
            .replace(new RegExp('__prefix__', 'g'), newIndex)
            .replace(new RegExp('<\\\\/script>', 'g'), '</script>');

        var $newForm = $(newFormHtml);
        this.$body.append($newForm);
        this.bindForm($newForm, newIndex);
        $newForm.slideUp(0);
        $newForm.slideDown();

        return $newForm;
    };

    /**
    * Attach any events needed to a new form
    */
    Formset.prototype.bindForm = function($form, index) {
        var prefix = this.formsetPrefix + '-' + index;
        $form.data(pluginName + '__formPrefix', prefix);

        var $delete = $form.find('[name=' + prefix + '-DELETE]');
        var $deleteButton = $form.find(this.opts.deleteButton);

        if ($deleteButton.length) {
            $deleteButton.bind('click', function() {
                $form.slideUp();
                $delete.attr('checked', true);
            });

            if ($delete.is(':checked')) {
                $form.slideUp(0);
            }
        }
    };

    Formset.prototype.managementForm = function(name) {
        return this.$formset.find('[name=' + this.formsetPrefix + '-' + name + ']');
    };

    Formset.prototype.totalFormCount = function() {
        return this.$body.find(this.opts.form).length;
    };

    Formset.getOrCreate = function(el, options) {
        var rev = $(el).data(pluginName);
        if (!rev) {
            rev = new Formset(el, options);
        }
{'fraction_non_alphanumeric': 0.16195704828031648, 'fraction_numerical': 0.0016147263038914905, 'mean_word_length': 2.1156941649899395, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2639645', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1847, 'n_words': 369}