From 28a2dd9a18e1aafe14d26995ad0cfcacb83f6d6e Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Mon, 28 Jan 2013 16:33:28 -0500 Subject: [PATCH] support reordering of static tabs in studio --- cms/djangoapps/contentstore/views.py | 41 +++++++++++++++++++++++-- cms/static/coffee/src/views/tabs.coffee | 16 +++++++++- cms/urls.py | 1 + 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 816ccab091..326b47ee64 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -903,6 +903,36 @@ def static_pages(request, org, course, coursename): def edit_static(request, org, course, coursename): return render_to_response('edit-static-page.html', {}) + +@login_required +@expect_json +def reorder_tabs(request): + tabs = request.POST['tabs'] + logging.debug('tabs = {0} {1}'.format(tabs.__class__, tabs)) + + if len(tabs) > 0: + course = get_course_for_item(tabs[0]) + + if not has_access(request.user, course.location): + raise PermissionDenied() + + # first filter out all non-static tabs from the tabs list + course_tabs = [t for t in course.tabs if t['type'] != 'static_tab'] + + # OK, re-assemble the static tabs in the new order + for tab in tabs: + item = modulestore('direct').get_item(Location(tab)) + + course_tabs.append({ 'type':'static_tab', + 'name' : item.metadata.get('display_name'), + 'url_slug' : item.location.name} + ) + + course.tabs = course_tabs + modulestore('direct').update_metadata(course.location, course.metadata) + + return HttpResponse() + @login_required @ensure_csrf_cookie def edit_tabs(request, org, course, coursename): @@ -914,12 +944,19 @@ def edit_tabs(request, org, course, coursename): if not has_access(request.user, location): raise PermissionDenied() - static_tabs = modulestore('direct').get_items(static_tabs_loc) - # see tabs have been uninitialized (e.g. supporing courses created before tab support in studio) if course_item.tabs is None or len(course_item.tabs) == 0: initialize_course_tabs(course_item) + # first get all static tabs from the tabs list + # we do this because this is also the order in which items are displayed in the LMS + static_tabs_refs = [t for t in course_item.tabs if t['type'] == 'static_tab'] + + static_tabs = [] + for static_tab_ref in static_tabs_refs: + static_tab_loc = Location(location)._replace(category='static_tab', name=static_tab_ref['url_slug']) + static_tabs.append(modulestore('direct').get_item(static_tab_loc)) + components = [ static_tab.location.url() for static_tab diff --git a/cms/static/coffee/src/views/tabs.coffee b/cms/static/coffee/src/views/tabs.coffee index 1fbc6ffa7f..3799eb25ee 100644 --- a/cms/static/coffee/src/views/tabs.coffee +++ b/cms/static/coffee/src/views/tabs.coffee @@ -15,7 +15,7 @@ class CMS.Views.TabsEdit extends Backbone.View @$('.components').sortable( handle: '.drag-handle' - update: (event, ui) => alert 'not yet implemented!' + update: @tabMoved helper: 'clone' opacity: '0.5' placeholder: 'component-placeholder' @@ -24,6 +24,20 @@ class CMS.Views.TabsEdit extends Backbone.View items: '> .component' ) + tabMoved: (event, ui) => + tabs = [] + @$('.component').each((idx, element) => + tabs.push($(element).data('id')) + ) + $.ajax({ + type:'POST', + url: '/reorder_tabs', + data: JSON.stringify({ + tabs : tabs + }), + contentType: 'application/json' + }) + addNewTab: (event) => event.preventDefault() diff --git a/cms/urls.py b/cms/urls.py index 6f8736551b..2b329dc16b 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -17,6 +17,7 @@ urlpatterns = ('', url(r'^publish_draft$', 'contentstore.views.publish_draft', name='publish_draft'), url(r'^unpublish_unit$', 'contentstore.views.unpublish_unit', name='unpublish_unit'), url(r'^create_new_course', 'contentstore.views.create_new_course', name='create_new_course'), + url(r'^reorder_tabs', 'contentstore.views.reorder_tabs', name='reorder_tabs'), url(r'^(?P[^/]+)/(?P[^/]+)/course/(?P[^/]+)$', 'contentstore.views.course_index', name='course_index'),