<commit_before>/*
 * jQuery UI Depends:
 *        jquery.ui.autocomplete.js
 *        jquery.ui.button.js
 *        jquery.ui.core.js
 *        jquery.ui.widget.js
 *        jquery.ui.position.js
 *        jquery.ui.menu.js
 */


/**
 * @class nb.block.Select
 * @augments nb.block.Base
 */
nb.define('select', {
    events: {
        'mousedown': '_onclick'
        //'open' { event, ui}
        //'close' { event, ui}
    },

    /**
     * Init select
     * @fires 'nb-inited'
     * @constructor
     */
    oninit: function() {
        this.isOpen = false;
        this.$control = this.$node.find('select');
        this.$dropdown = this.$node.children('.nb-select__dropdown').appendTo('body');
        this.data = this.nbdata();

        this._updateFromSelect();

        // degradation to native control in IE < 9
        if (nb.IE_LT9) {
            var that = this;
            this.$control.on('change', function(e) {
                that.setState({
                    value: e.target.value
                });
            });
        } else {
            // preparing control depending on configuration and content
            this.controlPrepare();
        }

        this.trigger('nb-inited', this);
    },

    /**
     * preparing control depending on configuration and content
     */
    controlPrepare: function() {
        var that = this;
        // preparing position parameters for popup from direction data
        var position = {};
        position.collision = 'flip';

        if (that.data.within) {
            position.within = that.data.within;
        }

        if (that.data.direction == 'top') {
            position.my = "left bottom";
            position.at = "left top";

        } else {
            position.my = "left top";
            position.at = "left bottom";
        }

        // select JUI control init
        this.$node.autocomplete({
            delay: 0,
            minLength: 0,
            autoFocus: false,
            position: position,
            appendTo: that.$dropdown,
            source: function(request, response) {
                response(that.$control.children(['option', 'optgroup']).map(function() {
                    var returnObj;
                    var $this = $(this);

                    if (this.tagName == 'OPTGROUP') {
                        returnObj = {
                            type: 'group',
                            label: $this.attr('label'),
                            option: this,
                            group: $this.children('option').map(function() {
                                return {
                                    label: $(this).text(),
                                    value: $(this).val(),
                                    option: this
                                };
                            })
                        };
                    } else {
                        var icon = $this.data('icon');
                        returnObj = {
                            label: $this.text(),
                            value: $this.val(),
                            option: this
                        };

                        if (icon) {
                            returnObj['icon'] = icon;
                        }
                    }

                    return returnObj;
                }));
            },
            select: function(event, ui) {

                if (ui.item.type != 'group') {
                    ui.item.option.selected = true;

                    that.$jUI._trigger('selected', event, {
                        item: ui.item.option
                    });
                }
            },
            // delegate handler on 'outer' click on open
            open: function() {
                that.$jUI._on(that.$jUI.document, {
                    // on 'outer' mousedown close control
                    mousedown: function(e) {
                        if (e.which == 1 && !$.contains(that.$jUI.element.get(0), e.target) && !$.contains(that.$dropdown[0], e.target)) {
                            this.close();
                        }
                    }
                });
                that.trigger('nb-opened', that);
            },

            close: function() {
                that.$jUI._off(that.$jUI.document, 'mousedown');
                that.trigger('nb-closed', that);
            }
        }).addClass('ui-widget ui-widget-content');

        that.$jUI = that.$node.data('uiAutocomplete');

        that.$node.on('autocompleteopen', function() {
            that.isOpen = true;
        });

        that.$node.on('autocompleteclose', function() {
            that.isOpen = false;
        });


        // redefine one menu item rendering method, fires every time, then popup opening
        that.$jUI._renderItem = function(ul, item) {
            var $itemNode = $('<li class="nb-select__item"></li>');

            if (item.option.selected) {
                $itemNode.addClass('nb-select__item_selected_yes');
            }

            if (item.type == 'group') {

                $itemNode.addClass('nb-select__item_type_group');
                var $innerUL = $('<ul></ul>');

                item.group.each(function(index, item) {
                    that.$jUI._renderItem($innerUL, item);
                });

                $itemNode.append($innerUL);
            }

            $itemNode.data('ui-autocomplete-item', item);

            var $itemNodeContent = $('<a></a>');
            var $itemText = $('<span class="nb-select__text"></span>').text(item.label).appendTo($itemNodeContent);
            if (item.icon) {
                $itemText.prepend('<img src="//yandex.st/lego/_/La6qi18Z8LwgnZdsAr1qy1GwCwo.gif" class="nb-icon nb-icon_' + item.icon + '">');
            }
            $itemNode
                .append($itemNodeContent)
                .appendTo(ul);

            return $itemNode;
        };

        // redefine valueMethod, extend with button text changing and fallback select value changing
        // if value not provided, return current value of fallback select
        that.$jUI.valueMethod = function(value) {

            if (typeof value === 'string') {
                var text = that.$control.find('option[value="' + value + '"]').text();
                that.setState({
                    value: value,
                    text: text
                });
            }<commit_msg>fix #286<commit_after>/*
 * jQuery UI Depends:
 *        jquery.ui.autocomplete.js
 *        jquery.ui.button.js
 *        jquery.ui.core.js
 *        jquery.ui.widget.js
 *        jquery.ui.position.js
 *        jquery.ui.menu.js
 */


/**
 * @class nb.block.Select
 * @augments nb.block.Base
 */
nb.define('select', {
    events: {
        'mousedown': '_onclick'
        //'open' { event, ui}
        //'close' { event, ui}
    },

    /**
     * Init select
     * @fires 'nb-inited'
     * @constructor
     */
    oninit: function() {
        this.isOpen = false;
        this.$control = this.$node.find('select');
        this.$dropdown = this.$node.children('.nb-select__dropdown').appendTo('body');
        this.data = this.nbdata();

        this._updateFromSelect();

        // degradation to native control in IE < 9
        if (nb.IE_LT9) {
            var that = this;
            this.$control.on('change', function(e) {
                that.setState({
                    value: e.target.value
                });
            });
        } else {
            // preparing control depending on configuration and content
            this.controlPrepare();
        }

        this.trigger('nb-inited', this);
    },

    /**
     * preparing control depending on configuration and content
     */
    controlPrepare: function() {
        var that = this;
        // preparing position parameters for popup from direction data
        var position = {};
        position.collision = 'flip';

        if (that.data.within) {
            position.within = that.data.within;
        }

        if (that.data.direction == 'top') {
            position.my = "left bottom";
            position.at = "left top";

        } else {
            position.my = "left top";
            position.at = "left bottom";
        }

        // select JUI control init
        this.$node.autocomplete({
            delay: 0,
            minLength: 0,
            autoFocus: false,
            position: position,
            appendTo: that.$dropdown,
            source: function(request, response) {
                response(that.$control.children(['option', 'optgroup']).map(function() {
                    var returnObj;
                    var $this = $(this);

                    if (this.tagName == 'OPTGROUP') {
                        returnObj = {
                            type: 'group',
                            label: $this.attr('label'),
                            option: this,
                            group: $this.children('option').map(function() {
                                return {
                                    label: $(this).text(),
                                    value: $(this).val(),
                                    option: this
                                };
                            })
                        };
                    } else {
                        var icon = $this.data('icon');
                        returnObj = {
                            label: $this.text(),
                            value: $this.val(),
                            option: this
                        };

                        if (icon) {
                            returnObj['icon'] = icon;
                        }
                    }

                    return returnObj;
                }));
            },
            select: function(event, ui) {

                if (ui.item.type != 'group') {
                    ui.item.option.selected = true;

                    that.$jUI._trigger('selected', event, {
                        item: ui.item.option
                    });
                }
            },
            // delegate handler on 'outer' click on open
            open: function() {
                that.$jUI._on(that.$jUI.document, {
                    // on 'outer' mousedown close control
                    mousedown: function(e) {
                        if (e.which == 1 && !$.contains(that.$jUI.element.get(0), e.target) && !$.contains(that.$dropdown[0], e.target)) {
                            this.close();
                        }
                    }
                });
                that.trigger('nb-opened', that);
            },

            close: function() {
                that.$jUI._off(that.$jUI.document, 'mousedown');
                that.trigger('nb-closed', that);
            }
        }).addClass('ui-widget ui-widget-content');

        that.$jUI = that.$node.data('uiAutocomplete');

        that.$node.on('autocompleteopen', function() {
            that.isOpen = true;
        });

        that.$node.on('autocompleteclose', function() {
            that.isOpen = false;
        });


        // redefine one menu item rendering method, fires every time, then popup opening
        that.$jUI._renderItem = function(ul, item) {
            var $itemNode = $('<li class="nb-select__item"></li>');

            if (item.option.selected) {
                $itemNode.addClass('nb-select__item_selected_yes');
            }

            if (item.type == 'group') {

                $itemNode.addClass('nb-select__item_type_group');
                var $innerUL = $('<ul></ul>');

                item.group.each(function(index, item) {
                    that.$jUI._renderItem($innerUL, item);
                });

                $itemNode.append($innerUL);
            }

            $itemNode.data('ui-autocomplete-item', item);

            var $itemNodeContent = $('<a class="nb-select__a"></a>');
            var $itemText = $('<span class="nb-select__text"></span>').text(item.label).appendTo($itemNodeContent);
            if (item.icon) {
                $itemText.prepend('<img src="//yandex.st/lego/_/La6qi18Z8LwgnZdsAr1qy1GwCwo.gif" class="nb-icon nb-icon_' + item.icon + '">');
            }
            $itemNode
                .append($itemNodeContent)
                .appendTo(ul);

            return $itemNode;
        };

        // redefine valueMethod, extend with button text changing and fallback select value changing
        // if value not provided, return current value of fallback select
        that.$jUI.valueMethod = function(value) {

            if (typeof value === 'string') {
                var text = that.$control.find('option[value="' + value + '"]').text();
                that.setState({
                    value: value,
                    text: text
                });
            }
{'fraction_non_alphanumeric': 0.13117427772600188, 'fraction_numerical': 0.0022522522522522522, 'mean_word_length': 1.1795870006770481, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '201668', 'n_tokens_mistral': 3438, 'n_tokens_neox': 3202, 'n_words': 648}