unit_outline.js 2.41 KB
Newer Older
1 2 3 4 5
/**
 * The UnitOutlineView is used to render the Unit Outline component on the unit page. It shows
 * the ancestors of the unit along with its direct siblings. It also has a single "New Unit"
 * button to allow a new sibling unit to be added.
 */
6 7
define(['underscore', 'js/views/xblock_outline', 'js/views/unit_outline_child'],
    function(_, XBlockOutlineView, UnitOutlineChildView) {
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
        var UnitOutlineView = XBlockOutlineView.extend({
            // takes XBlockInfo as a model

            templateName: 'unit-outline',

            render: function() {
                XBlockOutlineView.prototype.render.call(this);
                this.renderAncestors();
                return this;
            },

            renderAncestors: function() {
                var i, listElement,
                    ancestors, ancestor, ancestorView = this,
                    previousAncestor = null;
                if (this.model.get('ancestor_info')) {
                    ancestors = this.model.get('ancestor_info').ancestors;
25
                    listElement = this.getListElement();
26 27 28 29 30
                    // Note: the ancestors are processed in reverse order because the tree wants to
                    // start at the root, but the ancestors are ordered by closeness to the unit,
                    // i.e. subsection and then section.
                    for (i=ancestors.length - 1; i >= 0; i--) {
                        ancestor = ancestors[i];
31 32 33 34 35
                        ancestorView = this.createChildView(
                            ancestor,
                            previousAncestor,
                            {parentView: ancestorView, currentUnitId: this.model.get('id')}
                        );
36 37 38
                        ancestorView.render();
                        listElement.append(ancestorView.$el);
                        previousAncestor = ancestor;
39
                        listElement = ancestorView.getListElement();
40 41 42
                    }
                }
                return ancestorView;
43 44 45 46 47 48 49 50 51 52 53
            },

            getChildViewClass: function() {
                return UnitOutlineChildView;
            },

            getTemplateContext: function() {
                return _.extend(
                    XBlockOutlineView.prototype.getTemplateContext.call(this),
                    {currentUnitId: this.model.get('id')}
                );
54 55 56 57 58
            }
        });

        return UnitOutlineView;
    }); // end define();