From de3037558a8621bb2b53b84526f87cad9d9ff100 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Wed, 10 Oct 2012 13:45:36 -0400 Subject: [PATCH 1/2] Add basic section/subsection CRUD on the overview page --- cms/djangoapps/contentstore/views.py | 3 + cms/static/js/base.js | 116 +++++++++++++++++++++++++-- cms/templates/base.html | 2 + cms/templates/overview.html | 48 +++++++++-- 4 files changed, 155 insertions(+), 14 deletions(-) diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 190b463383..e2ecfd8ba9 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -141,6 +141,9 @@ def course_index(request, org, course, name): return render_to_response('overview.html', { 'sections': sections, + 'parent_location': course.location, + 'new_section_template': Location('i4x', 'edx', 'templates', 'sequential', 'Empty'), + 'new_subsection_template': Location('i4x', 'edx', 'templates', 'sequential', 'Empty'), # for now they are the same, but the could be different at some point... 'upload_asset_callback_url': upload_asset_callback_url, 'create_new_unit_template': Location('i4x', 'edx', 'templates', 'vertical', 'Empty') }) diff --git a/cms/static/js/base.js b/cms/static/js/base.js index 3127c1bd90..d9585fcf66 100644 --- a/cms/static/js/base.js +++ b/cms/static/js/base.js @@ -36,6 +36,14 @@ $(document).ready(function() { $('.set-date').bind('click', showDateSetter); $('.remove-date').bind('click', removeDateSetter); + // add new/delete section + $('.new-courseware-section-button').bind('click', addNewSection); + $('.delete-section-button').bind('click', deleteSection); + + // add new/delete subsection + $('.new-subsection-item').bind('click', addNewSubsection); + $('.delete-subsection-button').bind('click', deleteSubsection); + }); // This method only changes the ordering of the child objects in a subsection @@ -120,6 +128,7 @@ function saveSubsection(e) { }); } + function createNewUnit(e) { e.preventDefault(); @@ -139,18 +148,30 @@ function createNewUnit(e) { function deleteUnit(e) { e.preventDefault(); + _deleteItem($(this).parents('li.leaf')); +} - if(!confirm('Are you sure you wish to delete this item. It cannot be reversed!')) - return; +function deleteSubsection(e) { + e.preventDefault(); + _deleteItem($(this).parents('li.branch')); +} - var _li_el = $(this).parents('li.leaf'); - var id = _li_el.data('id'); +function deleteSection(e) { + e.preventDefault(); + _deleteItem($(this).parents('section.branch')); +} + +function _deleteItem($el) { + if(!confirm('Are you sure you wish to delete this item. It cannot be reversed!')) + return; + + var id = $el.data('id'); $.post('/delete_item', - {'id': id, 'delete_children' : true}, - function(data) { - _li_el.remove(); - }); + {'id': id, 'delete_children' : true}, + function(data) { + $el.remove(); + }); } function showUploadModal(e) { @@ -282,3 +303,82 @@ function hideToastMessage(e) { e.preventDefault(); $(this).closest('.toast-notification').remove(); } + +function addNewSection(e) { + e.preventDefault(); + var $newSection = $($('#new-section-template').html()); + $('.new-courseware-section-button').after($newSection); + $newSection.find('.new-section-name').focus().select(); + $newSection.find('.new-section-name-save').bind('click', saveNewSection); + $newSection.find('.new-section-name-cancel').bind('click', cancelNewSection); +} + +function saveNewSection(e) { + e.preventDefault(); + + parent = $(this).data('parent'); + template = $(this).data('template'); + + display_name = $(this).prev('.new-section-name').val(); + + $.post('/clone_item', + {'parent_location' : parent, + 'template' : template, + 'display_name': display_name, + }, + function(data) { + if (data.id != undefined) + location.reload(); + }); +} + +function cancelNewSection(e) { + e.preventDefault(); + $(this).parents('section.new-section').remove(); +} + +function addNewSubsection(e) { + e.preventDefault(); + var $section = $(this).closest('.courseware-section'); + var $newSubsection = $($('#new-subsection-template').html()); + $section.find('.unit-list > ol').append($newSubsection); + $section.find('.new-subsection-name-input').focus().select(); + + var $saveButton = $newSubsection.find('.new-subsection-name-save'); + $saveButton.bind('click', saveNewSubsection); + + parent = $(this).parents("section.branch").data("id"); + + $saveButton.data('parent', parent) + $saveButton.data('template', $(this).data('template')); + + $newSubsection.find('.new-subsection-name-cancel').bind('click', cancelNewSubsection); +} + +function saveNewSubsection(e) { + e.preventDefault(); + + parent = $(this).data('parent'); + template = $(this).data('template'); + + + display_name = $(this).prev('.subsection-name').find('.new-subsection-name-input').val() + + $.post('/clone_item', + {'parent_location' : parent, + 'template' : template, + 'display_name': display_name, + }, + function(data) { + if (data.id != undefined) { + location.reload(); + } + }); + + +} + +function cancelNewSubsection(e) { + e.preventDefault(); + $(this).parents('li.branch').remove(); +} diff --git a/cms/templates/base.html b/cms/templates/base.html index cb827b9876..915394f458 100644 --- a/cms/templates/base.html +++ b/cms/templates/base.html @@ -13,6 +13,8 @@ + + <%block name="header_extras"> diff --git a/cms/templates/overview.html b/cms/templates/overview.html index d650f5e6df..93a4ff2678 100644 --- a/cms/templates/overview.html +++ b/cms/templates/overview.html @@ -4,6 +4,42 @@ <%namespace name="units" file="widgets/units.html" /> +<%block name="header_extras"> + + + + + + <%block name="content">
@@ -13,9 +49,9 @@
- New Section + New Section % for section in sections: -
+
@@ -23,19 +59,19 @@

Unscheduled: click here to set

- +
    % for subsection in section.get_children(): -