/*
 * A view for a collection of branches.
 *
 * This is presented as a <select> in the document.
 *
 * Users can listen to the "selected" event to know which branch is currently
 * chosen. This event will be triggered with the branch model as its argument.
 */
RB.BranchesView = RB.CollectionView.extend({
    tagName: 'select',

    itemViewType: RB.BranchView,

    events: {
        'change': '_onChange'
    },

    /*
     * Render the view.
     */
    render: function() {
        _super(this).render.apply(this, arguments);

        this.collection.each(function(branch) {
            if (branch.get('isDefault')) {
                this.trigger('selected', branch);
            }
        }, this);

        return this;
    },

    /*
     * Override for CollectionView._add.
     *
     * If the newly-added branch is the default (for example, 'trunk' in SVN or
     * 'master' in git), start with it selected.
     */
    _add: function(branch) {
        _super(this)._add.apply(this, arguments);

        if (this._rendered && branch.get('isDefault')) {
            this.trigger('selected', branch);
        }
    },

    /*
     * Callback for the 'change' event on the select node.
     *
     * Triggers 'selected' with the given model.
     */
    _onChange: function() {
        var selectedIx = this.$el.prop('selectedIndex');
        this.trigger('selected', this.collection.models[selectedIx]);
    }
});

/*
 * A view for a collection of branches.
 *
 * This is presented as a <select> in the document.
 *
 * Users can listen to the "selected" event to know which branch is currently
 * chosen. This event will be triggered with the branch model as its argument.
 */
RB.BranchesView = RB.CollectionView.extend({
    tagName: 'select',

    itemViewType: RB.BranchView,

    events: {
        'change': '_onChange'
    },

    /*
     * Render the view.
     */
    render: function() {
        _super(this).render.apply(this, arguments);

        this.collection.each(function(branch) {
            if (branch.get('isDefault')) {
                this.trigger('selected', branch);
            }
        }, this);

        return this;
    },

    /*
     * Override for CollectionView._onAdded.
     *
     * If the newly-added branch is the default (for example, 'trunk' in SVN or
     * 'master' in git), start with it selected.
     */
    _onAdded: function(branch) {
        _super(this)._onAdded.apply(this, arguments);

        if (this._rendered && branch.get('isDefault')) {
            this.trigger('selected', branch);
        }
    },

    /*
     * Callback for the 'change' event on the select node.
     *
     * Triggers 'selected' with the given model.
     */
    _onChange: function() {
        var selectedIx = this.$el.prop('selectedIndex');
        this.trigger('selected', this.collection.models[selectedIx]);
    }
});

Fix default branch selection in post-commit UI.
Fix default branch selection in post-commit UI.

The refactoring and improvements I did to `RB.CollectionView` broke the
selection of the default branch in the post-commit section of the New Review
Request UI. This wasn't super critical, except in the case where there's only a
single branch, in which case the commits would never be listed.

I've updated the function naming to match the new name in CollectionView.

Testing done:
Loaded a repository in the New Review Request UI and saw the list of commits on
the default branch without having to click anything.

Reviewed at https://reviews.reviewboard.org/r/8032/
