<commit_before>/**
 * View base class that automatically cleans up its sub-views.
 */
App.NestedView = Backbone.View.extend({
    name: 'NestedView',
    close: function () {
        App.debug('Closing ' + this.cid);
        this.remove();
        // Unbind any objects listening to this
        this.unbind();
        // Unbind any objects this is listening to
        this.stopListening();
        this.closeSubViews();
        this.onClose();
    },
    closeSubViews: function () {
        if (this.subViews) {
            _.each(this.subViews, function (view) {
                if (typeof(view.close) !== 'undefined') {
                    view.close();
                }
            });
        }
    },
    onClose: function () {
    },
    addSubView: function (subView) {
        if (!this.subViews) {
            this.subViews = [];
        }
        this.subViews.push(subView);
    }
});

/**
 * View to display error messages
 */
App.ErrorListView = Backbone.View.extend({
    name: 'ErrorListView',
    template: _.template($('#tpl-error-view').html()),
    initialize: function (options) {
        App.debug('App.ErrorListView.initialize()');
        this.listenTo(this.collection, 'add', this.onAdd);
        this.listenTo(this.collection, 'remove', this.onRemove);
        this.render();
    },
    render: function () {
        this.$el.html(this.template);
        this.setVisibility();
    },
    onAdd: function (model) {
        var view = new App.ErrorView({"model":model});
        this.$('ul').append(view.$el);
        this.setVisibility();
    },
    onRemove: function () {
        this.setVisibility();
    },
    setVisibility: function () {
        if (this.collection.length == 0) {
            this.$el.hide();
        } else {
            this.$el.show();
        }
    }
});

/**
 * Single error message
 */
App.ErrorView = Backbone.View.extend({
    tagName: 'li',
    initialize: function (options) {
        this.render();
    },
    render: function () {
        this.$el.html(this.model.get('message'));
    }
})

/**
 * Login form.
 */
App.LoginView = App.NestedView.extend({
    name: 'LoginView',

    template: _.template($('#tpl-login-view').html()),
    
    initialize: function (options) {
        App.debug('App.LoginView.initialize()');
        this.options = options || {};
        _.bindAll(this, 'render');
        _.bindAll(this, 'login');
        _.bindAll(this, 'error');
        this.model.on('unauthorized', this.error)
        this.render();
    },
    
    events: {
        'submit form': 'login'
    },
    
    render: function () {
        App.debug('App.LoginView.render()');
        this.$el.html(this.template());
        var $el = this.$el;
        _.defer(function () {
            $('input[name=username]', $el).focus();
        });
        return this;
    },
    
    login: function (event) {
        App.debug('App.LoginView.login()');
        // Allow default to enable passsword saving
        // See: http://stackoverflow.com/questions/5430129/how-to-make-chrome-remember-password-for-an-ajax-form
        //event.preventDefault();
        var that = this;
        _.defer(function () {
            username = $('input[name=username]', that.$el).val();
            password = $('input[name=password]', that.$el).val();
            $('input[name=username]', that.$el).val('');
            $('input[name=password]', that.$el).val('');
            that.model.signIn({username:username, password:password});
            var progress = _.template($('#tpl-progress').html());
            that.$('.message').html(progress());
            that.$('form').hide();
        });
    },
    
    error: function (message) {
        this.$('.message').html('<p class="text-danger">' + message + '</p>');
        this.$('form').show()
        this.$('input[name=username]').focus();
    }
});

App.LegendView = Backbone.View.extend({
    name: 'LegendView',
    tagName: 'ul',
    initialize: function (options) {
        this.render();
    },
    render: function () {
        var that = this;
        this.$el.addClass('query-legend');
        this.collection.each(function (model) {
            this.onAdd(model);
        });
        this.listenTo(this.collection, "add", this.onAdd);
        this.listenTo(this.collection, "reset", function () {
            $('.query-legend').html("");
            that.collection.each(function (model) {
                that.onAdd(model);
            });
        });
    },
    onAdd: function (model) {
        var that = this;
        console.log('Adding legend model')
        $('.query-legend').each(function () {
            console.log('    boop');
            var $el = $(this);
            var li = $('<li>');
            li.html('<span class="glyphicon glyphicon-certificate query-color"></span> <span class="query-title"></span>');
            $('.query-title', li).text(model.getName());
            $('.query-color', li).css('color', model.getColor());
            $el.append(li);
            that.listenTo(model, 'remove', function () {
                li.remove();
            });
        });
    }
});

/**
 * Controls drop-down menu
 */
