<commit_before>/*!
 * jQuery Mobile Editable Listview Plugin
 * https://github.com/baig/jquerymobile-editablelistview
 *
 * Copyright 2014 (c) <NAME>
 *
 * Released under the MIT license
 * https://github.com/baig/jquerymobile-editablelistview/blob/master/LICENSE.txt
 */

(function ($, undefined) {

    "use strict";

    $.widget("mobile.listview", $.mobile.listview, {

        // Declaring some private instace state variables
        _created: false,
        _origDom: null,
        _editMode: false,
        _counter: 1,
        _dataItemName: "item",
        _itemNames: [],
        _evt: null,
        _clickHandler: null,

        // The options hash
        options: {
            editable: false,
            editableType: 'simple',
            editableForm: '',

            listTitle: "View list items",
            listEmptyTitle: "No items to view",
            editLabel: "Edit",
            addLabel: "Add",
            doneLabel: "Done",
            addIcon: "plus",
            editIcon: "edit",
            doneIcon: "check",

            buttonTheme: 'a',
            buttonCorner: true,
            buttonShadow: true,
        },

        _beforeListviewRefresh: function () {
            
            // Returning immediately if `data-editable="false"`
            if (!this.options.editable) return;
            
            var $el = this.element,
                opts = this.options,
                $origDom = this._origDom,
                dataItemName = this._dataItemName,
                counter = this._counter,
                ui, $orig, $origLis,
                evt = this._evt,
                $lis = $el.find("li"),
                $markup = this._$markup;

            // saving original DOM structure
            if ($origDom === null) {
                $origDom = $el.clone();
                // Assign each list item a unique number value
                $.each($origDom.children('li'), function (idx, val) {
                    $(val).attr("data-" + dataItemName, counter);
                    counter++;
                });

                // Incrementing the counter that is used to assign unique value to `data-item` attribute on each list item
                this._counter = counter;

                // Removing all css classes to get the original DOM structure
                $origDom.removeClass("ui-listview ui-shadow ui-corner-all ui-listview-inset ui-group-theme-" + this.options.theme)
                    .find("li")
                    .removeClass("ui-li-static ui-body-inherit ui-first-child ui-last-child")
                    .end()
                    .find("a")
                    .removeClass("ui-link")
                    .end();

                // Caching the original DOM to the widget instance
                this._origDom = $origDom;
            }

            // ## Creation
            if (!this._created) {
                // Wrapping the list structure inside Collapsible
                var wrapper = this._wrapCollapsible();

                ui = {
                    wrapper: wrapper,
                    header: wrapper.find('h1'),
                    button: wrapper.find('h1 + a, h1 + button'),
                    content: wrapper.find('div.ui-collapsible-content'),
                };

                if (this.options.editableType === 'complex') {
//                    ui.form = wrapper.closest(':jqmData(role="page")').find('#' + this.options.editableForm).detach();
                    var inputs = ui.content.find('li:first-child').find('[data-item-name]');

                    var itemNames = this._itemNames;
                    $.each(inputs, function (idx, val) {
                        itemNames.push($(val).data("item-name"));
                    });
                }

                $.extend(this, {
                    _ui: ui
                });

                ui.header.addClass('ui-btn-icon-left');

                evt = this._evt = $._data(ui.header.parent()[0], "events");
                this._clickHandler = evt.click[0].handler;

                this._attachEditEventButtons();

                this._created = true;
            }

            if (this._editMode) {
                ui = this._ui;
                $orig = $origDom.clone();
                $origLis = $orig.find('li');

                if ($orig.find('a').length === 0) {
                    $origLis.wrapInner('<a></a>').append('<a class="ui-editable-btn-del">Delete</a>');
                } else {
                    $origLis.append('<a class="ui-editable-btn-del">Delete</a>');
                }

                this.option("splitIcon", "minus");

                if (opts.editableType === 'complex') {
                    $orig.prepend($('<li>' + ui.form[0].innerHTML + '</li>'));
                }
                if (opts.editableType === 'simple') {
                    $orig.prepend($markup.listTextInput);
                }

                $lis.remove();
                $el.append($orig.find('li'));

                // Disabling the click event on header when list is in `Edit` mode
                evt.click[0].handler = $.noop;
            } else {
                // Re-enabling the click event handler when the list is in `View` mode
                evt.click[0].handler = this._clickHandler;
                $lis.remove().end().append($origDom.clone().find('li'));
            }

            // Updating the header title, header button label and icon based on the list contents and its state (`Edit` or `View`)
            this._updateHeader();
        },

        _afterListviewRefresh: function () {
            
            // Returning immediately if `data-editable="false"`
            if (!this.options.editable) return;
            
            this._attachDetachEventHandlers();
        },

        // Detaching form from the DOM if the listview is initialized programmatically
        _init: function() {
            
            // Returning immediately if `data-editable="false"`
            if (!this.options.editable) return;
            
            var $el = this.element,
                opts = this.options,
                ui = this._ui;

                if (this.options.editableType === 'complex') {

                    if (opts.editableForm.length === 0) {
                        throw new Error("Form not specified for the Complex Editable Listview type.")
                    }

                    var form = $el.closest(':jqmData(role="page")').find('#' + opts.editableForm);

                    if (form.is("form, div") && form.attr("data-editable-form")) {
                        ui.form = form.detach();
                    } else {
                        throw new Error("Reference Error: the form's id should match the \"data-editable-form\" attribute on ul and the form element itself should have data-editable-form=\"true\" attribute.")
                    }
                }
        },

        _wrapCollapsible: function () {
            var $el = this.element,
                opts = this.options,
                isListEmpty = this._isListEmpty();

            $el.wrap('<div data-role="collapsible"></div>');
            $el.parent().prepend(this._$markup.header(opts));

            $el.parents($.mobile['collapsible'].initSelector)
                .not($.mobile.page.prototype.keepNativeSelector())['collapsible']({
                    expandedIcon: this.options.expandedIcon,
                    collapsedIcon: this.options.collapsedIcon
                });

            return $el.parent().parent();
        },

        _attachEditEventButtons: function () {
            if (this._isListEmpty()) {
                this._ui.header.off("click");
            }

            this._on(this.element.parent().parent().find('.ui-collapsible-heading a, .ui-collapsible-heading button'), {
                "click": "_onEditButtonTapped"
            });
        },

        // --(start)-- Event Handlers --<commit_msg>Checking if ui.form is already initialized or not<commit_after>/*!
 * jQuery Mobile Editable Listview Plugin
 * https://github.com/baig/jquerymobile-editablelistview
 *
 * Copyright 2014 (c) <NAME>
 *
 * Released under the MIT license
 * https://github.com/baig/jquerymobile-editablelistview/blob/master/LICENSE.txt
 */

(function ($, undefined) {

    "use strict";

    $.widget("mobile.listview", $.mobile.listview, {

        // Declaring some private instace state variables
        _created: false,
        _origDom: null,
        _editMode: false,
        _counter: 1,
        _dataItemName: "item",
        _itemNames: [],
        _evt: null,
        _clickHandler: null,

        // The options hash
        options: {
            editable: false,
            editableType: 'simple',
            editableForm: '',

            listTitle: "View list items",
            listEmptyTitle: "No items to view",
            editLabel: "Edit",
            addLabel: "Add",
            doneLabel: "Done",
            addIcon: "plus",
            editIcon: "edit",
            doneIcon: "check",

            buttonTheme: 'a',
            buttonCorner: true,
            buttonShadow: true,
        },

        _beforeListviewRefresh: function () {
            
            // Returning immediately if `data-editable="false"`
            if (!this.options.editable) return;
            
            var $el = this.element,
                opts = this.options,
                $origDom = this._origDom,
                dataItemName = this._dataItemName,
                counter = this._counter,
                ui, $orig, $origLis,
                evt = this._evt,
                $lis = $el.find("li"),
                $markup = this._$markup;

            // saving original DOM structure
            if ($origDom === null) {
                $origDom = $el.clone();
                // Assign each list item a unique number value
                $.each($origDom.children('li'), function (idx, val) {
                    $(val).attr("data-" + dataItemName, counter);
                    counter++;
                });

                // Incrementing the counter that is used to assign unique value to `data-item` attribute on each list item
                this._counter = counter;

                // Removing all css classes to get the original DOM structure
                $origDom.removeClass("ui-listview ui-shadow ui-corner-all ui-listview-inset ui-group-theme-" + this.options.theme)
                    .find("li")
                    .removeClass("ui-li-static ui-body-inherit ui-first-child ui-last-child")
                    .end()
                    .find("a")
                    .removeClass("ui-link")
                    .end();

                // Caching the original DOM to the widget instance
                this._origDom = $origDom;
            }

            // ## Creation
            if (!this._created) {
                // Wrapping the list structure inside Collapsible
                var wrapper = this._wrapCollapsible();

                ui = {
                    wrapper: wrapper,
                    header: wrapper.find('h1'),
                    button: wrapper.find('h1 + a, h1 + button'),
                    content: wrapper.find('div.ui-collapsible-content'),
                };

                if (this.options.editableType === 'complex') {
//                    ui.form = wrapper.closest(':jqmData(role="page")').find('#' + this.options.editableForm).detach();
                    var inputs = ui.content.find('li:first-child').find('[data-item-name]');

                    var itemNames = this._itemNames;
                    $.each(inputs, function (idx, val) {
                        itemNames.push($(val).data("item-name"));
                    });
                }

                $.extend(this, {
                    _ui: ui
                });

                ui.header.addClass('ui-btn-icon-left');

                evt = this._evt = $._data(ui.header.parent()[0], "events");
                this._clickHandler = evt.click[0].handler;

                this._attachEditEventButtons();

                this._created = true;
            }

            if (this._editMode) {
                ui = this._ui;
                $orig = $origDom.clone();
                $origLis = $orig.find('li');

                if ($orig.find('a').length === 0) {
                    $origLis.wrapInner('<a></a>').append('<a class="ui-editable-btn-del">Delete</a>');
                } else {
                    $origLis.append('<a class="ui-editable-btn-del">Delete</a>');
                }

                this.option("splitIcon", "minus");

                if (opts.editableType === 'complex') {
                    $orig.prepend($('<li>' + ui.form[0].innerHTML + '</li>'));
                }
                if (opts.editableType === 'simple') {
                    $orig.prepend($markup.listTextInput);
                }

                $lis.remove();
                $el.append($orig.find('li'));

                // Disabling the click event on header when list is in `Edit` mode
                evt.click[0].handler = $.noop;
            } else {
                // Re-enabling the click event handler when the list is in `View` mode
                evt.click[0].handler = this._clickHandler;
                $lis.remove().end().append($origDom.clone().find('li'));
            }

            // Updating the header title, header button label and icon based on the list contents and its state (`Edit` or `View`)
            this._updateHeader();
        },

        _afterListviewRefresh: function () {
            
            // Returning immediately if `data-editable="false"`
            if (!this.options.editable) return;
            
            this._attachDetachEventHandlers();
        },

        // Detaching form from the DOM if the listview is initialized programmatically
        _init: function() {
            
            // Returning immediately if `data-editable="false"`
            if (!this.options.editable) return;
            
            var $el = this.element,
                opts = this.options,
                ui = this._ui;

            if (!ui.form) {

                if (this.options.editableType === 'complex') {

                    if (opts.editableForm.length === 0) {
                        throw new Error("Form not specified for the Complex Editable Listview type.")
                    }

                    var form = $el.closest(':jqmData(role="page")').find('#' + opts.editableForm);

                    if (form.is("form, div") && form.attr("data-editable-form")) {
                        ui.form = form.detach();
                    } else {
                        throw new Error("Reference Error: the form's id should match the \"data-editable-form\" attribute on ul and the form element itself should have data-editable-form=\"true\" attribute.")
                    }
                }
            }
        },

        _wrapCollapsible: function () {
            var $el = this.element,
                opts = this.options,
                isListEmpty = this._isListEmpty();

            $el.wrap('<div data-role="collapsible"></div>');
            $el.parent().prepend(this._$markup.header(opts));

            $el.parents($.mobile['collapsible'].initSelector)
                .not($.mobile.page.prototype.keepNativeSelector())['collapsible']({
                    expandedIcon: this.options.expandedIcon,
                    collapsedIcon: this.options.collapsedIcon
                });

            return $el.parent().parent();
        },

        _attachEditEventButtons: function () {
            if (this._isListEmpty()) {
                this._ui.header.off("click");
            }

            this._on(this.element.parent().parent().find('.ui-collapsible-heading a, .ui-collapsible-heading button'), {
                "click": "_onEditButtonTapped"
            });
        },

        // --(start)-- Event Handlers --
{'fraction_non_alphanumeric': 0.12866704197160192, 'fraction_numerical': 0.0018765246762994933, 'mean_word_length': 1.3866248693834902, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, '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': '6924095', 'n_tokens_mistral': 4284, 'n_tokens_neox': 4008, 'n_words': 899}