<commit_before>var flight = require('flight'),
    _ = require('lodash')
    ;

module.exports = flight.component(NoeAutocomplete);

function noOp(){};

function NoeAutocomplete() {
    this.attributes({
        events: {},
        inputSelector: 'input[type=text]',
        listSelector: 'aside',
        suggestionsSelector: 'aside li',
        hintSelector: 'small',
        templates: {},
        minLength: 3
    });

    this.after('initialize', function() {
        this.suggestions = [];
        this.suggestionIndex = -1;
        this.attr.minLength = Math.max(1, this.attr.minLength);
        this.attr.events = $.extend({
            uiGetSuggestions: 'uiGetSuggestions',
            dataSuggestions: 'dataSuggestions',
            uiSelectedSuggestion: 'uiSelectedSuggestion',
            uiMatchedSuggestion: 'uiMatchedSuggestion',
            uiClose: 'uiClose'
        }, this.attr.events);

        this.attr.templates = $.extend({
            suggestion: noOp,
            hint: noOp
        }, this.attr.templates);

        this.on('keyup', {
            inputSelector: function(e) {
                if(e.keyCode === 27) {
                    this.clear();
                } else if(e.keyCode === 38) {
                    this.highlightPreviousSuggestion(e);
                } else if(e.keyCode === 40) {
                    this.highlightNextSuggestion();
                }
            }
        });

        this.on('keydown', {
            inputSelector: function(e) {
                if(e.keyCode === 38) {
                    e.preventDefault();
                } else if (e.keyCode === 13){
                    this.triggerSelection(e);
                } else if ([38, 40, 13].indexOf(e.keyCode) === -1) {
                    this.getSuggestions();
                }
            }
        });

        this.on('keypress', {
            inputSelector: function(e) {
                if([13, 38, 40].indexOf(e.keyCode) === -1) {
                    this.getSuggestions();
                }
            }
        });

        this.on('hover', {
            suggestionsSelector: function(e) {
                (e.target);
            }
        });

        this.on(this.attr.events.dataSuggestions, function(e, suggestions) {
            this.update(suggestions);
        });

        this.on(this.attr.events.uiClose, function(e) {
            this.clear();
        });
    });

    this.triggerSelection = function(e) {
        var uiEvent, suggestion
            ;

        if(this.suggestionIndex > -1) {
            uiEvent = this.attr.events.uiSelectedSuggestion;
            suggestion = this.suggestions[this.suggestionIndex];
        } else if(this.suggestions.length) {
            var query = this.getValue(),
                matchedSuggestions = this.suggestions.filter(function(suggestion) {
                    return $.trim(query).toLowerCase() === $.trim(suggestion.name).toLowerCase();
                });

            if(matchedSuggestions.length) {
                uiEvent = this.attr.events.uiMatchedSuggestion;
                suggestion = matchedSuggestions.shift();
            }
        }

        if(!suggestion) return;

        if(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        this.trigger(uiEvent, suggestion);
    }

    this.getSuggestions = _.debounce(function() {
        this.suggestionIndex = -1;
        this.select('suggestionsSelector').removeClass('active');
        (function(component, val) {
            if(val.length >= component.attr.minLength) {
                component.query = val;
                component.trigger(component.attr.events.uiGetSuggestions, [val]);
                component.showHint();
            } else if(val.length < component.attr.minLength) {
                component.clear();
            }
        })(this, this.getValue() || '');
    }, 10);

    this.highlight = function(index) {
        var label = this.query,
            hintAction = 'showHint'
            ;

        if(index < -1) {
            index = this.suggestions.length - 1;
        } else if(index >= this.suggestions.length) {
            index = -1;
        }

        var $suggestions = this.select('suggestionsSelector').removeClass('active');

        if(index > -1) {
            label = $suggestions.eq(index)
                .addClass('active')
                .data('label')
                ;
            hintAction = 'hideHint';
        }

        this.select('inputSelector').val(label);
        this[hintAction]();
        this.suggestionIndex = index;
    }

    this.highlightPreviousSuggestion = function(e) {
        e.preventDefault();
        this.highlight(this.suggestionIndex - 1);
    };

    this.highlightNextSuggestion = function() {
        this.highlight(this.suggestionIndex + 1);
    };

    this.clear = function() {
        this.update();
    };

    this.getValue = function() {<commit_msg>remove clear in favour of directly calling update<commit_after>var flight = require('flight'),
    _ = require('lodash')
    ;

module.exports = flight.component(NoeAutocomplete);

function noOp(){};

function NoeAutocomplete() {
    this.attributes({
        events: {},
        inputSelector: 'input[type=text]',
        listSelector: 'aside',
        suggestionsSelector: 'aside li',
        hintSelector: 'small',
        templates: {},
        minLength: 3
    });

    this.after('initialize', function() {
        this.suggestions = [];
        this.suggestionIndex = -1;
        this.attr.minLength = Math.max(1, this.attr.minLength);
        this.attr.events = $.extend({
            uiGetSuggestions: 'uiGetSuggestions',
            dataSuggestions: 'dataSuggestions',
            uiSelectedSuggestion: 'uiSelectedSuggestion',
            uiMatchedSuggestion: 'uiMatchedSuggestion',
            uiClose: 'uiClose'
        }, this.attr.events);

        this.attr.templates = $.extend({
            suggestion: noOp,
            hint: noOp
        }, this.attr.templates);

        this.on('keyup', {
            inputSelector: function(e) {
                if(e.keyCode === 27) {
                    this.update();
                } else if(e.keyCode === 38) {
                    this.highlightPreviousSuggestion(e);
                } else if(e.keyCode === 40) {
                    this.highlightNextSuggestion();
                }
            }
        });

        this.on('keydown', {
            inputSelector: function(e) {
                if(e.keyCode === 38) {
                    e.preventDefault();
                } else if (e.keyCode === 13){
                    this.triggerSelection(e);
                } else if ([38, 40, 13].indexOf(e.keyCode) === -1) {
                    this.getSuggestions();
                }
            }
        });

        this.on('keypress', {
            inputSelector: function(e) {
                if([13, 38, 40].indexOf(e.keyCode) === -1) {
                    this.getSuggestions();
                }
            }
        });

        this.on('hover', {
            suggestionsSelector: function(e) {
                (e.target);
            }
        });

        this.on(this.attr.events.dataSuggestions, function(e, suggestions) {
            this.update(suggestions);
        });

        this.on(this.attr.events.uiClose, function(e) {
            this.update();
        });
    });

    this.triggerSelection = function(e) {
        var uiEvent, suggestion
            ;

        if(this.suggestionIndex > -1) {
            uiEvent = this.attr.events.uiSelectedSuggestion;
            suggestion = this.suggestions[this.suggestionIndex];
        } else if(this.suggestions.length) {
            var query = this.getValue(),
                matchedSuggestions = this.suggestions.filter(function(suggestion) {
                    return $.trim(query).toLowerCase() === $.trim(suggestion.name).toLowerCase();
                });

            if(matchedSuggestions.length) {
                uiEvent = this.attr.events.uiMatchedSuggestion;
                suggestion = matchedSuggestions.shift();
            }
        }

        if(!suggestion) return;

        if(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        this.trigger(uiEvent, suggestion);
    }

    this.getSuggestions = _.debounce(function() {
        this.suggestionIndex = -1;
        this.select('suggestionsSelector').removeClass('active');
        (function(component, val) {
            if(val.length >= component.attr.minLength) {
                component.query = val;
                component.trigger(component.attr.events.uiGetSuggestions, [val]);
                component.showHint();
            } else if(val.length < component.attr.minLength) {
                component.update();
            }
        })(this, this.getValue() || '');
    }, 10);

    this.highlight = function(index) {
        var label = this.query,
            hintAction = 'showHint'
            ;

        if(index < -1) {
            index = this.suggestions.length - 1;
        } else if(index >= this.suggestions.length) {
            index = -1;
        }

        var $suggestions = this.select('suggestionsSelector').removeClass('active');

        if(index > -1) {
            label = $suggestions.eq(index)
                .addClass('active')
                .data('label')
                ;
            hintAction = 'hideHint';
        }

        this.select('inputSelector').val(label);
        this[hintAction]();
        this.suggestionIndex = index;
    }

    this.highlightPreviousSuggestion = function(e) {
        e.preventDefault();
        this.highlight(this.suggestionIndex - 1);
    };

    this.highlightNextSuggestion = function() {
        this.highlight(this.suggestionIndex + 1);
    };

    this.getValue = function() {
{'fraction_non_alphanumeric': 0.13225412339645692, 'fraction_numerical': 0.007534107106495622, 'mean_word_length': 1.5938737787166624, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '705979', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2595, 'n_words': 410}