Commit d2b61f14 by Andy Armstrong Committed by cahrens

Support faster deletes of sections

parent 20782f04
...@@ -259,7 +259,7 @@ class TestCourseOutline(CourseTestCase): ...@@ -259,7 +259,7 @@ class TestCourseOutline(CourseTestCase):
def test_course_outline_initial_state(self): def test_course_outline_initial_state(self):
course_module = modulestore().get_item(self.course.location) course_module = modulestore().get_item(self.course.location)
course_structure = create_xblock_info( course_structure = create_xblock_info(
course_module, course_module,
include_child_info=True, include_child_info=True,
include_children_predicate=lambda xblock: not xblock.category == 'vertical' include_children_predicate=lambda xblock: not xblock.category == 'vertical'
) )
......
...@@ -1190,7 +1190,7 @@ class TestXBlockInfo(ItemTest): ...@@ -1190,7 +1190,7 @@ class TestXBlockInfo(ItemTest):
for child_response in xblock_info['child_info']['children']: for child_response in xblock_info['child_info']['children']:
self.validate_xblock_info_consistency( self.validate_xblock_info_consistency(
child_response, child_response,
has_child_info=(not child_response.get('child_info', None) == None) has_child_info=(not child_response.get('child_info', None) is None)
) )
else: else:
self.assertIsNone(xblock_info.get('child_info', None)) self.assertIsNone(xblock_info.get('child_info', None))
...@@ -245,6 +245,22 @@ define(["jquery", "js/spec_helpers/create_sinon", "js/spec_helpers/view_helpers" ...@@ -245,6 +245,22 @@ define(["jquery", "js/spec_helpers/create_sinon", "js/spec_helpers/view_helpers"
describe("Section", function() { describe("Section", function() {
it('can be deleted', function() { it('can be deleted', function() {
var promptSpy = view_helpers.createPromptSpy(), requestCount;
createCourseOutlinePage(this, createMockCourseJSON('mock-course', 'Mock Course', [
createMockSectionJSON('mock-section', 'Mock Section', []),
createMockSectionJSON('mock-section-2', 'Mock Section 2', [])
]));
outlinePage.$('.outline-item-section .delete-button').first().click();
view_helpers.confirmPrompt(promptSpy);
requestCount = requests.length;
create_sinon.expectJsonRequest(requests, 'DELETE', '/xblock/mock-section');
create_sinon.respondWithJson(requests, {});
expect(requests.length).toBe(requestCount); // No fetch should be performed
expect(outlinePage.$('[data-locator="mock-section"]')).not.toExist();
expect(outlinePage.$('[data-locator="mock-section-2"]')).toExist();
});
it('can be deleted if it is the only section', function() {
var promptSpy = view_helpers.createPromptSpy(); var promptSpy = view_helpers.createPromptSpy();
createCourseOutlinePage(this, mockSingleSectionCourseJSON); createCourseOutlinePage(this, mockSingleSectionCourseJSON);
outlinePage.$('.outline-item-section .delete-button').click(); outlinePage.$('.outline-item-section .delete-button').click();
......
...@@ -117,6 +117,19 @@ define(["jquery", "underscore", "js/views/xblock_outline", "js/views/utils/view_ ...@@ -117,6 +117,19 @@ define(["jquery", "underscore", "js/views/xblock_outline", "js/views/utils/view_
} }
}, },
onChildDeleted: function(childView) {
var xblockInfo = this.model,
childCategory = childView.model.get('category'),
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.
if (childCategory === 'chapter' && children && children.length > 1) {
childView.$el.remove();
} else {
this.refresh();
}
},
createNewItemViewState: function(locator, scrollOffset) { createNewItemViewState: function(locator, scrollOffset) {
return { return {
locator_to_show: locator, locator_to_show: locator,
......
...@@ -135,10 +135,7 @@ define(["jquery", "underscore", "gettext", "js/views/baseview", "js/views/utils/ ...@@ -135,10 +135,7 @@ define(["jquery", "underscore", "gettext", "js/views/baseview", "js/views/utils/
*/ */
addButtonActions: function(element) { addButtonActions: function(element) {
var self = this; var self = this;
element.find('.delete-button').click(function(event) { element.find('.delete-button').click(_.bind(this.handleDeleteEvent, this));
event.preventDefault();
self.deleteXBlock($(event.target));
});
element.find('.add-button').click(_.bind(this.handleAddEvent, this)); element.find('.add-button').click(_.bind(this.handleAddEvent, this));
}, },
...@@ -203,6 +200,14 @@ define(["jquery", "underscore", "gettext", "js/views/baseview", "js/views/utils/ ...@@ -203,6 +200,14 @@ define(["jquery", "underscore", "gettext", "js/views/baseview", "js/views/utils/
this.initialState = null; this.initialState = null;
}, },
/**
* Refresh the view's model from the server, which will cause the view to refresh.
* @returns {jQuery promise} A promise representing the refresh operation.
*/
refresh: function() {
return this.model.fetch();
},
onChildAdded: function(locator, category) { onChildAdded: function(locator, category) {
// For units, redirect to the new page, and for everything else just refresh inline. // For units, redirect to the new page, and for everything else just refresh inline.
if (category === 'vertical') { if (category === 'vertical') {
...@@ -220,23 +225,17 @@ define(["jquery", "underscore", "gettext", "js/views/baseview", "js/views/utils/ ...@@ -220,23 +225,17 @@ define(["jquery", "underscore", "gettext", "js/views/baseview", "js/views/utils/
this.refresh(); this.refresh();
}, },
deleteXBlock: function() { handleDeleteEvent: function(event) {
var parentView = this.parentView; var self = this,
parentView = this.parentView;
event.preventDefault();
XBlockViewUtils.deleteXBlock(this.model).done(function() { XBlockViewUtils.deleteXBlock(this.model).done(function() {
if (parentView) { if (parentView) {
parentView.onChildDeleted(); parentView.onChildDeleted(self, event);
} }
}); });
}, },
/**
* Refresh the view's model from the server, which will cause the view to refresh.
* @returns {jQuery promise} A promise representing the refresh operation.
*/
refresh: function() {
return this.model.fetch();
},
handleAddEvent: function(event) { handleAddEvent: function(event) {
var self = this, var self = this,
target = $(event.target), target = $(event.target),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment