course_outline.js 9.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/**
 * The CourseOutlineView is used to render the contents of the course for the Course Outline page.
 * It is a recursive set of views, where each XBlock has its own instance, and each of the children
 * are shown as child CourseOutlineViews.
 *
 * This class extends XBlockOutlineView to add unique capabilities needed by the course outline:
 *  - sections are initially expanded but subsections and other children are shown as collapsed
 *  - changes cause a refresh of the entire section rather than just the view for the changed xblock
 *  - adding units will automatically redirect to the unit page rather than showing them inline
 */
11 12 13 14 15 16
define(["jquery", "underscore", "js/views/xblock_outline", "js/views/utils/view_utils", "js/views/utils/xblock_utils",
        "js/models/xblock_outline_info", "js/views/modals/course_outline_modals", "js/utils/drag_and_drop"],
    function(
        $, _, XBlockOutlineView, ViewUtils, XBlockViewUtils,
        XBlockOutlineInfo, CourseOutlineModalsFactory, ContentDragger
    ) {
17 18 19 20 21 22

        var CourseOutlineView = XBlockOutlineView.extend({
            // takes XBlockOutlineInfo as a model

            templateName: 'course-outline',

23 24 25 26 27 28
            render: function() {
                var renderResult = XBlockOutlineView.prototype.render.call(this);
                this.makeContentDraggable(this.el);
                return renderResult;
            },

29
            shouldExpandChildren: function() {
30
                return this.expandedLocators.contains(this.model.get('id'));
31 32 33 34
            },

            shouldRenderChildren: function() {
                // Render all nodes up to verticals but not below
35
                return !this.model.isVertical();
36 37
            },

38 39
            getChildViewClass: function() {
                return CourseOutlineView;
40 41 42 43 44 45 46 47 48 49 50 51 52
            },

            /**
             * Refresh the containing section (if there is one) or else refresh the entire course.
             * Note that the refresh will preserve the expanded state of this view and all of its
             * children.
             * @param viewState The desired initial state of the view, or null if none.
             * @returns {jQuery promise} A promise representing the refresh operation.
             */
            refresh: function(viewState) {
                var getViewToRefresh, view, expandedLocators;

                getViewToRefresh = function(view) {
53
                    if (view.model.isChapter() || !view.parentView) {
54 55 56 57 58 59 60 61 62 63 64
                        return view;
                    }
                    return getViewToRefresh(view.parentView);
                };

                view = getViewToRefresh(this);
                viewState = viewState || {};
                view.initialState = viewState;
                return view.model.fetch({});
            },

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            /**
             * Updates the collapse/expand state for this outline element, and then calls refresh.
             * @param isCollapsed true if the element should be collapsed, else false
             */
            refreshWithCollapsedState: function(isCollapsed) {
                var locator =  this.model.get('id');
                if (isCollapsed) {
                    this.expandedLocators.remove(locator);
                }
                else {
                    this.expandedLocators.add(locator);
                }
                this.refresh();
            },

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
            onChildAdded: function(locator, category, event) {
                if (category === 'vertical') {
                    // For units, redirect to the new unit's page in inline edit mode
                    this.onUnitAdded(locator);
                } else if (category === 'chapter' && this.model.hasChildren()) {
                    this.onSectionAdded(locator);
                } else {
                    // For all other block types, refresh the view and do the following:
                    //  - show the new block expanded
                    //  - ensure it is scrolled into view
                    //  - make its name editable
                    this.refresh(this.createNewItemViewState(locator, ViewUtils.getScrollOffset($(event.target))));
                }
            },

            onSectionAdded: function(locator) {
                var self = this,
                    initialState = self.createNewItemViewState(locator),
                    sectionInfo, sectionView;
                // For new chapters in a non-empty view, add a new child view and render it
                // to avoid the expense of refreshing the entire page.
                if (this.model.hasChildren()) {
                    sectionInfo = new XBlockOutlineInfo({
                        id: locator,
                        category: 'chapter'
                    });
                    // Fetch the full xblock info for the section and then create a view for it
                    sectionInfo.fetch().done(function() {
108
                        sectionView = self.createChildView(sectionInfo, self.model, {parentView: self});
109
                        sectionView.initialState = initialState;
110
                        sectionView.expandedLocators = self.expandedLocators;
111 112 113 114 115 116 117 118 119
                        sectionView.render();
                        self.addChildView(sectionView);
                        sectionView.setViewState(initialState);
                    });
                } else {
                    this.refresh(initialState);
                }
            },

120 121 122 123 124
            onChildDeleted: function(childView) {
                var xblockInfo = this.model,
                    children = xblockInfo.get('child_info') && xblockInfo.get('child_info').children;
                // If deleting a section that isn't the final one, just remove it for efficiency
                // as it cannot visually effect the other sections.
125
                if (childView.model.isChapter() && children && children.length > 1) {
126
                    childView.$el.remove();
127
                    children.splice(children.indexOf(childView.model), 1);
128 129 130 131 132
                } else {
                    this.refresh();
                }
            },

133
            createNewItemViewState: function(locator, scrollOffset) {
134
                this.expandedLocators.add(locator);
135 136 137 138 139
                return {
                    locator_to_show: locator,
                    edit_display_name: true,
                    scroll_offset: scrollOffset || 0
                };
140 141 142
            },

            editXBlock: function() {
143
                var modal = CourseOutlineModalsFactory.getModal('edit', this.model, {
144
                    onSave: this.refresh.bind(this),
145 146 147 148
                    parentInfo: this.parentInfo,
                    xblockType: XBlockViewUtils.getXBlockType(
                        this.model.get('category'), this.parentView.model, true
                    )
149 150
                });

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                if (modal) {
                    modal.show();
                }
            },

            publishXBlock: function() {
                var modal = CourseOutlineModalsFactory.getModal('publish', this.model, {
                    onSave: this.refresh.bind(this),
                    xblockType: XBlockViewUtils.getXBlockType(
                        this.model.get('category'), this.parentView.model, true
                    )
                });

                if (modal) {
                    modal.show();
                }
167 168 169 170 171 172 173 174
            },

            addButtonActions: function(element) {
                XBlockOutlineView.prototype.addButtonActions.apply(this, arguments);
                element.find('.configure-button').click(function(event) {
                    event.preventDefault();
                    this.editXBlock();
                }.bind(this));
175 176 177 178
                element.find('.publish-button').click(function(event) {
                    event.preventDefault();
                    this.publishXBlock();
                }.bind(this));
179 180 181 182 183 184 185 186 187
            },

            makeContentDraggable: function(element) {
                if ($(element).hasClass("outline-section")) {
                    ContentDragger.makeDraggable(element, {
                        type: '.outline-section',
                        handleClass: '.section-drag-handle',
                        droppableClass: 'ol.list-sections',
                        parentLocationSelector: 'article.outline',
188 189
                        refresh: this.refreshWithCollapsedState.bind(this),
                        ensureChildrenRendered: this.ensureChildrenRendered.bind(this)
190 191 192 193 194 195 196 197
                    });
                }
                else if ($(element).hasClass("outline-subsection")) {
                    ContentDragger.makeDraggable(element, {
                        type: '.outline-subsection',
                        handleClass: '.subsection-drag-handle',
                        droppableClass: 'ol.list-subsections',
                        parentLocationSelector: 'li.outline-section',
198 199
                        refresh: this.refreshWithCollapsedState.bind(this),
                        ensureChildrenRendered: this.ensureChildrenRendered.bind(this)
200 201 202 203 204 205 206 207
                    });
                }
                else if ($(element).hasClass("outline-unit")) {
                    ContentDragger.makeDraggable(element, {
                        type: '.outline-unit',
                        handleClass: '.unit-drag-handle',
                        droppableClass: 'ol.list-units',
                        parentLocationSelector: 'li.outline-subsection',
208 209
                        refresh: this.refreshWithCollapsedState.bind(this),
                        ensureChildrenRendered: this.ensureChildrenRendered.bind(this)
210 211
                    });
                }
212 213 214 215 216
            }
        });

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