<commit_before>(function($) {

    var pluginName = "djangoFormset";

    var DjangoFormset = Class.extend({
        opts: {
            emptyClass: 'empty-form grp-empty-form',
            predeleteClass: 'grp-predelete'
        },
        init: function(inline) {
            this.$inline = $(inline);
            this.prefix = this.$inline.djangoFormsetPrefix();
            this._$totalForms = this.$inline.find('#id_' + this.prefix + '-TOTAL_FORMS');
            this._$totalForms.attr('autocomplete', 'off');
            this._$template = $('#' + this.prefix + '-empty');

            var inlineModelClassName = this.$inline.data('inlineModel');

            this.opts = $.extend({}, this.opts, {
                addButtonSelector: '.add-handler.' + inlineModelClassName,
                removeButtonSelector: '.remove-handler.' + inlineModelClassName,
                deleteButtonSelector: '.delete-handler.' + inlineModelClassName,
                formClass: 'dynamic-form-' + inlineModelClassName
            });

            DJNesting.initRelatedFields(this.prefix, this.$inline.data());
            DJNesting.initAutocompleteFields(this.prefix, this.$inline.data());

            this._bindEvents();

            this._initializeForms();

            this.$inline.find('.nested-sortable-container:not([id*="-empty"])').trigger('djnesting:init');

            // initialize nested formsets
            this.$inline.find('.group[id$="-group"][id^="' + this.prefix + '"][data-field-names]:not([id*="-empty"])').each(function() {
                $(this)[pluginName]();
            });

            if (this.$inline.is('.djnesting-stacked-root')) {
                DJNesting.createSortable(this.$inline);
            }

            $(document).trigger('djnesting:initialized', [this.$inline, this]);
        },
        _initializeForms: function() {
            var totalForms = this.mgmtVal('TOTAL_FORMS');
            for (var i = 0; i < totalForms; i++) {
                this._initializeForm('#' + this.prefix + i);
            }
        },
        _initializeForm: function(form) {
            var $form = $(form);
            var formPrefix = $form.djangoFormPrefix();
            $form.addClass(this.opts.formClass);
            if ($form.hasClass('has_original')) {
                $('#id_' + formPrefix + 'DELETE:checked').toggleClass(this.opts.predeleteClass);
            }
        },
        _bindEvents: function($el) {
            var self = this;
            if (typeof $el == 'undefined') {
                $el = this.$inline;
            }
            $el.find(this.opts.addButtonSelector).click(function(e) {
                e.preventDefault();
                e.stopPropagation();
                self.add();
            });
            $el.find(this.opts.removeButtonSelector).click(function(e) {
                e.preventDefault();
                e.stopPropagation();
                var $form = $(this).closest('.' + self.opts.formClass);
                self.remove($form);
            });
            $el.find(this.opts.deleteButtonSelector).click(function(e) {
                e.preventDefault();
                e.stopPropagation();
                var $form = $(this).closest('.' + self.opts.formClass);
                var $deleteInput = $('#id_' + $form.djangoFormPrefix() + 'DELETE');
                if (!$deleteInput.is(':checked')) {
                    self['delete']($form);
                } else {
                    self.undelete($form);
                }
            });
        },
        remove: function(form) {
            var $form = $(form);
            var totalForms = this.mgmtVal('TOTAL_FORMS');
            var maxForms = this.mgmtVal('MAX_NUM_FORMS') || Infinity;
            var index = $form.djangoFormIndex();
            var isInitial = $form.data('isInitial');

            $form.parent().remove();

            this.mgmtVal('TOTAL_FORMS', totalForms - 1);

            if (maxForms - totalForms >= 0) {
                this.$inline.find(this.opts.addButtonSelector).parents('.grp-add-item').show();
            }

            this._fillGap(index, isInitial);

            DJNesting.updatePositions(this.prefix);
            $(document).trigger('djnesting:mutate', [this.$formset]);
        },
        "delete": function(form) {
            var self = this,
                $form = $(form),
                formPrefix = $form.djangoFormPrefix(),
                $deleteInput = $('#id_' + formPrefix + 'DELETE');

            if ($form.hasClass(this.opts.predeleteClass)) {
                return;
            }

            if (!$form.data('isInitial')) {
                return;
            }
            $deleteInput.attr('checked', 'checked');
            $form.addClass(this.opts.predeleteClass);

            $form.find('.djnesting-stacked').each(function() {
                var $childInline = $(this);
                var childFormset = $childInline.djangoFormset();
                $childInline.djangoFormsetForms().each(function() {
                    if ($(this).hasClass(self.opts.predeleteClass)) {
                        $(this).data('alreadyDeleted', true);
                    } else {
                        childFormset.delete(this);
                    }
                });
            });
            DJNesting.updatePositions(this.prefix);
            $(document).trigger('djnesting:mutate', [this.$formset]);
        },
        undelete: function(form) {
            var $form = $(form),
                formPrefix = $form.djangoFormPrefix(),
                $deleteInput = $('#id_' + formPrefix + 'DELETE');

            if ($form.parent().closest('.' + this.opts.predeleteClass).length) {
                return;
            }
            if ($form.hasClass('has_original')) {
                $deleteInput.removeAttr('checked');
                $form.removeClass(this.opts.predeleteClass);
            }
            $form.data('alreadyDeleted', false);
            $form.find('.djnesting-stacked').each(function() {
                var $childInline = $(this);
                var childFormset = $childInline.djangoFormset();
                $childInline.djangoFormsetForms().each(function() {
                    if ($(this).data('alreadyDeleted')) {
                        $(this).data('alreadyDeleted', false);
                    } else {
                        childFormset.undelete(this);
                    }
                });
            });
            DJNesting.updatePositions(this.prefix);
            $(document).trigger('djnesting:mutate', [this.$formset]);
        },
        add: function(spliceIndex) {
            var self = this;
            var $form = this._$template.clone();
            var index = this.mgmtVal('TOTAL_FORMS');
            var maxForms = this.mgmtVal('MAX_NUM_FORMS') || Infinity;
            var isNested = this.$inline.hasClass('djnesting-stacked');

            $form.removeClass(this.opts.emptyClass);
            $form.attr('id', $form.attr('id').replace("-empty", index));

            if (isNested) {
                $form.wrap('<div class="nested-sortable-item"></div>').parent().insertBefore(this._$template.parent());
                $form.parent().append(DJNesting.createContainerElement());
            } else {
                $form.insertBefore(this._$template);
                $form.parent.addClass('nested-sortable-item');
            }

            this.mgmtVal('TOTAL_FORMS', index + 1);
            if (maxForms - index <= 0) {
                this.$inline.find(this.opts.addButtonSelector).parents('.grp-add-item').hide();
            }

            DJNesting.updateFormAttributes($form, /__prefix__/, index);

            DJNesting.updateNestedFormIndex($form, this.prefix);
            DJNesting.updatePositions(this.prefix);
<commit_msg>Pass true to $(template).clone() so that events are copied<commit_after>(function($) {

    var pluginName = "djangoFormset";

    var DjangoFormset = Class.extend({
        opts: {
            emptyClass: 'empty-form grp-empty-form',
            predeleteClass: 'grp-predelete'
        },
        init: function(inline) {
            this.$inline = $(inline);
            this.prefix = this.$inline.djangoFormsetPrefix();
            this._$totalForms = this.$inline.find('#id_' + this.prefix + '-TOTAL_FORMS');
            this._$totalForms.attr('autocomplete', 'off');
            this._$template = $('#' + this.prefix + '-empty');

            var inlineModelClassName = this.$inline.data('inlineModel');

            this.opts = $.extend({}, this.opts, {
                addButtonSelector: '.add-handler.' + inlineModelClassName,
                removeButtonSelector: '.remove-handler.' + inlineModelClassName,
                deleteButtonSelector: '.delete-handler.' + inlineModelClassName,
                formClass: 'dynamic-form-' + inlineModelClassName
            });

            DJNesting.initRelatedFields(this.prefix, this.$inline.data());
            DJNesting.initAutocompleteFields(this.prefix, this.$inline.data());

            this._bindEvents();

            this._initializeForms();

            this.$inline.find('.nested-sortable-container:not([id*="-empty"])').trigger('djnesting:init');

            // initialize nested formsets
            this.$inline.find('.group[id$="-group"][id^="' + this.prefix + '"][data-field-names]:not([id*="-empty"])').each(function() {
                $(this)[pluginName]();
            });

            if (this.$inline.is('.djnesting-stacked-root')) {
                DJNesting.createSortable(this.$inline);
            }

            $(document).trigger('djnesting:initialized', [this.$inline, this]);
        },
        _initializeForms: function() {
            var totalForms = this.mgmtVal('TOTAL_FORMS');
            for (var i = 0; i < totalForms; i++) {
                this._initializeForm('#' + this.prefix + i);
            }
        },
        _initializeForm: function(form) {
            var $form = $(form);
            var formPrefix = $form.djangoFormPrefix();
            $form.addClass(this.opts.formClass);
            if ($form.hasClass('has_original')) {
                $('#id_' + formPrefix + 'DELETE:checked').toggleClass(this.opts.predeleteClass);
            }
        },
        _bindEvents: function($el) {
            var self = this;
            if (typeof $el == 'undefined') {
                $el = this.$inline;
            }
            $el.find(this.opts.addButtonSelector).click(function(e) {
                e.preventDefault();
                e.stopPropagation();
                self.add();
            });
            $el.find(this.opts.removeButtonSelector).click(function(e) {
                e.preventDefault();
                e.stopPropagation();
                var $form = $(this).closest('.' + self.opts.formClass);
                self.remove($form);
            });
            $el.find(this.opts.deleteButtonSelector).click(function(e) {
                e.preventDefault();
                e.stopPropagation();
                var $form = $(this).closest('.' + self.opts.formClass);
                var $deleteInput = $('#id_' + $form.djangoFormPrefix() + 'DELETE');
                if (!$deleteInput.is(':checked')) {
                    self['delete']($form);
                } else {
                    self.undelete($form);
                }
            });
        },
        remove: function(form) {
            var $form = $(form);
            var totalForms = this.mgmtVal('TOTAL_FORMS');
            var maxForms = this.mgmtVal('MAX_NUM_FORMS') || Infinity;
            var index = $form.djangoFormIndex();
            var isInitial = $form.data('isInitial');

            $form.parent().remove();

            this.mgmtVal('TOTAL_FORMS', totalForms - 1);

            if (maxForms - totalForms >= 0) {
                this.$inline.find(this.opts.addButtonSelector).parents('.grp-add-item').show();
            }

            this._fillGap(index, isInitial);

            DJNesting.updatePositions(this.prefix);
            $(document).trigger('djnesting:mutate', [this.$formset]);
        },
        "delete": function(form) {
            var self = this,
                $form = $(form),
                formPrefix = $form.djangoFormPrefix(),
                $deleteInput = $('#id_' + formPrefix + 'DELETE');

            if ($form.hasClass(this.opts.predeleteClass)) {
                return;
            }

            if (!$form.data('isInitial')) {
                return;
            }
            $deleteInput.attr('checked', 'checked');
            $form.addClass(this.opts.predeleteClass);

            $form.find('.djnesting-stacked').each(function() {
                var $childInline = $(this);
                var childFormset = $childInline.djangoFormset();
                $childInline.djangoFormsetForms().each(function() {
                    if ($(this).hasClass(self.opts.predeleteClass)) {
                        $(this).data('alreadyDeleted', true);
                    } else {
                        childFormset.delete(this);
                    }
                });
            });
            DJNesting.updatePositions(this.prefix);
            $(document).trigger('djnesting:mutate', [this.$formset]);
        },
        undelete: function(form) {
            var $form = $(form),
                formPrefix = $form.djangoFormPrefix(),
                $deleteInput = $('#id_' + formPrefix + 'DELETE');

            if ($form.parent().closest('.' + this.opts.predeleteClass).length) {
                return;
            }
            if ($form.hasClass('has_original')) {
                $deleteInput.removeAttr('checked');
                $form.removeClass(this.opts.predeleteClass);
            }
            $form.data('alreadyDeleted', false);
            $form.find('.djnesting-stacked').each(function() {
                var $childInline = $(this);
                var childFormset = $childInline.djangoFormset();
                $childInline.djangoFormsetForms().each(function() {
                    if ($(this).data('alreadyDeleted')) {
                        $(this).data('alreadyDeleted', false);
                    } else {
                        childFormset.undelete(this);
                    }
                });
            });
            DJNesting.updatePositions(this.prefix);
            $(document).trigger('djnesting:mutate', [this.$formset]);
        },
        add: function(spliceIndex) {
            var self = this;
            var $form = this._$template.clone(true);
            var index = this.mgmtVal('TOTAL_FORMS');
            var maxForms = this.mgmtVal('MAX_NUM_FORMS') || Infinity;
            var isNested = this.$inline.hasClass('djnesting-stacked');

            $form.removeClass(this.opts.emptyClass);
            $form.attr('id', $form.attr('id').replace("-empty", index));

            if (isNested) {
                $form.wrap('<div class="nested-sortable-item"></div>').parent().insertBefore(this._$template.parent());
                $form.parent().append(DJNesting.createContainerElement());
            } else {
                $form.insertBefore(this._$template);
                $form.parent.addClass('nested-sortable-item');
            }

            this.mgmtVal('TOTAL_FORMS', index + 1);
            if (maxForms - index <= 0) {
                this.$inline.find(this.opts.addButtonSelector).parents('.grp-add-item').hide();
            }

            DJNesting.updateFormAttributes($form, /__prefix__/, index);

            DJNesting.updateNestedFormIndex($form, this.prefix);
            DJNesting.updatePositions(this.prefix);

{'fraction_non_alphanumeric': 0.1519084457515504, 'fraction_numerical': 0.0006393453104021483, 'mean_word_length': 1.6987577639751552, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 9, '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': '6605380', 'n_tokens_mistral': 4303, 'n_tokens_neox': 4094, 'n_words': 584}