diff --git a/app/assets/javascripts/exercise_collections.js.erb b/app/assets/javascripts/exercise_collections.js.erb index d1c4aae8..03ee56d7 100644 --- a/app/assets/javascripts/exercise_collections.js.erb +++ b/app/assets/javascripts/exercise_collections.js.erb @@ -147,31 +147,47 @@ $(function() { var addExercisesForm = $('#exercise-selection'); var addExercisesButton = $('#add-exercises'); + var removeExerciseButtons = $('.remove-exercise'); + var sortButton = $('#sort-button'); var collectContainedExercises = function () { return exerciseList.find('tbody > tr').toArray().map(function (item) {return $(item).data('id')}); } + var sortExercises = function() { + var listitems = $('tr', list); + listitems.sort(function (a, b) { + return ($(a).find('td:nth-child(2)').text().toUpperCase() > $(b).find('td:nth-child(2)').text().toUpperCase()) ? 1 : -1; + }); + list.append(listitems); + list.sortable('refresh'); + updateExerciseList(); + } + + var addExercise = function (id, title) { + var exercise = {id: id, title: title} + var collectionExercises = collectContainedExercises(); + if (collectionExercises.indexOf(exercise.id) === -1) { + // only add exercises that are not already contained in the collection + var template = '' + + '' + + '' + exercise.title + '' + + '<%= I18n.t('shared.show') %>' + + '<%= I18n.t('shared.destroy') %>'; + exerciseList.find('tbody').append(template); + $('#exercise-list').find('option[value="' + exercise.id + '"]').prop('selected', true); + } + } + addExercisesButton.on('click', function (e) { e.preventDefault(); - var collectionExercises = collectContainedExercises(); var selectedExercises = addExercisesForm.find('select')[0].selectedOptions; for (var i = 0; i < selectedExercises.length; i++) { - var exercise = {id: selectedExercises[i].value, title: selectedExercises[i].label} - if (collectionExercises.indexOf(exercise.id) === -1) { - // only add exercises that are not already contained in the collection - var template = '' + - '' + - '' + exercise.title + '' + - '<%= I18n.t('shared.show') %>' + - '<%= I18n.t('shared.destroy') %>'; - exerciseList.find('tbody').append(template); - $('#exercise-list').find('option[value="' + exercise.id + '"]').prop('selected', true); - } + addExercise(selectedExercises[i].value, selectedExercises[i].label); } }); - $('.remove-exercise').on('click', function (e) { + removeExerciseButtons.on('click', function (e) { e.preventDefault(); var row = $(this).parent().parent(); @@ -179,6 +195,11 @@ $(function() { $('#exercise-list').find('option[value="' + exerciseId + '"]').prop('selected', false); row.remove() }); + + sortButton.on('click', function (e) { + e.preventDefault(); + sortExercises(); + }); } } }); diff --git a/app/assets/stylesheets/exercise_collections.scss b/app/assets/stylesheets/exercise_collections.scss index 737dccbc..527ebd07 100644 --- a/app/assets/stylesheets/exercise_collections.scss +++ b/app/assets/stylesheets/exercise_collections.scss @@ -75,3 +75,7 @@ rect.value-bar { #add-exercise-list { min-height: 450px; } + +button { + margin-right: 10px; +} diff --git a/app/views/exercise_collections/_form.html.slim b/app/views/exercise_collections/_form.html.slim index 2035fe85..05b07d93 100644 --- a/app/views/exercise_collections/_form.html.slim +++ b/app/views/exercise_collections/_form.html.slim @@ -27,6 +27,7 @@ td a.remove-exercise href='#' = t('shared.destroy') button.btn.btn-primary type='button' data-toggle='modal' data-target='#add-exercise-modal' = t('exercise_collections.form.add_exercises') + button.btn.btn-secondary#sort-button type='button' = t('exercise_collections.form.sort_by_title') .hidden = f.collection_select(:exercise_ids, Exercise.all, :id, :title, {}, {id: 'exercise-select', class: 'form-control', multiple: true}) diff --git a/config/locales/de.yml b/config/locales/de.yml index 163afc40..be078620 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -787,3 +787,4 @@ de: exercise_collections: form: add_exercises: "Aufgaben hinzufügen" + sort_by_title: "Nach Titel sortieren" diff --git a/config/locales/en.yml b/config/locales/en.yml index 5a144428..863b0a64 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -787,3 +787,4 @@ en: exercise_collections: form: add_exercises: "Add exercises" + sort_by_title: "Sort by title"