<commit_before>/**
= FontanaGUI =

FontanaGUI is the main class responsible for displaying messages.

The constructor takes a datasource instance and a settings instance.

== Display methods ==

There are 3 methods to control the visualisation:

 * start
 * pause
 * resume
 * clear

`Start` takes a DOM node (selected with jQuery). This is DOM element
that the Fontana will animate in.

`Pause` will halt all animations and data retrieval.

`Resume` will continue a halted Fontana.

`Clear` will remove Fontana.
*/

var Fontana = window.Fontana || {};


Fontana.GUI = (function ($) {
    var GUI;

    GUI = function (datasource, settings) {
        var self = this;
        this.datasource = datasource;
        this.datasourceListener = null;
        this.settings = settings;
        this.messages = [];
        this.animateTimer = -1;
        this.animateScheduled = null;
        this.animatePause = null;
        this.current = null;
        this.effect = null;
        this.style_settings = [ 'font_face', 'text_color', 'custom_css',
                                'special_color', 'bg_color',
                                'bg_image', 'box_bg'];
        this.settings.bind('change', function () {
            self.handleSettingsChange.apply(self, arguments);
        });
    };

    /**
     * Handle settings changes
     */
    GUI.prototype.handleSettingsChange = function (setting, old, value) {
        if (setting == 'twitter_search') {
            this.clear();
            if (value) {
                if (this.datasource) {
                    this.datasource.stop();
                }
                this.datasource = new Fontana.datasources.Twitter(value);
                this.setupDatasourceListener();
                this.datasource.getMessages();
            }
        }
        if (setting == 'effect') {
            this.pause();
            if (this.effect) {
                this.effect.destroy();
                this.effect = null;
            }
            this.animateMessages();
        }
        if ($.inArray(setting, this.style_settings) > -1) {
            this.updateStyle();
        }
    };

    /* Setup datasource listener */
    GUI.prototype.setupDatasourceListener = function () {
        var self = this;
        this.datasourceListener = function (messages) {
            self.handleMessages.call(self, messages);
        };
        this.datasource.bind('messages', this.datasourceListener);
    };

    /**
     * Handle the messages from the datasource
     */
    GUI.prototype.handleMessages = function (messages) {
        var self = this, elements = [];
        this.pause();
        elements = $.map(messages, function (message) {
            return self.formatMessage(message)[0];
        });
        this.container.prepend(elements);
        this.current = null;
        this.resume();
    };

    /**
     * Purge messages in the DOM (if necesarry)
     */
    GUI.prototype.purgeMessages = function (messages) {
        var all = $('.fontana-message', this.container);
        if (all.length >= 30) {
            if (all.index(this.current) < all.length / 2) {
                all.slice(Math.floor(all.length / 2)).remove();
            }
            else {
                this.current.nextAll('.fontana-message').remove();
            }
        }
    };

    /* display methods */

    /**
     * Create HTML from a datasource object
     */
    GUI.prototype.formatMessage = function (message) {
        message.html = twttr.txt.autoLink(message.text);<commit_msg>Open links in a new tab/window<commit_after>/**
= FontanaGUI =

FontanaGUI is the main class responsible for displaying messages.

The constructor takes a datasource instance and a settings instance.

== Display methods ==

There are 3 methods to control the visualisation:

 * start
 * pause
 * resume
 * clear

`Start` takes a DOM node (selected with jQuery). This is DOM element
that the Fontana will animate in.

`Pause` will halt all animations and data retrieval.

`Resume` will continue a halted Fontana.

`Clear` will remove Fontana.
*/

var Fontana = window.Fontana || {};


Fontana.GUI = (function ($) {
    var GUI;

    GUI = function (datasource, settings) {
        var self = this;
        this.datasource = datasource;
        this.datasourceListener = null;
        this.settings = settings;
        this.messages = [];
        this.animateTimer = -1;
        this.animateScheduled = null;
        this.animatePause = null;
        this.current = null;
        this.effect = null;
        this.style_settings = [ 'font_face', 'text_color', 'custom_css',
                                'special_color', 'bg_color',
                                'bg_image', 'box_bg'];
        this.settings.bind('change', function () {
            self.handleSettingsChange.apply(self, arguments);
        });
    };

    /**
     * Handle settings changes
     */
    GUI.prototype.handleSettingsChange = function (setting, old, value) {
        if (setting == 'twitter_search') {
            this.clear();
            if (value) {
                if (this.datasource) {
                    this.datasource.stop();
                }
                this.datasource = new Fontana.datasources.Twitter(value);
                this.setupDatasourceListener();
                this.datasource.getMessages();
            }
        }
        if (setting == 'effect') {
            this.pause();
            if (this.effect) {
                this.effect.destroy();
                this.effect = null;
            }
            this.animateMessages();
        }
        if ($.inArray(setting, this.style_settings) > -1) {
            this.updateStyle();
        }
    };

    /* Setup datasource listener */
    GUI.prototype.setupDatasourceListener = function () {
        var self = this;
        this.datasourceListener = function (messages) {
            self.handleMessages.call(self, messages);
        };
        this.datasource.bind('messages', this.datasourceListener);
    };

    /**
     * Handle the messages from the datasource
     */
    GUI.prototype.handleMessages = function (messages) {
        var self = this, elements = [];
        this.pause();
        elements = $.map(messages, function (message) {
            return self.formatMessage(message)[0];
        });
        this.container.prepend(elements);
        this.current = null;
        this.resume();
    };

    /**
     * Purge messages in the DOM (if necesarry)
     */
    GUI.prototype.purgeMessages = function (messages) {
        var all = $('.fontana-message', this.container);
        if (all.length >= 30) {
            if (all.index(this.current) < all.length / 2) {
                all.slice(Math.floor(all.length / 2)).remove();
            }
            else {
                this.current.nextAll('.fontana-message').remove();
            }
        }
    };

    /* display methods */

    /**
     * Create HTML from a datasource object
     */
    GUI.prototype.formatMessage = function (message) {
        message.html = twttr.txt.autoLink(message.text, {target: '_blank'});
{'fraction_non_alphanumeric': 0.1193462026208257, 'fraction_numerical': 0.0022544737212906864, 'mean_word_length': 1.8290155440414508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3570310', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1878, 'n_words': 462}