<commit_before>/*----------------------------------------------------------------------------*
 * TextFieldSuggester
 *
 * JavaScript library providing automatic suggestion/completion support for
 * text input fields.
 *
 * This project and its licensing terms can be found at:
 * https://github.com/Sensis/text-field-suggester
 *----------------------------------------------------------------------------*/

if (typeof Sensis === 'undefined')
	Sensis = {};

(function () {
	var defaults = {
		suggestionFetchDelayMs: 100
	};


	Sensis.TextFieldSuggester = function (name, textInputSelector, maxSuggestions, suggestionFetchFunc) {
		var that = this;

		this.name = name;
		this.textField = $(textInputSelector);
		this.maxSuggestions = maxSuggestions;
		this.suggestionFetchFunc = suggestionFetchFunc;
		this.suggestionFetchDelayMs = defaults.suggestionFetchDelayMs;
		this.suggestions = [];
		this.suggestionUpdateTimeout = null;
		this.selectedSuggestionIndex = -1;
		this.bestSuggestion = '';
		this.lastValue = '';
		this.lastValueWithCompletion = '';
		this.updateOnNextFocus = true;

		this.completion = this.createCompletion();
		this.suggestionList = this.createSuggestionList();

		$('body').append(this.completion, this.suggestionList);

		this.reposition();
		this.updateSuggestions();

		this.textField.focus(function (e) {
			if (that.updateOnNextFocus) {
				that.fetchSuggestions(that.textField.val());
				that.updateSuggestions();
			}

			that.bestSuggestion = that.textField.val();
			that.reportValueUpdated(that.textField.val());
		});

		this.textField.blur(function () {
			if (that.textField.val() !== '' && that.bestSuggestion.toLowerCase().indexOf(that.textField.val().toLowerCase()) === 0)
				that.textField.val(that.textField.val() + that.bestSuggestion.substring(that.textField.val().length));
			that.lastValue = that.textField.val();
			that.updateSuggestions();
			that.updateOnNextFocus = true;
		});

		this.textField.keydown(function (e) {
			var value;

			// Enter key pressed
			if (e.keyCode === 13) {
				e.preventDefault();
				that.cancelCompletion();
				that.hideSuggestions();
			}

			else {
				value = that.textField.val();
				that.updateOnNextFocus = true;

				if (value.length < that.lastValue.length)<commit_msg>Queue the completion-on-blur so that other code can have a chance to cancel it.<commit_after>/*----------------------------------------------------------------------------*
 * TextFieldSuggester
 *
 * JavaScript library providing automatic suggestion/completion support for
 * text input fields.
 *
 * This project and its licensing terms can be found at:
 * https://github.com/Sensis/text-field-suggester
 *----------------------------------------------------------------------------*/

if (typeof Sensis === 'undefined')
	Sensis = {};

(function () {
	var defaults = {
		suggestionFetchDelayMs: 100
	};


	Sensis.TextFieldSuggester = function (name, textInputSelector, maxSuggestions, suggestionFetchFunc) {
		var that = this;

		this.name = name;
		this.textField = $(textInputSelector);
		this.maxSuggestions = maxSuggestions;
		this.suggestionFetchFunc = suggestionFetchFunc;
		this.suggestionFetchDelayMs = defaults.suggestionFetchDelayMs;
		this.suggestions = [];
		this.suggestionUpdateTimeout = null;
		this.selectedSuggestionIndex = -1;
		this.bestSuggestion = '';
		this.lastValue = '';
		this.lastValueWithCompletion = '';
		this.updateOnNextFocus = true;

		this.completion = this.createCompletion();
		this.suggestionList = this.createSuggestionList();

		$('body').append(this.completion, this.suggestionList);

		this.reposition();
		this.updateSuggestions();

		this.textField.focus(function (e) {
			if (that.updateOnNextFocus) {
				that.fetchSuggestions(that.textField.val());
				that.updateSuggestions();
			}

			that.bestSuggestion = that.textField.val();
			that.reportValueUpdated(that.textField.val());
		});

		this.textField.blur(function () {
			window.setTimeout(function () {
				if (that.textField.val() !== '' && that.bestSuggestion.toLowerCase().indexOf(that.textField.val().toLowerCase()) === 0)
					that.textField.val(that.textField.val() + that.bestSuggestion.substring(that.textField.val().length));
				that.lastValue = that.textField.val();
				that.updateSuggestions();
				that.updateOnNextFocus = true;
			}, 1);
		});

		this.textField.keydown(function (e) {
			var value;

			// Enter key pressed
			if (e.keyCode === 13) {
				e.preventDefault();
				that.cancelCompletion();
				that.hideSuggestions();
			}

			else {
				value = that.textField.val();
				that.updateOnNextFocus = true;

				if (value.length < that.lastValue.length)
{'fraction_non_alphanumeric': 0.1952688172043011, 'fraction_numerical': 0.0032258064516129032, 'mean_word_length': 3.0303292894280762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '940751', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1331, 'n_words': 238}