<commit_before>import Ember from 'ember';
import InputBased from '../mixins/sl-input-based';
import TooltipEnabled from '../mixins/sl-tooltip-enabled';

/**
 * @module components
 * @class sl-input
 */
export default Ember.Component.extend( InputBased, TooltipEnabled, {

    /**
     * Object containing action functions
     * @property {object} actions
     */
    actions: {

        /**
         * Sends the 'blur' bound action when the input loses focus
         * @method actions.blurred
         */
        blur: function () {
            this.sendAction( 'blur' );
        },

        /**
         * Sends the primary bound action when `enter` is pressed
         * @method actions.enter
         */
        enter: function () {
            this.sendAction();
        }
    },

    /**
     * Attribute bindings for the containing div
     * @property {array} attributeBindings
     */
    attributeBindings: [ 'class' ],

    /**
     * Class names for the containing div
     * @property {array} classNames
     */
    classNames: [ 'form-group', 'sl-input' ],

    /**
     * Enable the click to edit styling
     * @property {boolean} clickToEdit
     * @default false
     */
    clickToEdit: false,

    /**
     * Get a reference to the internal input element
     * @method getInput
     */
    getInput: function () {
        return this.$( '#' + this.get( 'inputId' ));
    },

    /**
     * Class string for the internal input element
     * @property {string} inputClass
     */
    inputClass: function () {
        var classes = [ 'form-control' ];

        if ( this.get( 'clickToEdit' )) {
            classes.push( 'click-to-edit' );
        }

        if ( this.get( 'suggestions' )) {
            classes.push( 'typeahead' );
        }

        return classes.join( ' ' );
    }.property(),

    /**
     * ID for the actual input element
     * @property {string} inputId
     */
    inputId: function () {
        return this.get( 'elementId' ) + 'Input';
    }.property( 'elementId' ),

    /**
     * Whether the typeahead.js functionality has been setup
     * @property {boolean} isTypeaheadSetup
     * @default false
     */
    isTypeaheadSetup: false,

    /**
     * Sets up the input event listeners exposed to the component's
     * parent controller
     * @method setupInputEvents
     */
    setupInputEvents: function () {
        var blurAction = this.get( 'blur' ),
            self = this;

        if ( blurAction ) {
            this.$( 'input' ).on( 'blur', function () {
                self.sendAction( 'blur' );
            });
        }
    }.on( 'didInsertElement' ),

    /**
     * Sets up the typeahead behavior when `suggestions` are supplied
     * @method setupTypeahead
     */
    setupTypeahead: function () {
        var self = this;

        if ( this.get( 'suggestions' ) && !this.get( 'isTypeaheadSetup' )) {
            var namePath = this.get( 'suggestionNamePath' );
            var typeahead;

            typeahead = this.getInput().typeahead({
                highlight: true,
                hint: true
            }, {
                displayKey: namePath,

                source: function ( query, callback ) {
                    var pattern = new RegExp( query, 'i' );

                    callback( self.get( 'suggestions' ).filter( function ( suggestion ) {
                        return Ember.get( suggestion, namePath ).match( pattern );
                    }));
                }
            });

            /* jshint ignore:start */
            var selectItem = function ( event, item ) {
                self.set( 'value', Ember.get( item, namePath ));
            };

            typeahead.on( 'typeahead:autocompleted', selectItem );
            typeahead.on( 'typeahead:selected', selectItem );
            /* jshint ignore:end */

            this.set( 'isTypeaheadSetup', true );
        }
    }.on( 'didInsertElement' ).observes( 'suggestions' ),

    /**
     * Lookup path for the suggestion items' name
     * @property {string} suggestionLabelPath
     * @default "name"
     */
    suggestionNamePath: 'name',

    /**
     * Type attribute for the containing div
     * @property {string} type
     * @default "text"
     */
    type: 'text'<commit_msg>Fixed the typeahead to work with ember objects and run inside an ember.run loop<commit_after>import Ember from 'ember';
import InputBased from '../mixins/sl-input-based';
import TooltipEnabled from '../mixins/sl-tooltip-enabled';

/**
 * @module components
 * @class sl-input
 */
export default Ember.Component.extend( InputBased, TooltipEnabled, {

    /**
     * Object containing action functions
     * @property {object} actions
     */
    actions: {

        /**
         * Sends the 'blur' bound action when the input loses focus
         * @method actions.blurred
         */
        blur: function () {
            this.sendAction( 'blur' );
        },

        /**
         * Sends the primary bound action when `enter` is pressed
         * @method actions.enter
         */
        enter: function () {
            this.sendAction();
        }
    },

    /**
     * Attribute bindings for the containing div
     * @property {array} attributeBindings
     */
    attributeBindings: [ 'class' ],

    /**
     * Class names for the containing div
     * @property {array} classNames
     */
    classNames: [ 'form-group', 'sl-input' ],

    /**
     * Enable the click to edit styling
     * @property {boolean} clickToEdit
     * @default false
     */
    clickToEdit: false,

    /**
     * Get a reference to the internal input element
     * @method getInput
     */
    getInput: function () {
        return this.$( '#' + this.get( 'inputId' ));
    },

    /**
     * Class string for the internal input element
     * @property {string} inputClass
     */
    inputClass: function () {
        var classes = [ 'form-control' ];

        if ( this.get( 'clickToEdit' )) {
            classes.push( 'click-to-edit' );
        }

        if ( this.get( 'suggestions' )) {
            classes.push( 'typeahead' );
        }

        return classes.join( ' ' );
    }.property(),

    /**
     * ID for the actual input element
     * @property {string} inputId
     */
    inputId: function () {
        return this.get( 'elementId' ) + 'Input';
    }.property( 'elementId' ),

    /**
     * Whether the typeahead.js functionality has been setup
     * @property {boolean} isTypeaheadSetup
     * @default false
     */
    isTypeaheadSetup: false,

    /**
     * Sets up the input event listeners exposed to the component's
     * parent controller
     * @method setupInputEvents
     */
    setupInputEvents: function () {
        var blurAction = this.get( 'blur' ),
            self = this;

        if ( blurAction ) {
            this.$( 'input' ).on( 'blur', function () {
                self.sendAction( 'blur' );
            });
        }
    }.on( 'didInsertElement' ),

    /**
     * Sets up the typeahead behavior when `suggestions` are supplied
     * @method setupTypeahead
     */
    setupTypeahead: function () {
        var self = this;

        if ( this.get( 'suggestions' ) && !this.get( 'isTypeaheadSetup' )) {
            var namePath = this.get( 'suggestionNamePath' );
            var typeahead;

            typeahead = this.getInput().typeahead({
                highlight: true,
                hint: true
            }, {
                displayKey: function( obj ){
                    return Ember.get( obj, namePath );
                },

                source: function ( query, callback ) {
                    var pattern = new RegExp( query, 'i' );

                    callback( self.get( 'suggestions' ).filter( function ( suggestion ) {
                        var searchCandidate = Ember.get( suggestion, namePath );
                        return searchCandidate ? searchCandidate.match( pattern ) : false;
                    }));
                }
            });

            /* jshint ignore:start */
            var selectItem = function ( event, item ) {
                Ember.run( function(){ self.set( 'value', Ember.get( item, namePath )); });
            };

            typeahead.on( 'typeahead:autocompleted', selectItem );
            typeahead.on( 'typeahead:selected', selectItem );
            /* jshint ignore:end */

            this.set( 'isTypeaheadSetup', true );
        }
    }.on( 'didInsertElement' ).observes( 'suggestions' ),

    /**
     * Lookup path for the suggestion items' name
     * @property {string} suggestionLabelPath
     * @default "name"
     */
    suggestionNamePath: 'name',

    /**
     * Type attribute for the containing div
     * @property {string} type
     * @default "text"
     */
    type: 'text'
{'fraction_non_alphanumeric': 0.12467236467236467, 'fraction_numerical': 0.0, 'mean_word_length': 1.7339563862928349, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4119920', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2341, 'n_words': 651}