App.ControlsView = App.NestedView.extend({
    name: 'ControlsView',
    tagName: 'ul',
    initialize: function (options) {
        App.debug('App.ControlsView.initialize()');
        this.options = options || {};
        this.userModel = options.userModel;
        _.bindAll(this, 'render');
        // Add listeners
        this.userModel.on('change:authenticated', this.render);
        // Render
        this.render();
    },
    render: function () {
        App.debug('App.ControlsView.render()');
        // Reset sub-views
        this.closeSubViews();
        // Recreate sub-views
        if (this.userModel.get('authenticated')) {<commit_msg>Update legend when query name changes.<commit_after>/**
 * View base class that automatically cleans up its sub-views.
 */
App.NestedView = Backbone.View.extend({
    name: 'NestedView',
    close: function () {
        App.debug('Closing ' + this.cid);
        this.remove();
        // Unbind any objects listening to this
        this.unbind();
        // Unbind any objects this is listening to
        this.stopListening();
        this.closeSubViews();
        this.onClose();
    },
    closeSubViews: function () {
        if (this.subViews) {
            _.each(this.subViews, function (view) {
                if (typeof(view.close) !== 'undefined') {
                    view.close();
                }
            });
        }
    },
    onClose: function () {
    },
    addSubView: function (subView) {
        if (!this.subViews) {
            this.subViews = [];
        }
        this.subViews.push(subView);
    }
});

/**
 * View to display error messages
 */
App.ErrorListView = Backbone.View.extend({
    name: 'ErrorListView',
    template: _.template($('#tpl-error-view').html()),
    initialize: function (options) {
        App.debug('App.ErrorListView.initialize()');
        this.listenTo(this.collection, 'add', this.onAdd);
        this.listenTo(this.collection, 'remove', this.onRemove);
        this.render();
    },
    render: function () {
        this.$el.html(this.template);
        this.setVisibility();
    },
    onAdd: function (model) {
        var view = new App.ErrorView({"model":model});
        this.$('ul').append(view.$el);
        this.setVisibility();
    },
    onRemove: function () {
        this.setVisibility();
    },
    setVisibility: function () {
        if (this.collection.length == 0) {
            this.$el.hide();
        } else {
            this.$el.show();
        }
    }
});

/**
 * Single error message
 */
App.ErrorView = Backbone.View.extend({
    tagName: 'li',
    initialize: function (options) {
        this.render();
    },
    render: function () {
        this.$el.html(this.model.get('message'));
    }
})

/**
 * Login form.
 */
App.LoginView = App.NestedView.extend({
    name: 'LoginView',

    template: _.template($('#tpl-login-view').html()),
    
    initialize: function (options) {
        App.debug('App.LoginView.initialize()');
        this.options = options || {};
        _.bindAll(this, 'render');
        _.bindAll(this, 'login');
        _.bindAll(this, 'error');
        this.model.on('unauthorized', this.error)
        this.render();
    },
    
    events: {
        'submit form': 'login'
    },
    
    render: function () {
        App.debug('App.LoginView.render()');
        this.$el.html(this.template());
        var $el = this.$el;
        _.defer(function () {
            $('input[name=username]', $el).focus();
        });
        return this;
    },
    
    login: function (event) {
        App.debug('App.LoginView.login()');
        // Allow default to enable passsword saving
        // See: http://stackoverflow.com/questions/5430129/how-to-make-chrome-remember-password-for-an-ajax-form
        //event.preventDefault();
        var that = this;
        _.defer(function () {
            username = $('input[name=username]', that.$el).val();
            password = $('input[name=password]', that.$el).val();
            $('input[name=username]', that.$el).val('');
            $('input[name=password]', that.$el).val('');
            that.model.signIn({username:username, password:password});
            var progress = _.template($('#tpl-progress').html());
            that.$('.message').html(progress());
            that.$('form').hide();
        });
    },
    
    error: function (message) {
        this.$('.message').html('<p class="text-danger">' + message + '</p>');
        this.$('form').show()
        this.$('input[name=username]').focus();
    }
});

App.LegendView = Backbone.View.extend({
    name: 'LegendView',
    tagName: 'ul',
    initialize: function (options) {
        this.render();
    },
    render: function () {
        var that = this;
        this.$el.addClass('query-legend');
        this.collection.each(function (model) {
            this.onAdd(model);
        });
        this.listenTo(this.collection, "add", this.onAdd);
        this.listenTo(this.collection, "reset", function () {
            $('.query-legend').html("");
            that.collection.each(function (model) {
                that.onAdd(model);
            });
        });
    },
    onAdd: function (model) {
        var that = this;
        console.log('Adding legend model')
        $('.query-legend').each(function () {
            var $el = $(this);
            var li = $('<li>');
            li.html('<span class="glyphicon glyphicon-certificate query-color"></span> <span class="query-title"></span>');
            $('.query-title', li).text(model.getName());
            $('.query-color', li).css('color', model.getColor());
            $el.append(li);
            that.listenTo(model, 'change', function () {
                $('.query-title', li).text(model.getName());
            })
            that.listenTo(model, 'remove', function () {
                li.remove();
            });
        });
    }
});

/**
 * Controls drop-down menu
 */
App.ControlsView = App.NestedView.extend({
    name: 'ControlsView',
    tagName: 'ul',
    initialize: function (options) {
        App.debug('App.ControlsView.initialize()');
        this.options = options || {};
        this.userModel = options.userModel;
        _.bindAll(this, 'render');
        // Add listeners
        this.userModel.on('change:authenticated', this.render);
        // Render
        this.render();
    },
    render: function () {
        App.debug('App.ControlsView.render()');
        // Reset sub-views
        this.closeSubViews();
        // Recreate sub-views
        if (this.userModel.get('authenticated')) {
{'fraction_non_alphanumeric': 0.17988922028121004, 'fraction_numerical': 0.0013634426927993182, 'mean_word_length': 1.993877551020408, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, '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': '639733', 'n_tokens_mistral': 3435, 'n_tokens_neox': 3391, 'n_words': 600}