<commit_before>// brfs doesn't work with ES6 import syntax
// https://github.com/substack/brfs/issues/39
const fs = require('fs');
const widgetTemplate = fs.readFileSync('src/giphy-widget.html', 'utf8');
const hiddenClass = 'giphy-hidden';
const noResultsMsg = 'No GIFs Found :(';

export default {
    create(...args) {
        return Object.create(this).init(...args);
    },

    init({ onSelection, onDispose, onTextChange }) {
        const $widget = this.$widget = $(widgetTemplate);
        this.$input = $widget.find('input');
        this.$imgList = $widget.find('.js-gif-list');
        this.$message = $widget.find('.js-giphy-message');
        this.onSelection = onSelection;
        this.onTextChange = onTextChange;
        this.onDispose = onDispose;
        return this.setupListeners();
    },

    appendToDOM(selector = 'body') {
        $(selector).append(this.$widget);
        return this;
    },

    setupListeners() {
        this.$input.on('keydown', e => {
            this.onSearchStart();
            this.onTextChange(e.currentTarget.value);
        });

        this.$widget.on('click', 'img', e => this.imageSelected(e.currentTarget));

        $(document).on('click.giphy', e => {
            if (this.$widget.get(0).contains(e.target)) return;
            this.dispose();
        });

        return this;
    },

    onSearchStart() {
        this.$imgList.empty();
        this.toggleLoading(true);
        this.toggleMessage(false);
    },

    imageSelected(img) {
        this.onSelection({
            uri: img.src,
            name: img.title
        });
        this.dispose();
    },

    showAt(top = 0, left = 0) {
        this.$widget.removeClass(hiddenClass).css({ top, left });
        this.$input.focus();
        return this;
    },

    dispose() {
        $(document).off('click.giphy');
        this.$widget.remove();
        this.disposed = true;
        this.onDispose && this.onDispose();
    },

    toggleLoading(show) {
        const action = show ? 'removeClass' : 'addClass';
        this.$widget.find('.js-giphy-loading')[action](hiddenClass);
        return this;
    },

    toggleMessage(show, message) {
        this.$message.text(message)[show ? 'removeClass' : 'addClass'](hiddenClass);
        return this;
    },
<commit_msg>Esc key closes popup<commit_after>// brfs doesn't work with ES6 import syntax
// https://github.com/substack/brfs/issues/39
const fs = require('fs');
const widgetTemplate = fs.readFileSync('src/giphy-widget.html', 'utf8');
const hiddenClass = 'giphy-hidden';
const noResultsMsg = 'No GIFs Found :(';

export default {
    create(...args) {
        return Object.create(this).init(...args);
    },

    init({ onSelection, onDispose, onTextChange }) {
        const $widget = this.$widget = $(widgetTemplate);
        this.$input = $widget.find('input');
        this.$imgList = $widget.find('.js-gif-list');
        this.$message = $widget.find('.js-giphy-message');
        this.onSelection = onSelection;
        this.onTextChange = onTextChange;
        this.onDispose = onDispose;
        return this.setupListeners();
    },

    appendToDOM(selector = 'body') {
        $(selector).append(this.$widget);
        return this;
    },

    setupListeners() {
        this.$input.on('keydown', e => {
            this.onSearchStart();
            this.onTextChange(e.currentTarget.value);
        });

        this.$widget.on('click', 'img', e => this.imageSelected(e.currentTarget));

        $(document).on('click.giphy', e => {
            if (this.$widget.get(0).contains(e.target)) return;
            this.dispose();
        });

        $(document).on('keyup.giphy', ({ keyCode }) => {
            if (keyCode === 27) this.dispose();
        });

        return this;
    },

    onSearchStart() {
        this.$imgList.empty();
        this.toggleLoading(true);
        this.toggleMessage(false);
    },

    imageSelected(img) {
        this.onSelection({
            uri: img.src,
            name: img.title
        });
        this.dispose();
    },

    showAt(top = 0, left = 0) {
        this.$widget.removeClass(hiddenClass).css({ top, left });
        this.$input.focus();
        return this;
    },

    dispose() {
        $(document).off('click.giphy');
        $(document).off('keyup.giphy');
        this.$widget.remove();
        this.disposed = true;
        this.onDispose && this.onDispose();
    },

    toggleLoading(show) {
        const action = show ? 'removeClass' : 'addClass';
        this.$widget.find('.js-giphy-loading')[action](hiddenClass);
        return this;
    },

    toggleMessage(show, message) {
        this.$message.text(message)[show ? 'removeClass' : 'addClass'](hiddenClass);
        return this;
    },

{'fraction_non_alphanumeric': 0.1665615141955836, 'fraction_numerical': 0.0033648790746582543, 'mean_word_length': 2.241990456714383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '4286355', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1424, 'n_words': 244}