Commit 67eec948 by Matthew Piatetsky

Sortable organizations in admin program form

ECOM-5680
parent d0565ea7
...@@ -111,6 +111,8 @@ class ProgramAdmin(admin.ModelAdmin): ...@@ -111,6 +111,8 @@ class ProgramAdmin(admin.ModelAdmin):
try: try:
# courses are ordered by django id, but form.cleaned_data is ordered correctly # courses are ordered by django id, but form.cleaned_data is ordered correctly
obj.courses = form.cleaned_data.get('courses') obj.courses = form.cleaned_data.get('courses')
obj.authoring_organizations = form.cleaned_data.get('authoring_organizations')
obj.credit_backing_organizations = form.cleaned_data.get('credit_backing_organizations')
obj.save() obj.save()
self.save_error = False self.save_error = False
except ProgramPublisherException as ex: except ProgramPublisherException as ex:
......
...@@ -39,18 +39,21 @@ class ProgramAdminForm(HackDjangoAutocompleteMixin, forms.ModelForm): ...@@ -39,18 +39,21 @@ class ProgramAdminForm(HackDjangoAutocompleteMixin, forms.ModelForm):
url='admin_metadata:course-autocomplete', url='admin_metadata:course-autocomplete',
attrs={ attrs={
'data-minimum-input-length': 3, 'data-minimum-input-length': 3,
'class': 'sortable-select',
}, },
), ),
'authoring_organizations': autocomplete.ModelSelect2Multiple( 'authoring_organizations': autocomplete.ModelSelect2Multiple(
url='admin_metadata:organisation-autocomplete', url='admin_metadata:organisation-autocomplete',
attrs={ attrs={
'data-minimum-input-length': 3, 'data-minimum-input-length': 3,
'class': 'sortable-select',
} }
), ),
'credit_backing_organizations': autocomplete.ModelSelect2Multiple( 'credit_backing_organizations': autocomplete.ModelSelect2Multiple(
url='admin_metadata:organisation-autocomplete', url='admin_metadata:organisation-autocomplete',
attrs={ attrs={
'data-minimum-input-length': 3, 'data-minimum-input-length': 3,
'class': 'sortable-select',
} }
), ),
'video': autocomplete.ModelSelect2( 'video': autocomplete.ModelSelect2(
......
function updateSelect2Data(visibleCourseTitles){ function updateSelect2Data(el){
var i, j, var i, j,
visibleCourseTitlesLength, visibleTitlesLength,
selectOptionsLength, selectOptionsLength,
visibleCourseTitles = [], visibleTitles = [],
selectOptions = [], selectOptions = [],
items = [], items = [],
selectOptionsSelector = '.field-courses .select2-hidden-accessible'; selectOptionsElement = $(el).find('.select2-hidden-accessible'),
selectChoicesElement = $(el).find('.select2-selection__choice'),
selectOptionElement = $(selectOptionsElement).find('option');
$('.field-courses .select2-selection__choice').each(function(index, value){ selectChoicesElement.each(function(index, value){
if (value.title){ if (value.title){
visibleCourseTitles.push(value.title); visibleTitles.push(value.title);
} }
}); });
$('.field-courses .select2-hidden-accessible option').each(function(index, value){ selectOptionElement.each(function(index, value){
selectOptions.push({id: value.value, text: value.text}); selectOptions.push({id: value.value, text: value.text});
}); });
// Update select2 options with new data // Update select2 options with new data
visibleCourseTitlesLength = visibleCourseTitles.length; visibleTitlesLength = visibleTitles.length;
selectOptionsLength = selectOptions.length; selectOptionsLength = selectOptions.length;
for (i = 0; i < visibleCourseTitlesLength; i++) { for (i = 0; i < visibleTitlesLength; i++) {
for (j = 0; j < selectOptionsLength; j++) { for (j = 0; j < selectOptionsLength; j++) {
if (selectOptions[j].text === visibleCourseTitles[i]){ if (selectOptions[j].text === visibleTitles[i]){
items.push('<option selected="selected" value="' + selectOptions[j].id + '">' + items.push('<option selected="selected" value="' + selectOptions[j].id + '">' +
selectOptions[j].text + '</option>' selectOptions[j].text + '</option>'
); );
} }
} }
} }
if (items){ if (items){
$(selectOptionsSelector).html(items.join('\n')); selectOptionsElement.html(items.join('\n'));
} }
} }
$(window).load(function(){ $(window).load(function(){
$(function() { $(function() {
var domSelector = '.field-courses .select2-selection--multiple'; $('.sortable-select').parents('.form-row').each(function(index, el){
$('.field-courses ul.select2-selection__rendered').sortable({ $(el).find('ul.select2-selection__rendered').sortable({
containment: 'parent', containment: 'parent',
update: updateSelect2Data update: function(){updateSelect2Data(el);}
})
}) })
}) })
}); });
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